Search in sources :

Example 6 with Kernel

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);
}
Also used : TestUtils.createCloseableLogListener(com.aws.greengrass.testcommons.testutilities.TestUtils.createCloseableLogListener) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) Deployment(com.aws.greengrass.deployment.model.Deployment) TimeoutException(java.util.concurrent.TimeoutException) Matchers.containsInRelativeOrder(org.hamcrest.Matchers.containsInRelativeOrder) ComponentUpdatePolicyEvents(software.amazon.awssdk.aws.greengrass.model.ComponentUpdatePolicyEvents) SubscribeToComponentUpdatesResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToComponentUpdatesResponse) AfterAll(org.junit.jupiter.api.AfterAll) DEFAULT_NUCLEUS_COMPONENT_NAME(com.aws.greengrass.deployment.DeviceConfiguration.DEFAULT_NUCLEUS_COMPONENT_NAME) IPCTestUtils(com.aws.greengrass.integrationtests.ipc.IPCTestUtils) Future(java.util.concurrent.Future) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) State(com.aws.greengrass.dependency.State) BaseITCase(com.aws.greengrass.integrationtests.BaseITCase) BeforeAll(org.junit.jupiter.api.BeforeAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GenericExternalService(com.aws.greengrass.lifecyclemanager.GenericExternalService) FleetStatusService(com.aws.greengrass.status.FleetStatusService) DEFAULT(com.aws.greengrass.deployment.model.Deployment.DeploymentStage.DEFAULT) Duration(java.time.Duration) Map(java.util.Map) LogManager(com.aws.greengrass.logging.impl.LogManager) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) ComponentUpdatePolicy(com.aws.greengrass.deployment.model.ComponentUpdatePolicy) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) EventuallyLambdaMatcher.eventuallyEval(com.github.grantwest.eventually.EventuallyLambdaMatcher.eventuallyEval) Collectors(java.util.stream.Collectors) DeferComponentUpdateRequest(software.amazon.awssdk.aws.greengrass.model.DeferComponentUpdateRequest) TestUtils.createServiceStateChangeWaiter(com.aws.greengrass.testcommons.testutilities.TestUtils.createServiceStateChangeWaiter) Test(org.junit.jupiter.api.Test) Topics(com.aws.greengrass.config.Topics) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) GlobalStateChangeListener(com.aws.greengrass.lifecyclemanager.GlobalStateChangeListener) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) LIFECYCLE_RUN_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GenericExternalService.LIFECYCLE_RUN_NAMESPACE_TOPIC) Matchers.is(org.hamcrest.Matchers.is) DeploymentConfigMerger(com.aws.greengrass.deployment.DeploymentConfigMerger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigPlatformResolver(com.aws.greengrass.integrationtests.util.ConfigPlatformResolver) DeploymentConfigurationValidationPolicy(software.amazon.awssdk.services.greengrassv2.model.DeploymentConfigurationValidationPolicy) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Coerce(com.aws.greengrass.util.Coerce) SKIP_NOTIFY_COMPONENTS(software.amazon.awssdk.services.greengrassv2.model.DeploymentComponentUpdatePolicyAction.SKIP_NOTIFY_COMPONENTS) ArrayList(java.util.ArrayList) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) SudoUtil.assumeCanSudoShell(com.aws.greengrass.testcommons.testutilities.SudoUtil.assumeCanSudoShell) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PlatformResolver(com.aws.greengrass.config.PlatformResolver) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) SystemUtils(org.apache.commons.lang3.SystemUtils) Matchers(org.hamcrest.Matchers) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) IOException(java.io.IOException) NOTIFY_COMPONENTS(software.amazon.awssdk.services.greengrassv2.model.DeploymentComponentUpdatePolicyAction.NOTIFY_COMPONENTS) SubscribeToComponentUpdatesRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToComponentUpdatesRequest) SUCCESSFUL(com.aws.greengrass.deployment.model.DeploymentResult.DeploymentStatus.SUCCESSFUL) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Topic(com.aws.greengrass.config.Topic) AfterEach(org.junit.jupiter.api.AfterEach) Matchers.hasItem(org.hamcrest.Matchers.hasItem) WhatHappened(com.aws.greengrass.config.WhatHappened) NoOpPathOwnershipHandler(com.aws.greengrass.testcommons.testutilities.NoOpPathOwnershipHandler) FailureHandlingPolicy(com.aws.greengrass.deployment.model.FailureHandlingPolicy) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) Logger(com.aws.greengrass.logging.api.Logger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) GlobalStateChangeListener(com.aws.greengrass.lifecyclemanager.GlobalStateChangeListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with Kernel

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);
}
Also used : FleetStatusService(com.aws.greengrass.status.FleetStatusService) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) DeploymentQueue(com.aws.greengrass.deployment.DeploymentQueue) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with Kernel

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");
}
Also used : MqttClient(com.aws.greengrass.mqttclient.MqttClient) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) Test(org.junit.jupiter.api.Test)

Example 9 with Kernel

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);
}
Also used : MqttClient(com.aws.greengrass.mqttclient.MqttClient) Topics(com.aws.greengrass.config.Topics) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with 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);
}
Also used : GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Kernel (com.aws.greengrass.lifecyclemanager.Kernel)49 CountDownLatch (java.util.concurrent.CountDownLatch)22 BeforeEach (org.junit.jupiter.api.BeforeEach)22 Test (org.junit.jupiter.api.Test)22 HashMap (java.util.HashMap)13 IOException (java.io.IOException)12 DeploymentConfigMerger (com.aws.greengrass.deployment.DeploymentConfigMerger)11 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)11 Map (java.util.Map)11 List (java.util.List)10 TimeUnit (java.util.concurrent.TimeUnit)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10 State (com.aws.greengrass.dependency.State)9 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)9 BaseITCase (com.aws.greengrass.integrationtests.BaseITCase)9 ConfigPlatformResolver (com.aws.greengrass.integrationtests.util.ConfigPlatformResolver)9 TestUtils (com.aws.greengrass.testcommons.testutilities.TestUtils)9 Coerce (com.aws.greengrass.util.Coerce)9 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)9 LogManager (com.aws.greengrass.logging.impl.LogManager)8