Search in sources :

Example 26 with Topics

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

the class DeploymentServiceTest method GIVEN_deployment_job_WHEN_deployment_process_succeeds_THEN_correctly_map_components_to_groups.

@ParameterizedTest
@EnumSource(Deployment.DeploymentType.class)
void GIVEN_deployment_job_WHEN_deployment_process_succeeds_THEN_correctly_map_components_to_groups(Deployment.DeploymentType type) throws Exception {
    String expectedGroupName = EXPECTED_GROUP_NAME;
    if (type.equals(Deployment.DeploymentType.LOCAL)) {
        expectedGroupName = LOCAL_DEPLOYMENT_GROUP_NAME;
    }
    String deploymentDocument = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("TestDeploymentDocument.json"), StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
    deploymentQueue.offer(new Deployment(deploymentDocument, type, TEST_JOB_ID_1));
    Topics allGroupTopics = Topics.of(context, GROUP_TO_ROOT_COMPONENTS_TOPICS, null);
    Topics groupMembershipTopics = Topics.of(context, GROUP_MEMBERSHIP_TOPICS, null);
    groupMembershipTopics.lookup(expectedGroupName);
    Topics deploymentGroupTopics = Topics.of(context, expectedGroupName, allGroupTopics);
    Topic pkgTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_VERSION_KEY, "1.0.0");
    Topic groupTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN, "arn:aws:greengrass:testRegion:12345:configuration:testGroup:12");
    Topic groupNameTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_NAME, expectedGroupName);
    Map<CaseInsensitiveString, Node> pkgDetails = new HashMap<>();
    pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_VERSION_KEY), pkgTopic1);
    pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN), groupTopic1);
    pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_NAME), groupNameTopic1);
    Topics pkgTopics = Topics.of(context, EXPECTED_ROOT_PACKAGE_NAME, deploymentGroupTopics);
    pkgTopics.children.putAll(pkgDetails);
    deploymentGroupTopics.children.put(new CaseInsensitiveString(EXPECTED_ROOT_PACKAGE_NAME), pkgTopics);
    allGroupTopics.children.put(new CaseInsensitiveString(expectedGroupName), deploymentGroupTopics);
    when(config.lookupTopics(GROUP_MEMBERSHIP_TOPICS)).thenReturn(groupMembershipTopics);
    when(config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS)).thenReturn(allGroupTopics);
    lenient().when(config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS, expectedGroupName)).thenReturn(deploymentGroupTopics);
    when(config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS)).thenReturn(mockComponentsToGroupPackages);
    when(mockKernel.locate(any())).thenReturn(mockGreengrassService);
    when(mockGreengrassService.getName()).thenReturn(EXPECTED_ROOT_PACKAGE_NAME);
    mockFuture.complete(new DeploymentResult(DeploymentStatus.SUCCESSFUL, null));
    when(mockExecutorService.submit(any(DefaultDeploymentTask.class))).thenReturn(mockFuture);
    doNothing().when(deploymentStatusKeeper).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.IN_PROGRESS.toString()), any());
    startDeploymentServiceInAnotherThread();
    verify(deploymentStatusKeeper, timeout(1000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.IN_PROGRESS.toString()), any());
    verify(deploymentStatusKeeper, timeout(10000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.SUCCEEDED.toString()), any());
    verify(mockExecutorService, timeout(1000)).submit(any(DefaultDeploymentTask.class));
    verify(deploymentStatusKeeper, timeout(2000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.SUCCEEDED.toString()), any());
    ArgumentCaptor<Map<String, Object>> mapCaptor = ArgumentCaptor.forClass(Map.class);
    verify(mockComponentsToGroupPackages).replaceAndWait(mapCaptor.capture());
    Map<String, Object> groupToRootPackages = mapCaptor.getValue();
    assertThat(groupToRootPackages, is(IsNull.notNullValue()));
    assertThat(groupToRootPackages.entrySet(), IsNot.not(IsEmptyCollection.empty()));
    assertThat(groupToRootPackages, hasKey(EXPECTED_ROOT_PACKAGE_NAME));
    assertThat((Map<String, Boolean>) groupToRootPackages.get(EXPECTED_ROOT_PACKAGE_NAME), hasKey("arn:aws:greengrass:testRegion:12345:configuration:testGroup:12"));
}
Also used : Topics(com.aws.greengrass.config.Topics) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Node(com.aws.greengrass.config.Node) Deployment(com.aws.greengrass.deployment.model.Deployment) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) BufferedReader(java.io.BufferedReader) Topic(com.aws.greengrass.config.Topic) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with Topics

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

