forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentShellTest.java
More file actions
372 lines (275 loc) · 13.6 KB
/
AgentShellTest.java
File metadata and controls
372 lines (275 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.naming.ConfigurationException;
import com.cloud.agent.properties.AgentProperties;
import com.cloud.agent.properties.AgentPropertiesFileHandler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.cloud.utils.StringUtils;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class AgentShellTest {
@InjectMocks
@Spy
AgentShell agentShellSpy = new AgentShell();
@Mock
AgentProperties agentPropertiesMock;
@Mock
AgentProperties.Property<Integer> propertyIntegerMock;
@Mock
AgentProperties.Property<String> propertyStringMock;
@Mock
UUID uuidMock;
MockedStatic<AgentPropertiesFileHandler> agentPropertiesFileHandlerMocked;
@Before
public void setUp() throws Exception {
agentPropertiesFileHandlerMocked = Mockito.mockStatic(AgentPropertiesFileHandler.class, Mockito.CALLS_REAL_METHODS);
}
@After
public void tearDown() throws Exception {
agentPropertiesFileHandlerMocked.close();
}
@Test
public void parseCommand() throws ConfigurationException {
AgentShell shell = new AgentShell();
UUID anyUuid = UUID.randomUUID();
shell.parseCommand(new String[] {"port=55555", "threads=4", "host=localhost", "pod=pod1", "guid=" + anyUuid, "zone=zone1"});
Assert.assertEquals(55555, shell.getPort());
Assert.assertEquals(4, shell.getWorkers());
Assert.assertEquals("localhost", shell.getNextHost());
Assert.assertEquals(anyUuid.toString(), shell.getGuid());
Assert.assertEquals("pod1", shell.getPod());
Assert.assertEquals("zone1", shell.getZone());
}
@Test
public void loadProperties() throws ConfigurationException {
AgentShell shell = new AgentShell();
shell.loadProperties();
Assert.assertNotNull(shell.getProperties());
Assert.assertFalse(shell.getProperties().entrySet().isEmpty());
}
@Test
public void testGetHost() {
AgentShell shell = new AgentShell();
List<String> hosts = Arrays.asList("10.1.1.1", "20.2.2.2", "30.3.3.3", "2001:db8::1");
shell.setHosts(StringUtils.listToCsvTags(hosts));
for (String host : hosts) {
Assert.assertEquals(host, shell.getNextHost());
}
Assert.assertEquals(shell.getNextHost(), hosts.get(0));
}
@Test
public void isValueStartingAndEndingWithAtSignTestValues() {
Map<String, Boolean> valuesAndExpects = new HashMap<>();
valuesAndExpects.put("@test@", true);
valuesAndExpects.put("test@", false);
valuesAndExpects.put("@test", false);
valuesAndExpects.put("test", false);
valuesAndExpects.put("te@st", false);
valuesAndExpects.forEach((value, expected) -> {
boolean result = agentShellSpy.isValueStartingAndEndingWithAtSign(value);
Assert.assertEquals(String.format("Test with value [%s] does not return as expected.", value), expected, result);
});
}
@Test
public void getPortOrWorkersTestValueIsNullGetFromProperty() {
int expected = 195;
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
int result = agentShellSpy.getPortOrWorkers(null, propertyIntegerMock);
Assert.assertEquals(expected, result);
}
@Test
public void getPortOrWorkersTestValueIsNotAValidIntegerReturnDefaultFromProperty() {
int expected = 42;
Mockito.doReturn(expected).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getPortOrWorkers("test", propertyIntegerMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getPortOrWorkersTestValueIsAValidIntegerReturnValue() {
int expected = 42;
Mockito.doReturn(79).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getPortOrWorkers(String.valueOf(expected), propertyIntegerMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getWorkersTestWorkersLessThan0ReturnDefault() {
int expected = 42;
Mockito.doReturn(propertyIntegerMock).when(agentPropertiesMock).getWorkers();
Mockito.doReturn(-1).when(agentShellSpy).getPortOrWorkers(Mockito.any(), Mockito.any());
Mockito.doReturn(expected).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getWorkers("");
Assert.assertEquals(expected, result);
}
@Test
public void getWorkersTestWorkersEqualTo0ReturnDefault() {
int expected = 42;
Mockito.doReturn(propertyIntegerMock).when(agentPropertiesMock).getWorkers();
Mockito.doReturn(0).when(agentShellSpy).getPortOrWorkers(Mockito.any(), Mockito.any());
Mockito.doReturn(expected).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getWorkers("");
Assert.assertEquals(expected, result);
}
@Test
public void getWorkersTestWorkersHigherThan0ReturnValue() {
int expected = 1;
Mockito.doReturn(expected).when(agentShellSpy).getPortOrWorkers(Mockito.any(), Mockito.any());
int result = agentShellSpy.getWorkers("");
Assert.assertEquals(expected, result);
}
@Test
public void getZoneOrPodTestValueIsNullAndPropertyStartsAndEndsWithAtSignReturnPropertyDefaultValue() {
String expected = "default";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn("test");
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
Mockito.doReturn(expected).when(propertyStringMock).getDefaultValue();
String result = agentShellSpy.getZoneOrPod(null, propertyStringMock);
Assert.assertEquals(expected, result);
}
@Test
public void getZoneOrPodTestValueIsNullAndPropertyDoesNotStartAndEndWithAtSignReturnPropertyDefaultValue() {
String expected = "test";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
String result = agentShellSpy.getZoneOrPod(null, propertyStringMock);
Assert.assertEquals(expected, result);
}
@Test
public void getZoneOrPodTestValueIsNotNullAndStartsAndEndsWithAtSignReturnPropertyDefaultValue() {
String expected = "default";
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
Mockito.doReturn(expected).when(propertyStringMock).getDefaultValue();
String result = agentShellSpy.getZoneOrPod("test", propertyStringMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getZoneOrPodTestValueIsNotNullAndDoesNotStartAndEndWithAtSignReturnPropertyDefaultValue() {
String expected = "test";
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
String result = agentShellSpy.getZoneOrPod(expected, propertyStringMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getGuidTestGuidNotNullReturnIt() throws ConfigurationException {
String expected = "test";
String result = agentShellSpy.getGuid(expected);
Assert.assertEquals(expected, result);
}
@Test
public void getGuidTestGuidIsNullReturnProperty() throws ConfigurationException {
String expected = "test";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
String result = agentShellSpy.getGuid(null);
Assert.assertEquals(expected, result);
}
@Test
public void getGuidTestGuidAndPropertyAreNullIsDeveloperGenerateNewUuid() throws ConfigurationException {
String expected = "test";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(null, true);
MockedStatic<UUID> uuidMocked = Mockito.mockStatic(UUID.class);
uuidMocked.when(() -> UUID.randomUUID()).thenReturn(uuidMock);
Mockito.doReturn(expected).when(uuidMock).toString();
String result = agentShellSpy.getGuid(null);
Assert.assertEquals(expected, result);
uuidMocked.close();
}
@Test(expected = ConfigurationException.class)
public void getGuidTestGuidAndPropertyAreNullIsNotDeveloperThrowConfigurationException() throws ConfigurationException {
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(null, false);
agentShellSpy.getGuid(null);
}
@Test
public void setHostTestValueIsNotNullAndStartsAndEndsWithAtSignThrowConfigurationException(){
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
boolean error = false;
try {
agentShellSpy.setHost("test");
} catch (ConfigurationException e) {
error = true;
}
if (!error) {
throw new AssertionError("This test expects a ConfigurationException.");
}
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void setHostTestValueIsNullPropertyStartsAndEndsWithAtSignThrowConfigurationException(){
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn("test");
boolean error = false;
try {
agentShellSpy.setHost(null);
} catch (ConfigurationException e) {
error = true;
}
if (!error) {
throw new AssertionError("This test expects a ConfigurationException.");
}
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()));
}
@Test
public void setHostTestValueIsNotNullAndDoesNotStartAndEndWithAtSignSetHosts() throws ConfigurationException {
String expected = "test";
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
agentShellSpy.setHost(expected);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
Mockito.verify(agentShellSpy).setHosts(expected);
}
@Test
public void setHostTestValueIsNullPropertyDoesNotStartAndEndWithAtSignSetHosts() throws ConfigurationException {
String expected = "test";
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
agentShellSpy.setHost(null);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()));
AgentPropertiesFileHandler.getPropertyValue(Mockito.any());
Mockito.verify(agentShellSpy).setHosts(expected);
}
@Test
public void updateAndGetConnectedHost() {
String expected = "test";
AgentShell shell = new AgentShell();
shell.setHosts("test");
shell.getNextHost();
shell.updateConnectedHost("test");
Assert.assertEquals(expected, shell.getConnectedHost());
}
@Test
public void testGetSslHandshakeTimeout() {
Integer expected = 1;
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.eq(AgentProperties.SSL_HANDSHAKE_TIMEOUT))).thenReturn(expected);
Assert.assertEquals(expected, agentShellSpy.getSslHandshakeTimeout());
}
}