Search in sources :

Example 1 with CreateDeploymentRequest

use of software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentE2ETest method GIVEN_target_service_has_dependencies_WHEN_deploys_target_service_THEN_service_and_dependencies_should_be_deployed.

@Timeout(value = 10, unit = TimeUnit.MINUTES)
@Test
void GIVEN_target_service_has_dependencies_WHEN_deploys_target_service_THEN_service_and_dependencies_should_be_deployed() throws Exception {
    // Set up stdout listener to capture stdout for verify interpolation
    List<String> stdouts = new CopyOnWriteArrayList<>();
    Consumer<GreengrassLogMessage> listener = m -> {
        String messageOnStdout = m.getMessage();
        if (messageOnStdout != null && messageOnStdout.contains("CustomerApp output.")) {
            stdouts.add(messageOnStdout);
            // countdown when received output to verify
            stdoutCountdown.countDown();
        }
    };
    try (AutoCloseable l = TestUtils.createCloseableLogListener(listener)) {
        stdoutCountdown = new CountDownLatch(1);
        // 1st Deployment to have some services running in Kernel with default configuration
        CreateDeploymentRequest createDeployment1 = CreateDeploymentRequest.builder().components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build())).build();
        CreateDeploymentResponse createDeploymentResult1 = draftAndCreateDeployment(createDeployment1);
        IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, createDeploymentResult1.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(2), s -> s.equals(JobExecutionStatus.SUCCEEDED));
        assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
        assertThat(getCloudDeployedComponent("CustomerApp")::getState, eventuallyEval(is(State.FINISHED)));
        assertThat(getCloudDeployedComponent("Mosquitto")::getState, eventuallyEval(is(State.RUNNING)));
        assertThat(getCloudDeployedComponent("GreenSignal")::getState, eventuallyEval(is(State.FINISHED)));
        // verify config in kernel
        Map<String, Object> resultConfig = getCloudDeployedComponent("CustomerApp").getServiceConfig().findTopics(KernelConfigResolver.CONFIGURATION_CONFIG_KEY).toPOJO();
        assertThat(resultConfig, IsMapWithSize.aMapWithSize(3));
        assertThat(resultConfig, IsMapContaining.hasEntry("sampleText", "This is a test"));
        assertThat(resultConfig, IsMapContaining.hasEntry("listKey", Arrays.asList("item1", "item2")));
        assertThat(resultConfig, IsMapContaining.hasKey("path"));
        assertThat((Map<String, String>) resultConfig.get("path"), IsMapContaining.hasEntry("leafKey", "default value of /path/leafKey"));
        // verify stdout
        assertThat("The stdout should be captured within seconds.", stdoutCountdown.await(5, TimeUnit.SECONDS));
        String customerAppStdout = stdouts.get(0);
        assertThat(customerAppStdout, containsString("This is a test"));
        assertThat(customerAppStdout, containsString("Value for /path/leafKey: default value of /path/leafKey."));
        assertThat(customerAppStdout, containsString("Value for /listKey/0: item1."));
        assertThat(customerAppStdout, containsString("Value for /newKey: {configuration:/newKey}"));
        // reset countdown and stdouts
        stdoutCountdown = new CountDownLatch(1);
        stdouts.clear();
        // 2nd deployment to merge
        /*
             * {
             *   "MERGE": {
             *     "sampleText": "updated value for sampleText",
             *     "listKey": [
             *       "item3"
             *     ],
             *     "path": {
             *       "leafKey": "updated value of /path/leafKey"
             *     }
             *   }
             * }
             */
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode configUpdateInNode = mapper.createObjectNode();
        ObjectNode mergeNode = configUpdateInNode.with("MERGE");
        mergeNode.put("sampleText", "updated");
        mergeNode.put("newKey", "updated");
        mergeNode.withArray("listKey").add("item3");
        mergeNode.with("path").put("leafKey", "updated");
        CreateDeploymentRequest createDeployment2 = CreateDeploymentRequest.builder().targetArn(thingGroupArn).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").configurationUpdate(ComponentConfigurationUpdate.builder().merge(mapper.writeValueAsString(mergeNode)).build()).build())).build();
        CreateDeploymentResponse createDeploymentResult2 = draftAndCreateDeployment(createDeployment2);
        IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, createDeploymentResult2.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(2), s -> s.equals(JobExecutionStatus.SUCCEEDED));
        assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
        assertThat(getCloudDeployedComponent("CustomerApp")::getState, eventuallyEval(is(State.FINISHED)));
        assertThat(getCloudDeployedComponent("Mosquitto")::getState, eventuallyEval(is(State.RUNNING)));
        assertThat(getCloudDeployedComponent("GreenSignal")::getState, eventuallyEval(is(State.FINISHED)));
        // verify config in kernel
        resultConfig = getCloudDeployedComponent("CustomerApp").getServiceConfig().findTopics(KernelConfigResolver.CONFIGURATION_CONFIG_KEY).toPOJO();
        assertThat(resultConfig, IsMapWithSize.aMapWithSize(4));
        assertThat(resultConfig, IsMapContaining.hasEntry("sampleText", "updated"));
        assertThat(resultConfig, IsMapContaining.hasEntry("listKey", Collections.singletonList("item3")));
        assertThat(resultConfig, IsMapContaining.hasKey("path"));
        assertThat((Map<String, String>) resultConfig.get("path"), IsMapContaining.hasEntry("leafKey", "updated"));
        // verify stdout
        assertThat("The stdout should be captured within seconds.", stdoutCountdown.await(5, TimeUnit.SECONDS));
        customerAppStdout = stdouts.get(0);
        assertThat(customerAppStdout, containsString("Value for /sampleText: updated"));
        assertThat(customerAppStdout, containsString("Value for /path/leafKey: updated"));
        assertThat(customerAppStdout, containsString("Value for /listKey/0: item3."));
        assertThat(customerAppStdout, containsString("Value for /newKey: updated"));
        // reset countdown and stdouts
        stdoutCountdown = new CountDownLatch(1);
        stdouts.clear();
        // 3rd deployment to reset
        CreateDeploymentRequest createDeployment3 = CreateDeploymentRequest.builder().targetArn(thingGroupArn).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").configurationUpdate(ComponentConfigurationUpdate.builder().reset("/sampleText", "/path").build()).build())).build();
        CreateDeploymentResponse createDeploymentResult = draftAndCreateDeployment(createDeployment3);
        IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, createDeploymentResult.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(2), s -> s.equals(JobExecutionStatus.SUCCEEDED));
        assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
        assertThat(getCloudDeployedComponent("CustomerApp")::getState, eventuallyEval(is(State.FINISHED)));
        assertThat(getCloudDeployedComponent("Mosquitto")::getState, eventuallyEval(is(State.RUNNING)));
        assertThat(getCloudDeployedComponent("GreenSignal")::getState, eventuallyEval(is(State.FINISHED)));
        // verify config in kernel
        resultConfig = getCloudDeployedComponent("CustomerApp").getServiceConfig().findTopics(KernelConfigResolver.CONFIGURATION_CONFIG_KEY).toPOJO();
        assertThat(resultConfig, IsMapWithSize.aMapWithSize(4));
        assertThat(resultConfig, IsMapContaining.hasEntry("sampleText", "This is a test"));
        assertThat(resultConfig, IsMapContaining.hasEntry("listKey", Collections.singletonList("item3")));
        assertThat(resultConfig, IsMapContaining.hasKey("path"));
        assertThat((Map<String, String>) resultConfig.get("path"), IsMapContaining.hasEntry("leafKey", "default value of /path/leafKey"));
        // verify stdout
        assertThat("The stdout should be captured within seconds.", stdoutCountdown.await(5, TimeUnit.SECONDS));
        customerAppStdout = stdouts.get(0);
        assertThat(customerAppStdout, containsString("Value for /sampleText: This is a test"));
        assertThat(customerAppStdout, containsString("Value for /path/leafKey: default value of /path/leafKey"));
        assertThat(customerAppStdout, containsString("Value for /listKey/0: item3."));
        assertThat(customerAppStdout, containsString("Value for /newKey: updated"));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ComponentDeploymentSpecification(software.amazon.awssdk.services.greengrassv2.model.ComponentDeploymentSpecification) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) NoAvailableComponentVersionException(com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException) ComponentUpdatePolicyEvents(software.amazon.awssdk.aws.greengrass.model.ComponentUpdatePolicyEvents) IPCTestUtils(com.aws.greengrass.integrationtests.ipc.IPCTestUtils) SERVICE_LIFECYCLE_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) State(com.aws.greengrass.dependency.State) Duration(java.time.Duration) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) DeploymentPolicies(software.amazon.awssdk.services.greengrassv2.model.DeploymentPolicies) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) Set(java.util.Set) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) UUID(java.util.UUID) EventuallyLambdaMatcher.eventuallyEval(com.github.grantwest.eventually.EventuallyLambdaMatcher.eventuallyEval) ROLLBACK(software.amazon.awssdk.services.greengrassv2.model.DeploymentFailureHandlingPolicy.ROLLBACK) DeferComponentUpdateRequest(software.amazon.awssdk.aws.greengrass.model.DeferComponentUpdateRequest) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) DeploymentService(com.aws.greengrass.deployment.DeploymentService) IotJobsUtils(com.aws.greengrass.integrationtests.e2e.util.IotJobsUtils) DEPLOYMENT_DETAILED_STATUS_KEY(com.aws.greengrass.deployment.DeploymentService.DEPLOYMENT_DETAILED_STATUS_KEY) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JobExecutionStatus(software.amazon.awssdk.services.iot.model.JobExecutionStatus) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ExceptionLogProtector.ignoreExceptionUltimateCauseWithMessage(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionUltimateCauseWithMessage) DeploymentConfigurationValidationPolicy(software.amazon.awssdk.services.greengrassv2.model.DeploymentConfigurationValidationPolicy) UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) BaseE2ETestCase(com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SKIP_NOTIFY_COMPONENTS(software.amazon.awssdk.services.greengrassv2.model.DeploymentComponentUpdatePolicyAction.SKIP_NOTIFY_COMPONENTS) CreateDeploymentResponse(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse) StringContains.containsString(org.hamcrest.core.StringContains.containsString) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) IsCollectionWithSize(org.hamcrest.collection.IsCollectionWithSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) DO_NOTHING(software.amazon.awssdk.services.greengrassv2.model.DeploymentFailureHandlingPolicy.DO_NOTHING) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ComponentConfigurationUpdate(software.amazon.awssdk.services.greengrassv2.model.ComponentConfigurationUpdate) ExceptionLogProtector.ignoreExceptionUltimateCauseOfType(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionUltimateCauseOfType) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) NOTIFY_COMPONENTS(software.amazon.awssdk.services.greengrassv2.model.DeploymentComponentUpdatePolicyAction.NOTIFY_COMPONENTS) SubscribeToComponentUpdatesRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToComponentUpdatesRequest) IsMapContaining(org.hamcrest.collection.IsMapContaining) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Slf4jLogAdapter(com.aws.greengrass.logging.impl.Slf4jLogAdapter) AfterEach(org.junit.jupiter.api.AfterEach) DescribeJobExecutionRequest(software.amazon.awssdk.services.iot.model.DescribeJobExecutionRequest) Utils(com.aws.greengrass.util.Utils) KernelConfigResolver(com.aws.greengrass.componentmanager.KernelConfigResolver) DeploymentComponentUpdatePolicy(software.amazon.awssdk.services.greengrassv2.model.DeploymentComponentUpdatePolicy) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) IsMapWithSize(org.hamcrest.collection.IsMapWithSize) Timeout(org.junit.jupiter.api.Timeout) Collections(java.util.Collections) CreateDeploymentResponse(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with CreateDeploymentRequest

use of software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MultipleGroupsDeploymentE2ETest method GIVEN_deployment_to_2_groups_WHEN_remove_service_from_1_group_THEN_service_is_removed.

@Timeout(value = 10, unit = TimeUnit.MINUTES)
@Test
void GIVEN_deployment_to_2_groups_WHEN_remove_service_from_1_group_THEN_service_is_removed() throws Exception {
    CreateDeploymentRequest createDeploymentRequest1 = CreateDeploymentRequest.builder().targetArn(thingGroupArn).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("0.9.1").configurationUpdate(ComponentConfigurationUpdate.builder().merge("{\"sampleText\":\"FCS integ test\"}").build()).build(), "SomeService", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build())).build();
    CreateDeploymentResponse result1 = draftAndCreateDeployment(createDeploymentRequest1);
    IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, result1.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(5), s -> s.equals(JobExecutionStatus.SUCCEEDED));
    Topics groupToRootMapping = kernel.getConfig().lookupTopics(DEPLOYMENT_SERVICE_TOPICS, GROUP_TO_ROOT_COMPONENTS_TOPICS);
    logger.atInfo().log("Group to root mapping is: " + groupToRootMapping.toString());
    CreateDeploymentRequest createDeploymentRequest2 = CreateDeploymentRequest.builder().targetArn(secondThingGroupResponse.thingGroupArn()).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("0.9.1").configurationUpdate(ComponentConfigurationUpdate.builder().merge("{\"sampleText\":\"FCS integ test\"}").build()).build())).build();
    CreateDeploymentResponse result2 = draftAndCreateDeployment(createDeploymentRequest2);
    IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, result2.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(5), s -> s.equals(JobExecutionStatus.SUCCEEDED));
    CreateDeploymentRequest createDeploymentRequest3 = CreateDeploymentRequest.builder().targetArn(thingGroupArn).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("0.9.1").configurationUpdate(ComponentConfigurationUpdate.builder().merge("{\"sampleText\":\"FCS integ test\"}").build()).build())).build();
    CreateDeploymentResponse result3 = draftAndCreateDeployment(createDeploymentRequest3);
    IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, result3.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(5), s -> s.equals(JobExecutionStatus.SUCCEEDED));
    assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
    assertThrows(ServiceLoadException.class, () -> {
        GreengrassService service = getCloudDeployedComponent("SomeService");
        logger.atInfo().log("Service is " + service.getName());
    });
}
Also used : CreateDeploymentResponse(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse) Topics(com.aws.greengrass.config.Topics) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 3 with CreateDeploymentRequest

