use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentConfigMergingTest method GIVEN_kernel_running_single_service_WHEN_merge_same_doc_happens_twice_THEN_second_merge_should_not_restart_services.
@Test
void GIVEN_kernel_running_single_service_WHEN_merge_same_doc_happens_twice_THEN_second_merge_should_not_restart_services() throws Throwable {
// GIVEN
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("single_service.yaml"));
// launch Nucleus
CountDownLatch mainRunning = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.RUNNING)) {
mainRunning.countDown();
}
});
kernel.launch();
assertTrue(mainRunning.await(5, TimeUnit.SECONDS));
Map<String, Object> nucleusConfig = getNucleusConfig();
HashMap<String, Object> newConfig = new HashMap<String, Object>() {
{
put(SERVICES_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put("main", new HashMap<String, Object>() {
{
put(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, Arrays.asList("new_service", DEFAULT_NUCLEUS_COMPONENT_NAME));
}
});
put("new_service", new HashMap<String, Object>() {
{
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put(LIFECYCLE_RUN_NAMESPACE_TOPIC, "echo done");
}
});
put(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, Arrays.asList("new_service2"));
}
});
put("new_service2", new HashMap<String, Object>() {
{
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put(LIFECYCLE_RUN_NAMESPACE_TOPIC, "echo done");
}
});
}
});
put(DEFAULT_NUCLEUS_COMPONENT_NAME, nucleusConfig);
}
});
}
};
// do first merge
CountDownLatch mainRestarted = new CountDownLatch(1);
AtomicBoolean newService2Started = new AtomicBoolean(false);
AtomicBoolean newServiceStarted = new AtomicBoolean(false);
GlobalStateChangeListener listener = (service, oldState, newState) -> {
if (service.getName().equals("new_service2") && newState.equals(State.RUNNING)) {
newService2Started.set(true);
}
if (newService2Started.get() && service.getName().equals("new_service") && newState.equals(State.RUNNING)) {
newServiceStarted.set(true);
}
// Only count main as started if its dependency (new_service) has already been started
if (newServiceStarted.get() && service.getName().equals("main") && newState.equals(State.FINISHED) && oldState.equals(State.STARTING)) {
mainRestarted.countDown();
}
};
kernel.getContext().addGlobalStateChangeListener(listener);
GreengrassService main = kernel.locate("main");
deploymentConfigMerger.mergeInNewConfig(testDeployment(), newConfig).get(60, TimeUnit.SECONDS);
// Verify that first merge succeeded.
assertTrue(newService2Started.get());
assertTrue(newServiceStarted.get());
assertTrue(mainRestarted.await(10, TimeUnit.SECONDS));
assertThat(kernel.orderedDependencies().stream().map(GreengrassService::getName).collect(Collectors.toList()), containsInRelativeOrder("new_service2", "new_service", "main"));
// Wait for main to finish before continuing, otherwise the state change listner may cause a failure
assertThat(main::getState, eventuallyEval(is(State.FINISHED)));
// WHEN
AtomicBoolean stateChanged = new AtomicBoolean(false);
listener = (service, oldState, newState) -> {
System.err.println("State shouldn't change in merging the same config: " + service.getName() + " " + oldState + " => " + newState);
stateChanged.set(true);
};
kernel.getContext().addGlobalStateChangeListener(listener);
// THEN
// merge in the same config the second time
// merge shouldn't block
deploymentConfigMerger.mergeInNewConfig(testDeployment(), newConfig).get(60, TimeUnit.SECONDS);
// main should be finished
assertEquals(State.FINISHED, main.getState());
assertFalse(stateChanged.get(), "State shouldn't change in merging the same config.");
// remove listener
kernel.getContext().removeGlobalStateChangeListener(listener);
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentServiceIntegrationTest method before.
@BeforeEach
void before(ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, PackageDownloadException.class);
ignoreExceptionOfType(context, SdkClientException.class);
kernel = new Kernel();
kernel.getContext().put(DeploymentDocumentDownloader.class, deploymentDocumentDownloader);
NoOpPathOwnershipHandler.register(kernel);
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, DeploymentServiceIntegrationTest.class.getResource("onlyMain.yaml"));
// ensure deployment service starts
CountDownLatch deploymentServiceLatch = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals(DEPLOYMENT_SERVICE_TOPICS) && newState.equals(State.RUNNING)) {
deploymentServiceLatch.countDown();
}
});
setDeviceConfig(kernel, DeviceConfiguration.DEPLOYMENT_POLLING_FREQUENCY_SECONDS, 1L);
kernel.launch();
assertTrue(deploymentServiceLatch.await(10, TimeUnit.SECONDS));
deploymentQueue = kernel.getContext().get(DeploymentQueue.class);
FleetStatusService fleetStatusService = (FleetStatusService) kernel.locate(FLEET_STATUS_SERVICE_TOPICS);
fleetStatusService.getIsConnected().set(false);
// pre-load contents to package store
localStoreContentPath = Paths.get(DeploymentTaskIntegrationTest.class.getResource("local_store_content").toURI());
PreloadComponentStoreHelper.preloadRecipesFromTestResourceDir(localStoreContentPath.resolve("recipes"), kernel.getNucleusPaths().recipePath());
copyFolderRecursively(localStoreContentPath.resolve("artifacts"), kernel.getNucleusPaths().artifactPath(), REPLACE_EXISTING);
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class MqttTest method GIVEN_mqttclient_WHEN_closes_new_connection_is_created_THEN_previous_session_is_invalidated.
@Test
void GIVEN_mqttclient_WHEN_closes_new_connection_is_created_THEN_previous_session_is_invalidated() throws Throwable {
kernel = new Kernel().parseArgs("-r", tempRootDir.toAbsolutePath().toString());
setDefaultRunWithUser(kernel);
deviceProvisioningHelper.updateKernelConfigWithIotConfiguration(kernel, thingInfo, GAMMA_REGION.toString(), TES_ROLE_ALIAS_NAME);
MqttClient client = kernel.getContext().get(MqttClient.class);
// subscribe to 50 topics using first connection.
int numberOfTopics = 50;
for (int i = 0; i < numberOfTopics; i++) {
client.subscribe(SubscribeRequest.builder().topic("A/" + i).callback((m) -> {
}).build());
}
// close the first connections and create a second connection.
client.close();
client = kernel.getContext().newInstance(MqttClient.class);
CountDownLatch cdl = new CountDownLatch(numberOfTopics);
// if the session from first connection is not terminated, subscribe operations made by second connection will not succeed.
for (int i = 0; i < numberOfTopics; i++) {
client.subscribe(SubscribeRequest.builder().topic("B/" + i).callback((m) -> {
cdl.countDown();
logger.atInfo().kv("remaining", cdl.getCount()).log("Received 1 message from cloud.");
}).build());
}
for (int i = 0; i < numberOfTopics; i++) {
client.publish(PublishRequest.builder().topic("B/" + i).payload("What's up".getBytes(StandardCharsets.UTF_8)).build()).get(5, TimeUnit.SECONDS);
logger.atInfo().kv("total", i + 1).log("Added 1 message to spooler.");
}
assertTrue(cdl.await(1, TimeUnit.MINUTES), "All messages published and received");
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class IPCMqttProxyTest method beforeEach.
@BeforeEach
void beforeEach() throws Exception {
mqttClient = mock(MqttClient.class);
when(mqttClient.publish(any())).thenReturn(CompletableFuture.completedFuture(0));
System.setProperty("root", tempRootDir.toAbsolutePath().toString());
kernel = new Kernel();
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, IPCMqttProxyTest.class.getResource("mqttproxy.yaml"));
CountDownLatch awaitIpcServiceLatch = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals(TEST_SERVICE_NAME) && newState.equals(State.FINISHED)) {
awaitIpcServiceLatch.countDown();
}
});
kernel.getContext().put(MqttClient.class, mqttClient);
kernel.launch();
assertTrue(awaitIpcServiceLatch.await(10, TimeUnit.SECONDS));
Topics servicePrivateConfig = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC, TEST_SERVICE_NAME, PRIVATE_STORE_NAMESPACE_TOPIC);
String authToken = Coerce.toString(servicePrivateConfig.find(SERVICE_UNIQUE_ID_KEY));
socketOptions = TestUtils.getSocketOptionsForIPC();
clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class FileLoggerTest method GIVEN_root_path_given_as_system_param_WHEN_kernel_launches_THEN_logs_written_to_correct_directory.
@Test
void GIVEN_root_path_given_as_system_param_WHEN_kernel_launches_THEN_logs_written_to_correct_directory() throws Exception {
// launch Nucleus without config arg
kernel = new Kernel().parseArgs().launch();
GreengrassService mainService = kernel.locate("main");
assertNotNull(mainService);
// verify that log file exists are the correct location.
File logFile = tempRootDir.resolve("logs").resolve("greengrass.log").toFile();
MatcherAssert.assertThat(logFile, aFileNamed(equalToIgnoringCase("greengrass.log")));
assertTrue(logFile.length() > 0);
}
Aggregations