Skip to content

Commit 4f53abb

Browse files
committed
msid
1 parent ef4ee91 commit 4f53abb

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.utils.identity;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import java.lang.reflect.Method;
24+
25+
import org.junit.After;
26+
import org.junit.Test;
27+
28+
public class ManagementServerNodeTest {
29+
30+
private static final String FQDN_SYS_PROP = "cloudstack.msid.from.fqdn";
31+
32+
@After
33+
public void tearDown() {
34+
System.clearProperty(FQDN_SYS_PROP);
35+
}
36+
37+
private static boolean invokeIsTruthy(String value) throws Exception {
38+
Method m = ManagementServerNode.class.getDeclaredMethod("isTruthy", String.class);
39+
m.setAccessible(true);
40+
return (boolean) m.invoke(null, value);
41+
}
42+
43+
private static boolean invokeIsFqdnModeEnabled() throws Exception {
44+
Method m = ManagementServerNode.class.getDeclaredMethod("isFqdnModeEnabled");
45+
m.setAccessible(true);
46+
return (boolean) m.invoke(null);
47+
}
48+
49+
private static long invokeGenerateIdFromFqdn() throws Exception {
50+
Method m = ManagementServerNode.class.getDeclaredMethod("generateIdFromFqdn");
51+
m.setAccessible(true);
52+
return (long) m.invoke(null);
53+
}
54+
55+
@Test
56+
public void testGetManagementServerIdIsPositive() {
57+
assertTrue("Node id must be a positive, non-zero value", ManagementServerNode.getManagementServerId() > 0);
58+
}
59+
60+
@Test
61+
public void testCheckPassesWithValidNodeId() {
62+
// Node id is derived at class-load time and should be valid, so check() must not throw.
63+
new ManagementServerNode().check();
64+
}
65+
66+
@Test
67+
public void testIsTruthyRecognizesTrueValues() throws Exception {
68+
assertTrue(invokeIsTruthy("true"));
69+
assertTrue(invokeIsTruthy("TRUE"));
70+
assertTrue(invokeIsTruthy("True"));
71+
assertTrue(invokeIsTruthy("1"));
72+
assertTrue(invokeIsTruthy("yes"));
73+
assertTrue(invokeIsTruthy("YES"));
74+
}
75+
76+
@Test
77+
public void testIsTruthyTrimsWhitespace() throws Exception {
78+
assertTrue(invokeIsTruthy(" true "));
79+
assertTrue(invokeIsTruthy("\t1\n"));
80+
assertTrue(invokeIsTruthy(" yes "));
81+
}
82+
83+
@Test
84+
public void testIsTruthyRejectsFalseValues() throws Exception {
85+
assertFalse(invokeIsTruthy(null));
86+
assertFalse(invokeIsTruthy(""));
87+
assertFalse(invokeIsTruthy(" "));
88+
assertFalse(invokeIsTruthy("false"));
89+
assertFalse(invokeIsTruthy("0"));
90+
assertFalse(invokeIsTruthy("no"));
91+
assertFalse(invokeIsTruthy("enabled"));
92+
assertFalse(invokeIsTruthy("2"));
93+
}
94+
95+
@Test
96+
public void testIsFqdnModeEnabledDefaultsFalse() throws Exception {
97+
System.clearProperty(FQDN_SYS_PROP);
98+
assertFalse(invokeIsFqdnModeEnabled());
99+
}
100+
101+
@Test
102+
public void testIsFqdnModeEnabledWhenSystemPropertyTruthy() throws Exception {
103+
System.setProperty(FQDN_SYS_PROP, "true");
104+
assertTrue(invokeIsFqdnModeEnabled());
105+
}
106+
107+
@Test
108+
public void testIsFqdnModeEnabledWhenSystemPropertyFalsy() throws Exception {
109+
System.setProperty(FQDN_SYS_PROP, "false");
110+
assertFalse(invokeIsFqdnModeEnabled());
111+
}
112+
113+
@Test
114+
public void testGenerateIdFromFqdnIsPositiveAndNonZero() throws Exception {
115+
long id = invokeGenerateIdFromFqdn();
116+
assertTrue("Generated id must be positive and non-zero", id > 0);
117+
}
118+
119+
@Test
120+
public void testGenerateIdFromFqdnFitsIn48Bits() throws Exception {
121+
long id = invokeGenerateIdFromFqdn();
122+
assertTrue("Generated id must fit within 48 bits", id <= 0xFFFFFFFFFFFFL);
123+
}
124+
125+
@Test
126+
public void testGenerateIdFromFqdnIsDeterministic() throws Exception {
127+
long first = invokeGenerateIdFromFqdn();
128+
long second = invokeGenerateIdFromFqdn();
129+
assertEquals("Generated id must be stable across calls for the same FQDN", first, second);
130+
}
131+
}

0 commit comments

Comments
 (0)