use of software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MultipleGroupsDeploymentE2ETest method GIVEN_deployment_to_2_groups_WHEN_both_deployments_have_same_service_different_version_THEN_second_deployment_fails_due_to_conflict.

@Timeout(value = 10, unit = TimeUnit.MINUTES)
@Test
void GIVEN_deployment_to_2_groups_WHEN_both_deployments_have_same_service_different_version_THEN_second_deployment_fails_due_to_conflict(ExtensionContext context) throws Exception {
    ignoreExceptionOfType(context, ExecutionException.class);
    ignoreExceptionOfType(context, // Expect this to happen due to conflict
    NoAvailableComponentVersionException.class);
    CreateDeploymentRequest createDeploymentRequest1 = CreateDeploymentRequest.builder().targetArn(thingGroupArn).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("0.9.1").configurationUpdate(ComponentConfigurationUpdate.builder().merge("{\"sampleText\":\"FCS integ test\"}").build()).build())).build();
    CreateDeploymentResponse result1 = draftAndCreateDeployment(createDeploymentRequest1);
    IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, result1.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(5), s -> s.equals(JobExecutionStatus.SUCCEEDED));
    Topics groupToRootMapping = kernel.getConfig().lookupTopics(DEPLOYMENT_SERVICE_TOPICS, GROUP_TO_ROOT_COMPONENTS_TOPICS);
    logger.atInfo().log("Group to root mapping is: " + groupToRootMapping.toString());
    CreateDeploymentRequest createDeploymentRequest2 = CreateDeploymentRequest.builder().targetArn(secondThingGroupResponse.thingGroupArn()).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").configurationUpdate(ComponentConfigurationUpdate.builder().merge("{\"sampleText\":\"FCS integ test\"}").build()).build())).build();
    CreateDeploymentResponse result2 = draftAndCreateDeployment(createDeploymentRequest2);
    IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, result2.iotJobId(), thingInfo.getThingName(), Duration.ofMinutes(5), s -> s.equals(JobExecutionStatus.FAILED));
    assertThat("Incorrect component version running", getCloudDeployedComponent("CustomerApp").getServiceConfig().find(VERSION_CONFIG_KEY).getOnce().toString(), is("0.9.1"));
}
Also used : CreateDeploymentResponse(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse) Topics(com.aws.greengrass.config.Topics) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 4 with CreateDeploymentRequest

