Search in sources :

Example 1 with ConfigurationValidityReport

use of software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport in project aws-greengrass-nucleus by aws-greengrass.

the class DynamicComponentConfigurationValidationTest method GIVEN_deployment_changes_component_config_WHEN_component_validates_config_THEN_deployment_is_successful.

@Test
void GIVEN_deployment_changes_component_config_WHEN_component_validates_config_THEN_deployment_is_successful() throws Throwable {
    LogConfig.getRootLogConfig().setLevel(Level.DEBUG);
    // Subscribe to config validation on behalf of the running service
    CountDownLatch eventReceivedByClient = new CountDownLatch(1);
    Topics servicePrivateConfig = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC, "OldService", PRIVATE_STORE_NAMESPACE_TOPIC);
    String authToken = Coerce.toString(servicePrivateConfig.find(SERVICE_UNIQUE_ID_KEY));
    CountDownLatch subscriptionLatch = new CountDownLatch(1);
    try (EventStreamRPCConnection clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
        AutoCloseable l = TestUtils.createCloseableLogListener(m -> {
            if (m.getMessage().contains("Config IPC subscribe to config validation request")) {
                subscriptionLatch.countDown();
            }
        })) {
        GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
        SubscribeToValidateConfigurationUpdatesRequest subscribe = new SubscribeToValidateConfigurationUpdatesRequest();
        greengrassCoreIPCClient.subscribeToValidateConfigurationUpdates(subscribe, Optional.of(new StreamResponseHandler<ValidateConfigurationUpdateEvents>() {

            @Override
            public void onStreamEvent(ValidateConfigurationUpdateEvents events) {
                assertNotNull(events);
                assertNotNull(events.getValidateConfigurationUpdateEvent());
                assertNotNull(events.getValidateConfigurationUpdateEvent().getConfiguration());
                assertThat(events.getValidateConfigurationUpdateEvent().getConfiguration(), IsMapContaining.hasEntry("ConfigKey1", "ConfigValue2"));
                eventReceivedByClient.countDown();
                SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
                ConfigurationValidityReport report = new ConfigurationValidityReport();
                report.setStatus(ConfigurationValidityStatus.ACCEPTED);
                report.setDeploymentId(events.getValidateConfigurationUpdateEvent().getDeploymentId());
                reportRequest.setConfigurationValidityReport(report);
                try {
                    greengrassCoreIPCClient.sendConfigurationValidityReport(reportRequest, Optional.empty()).getResponse().get(10, TimeUnit.SECONDS);
                } catch (InterruptedException | ExecutionException | TimeoutException e) {
                    fail("received invalid update validate configuration event", e);
                }
            }

            @Override
            public boolean onStreamError(Throwable error) {
                log.atError().log("Received stream error.", error);
                return false;
            }

            @Override
            public void onStreamClosed() {
            }
        }));
        assertTrue(subscriptionLatch.await(20, TimeUnit.SECONDS));
        // Attempt changing the configuration for the running service
        Map<String, Object> newConfig = new HashMap<String, Object>() {

            {
                put(SERVICES_NAMESPACE_TOPIC, new HashMap<String, Object>() {

                    {
                        put("main", kernel.getMain().getServiceConfig().toPOJO());
                        put("OldService", new HashMap<String, Object>() {

                            {
                                put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {

                                    {
                                        put("ConfigKey1", "ConfigValue2");
                                    }
                                });
                                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);
                            }
                        });
                        put(DEFAULT_NUCLEUS_COMPONENT_NAME, getNucleusConfig(kernel));
                    }
                });
            }
        };
        DeploymentResult result = deploymentConfigMerger.mergeInNewConfig(createTestDeployment(), newConfig).get(60, TimeUnit.SECONDS);
        assertEquals(DeploymentResult.DeploymentStatus.SUCCESSFUL, result.getDeploymentStatus());
        assertTrue(eventReceivedByClient.await(20, TimeUnit.SECONDS));
    }
}
Also used : Topics(com.aws.greengrass.config.Topics) ValidateConfigurationUpdateEvents(software.amazon.awssdk.aws.greengrass.model.ValidateConfigurationUpdateEvents) SubscribeToValidateConfigurationUpdatesRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesRequest) HashMap(java.util.HashMap) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) SendConfigurationValidityReportRequest(software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) CountDownLatch(java.util.concurrent.CountDownLatch) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) ExecutionException(java.util.concurrent.ExecutionException) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationValidityReport

