-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathGenericExternalServiceTest.java
More file actions
369 lines (326 loc) · 16.6 KB
/
GenericExternalServiceTest.java
File metadata and controls
369 lines (326 loc) · 16.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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.aws.greengrass.lifecyclemanager;
import com.aws.greengrass.config.Topic;
import com.aws.greengrass.config.Topics;
import com.aws.greengrass.dependency.ComponentStatusCode;
import com.aws.greengrass.dependency.State;
import com.aws.greengrass.deployment.DeviceConfiguration;
import com.aws.greengrass.lifecyclemanager.exceptions.ServiceException;
import com.aws.greengrass.testcommons.testutilities.GGServiceTestUtil;
import com.aws.greengrass.util.Exec;
import com.aws.greengrass.util.platforms.Platform;
import com.aws.greengrass.util.platforms.StubResourceController;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.Mock;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.IntConsumer;
import static com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY;
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC;
import static com.aws.greengrass.lifecyclemanager.LogManagerHelper.SERVICE_CONFIG_LOGGING_TOPICS;
import static com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionOfType;
import static com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class GenericExternalServiceTest extends GGServiceTestUtil {
private GenericExternalService ges;
@Mock
private Platform platform;
@Mock
private StubResourceController resourceController;
@BeforeEach
void beforeEach() {
lenient().doReturn(Topics.of(context, SERVICE_CONFIG_LOGGING_TOPICS, null))
.when(config).lookupTopics(eq(SERVICE_CONFIG_LOGGING_TOPICS));
lenient().doReturn(Topic.of(context, VERSION_CONFIG_KEY, "1.0.0")).when(config).find(eq(VERSION_CONFIG_KEY));
lenient().when(platform.getSystemResourceController()).thenReturn(resourceController);
ges = spy(new GenericExternalService(initializeMockedConfig(), platform));
ges.deviceConfiguration = mock(DeviceConfiguration.class);
}
@Test
void GIVEN_new_config_without_bootstrap_WHEN_isBootstrapRequired_THEN_return_false() {
assertFalse(ges.isBootstrapRequired(Collections.emptyMap()));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put(Lifecycle.LIFECYCLE_INSTALL_NAMESPACE_TOPIC, "echo done");
}});
}}));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, null);
}});
}}));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", null);
}});
}}));
}
@Test
void GIVEN_new_config_with_new_version_WHEN_isBootstrapRequired_THEN_return_true() {
assertTrue(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.1");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, "echo done");
}});
}}));
assertTrue(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.1");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", "echo done");
}});
}}));
}
@Test
void GIVEN_new_config_with_new_bootstrap_definition_WHEN_isBootstrapRequired_THEN_return_true() {
doReturn(Topic.of(context, Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, "echo complete")).when(config)
.findNode(eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC), eq(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC));
assertTrue(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, "echo done");
}});
}}));
assertTrue(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", "echo done");
}});
}}));
}
@Test
void GIVEN_nested_bootstrap_definition_WHEN_isBootstrapRequired_THEN_return_false() {
Topics bootstrap = Topics.of(context, Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, null);
bootstrap.createLeafChild("script").withValue("\necho complete\n");
doReturn(bootstrap).when(config)
.findNode(eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC), eq(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("script", "\necho complete\n");
}});
}});
}}));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", new HashMap<String, Object>() {{
put("script", "\necho complete\n");
}});
}});
}}));
}
@Test
void GIVEN_bootstrap_definition_WHEN_isBootstrapRequired_THEN_key_case_insensitive_value_type_insensitive() {
Topics bootstrap = Topics.of(context, Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, null);
bootstrap.createLeafChild("script").withValue("\necho complete\n");
bootstrap.createLeafChild("RequiresPrivilege").withValue("true");
bootstrap.createLeafChild("Timeout").withValue("120");
doReturn(bootstrap).when(config)
.findNode(eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC), eq(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("script", "\necho complete\n");
put("requiresPrivilege", true);
put("Timeout", 120);
}});
}});
}}));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", new HashMap<String, Object>() {{
put("script", "\necho complete\n");
put("RequiresPrivilege", true);
put("timeout", 120);
}});
}});
}}));
assertTrue(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", new HashMap<String, Object>() {{
put("script", "\necho complete\n");
put("RequiresPrivilege", true);
put("timeout", 100);
}});
}});
}}));
assertTrue(ges.isBootstrapRequired(new HashMap<String, Object>() {{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {{
put("Bootstrap", new HashMap<String, Object>() {{
put("script", "\necho complete\n");
put("RequiresPrivilege", true);
put("timeout", 120);
put("Setenv", "");
}});
}});
}}));
}
@Test
@DisabledOnOs(OS.WINDOWS)
void GIVEN_runwith_info_WHEN_exec_add_group_THEN_use_runwith() throws Exception {
ges.runWith = RunWith.builder().user("foo").group("bar").build();
try (Exec exec = ges.addUserGroup(Platform.getInstance().createNewProcessRunner().withExec("echo", "hello"))) {
assertThat(exec.getCommand(), arrayContaining("sudo", "-n", "-E", "-H", "-u", "foo", "-g", "bar",
"--", "echo", "hello"));
}
}
@Test
void GIVEN_service_running_WHEN_pause_requested_THEN_pause() throws Exception {
ges.install();
ges.startup();
ges.pause();
verify(resourceController).pauseComponentProcesses(any(), any());
assertTrue(ges.isPaused());
ges.shutdown();
// On shutdown, expect resume and remove to be invoked.
verify(resourceController).resumeComponentProcesses(ges);
verify(resourceController).removeResourceController(ges);
}
@Test
void GIVEN_service_paused_WHEN_pause_fails_THEN_service_exception_thrown(ExtensionContext context)
throws Exception {
ignoreExceptionWithMessage(context, "Could not pause");
doThrow(new IOException("Could not pause")).when(resourceController).pauseComponentProcesses(any(), any());
ges.install();
ges.startup();
assertThrows(ServiceException.class, () -> ges.pause());
verify(resourceController).pauseComponentProcesses(any(), any());
assertFalse(ges.isPaused());
ges.shutdown();
// On shutdown, expect remove to be invoked. As the component could not be paused, resume should not be invoked
verify(resourceController, never()).resumeComponentProcesses(ges);
verify(resourceController).removeResourceController(ges);
}
@Test
void GIVEN_service_paused_WHEN_resume_requested_THEN_resume() throws Exception {
ges.install();
ges.startup();
ges.pause();
verify(resourceController).pauseComponentProcesses(any(), any());
assertTrue(ges.isPaused());
ges.resume();
verify(resourceController).resumeComponentProcesses(ges);
assertFalse(ges.isPaused());
ges.shutdown();
// On shutdown, expect remove to be invoked. As the component was already resumed, resume should
// not be invoked again
verify(resourceController).resumeComponentProcesses(ges);
verify(resourceController).removeResourceController(ges);
}
@Test
void GIVEN_service_paused_WHEN_resume_fails_THEN_retry_and_succeed(ExtensionContext context) throws Exception {
ignoreExceptionWithMessage(context, "Could not resume");
doThrow(new IOException("Could not resume")).doThrow(new IOException("Could not resume")).doNothing()
.when(resourceController).resumeComponentProcesses(ges);
ges.install();
ges.startup();
ges.pause();
verify(resourceController).pauseComponentProcesses(any(), any());
assertTrue(ges.isPaused());
ges.resume();
// Should be retried 3 times
verify(resourceController, times(3)).resumeComponentProcesses(ges);
assertFalse(ges.isPaused());
ges.shutdown();
// On shutdown, expect remove to be invoked. As the component was already resumed paused, resume should
// not be invoked again
verify(resourceController, times(3)).resumeComponentProcesses(ges);
verify(resourceController).removeResourceController(ges);
}
@Test
void GIVEN_service_paused_WHEN_resume_fails_all_attempts_THEN_service_exception_thrown_service_restarted(
ExtensionContext context) throws Exception {
ignoreExceptionWithMessage(context, "Could not resume");
doThrow(new IOException("Could not resume")).doThrow(new IOException("Could not resume"))
.doThrow(new IOException("Could not resume")).when(resourceController).resumeComponentProcesses(ges);
ges.install();
ges.startup();
ges.pause();
verify(resourceController).pauseComponentProcesses(any(), any());
assertTrue(ges.isPaused());
assertThrows(ServiceException.class, () -> ges.resume());
// Should be retried 3 times
verify(resourceController, times(3)).resumeComponentProcesses(ges);
verify(ges).requestRestart();
// Tracking flag should still be reset
assertFalse(ges.isPaused());
}
@Test
void GIVEN_service_paused_WHEN_shutdown_requested_and_resume_fails_THEN_service_force_stopped(
ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, ServiceException.class);
ignoreExceptionWithMessage(context, "Could not resume");
doThrow(new IOException("Could not resume")).when(resourceController).resumeComponentProcesses(ges);
ges.install();
ges.startup();
ges.pause();
verify(resourceController).pauseComponentProcesses(any(), any());
assertTrue(ges.isPaused());
ges.shutdown();
// On shutdown, expect resume (without retry) and remove to be invoked.
verify(resourceController).resumeComponentProcesses(ges);
verify(resourceController).removeResourceController(ges);
// Tracking flag should still be reset
assertFalse(ges.isPaused());
}
@Test
void GIVEN_startup_callback_WHEN_generation_changed_and_state_is_running_THEN_non_zero_exit_ignored() throws Exception {
AtomicReference<IntConsumer> capturedCallback = new AtomicReference<>();
AtomicLong stateGeneration = new AtomicLong(1);
lenient().doReturn(stateGeneration.get()).when(ges).getStateGeneration();
lenient().doReturn(State.STARTING).when(ges).getState();
doNothing().when(ges).cacheShutdownExec();
doNothing().when(ges).stopAllLifecycleProcesses();
// Create RunResult using reflection since it's a private class
Class<?> runResultClass = Class.forName(
"com.aws.greengrass.lifecyclemanager.GenericExternalService$RunResult");
Constructor<?> constructor = runResultClass.getDeclaredConstructor(
GreengrassService.RunStatus.class, Exec.class, ComponentStatusCode.class);
constructor.setAccessible(true);
Object runResult = constructor.newInstance(GreengrassService.RunStatus.NothingDone, null, null);
doAnswer(invocation -> {
capturedCallback.set(invocation.getArgument(1));
// Simulate generation change and state transition to RUNNING before callback executes
stateGeneration.incrementAndGet();
doReturn(stateGeneration.get()).when(ges).getStateGeneration();
doReturn(State.RUNNING).when(ges).getState();
return runResult;
}).when(ges).run(eq(Lifecycle.LIFECYCLE_STARTUP_NAMESPACE_TOPIC), any(IntConsumer.class), any());
ges.startup();
// Execute callback with non-zero exit code - should NOT call serviceErrored since generation differs
capturedCallback.get().accept(1);
verify(ges, never()).serviceErrored(any(ComponentStatusCode.class), any(Integer.class), any(String.class));
}
}