use of software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest in project aws-greengrass-nucleus by aws-greengrass.

the class ShadowDeploymentE2ETest method GIVEN_device_deployment_WHEN_shadow_update_messages_gets_delivered_out_of_order_THEN_shadow_updated_with_latest_deployment_status.

@Test
void GIVEN_device_deployment_WHEN_shadow_update_messages_gets_delivered_out_of_order_THEN_shadow_updated_with_latest_deployment_status() throws Exception {
    CreateDeploymentRequest createDeploymentRequest = CreateDeploymentRequest.builder().targetArn(thingInfo.getThingArn()).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build(), "SomeService", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build())).build();
    draftAndCreateDeployment(createDeploymentRequest);
    assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
    IotShadowClient shadowClient = new IotShadowClient(new WrapperMqttClientConnection(kernel.getContext().get(MqttClient.class)));
    UpdateNamedShadowSubscriptionRequest req = new UpdateNamedShadowSubscriptionRequest();
    req.shadowName = DEPLOYMENT_SHADOW_NAME;
    req.thingName = thingInfo.getThingName();
    CountDownLatch reportSucceededCdl = new CountDownLatch(1);
    CountDownLatch deviceSyncedStateToSucceededCdl = new CountDownLatch(1);
    AtomicReference<HashMap<String, Object>> reportedSection = new AtomicReference<>();
    AtomicReference<Integer> shadowVersionWhenDeviceFirstReportedSuccess = new AtomicReference<>();
    AtomicReference<Integer> shadowVersionWhenDeviceReportedInProgress = new AtomicReference<>();
    shadowClient.SubscribeToUpdateNamedShadowAccepted(req, QualityOfService.AT_LEAST_ONCE, (response) -> {
        try {
            logger.info("Got shadow update: {}", new ObjectMapper().writeValueAsString(response));
        } catch (JsonProcessingException e) {
        // ignore
        }
        if (response.state.reported == null) {
            return;
        }
        String reportedStatus = (String) response.state.reported.get(STATUS_KEY);
        if (JobStatus.IN_PROGRESS.toString().equals(reportedStatus)) {
            reportedSection.set(response.state.reported);
            shadowVersionWhenDeviceReportedInProgress.set(response.version);
        } else if (JobStatus.SUCCEEDED.toString().equals(reportedStatus)) {
            // state to SUCCESS second time the shadow version
            if (reportSucceededCdl.getCount() == 0 && response.version > shadowVersionWhenDeviceFirstReportedSuccess.get()) {
                deviceSyncedStateToSucceededCdl.countDown();
            }
            shadowVersionWhenDeviceFirstReportedSuccess.set(response.version);
            reportSucceededCdl.countDown();
        }
    });
    // waiting for the device to report success
    assertTrue(reportSucceededCdl.await(60, TimeUnit.SECONDS));
    // Updating the shadow with deployment status IN_PROGRESS to simulate out-of-order update of shadow
    ShadowState shadowState = new ShadowState();
    shadowState.reported = reportedSection.get();
    UpdateNamedShadowRequest updateNamedShadowRequest = new UpdateNamedShadowRequest();
    updateNamedShadowRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
    updateNamedShadowRequest.thingName = thingInfo.getThingName();
    updateNamedShadowRequest.state = shadowState;
    shadowClient.PublishUpdateNamedShadow(updateNamedShadowRequest, QualityOfService.AT_LEAST_ONCE).get(30, TimeUnit.SECONDS);
    // verify that the device updates shadow state to SUCCEEDED
    assertTrue(deviceSyncedStateToSucceededCdl.await(60, TimeUnit.SECONDS));
    // Updating the shadow with a lower version number to trigger a message to /update/rejected event
    shadowState = new ShadowState();
    shadowState.reported = reportedSection.get();
    updateNamedShadowRequest = new UpdateNamedShadowRequest();
    updateNamedShadowRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
    updateNamedShadowRequest.thingName = thingInfo.getThingName();
    updateNamedShadowRequest.state = shadowState;
    updateNamedShadowRequest.version = shadowVersionWhenDeviceReportedInProgress.get();
    shadowClient.PublishUpdateNamedShadow(updateNamedShadowRequest, QualityOfService.AT_LEAST_ONCE).get(30, TimeUnit.SECONDS);
    CountDownLatch deviceRetrievedShadowCdl = new CountDownLatch(1);
    GetNamedShadowSubscriptionRequest getNamedShadowSubscriptionRequest = new GetNamedShadowSubscriptionRequest();
    getNamedShadowSubscriptionRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
    getNamedShadowSubscriptionRequest.thingName = thingInfo.getThingName();
    shadowClient.SubscribeToGetNamedShadowAccepted(getNamedShadowSubscriptionRequest, QualityOfService.AT_MOST_ONCE, getShadowResponse -> {
        deviceRetrievedShadowCdl.countDown();
    }).get(30, TimeUnit.SECONDS);
    // verify that the device retrieved the shadow when an update operation was rejected.
    assertTrue(deviceRetrievedShadowCdl.await(60, TimeUnit.SECONDS));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ComponentDeploymentSpecification(software.amazon.awssdk.services.greengrassv2.model.ComponentDeploymentSpecification) HashMap(java.util.HashMap) Coerce(com.aws.greengrass.util.Coerce) StringUtils(org.apache.commons.lang3.StringUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) STATUS_KEY(com.aws.greengrass.status.DeploymentInformation.STATUS_KEY) BaseE2ETestCase(com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) State(com.aws.greengrass.dependency.State) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) DEPLOYMENT_SHADOW_NAME(com.aws.greengrass.deployment.ShadowDeploymentListener.DEPLOYMENT_SHADOW_NAME) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ComponentConfigurationUpdate(software.amazon.awssdk.services.greengrassv2.model.ComponentConfigurationUpdate) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) EventuallyLambdaMatcher.eventuallyEval(com.github.grantwest.eventually.EventuallyLambdaMatcher.eventuallyEval) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Topics(com.aws.greengrass.config.Topics) Slf4jLogAdapter(com.aws.greengrass.logging.impl.Slf4jLogAdapter) WrapperMqttClientConnection(com.aws.greengrass.mqttclient.WrapperMqttClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) Utils(com.aws.greengrass.util.Utils) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) MqttClient(com.aws.greengrass.mqttclient.MqttClient) Matchers.is(org.hamcrest.Matchers.is) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) HashMap(java.util.HashMap) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) WrapperMqttClientConnection(com.aws.greengrass.mqttclient.WrapperMqttClientConnection) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) CountDownLatch(java.util.concurrent.CountDownLatch) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 5 with CreateDeploymentRequest