use of software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport in project aws-greengrass-nucleus by aws-greengrass.

the class DynamicComponentConfigurationValidatorTest method GIVEN_deployment_has_internal_service_WHEN_validating_config_THEN_no_validation_attempted_for_internal_service.

@Test
void GIVEN_deployment_has_internal_service_WHEN_validating_config_THEN_no_validation_attempted_for_internal_service() throws Exception {
    when(configStoreIPCEventStreamAgent.validateConfiguration(any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        CompletableFuture<ConfigurationValidityReport> validityReportFuture = invocationOnMock.getArgument(3);
        ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
        validityReport.setStatus(ConfigurationValidityStatus.ACCEPTED);
        validityReportFuture.complete(validityReport);
        return true;
    });
    createMockGenericExternalService("OldService");
    createMockGreengrassService("OldInternalService");
    HashMap<String, Object> servicesConfig = new HashMap<String, Object>() {

        {
            put("OldService", new HashMap<String, Object>() {

                {
                    put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {

                        {
                            put("ConfigKey1", "ConfigValue2");
                        }
                    });
                    put(VERSION_CONFIG_KEY, DEFAULT_EXISTING_SERVICE_VERSION);
                }
            });
            put("OldInternalService", new HashMap<String, Object>() {

                {
                    put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {

                        {
                            put("ConfigKey1", "ConfigValue2");
                        }
                    });
                }
            });
        }
    };
    assertTrue(validator.validate(servicesConfig, createTestDeployment(), deploymentResultFuture));
    verify(configStoreIPCEventStreamAgent, times(1)).validateConfiguration(any(), any(), any(), any());
    assertFalse(deploymentResultFuture.isDone());
}
Also used : HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurationValidityReport

use of software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport in project aws-greengrass-nucleus by aws-greengrass.

the class ConfigStoreIPCEventStreamAgentTest method GIVEN_no_validation_event_is_tracked_WHEN_send_config_validity_report_request_THEN_fail.

@Test
void GIVEN_no_validation_event_is_tracked_WHEN_send_config_validity_report_request_THEN_fail() {
    when(mockAuthenticationData.getIdentityLabel()).thenReturn(TEST_COMPONENT_A);
    SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
    ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
    validityReport.setStatus(ConfigurationValidityStatus.ACCEPTED);
    validityReport.setDeploymentId("abc");
    reportRequest.setConfigurationValidityReport(validityReport);
    InvalidArgumentsError error = assertThrows(InvalidArgumentsError.class, () -> agent.getSendConfigurationValidityReportHandler(mockContext).handleRequest(reportRequest));
    assertEquals("Validation request either timed out or was never made", error.getMessage());
}
Also used : InvalidArgumentsError(software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError) SendConfigurationValidityReportRequest(software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigurationValidityReport

use of software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport in project aws-greengrass-nucleus by aws-greengrass.

the class DynamicComponentConfigurationValidatorTest method GIVEN_deployment_changes_service_config_WHEN_service_validates_config_THEN_succeed.

@Test
void GIVEN_deployment_changes_service_config_WHEN_service_validates_config_THEN_succeed() throws Exception {
    when(configStoreIPCEventStreamAgent.validateConfiguration(any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        CompletableFuture<ConfigurationValidityReport> validityReportFuture = invocationOnMock.getArgument(3);
        ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
        validityReport.setStatus(ConfigurationValidityStatus.ACCEPTED);
        validityReportFuture.complete(validityReport);
        return true;
    });
    createMockGenericExternalService("OldService");
    HashMap<String, Object> servicesConfig = new HashMap<String, Object>() {

        {
            put("OldService", new HashMap<String, Object>() {

                {
                    put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {

                        {
                            put("ConfigKey1", "ConfigValue2");
                        }
                    });
                    put(VERSION_CONFIG_KEY, DEFAULT_EXISTING_SERVICE_VERSION);
                }
            });
        }
    };
    assertTrue(validator.validate(servicesConfig, createTestDeployment(), deploymentResultFuture));
    verify(configStoreIPCEventStreamAgent, times(1)).validateConfiguration(any(), any(), any(), any());
    assertFalse(deploymentResultFuture.isDone());
}
Also used : HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Example 5 with ConfigurationValidityReport