the class DeviceConfigurationTest method beforeEach.

@BeforeEach
void beforeEach() {
    NucleusPaths nucleusPaths = mock(NucleusPaths.class);
    Topics rootConfigTopics = mock(Topics.class);
    when(rootConfigTopics.findOrDefault(any(), anyString(), anyString(), anyString())).thenReturn(new ArrayList<>());
    lenient().when(configuration.lookup(anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
    when(configuration.lookup(anyString(), anyString(), anyString(), anyString())).thenReturn(mockTopic);
    lenient().when(configuration.getRoot()).thenReturn(rootConfigTopics);
    when(mockKernel.getConfig()).thenReturn(configuration);
    lenient().when(mockKernel.getNucleusPaths()).thenReturn(nucleusPaths);
    Topics topics = Topics.of(mock(Context.class), SERVICES_NAMESPACE_TOPIC, mock(Topics.class));
    when(mockTopics.subscribe(any())).thenReturn(mockTopics);
    when(configuration.lookupTopics(anyString(), anyString(), anyString())).thenReturn(mockTopics);
    when(configuration.lookupTopics(anyString(), anyString(), anyString(), anyString())).thenReturn(mockTopics);
    when(configuration.lookupTopics(anyString())).thenReturn(topics);
    lenient().when(configuration.lookupTopics(anyString())).thenReturn(topics);
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) NucleusPaths(com.aws.greengrass.util.NucleusPaths) Topic(com.aws.greengrass.config.Topic) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 28 with Topics

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

the class DeviceConfigurationTest method GIVEN_no_existing_config_WHEN_init_device_config_THEN_use_default_nucleus_config.

@Test
void GIVEN_no_existing_config_WHEN_init_device_config_THEN_use_default_nucleus_config() throws Exception {
    try (Context context = new Context()) {
        Topics servicesConfig = Topics.of(context, SERVICES_NAMESPACE_TOPIC, null);
        lenient().when(configuration.lookupTopics(SERVICES_NAMESPACE_TOPIC)).thenReturn(servicesConfig);
        deviceConfiguration = new DeviceConfiguration(mockKernel);
        when(mockKernel.findServiceTopic(DEFAULT_NUCLEUS_COMPONENT_NAME)).thenReturn(servicesConfig.findTopics(DEFAULT_NUCLEUS_COMPONENT_NAME));
        // Expect fallback version in the absence of version information from build files
        assertEquals("0.0.0", deviceConfiguration.getNucleusVersion());
    }
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Test(org.junit.jupiter.api.Test)

Example 29 with Topics

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

the class DeviceConfigurationTest method GIVEN_existing_config_including_nucleus_version_WHEN_init_device_config_THEN_use_nucleus_version_from_config.

@Test
void GIVEN_existing_config_including_nucleus_version_WHEN_init_device_config_THEN_use_nucleus_version_from_config() throws Exception {
    try (Context context = new Context()) {
        Topics servicesConfig = Topics.of(context, SERVICES_NAMESPACE_TOPIC, null);
        Topics nucleusConfig = servicesConfig.lookupTopics(DEFAULT_NUCLEUS_COMPONENT_NAME);
        Topic componentTypeConfig = nucleusConfig.lookup(SERVICE_TYPE_TOPIC_KEY).withValue(ComponentType.NUCLEUS.name());
        Topic nucleusVersionConfig = nucleusConfig.lookup(VERSION_CONFIG_KEY).withValue("99.99.99");
        lenient().when(configuration.lookupTopics(SERVICES_NAMESPACE_TOPIC)).thenReturn(servicesConfig);
        lenient().when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, VERSION_CONFIG_KEY)).thenReturn(nucleusVersionConfig);
        lenient().when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, SERVICE_TYPE_TOPIC_KEY)).thenReturn(componentTypeConfig);
        when(mockKernel.findServiceTopic(DEFAULT_NUCLEUS_COMPONENT_NAME)).thenReturn(nucleusConfig);
        deviceConfiguration = new DeviceConfiguration(mockKernel);
        // Confirm version config didn't get overwritten with default
        assertEquals("99.99.99", Coerce.toString(nucleusVersionConfig));
        assertEquals("99.99.99", deviceConfiguration.getNucleusVersion());
    }
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Topic(com.aws.greengrass.config.Topic) Test(org.junit.jupiter.api.Test)