use of software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest in project aws-greengrass-nucleus by aws-greengrass.

the class ShadowDeploymentE2ETest method GIVEN_kernel_running_WHEN_device_deployment_adds_packages_THEN_new_services_should_be_running.

@Test
void GIVEN_kernel_running_WHEN_device_deployment_adds_packages_THEN_new_services_should_be_running() throws Exception {
    CountDownLatch cdlDeploymentFinished = new CountDownLatch(1);
    Consumer<GreengrassLogMessage> listener = m -> {
        if (m.getMessage() != null && m.getMessage().contains("Current deployment finished")) {
            cdlDeploymentFinished.countDown();
        }
    };
    Slf4jLogAdapter.addGlobalListener(listener);
    CreateDeploymentRequest createDeploymentRequest = CreateDeploymentRequest.builder().targetArn(thingInfo.getThingArn()).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").configurationUpdate(ComponentConfigurationUpdate.builder().merge("{\"sampleText\":\"FCS integ test\"}").build()).build(), "SomeService", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build())).build();
    draftAndCreateDeployment(createDeploymentRequest);
    assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
    IotShadowClient shadowClient = new IotShadowClient(new WrapperMqttClientConnection(kernel.getContext().get(MqttClient.class)));
    UpdateNamedShadowSubscriptionRequest req = new UpdateNamedShadowSubscriptionRequest();
    req.shadowName = DEPLOYMENT_SHADOW_NAME;
    req.thingName = thingInfo.getThingName();
    CountDownLatch reportInProgressCdl = new CountDownLatch(1);
    CountDownLatch reportSucceededCdl = new CountDownLatch(1);
    shadowClient.SubscribeToUpdateNamedShadowAccepted(req, QualityOfService.AT_LEAST_ONCE, (response) -> {
        try {
            logger.info("Got shadow update: {}", new ObjectMapper().writeValueAsString(response));
        } catch (JsonProcessingException e) {
        // ignore
        }
        if (response.state.reported == null) {
            return;
        }
        String reportedStatus = (String) response.state.reported.get(STATUS_KEY);
        if (JobStatus.IN_PROGRESS.toString().equals(reportedStatus)) {
            reportInProgressCdl.countDown();
        } else if (JobStatus.SUCCEEDED.toString().equals(reportedStatus)) {
            reportSucceededCdl.countDown();
        }
    });
    // wait for the shadow's reported section to be updated
    assertTrue(reportInProgressCdl.await(30, TimeUnit.SECONDS));
    assertTrue(reportSucceededCdl.await(30, TimeUnit.SECONDS));
    // deployment should succeed
    assertTrue(cdlDeploymentFinished.await(30, TimeUnit.SECONDS));
    Slf4jLogAdapter.removeGlobalListener(listener);
    assertThat(getCloudDeployedComponent("CustomerApp")::getState, eventuallyEval(is(State.FINISHED)));
    assertThat(getCloudDeployedComponent("SomeService")::getState, eventuallyEval(is(State.FINISHED)));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ComponentDeploymentSpecification(software.amazon.awssdk.services.greengrassv2.model.ComponentDeploymentSpecification) HashMap(java.util.HashMap) Coerce(com.aws.greengrass.util.Coerce) StringUtils(org.apache.commons.lang3.StringUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) STATUS_KEY(com.aws.greengrass.status.DeploymentInformation.STATUS_KEY) BaseE2ETestCase(com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) State(com.aws.greengrass.dependency.State) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) DEPLOYMENT_SHADOW_NAME(com.aws.greengrass.deployment.ShadowDeploymentListener.DEPLOYMENT_SHADOW_NAME) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ComponentConfigurationUpdate(software.amazon.awssdk.services.greengrassv2.model.ComponentConfigurationUpdate) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) EventuallyLambdaMatcher.eventuallyEval(com.github.grantwest.eventually.EventuallyLambdaMatcher.eventuallyEval) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Topics(com.aws.greengrass.config.Topics) Slf4jLogAdapter(com.aws.greengrass.logging.impl.Slf4jLogAdapter) WrapperMqttClientConnection(com.aws.greengrass.mqttclient.WrapperMqttClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) Utils(com.aws.greengrass.util.Utils) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) MqttClient(com.aws.greengrass.mqttclient.MqttClient) Matchers.is(org.hamcrest.Matchers.is) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) WrapperMqttClientConnection(com.aws.greengrass.mqttclient.WrapperMqttClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)20 CreateDeploymentRequest (software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest)20 CreateDeploymentResponse (software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse)18 Timeout (org.junit.jupiter.api.Timeout)15 CountDownLatch (java.util.concurrent.CountDownLatch)9 Topics (com.aws.greengrass.config.Topics)8 GreengrassLogMessage (com.aws.greengrass.logging.impl.GreengrassLogMessage)8 ComponentDeploymentSpecification (software.amazon.awssdk.services.greengrassv2.model.ComponentDeploymentSpecification)8 BaseE2ETestCase (com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase)7 GGExtension (com.aws.greengrass.testcommons.testutilities.GGExtension)7 Utils (com.aws.greengrass.util.Utils)7 TimeUnit (java.util.concurrent.TimeUnit)7 AfterEach (org.junit.jupiter.api.AfterEach)7 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 Tag (org.junit.jupiter.api.Tag)7 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)7 State (com.aws.greengrass.dependency.State)6 Slf4jLogAdapter (com.aws.greengrass.logging.impl.Slf4jLogAdapter)6 EventuallyLambdaMatcher.eventuallyEval (com.github.grantwest.eventually.EventuallyLambdaMatcher.eventuallyEval)6