use of software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport in project aws-greengrass-nucleus by aws-greengrass.

the class DynamicComponentConfigurationValidatorTest method GIVEN_deployment_changes_service_config_WHEN_service_invalidates_config_THEN_fail_deployment.

@Test
void GIVEN_deployment_changes_service_config_WHEN_service_invalidates_config_THEN_fail_deployment() throws Exception {
    when(configStoreIPCEventStreamAgent.validateConfiguration(any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        CompletableFuture<ConfigurationValidityReport> validityReportFuture = invocationOnMock.getArgument(3);
        ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
        validityReport.setStatus(ConfigurationValidityStatus.REJECTED);
        validityReport.setMessage("Proposed configuration is invalid");
        validityReportFuture.complete(validityReport);
        return true;
    });
    createMockGenericExternalService("OldService");
    HashMap<String, Object> servicesConfig = new HashMap<String, Object>() {

        {
            put("OldService", new HashMap<String, Object>() {

                {
                    put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {

                        {
                            put("ConfigKey1", "ConfigValue2");
                        }
                    });
                    put(VERSION_CONFIG_KEY, DEFAULT_EXISTING_SERVICE_VERSION);
                }
            });
        }
    };
    assertFalse(validator.validate(servicesConfig, createTestDeployment(), deploymentResultFuture));
    verify(configStoreIPCEventStreamAgent, times(1)).validateConfiguration(any(), any(), any(), any());
    DeploymentResult deploymentResult = deploymentResultFuture.get();
    assertEquals(DeploymentResult.DeploymentStatus.FAILED_NO_STATE_CHANGE, deploymentResult.getDeploymentStatus());
    assertTrue(deploymentResult.getFailureCause() instanceof ComponentConfigurationValidationException);
    assertTrue(deploymentResult.getFailureCause().getMessage() != null && deploymentResult.getFailureCause().getMessage().contains("Components reported that their to-be-deployed configuration is invalid { name = OldService, message = Proposed configuration is invalid }"));
}
Also used : HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) ComponentConfigurationValidationException(com.aws.greengrass.deployment.exceptions.ComponentConfigurationValidationException) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurationValidityReport (software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport)11 Test (org.junit.jupiter.api.Test)10 HashMap (java.util.HashMap)7 SendConfigurationValidityReportRequest (software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest)6 DeploymentResult (com.aws.greengrass.deployment.model.DeploymentResult)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ComponentConfigurationValidationException (com.aws.greengrass.deployment.exceptions.ComponentConfigurationValidationException)4 ExecutionException (java.util.concurrent.ExecutionException)4 SubscribeToValidateConfigurationUpdatesRequest (software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesRequest)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeoutException (java.util.concurrent.TimeoutException)3 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)3 SubscribeToValidateConfigurationUpdatesResponse (software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesResponse)3 ValidateConfigurationUpdateEvents (software.amazon.awssdk.aws.greengrass.model.ValidateConfigurationUpdateEvents)3 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)3 Topics (com.aws.greengrass.config.Topics)2 IOException (java.io.IOException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 InvalidArgumentsError (software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError)2 ConfigStoreIPCEventStreamAgent (com.aws.greengrass.builtin.services.configstore.ConfigStoreIPCEventStreamAgent)1