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"));
}
}
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());
});
}
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"));
}
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));
}
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)));
}
Aggregations