Example 30 with Topics

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

the class DeviceConfigurationTest method GIVEN_recipe_WHEN_initializeNucleusLifecycleConfig_THEN_init_lifecycle_and_dependencies.

@Test
void GIVEN_recipe_WHEN_initializeNucleusLifecycleConfig_THEN_init_lifecycle_and_dependencies(@Mock KernelConfigResolver kernelConfigResolver, @Mock Context context) throws Exception {
    deviceConfiguration = new DeviceConfiguration(mockKernel);
    List mockDependencies = Arrays.asList("A", "B", "C");
    doReturn(mockDependencies).when(kernelConfigResolver).generateServiceDependencies(anyMap());
    Object interpolatedLifecycle = new HashMap<String, String>() {

        {
            put("lifecycle", "step");
        }
    };
    doReturn(interpolatedLifecycle).when(kernelConfigResolver).interpolate(anyMap(), any(), anySet(), anyMap());
    doReturn(kernelConfigResolver).when(context).get(KernelConfigResolver.class);
    doReturn(context).when(mockKernel).getContext();
    String nucleusComponentName = "test.component.name";
    ComponentRecipe componentRecipe = ComponentRecipe.builder().lifecycle((Map<String, Object>) interpolatedLifecycle).build();
    Topics lifecycleTopics = spy(Topics.of(context, SERVICE_LIFECYCLE_NAMESPACE_TOPIC, null));
    when(configuration.lookupTopics(eq(DEFAULT_VALUE_TIMESTAMP), eq(SERVICES_NAMESPACE_TOPIC), eq(nucleusComponentName), eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC))).thenReturn(lifecycleTopics);
    Topic dependencyTopic = Topic.of(context, SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, null);
    when(configuration.lookup(eq(DEFAULT_VALUE_TIMESTAMP), eq(SERVICES_NAMESPACE_TOPIC), eq(nucleusComponentName), eq(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC))).thenReturn(dependencyTopic);
    deviceConfiguration.initializeNucleusLifecycleConfig(nucleusComponentName, componentRecipe);
    assertEquals(mockDependencies, dependencyTopic.toPOJO());
    verify(lifecycleTopics).replaceAndWait((Map<String, Object>) interpolatedLifecycle);
}
Also used : Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Topic(com.aws.greengrass.config.Topic) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Test(org.junit.jupiter.api.Test)

Aggregations

Topics (com.aws.greengrass.config.Topics)151 Test (org.junit.jupiter.api.Test)104 Topic (com.aws.greengrass.config.Topic)65 HashMap (java.util.HashMap)50 Map (java.util.Map)29 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)28 Context (com.aws.greengrass.dependency.Context)24 CountDownLatch (java.util.concurrent.CountDownLatch)24 IOException (java.io.IOException)23 ExecutionException (java.util.concurrent.ExecutionException)22 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)21 HashSet (java.util.HashSet)19 BeforeEach (org.junit.jupiter.api.BeforeEach)18 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)17 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ArrayList (java.util.ArrayList)15 Path (java.nio.file.Path)13 ComponentRecipe (com.aws.greengrass.componentmanager.models.ComponentRecipe)12