Search in sources :

Example 1 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class KernelTest method GIVEN_kernel_WHEN_locate_finds_definition_in_config_THEN_create_GenericExternalService.

@Test
void GIVEN_kernel_WHEN_locate_finds_definition_in_config_THEN_create_GenericExternalService() throws Exception {
    Configuration config = kernel.getConfig();
    config.lookupTopics(GreengrassService.SERVICES_NAMESPACE_TOPIC, "1", GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC);
    GreengrassService main = kernel.locate("1");
    assertEquals("1", main.getName());
}
Also used : Configuration(com.aws.greengrass.config.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Test(org.junit.jupiter.api.Test)

Example 2 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class KernelTest method GIVEN_kernel_WHEN_locate_finds_classname_but_not_class_THEN_throws_ServiceLoadException.

@Test
void GIVEN_kernel_WHEN_locate_finds_classname_but_not_class_THEN_throws_ServiceLoadException() {
    String badClassName = TestClass.class.getName() + "lkajsdklajglsdj";
    Configuration config = kernel.getConfig();
    config.lookup(GreengrassService.SERVICES_NAMESPACE_TOPIC, "2", "class").withValue(badClassName);
    ServiceLoadException ex = assertThrows(ServiceLoadException.class, () -> kernel.locate("2"));
    assertEquals("Can't load service class from " + badClassName, ex.getMessage());
}
Also used : Configuration(com.aws.greengrass.config.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) Test(org.junit.jupiter.api.Test)

Example 3 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleTest method GIVEN_service_starting_WHEN_dependency_errored_THEN_service_restarted.

@Test
void GIVEN_service_starting_WHEN_dependency_errored_THEN_service_restarted() throws Exception {
    Topics serviceRoot = new Configuration(context).getRoot().createInteriorChild(GreengrassService.SERVICES_NAMESPACE_TOPIC);
    Topics testServiceTopics = serviceRoot.createInteriorChild("testService");
    TestService testService = new TestService(testServiceTopics);
    Topics dependencyServiceTopics = serviceRoot.createInteriorChild("dependencyService");
    TestService dependencyService = new TestService(dependencyServiceTopics);
    testService.addOrUpdateDependency(dependencyService, DependencyType.HARD, false);
    CountDownLatch serviceStarted = new CountDownLatch(1);
    testService.setStartupRunnable(() -> {
        try {
            serviceStarted.countDown();
            Thread.sleep(10_000);
        } catch (InterruptedException ie) {
            return;
        }
    });
    dependencyService.setStartupRunnable(() -> dependencyService.reportState(State.RUNNING));
    // init lifecycle
    testService.postInject();
    testService.requestStart();
    dependencyService.postInject();
    dependencyService.requestStart();
    // GIVEN service in state STARTING
    assertTrue(serviceStarted.await(1500, TimeUnit.MILLISECONDS));
    assertEquals(State.STARTING, testService.getState());
    CountDownLatch serviceRestarted = new CountDownLatch(2);
    context.addGlobalStateChangeListener((service, oldState, newState) -> {
        if (!"testService".equals(service.getName())) {
            return;
        }
        if (State.STARTING.equals(oldState) && State.STOPPING.equals(newState) && serviceRestarted.getCount() == 2) {
            serviceRestarted.countDown();
            return;
        }
        if (State.INSTALLED.equals(oldState) && State.STARTING.equals(newState) && serviceRestarted.getCount() == 1) {
            serviceRestarted.countDown();
            return;
        }
    });
    // WHEN dependency errored
    dependencyService.reportState(State.ERRORED);
    // THEN
    assertTrue(serviceRestarted.await(500, TimeUnit.MILLISECONDS));
}
Also used : Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleTest method GIVEN_service_running_WHEN_service_broken_THEN_service_is_stopped.

@Test
void GIVEN_service_running_WHEN_service_broken_THEN_service_is_stopped() throws Exception {
    Topics serviceRoot = new Configuration(context).getRoot().createInteriorChild(GreengrassService.SERVICES_NAMESPACE_TOPIC);
    Topics testServiceTopics = serviceRoot.createInteriorChild("testService");
    TestService testService = new TestService(testServiceTopics);
    AtomicInteger serviceStartedCount = new AtomicInteger();
    AtomicInteger serviceStoppedCount = new AtomicInteger();
    AtomicInteger serviceInterruptedCount = new AtomicInteger();
    testService.setStartupRunnable(() -> {
        try {
            serviceStartedCount.incrementAndGet();
            testService.reportState(State.ERRORED);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            serviceInterruptedCount.incrementAndGet();
        }
    });
    testService.setShutdownRunnable(() -> serviceStoppedCount.incrementAndGet());
    // init lifecycle
    testService.postInject();
    testService.requestStart();
    assertThat(testService::getState, eventuallyEval(is(State.BROKEN)));
    assertThat(serviceStoppedCount::get, eventuallyEval(is(serviceStartedCount.get())));
    assertThat(serviceInterruptedCount::get, eventuallyEval(is(serviceStartedCount.get())));
    // assert that service remains in BROKEN state
    assertEquals(State.BROKEN, testService.getState());
}
Also used : Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 5 with Configuration

use of com.aws.greengrass.config.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class KernelUpdateDeploymentTaskTest method beforeEach.

@BeforeEach
void beforeEach() throws Exception {
    lenient().doReturn(kernelAlternatives).when(context).get(KernelAlternatives.class);
    lenient().doReturn(deploymentDirectoryManager).when(context).get(DeploymentDirectoryManager.class);
    lenient().doReturn(context).when(kernel).getContext();
    lenient().doReturn("A").when(greengrassService).getName();
    lenient().doReturn(mainService).when(kernel).getMain();
    lenient().doReturn(true).when(greengrassService).shouldAutoStart();
    lenient().doReturn(Arrays.asList(greengrassService)).when(kernel).orderedDependencies();
    lenient().doNothing().when(componentManager).cleanupStaleVersions();
    Topic topic = mock(Topic.class);
    lenient().doReturn(1L).when(topic).getModtime();
    Configuration configuration = mock(Configuration.class);
    lenient().doReturn(topic).when(configuration).lookup(any());
    lenient().doReturn(configuration).when(kernel).getConfig();
    DeploymentDocument document = mock(DeploymentDocument.class);
    doReturn("mockId").when(document).getDeploymentId();
    doReturn(document).when(deployment).getDeploymentDocumentObj();
    task = new KernelUpdateDeploymentTask(kernel, logger, deployment, componentManager);
}
Also used : Configuration(com.aws.greengrass.config.Configuration) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Topic(com.aws.greengrass.config.Topic) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Configuration (com.aws.greengrass.config.Configuration)20 Context (com.aws.greengrass.dependency.Context)12 Topics (com.aws.greengrass.config.Topics)8 Test (org.junit.jupiter.api.Test)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 Topic (com.aws.greengrass.config.Topic)3 UpdateBehaviorTree (com.aws.greengrass.config.UpdateBehaviorTree)3 Path (java.nio.file.Path)3 Map (java.util.Map)3 ComponentConfiguration (com.amazon.aws.iot.greengrass.component.common.ComponentConfiguration)2 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)2 KernelLifecycle (com.aws.greengrass.lifecyclemanager.KernelLifecycle)2 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)2 InputStream (java.io.InputStream)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2 ConfigurationWriter (com.aws.greengrass.config.ConfigurationWriter)1 State (com.aws.greengrass.dependency.State)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1