use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentTaskIntegrationTest method setupKernel.
@BeforeAll
static void setupKernel() throws IOException {
kernel = new Kernel();
rootDir = Paths.get(System.getProperty("root"));
NoOpPathOwnershipHandler.register(kernel);
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, DeploymentTaskIntegrationTest.class.getResource("onlyMain.yaml"));
kernel.launch();
// get required instances from context
componentManager = kernel.getContext().get(ComponentManager.class);
componentStore = kernel.getContext().get(ComponentStore.class);
dependencyResolver = kernel.getContext().get(DependencyResolver.class);
kernelConfigResolver = kernel.getContext().get(KernelConfigResolver.class);
deploymentConfigMerger = kernel.getContext().get(DeploymentConfigMerger.class);
deploymentDocumentDownloader = kernel.getContext().get(DeploymentDocumentDownloader.class);
thingGroupHelper = kernel.getContext().get(ThingGroupHelper.class);
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentTaskIntegrationTest method GIVEN_services_running_WHEN_new_service_breaks_failure_handling_policy_rollback_THEN_services_are_rolled_back.
/**
* First deployment starts some services. Second deployment tries to add a service that breaks and removes an
* existing service and the failure handling policy is to rollback As a result, kernel should be reverted to the
* state before deployment
*
* @throws Exception
*/
@Test
@Order(11)
void GIVEN_services_running_WHEN_new_service_breaks_failure_handling_policy_rollback_THEN_services_are_rolled_back(ExtensionContext context) throws Exception {
Map<String, Object> pkgDetails = new HashMap<>();
pkgDetails.put(GROUP_TO_ROOT_COMPONENTS_VERSION_KEY, "1.0.0");
groupToRootComponentsTopics.lookupTopics("RedSignal").replaceAndWait(pkgDetails);
groupToRootComponentsTopics.lookupTopics("YellowSignal").replaceAndWait(pkgDetails);
Future<DeploymentResult> resultFuture = submitSampleJobDocument(DeploymentTaskIntegrationTest.class.getResource("YellowAndRedSignal.json").toURI(), System.currentTimeMillis());
resultFuture.get(DEPLOYMENT_TIMEOUT, TimeUnit.SECONDS);
List<String> services = kernel.orderedDependencies().stream().filter(greengrassService -> greengrassService instanceof GenericExternalService).map(GreengrassService::getName).collect(Collectors.toList());
// should contain main, Nucleus, YellowSignal and RedSignal
assertEquals(4, services.size());
assertThat(services, containsInAnyOrder("main", DEFAULT_NUCLEUS_COMPONENT_NAME, "YellowSignal", "RedSignal"));
ignoreExceptionUltimateCauseOfType(context, ServiceUpdateException.class);
groupToRootComponentsTopics.lookupTopics("YellowSignal").remove();
groupToRootComponentsTopics.lookupTopics("BreakingService").replaceAndWait(ImmutableMap.of(GROUP_TO_ROOT_COMPONENTS_VERSION_KEY, "1.0.0"));
preloadLocalStoreContent();
resultFuture = submitSampleJobDocument(DeploymentTaskIntegrationTest.class.getResource("FailureRollbackDeployment.json").toURI(), System.currentTimeMillis());
DeploymentResult result = resultFuture.get(60, TimeUnit.SECONDS);
services = kernel.orderedDependencies().stream().filter(greengrassService -> greengrassService instanceof GenericExternalService).map(GreengrassService::getName).collect(Collectors.toList());
// should contain main, Nucleus, YellowSignal, RedSignal
assertEquals(4, services.size());
assertThat(services, containsInAnyOrder("main", DEFAULT_NUCLEUS_COMPONENT_NAME, "YellowSignal", "RedSignal"));
assertThrows(ServiceLoadException.class, () -> kernel.locate("BreakingService"));
assertThrows(ServiceLoadException.class, () -> kernel.locate("Mosquitto"));
assertThrows(ServiceLoadException.class, () -> kernel.locate("GreenSignal"));
assertEquals(DeploymentResult.DeploymentStatus.FAILED_ROLLBACK_COMPLETE, result.getDeploymentStatus());
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DynamicComponentConfigurationValidationTest method before.
@BeforeEach
void before(ExtensionContext context) throws Exception {
ignoreExceptionWithMessage(context, "Connection reset by peer");
socketOptions = TestUtils.getSocketOptionsForIPC();
kernel = new Kernel();
NoOpPathOwnershipHandler.register(kernel);
deploymentConfigMerger = kernel.getContext().get(DeploymentConfigMerger.class);
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, DynamicComponentConfigurationValidationTest.class.getResource("onlyMain.yaml"));
// launch kernel
Runnable mainFinished = createServiceStateChangeWaiter(kernel, "main", 10, State.FINISHED);
kernel.launch();
mainFinished.run();
// Start a new service
AtomicBoolean mainRestarted = new AtomicBoolean(false);
AtomicBoolean serviceStarted = new AtomicBoolean(false);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.RUNNING) || newState.equals(State.FINISHED)) {
mainRestarted.set(true);
}
if (service.getName().equals("OldService") && newState.equals(State.RUNNING) && oldState.equals(State.STARTING)) {
serviceStarted.set(true);
}
});
List<String> serviceList = kernel.getMain().getDependencies().keySet().stream().map(GreengrassService::getName).collect(Collectors.toList());
serviceList.add("OldService");
Map<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, serviceList);
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, kernel.getMain().getServiceConfig().lookupTopics(SERVICE_LIFECYCLE_NAMESPACE_TOPIC).toPOJO());
}
});
put(DEFAULT_NUCLEUS_COMPONENT_NAME, getNucleusConfig(kernel));
put("OldService", new HashMap<String, Object>() {
{
put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {
{
put("ConfigKey1", "ConfigValue1");
}
});
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put(LIFECYCLE_RUN_NAMESPACE_TOPIC, "echo Running OldService");
}
});
put(VERSION_CONFIG_KEY, DEFAULT_EXISTING_SERVICE_VERSION);
}
});
}
});
}
};
deploymentConfigMerger.mergeInNewConfig(createTestDeployment(), newConfig).get(60, TimeUnit.SECONDS);
assertTrue(mainRestarted.get());
assertTrue(serviceStarted.get());
}
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_changes_service_THEN_service_restarts_with_new_config.
@Test
void GIVEN_kernel_running_single_service_WHEN_merge_changes_service_THEN_service_restarts_with_new_config() throws Throwable {
// GIVEN
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("single_service.yaml"));
CountDownLatch mainRunning = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.RUNNING)) {
mainRunning.countDown();
}
});
AtomicBoolean safeUpdateRegistered = new AtomicBoolean();
Consumer<GreengrassLogMessage> listener = (m) -> {
if ("register-service-update-action".equals(m.getEventType())) {
safeUpdateRegistered.set(true);
}
};
try (AutoCloseable l = createCloseableLogListener(listener)) {
kernel.launch();
assertTrue(mainRunning.await(5, TimeUnit.SECONDS));
// WHEN
CountDownLatch mainRestarted = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.FINISHED) && oldState.equals(State.STARTING)) {
mainRestarted.countDown();
}
});
deploymentConfigMerger.mergeInNewConfig(testDeployment(), new HashMap<String, Object>() {
{
put(SERVICES_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put("main", new HashMap<String, Object>() {
{
put(SETENV_CONFIG_NAMESPACE, new HashMap<String, Object>() {
{
put("HELLO", "redefined");
}
});
}
});
put(DEFAULT_NUCLEUS_COMPONENT_NAME, getNucleusConfig());
}
});
}
}).get(60, TimeUnit.SECONDS);
// THEN
assertTrue(mainRestarted.await(10, TimeUnit.SECONDS), "main restarted");
assertEquals("redefined", kernel.findServiceTopic("main").find(SETENV_CONFIG_NAMESPACE, "HELLO").getOnce());
assertTrue(safeUpdateRegistered.get(), "safe update registered");
}
}
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_deployment_with_skip_safety_check_config_THEN_merge_without_checking_safety.
@Test
void GIVEN_kernel_running_single_service_WHEN_deployment_with_skip_safety_check_config_THEN_merge_without_checking_safety() throws Throwable {
// GIVEN
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("single_service.yaml"));
Runnable mainRunning = createServiceStateChangeWaiter(kernel, "main", 5, State.RUNNING);
kernel.launch();
mainRunning.run();
// WHEN
Runnable mainRestarted = createServiceStateChangeWaiter(kernel, "main", 10, State.FINISHED, State.STARTING);
AtomicBoolean safeUpdateSkipped = new AtomicBoolean();
Consumer<GreengrassLogMessage> listener = (m) -> {
if ("Deployment is configured to skip update policy check, not waiting for disruptable time to update".equals(m.getMessage())) {
safeUpdateSkipped.set(true);
}
};
try (AutoCloseable l = createCloseableLogListener(listener)) {
deploymentConfigMerger.mergeInNewConfig(testDeploymentWithSkipPolicyCheckConfig(), new HashMap<String, Object>() {
{
put(SERVICES_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put("main", new HashMap<String, Object>() {
{
put(SETENV_CONFIG_NAMESPACE, new HashMap<String, Object>() {
{
put("HELLO", "redefined");
}
});
}
});
put(DEFAULT_NUCLEUS_COMPONENT_NAME, getNucleusConfig());
}
});
}
}).get(60, TimeUnit.SECONDS);
// THEN
mainRestarted.run();
assertEquals("redefined", kernel.findServiceTopic("main").find(SETENV_CONFIG_NAMESPACE, "HELLO").getOnce());
assertTrue(safeUpdateSkipped.get(), "safe updated skipped");
}
}
Aggregations