Search in sources :

Example 1 with NetworkUtils

use of com.aws.greengrass.integrationtests.e2e.util.NetworkUtils in project aws-greengrass-nucleus by aws-greengrass.

the class MqttReconnectTest method GIVEN_new_deployment_while_device_online_WHEN_mqtt_disconnects_and_reconnects_THEN_job_executes_successfully.

// GG_NEEDS_REVIEW: TODO: Fix flaky test https://sim.amazon.com/issues/P40525318
@Disabled
@Timeout(value = 10, unit = TimeUnit.MINUTES)
@Test
void GIVEN_new_deployment_while_device_online_WHEN_mqtt_disconnects_and_reconnects_THEN_job_executes_successfully(ExtensionContext context) throws Exception {
    ignoreExceptionUltimateCauseOfType(context, MqttException.class);
    ignoreExceptionUltimateCauseOfType(context, TimeoutException.class);
    ignoreExceptionWithMessage(context, "No valid versions were found for this package based on provided requirement");
    CountDownLatch jobInProgress = new CountDownLatch(1);
    CountDownLatch jobCompleted = new CountDownLatch(1);
    CountDownLatch connectionInterrupted = new CountDownLatch(1);
    // Create Job
    CreateDeploymentRequest createDeploymentRequest = CreateDeploymentRequest.builder().components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build())).build();
    CreateDeploymentResponse result = draftAndCreateDeployment(createDeploymentRequest);
    String jobId = result.iotJobId();
    // Subscribe to persisted deployment status
    Topics deploymentServiceTopics = kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC, DEPLOYMENT_SERVICE_TOPICS);
    Topics processedDeployments = deploymentServiceTopics.lookupTopics(RUNTIME_STORE_NAMESPACE_TOPIC, PROCESSED_DEPLOYMENTS_TOPICS);
    processedDeployments.subscribe((whatHappened, newValue) -> {
        if (!(newValue instanceof Topic)) {
            return;
        }
        if (newValue.childOf(DEPLOYMENT_STATUS_KEY_NAME)) {
            newValue = newValue.parent;
        } else {
            return;
        }
        Map<String, Object> deploymentDetails = ((Topics) newValue).toPOJO();
        if (!deploymentDetails.get(DEPLOYMENT_ID_KEY_NAME).toString().equals(jobId)) {
            return;
        }
        String status = deploymentDetails.get(DEPLOYMENT_STATUS_KEY_NAME).toString();
        if (JobStatus.IN_PROGRESS.toString().equals(status)) {
            jobInProgress.countDown();
        } else if (jobInProgress.getCount() <= 0 && JobStatus.SUCCEEDED.toString().equals(status)) {
            jobCompleted.countDown();
        }
    });
    kernel.launch();
    assertTrue(jobInProgress.await(2, TimeUnit.MINUTES));
    NetworkUtils networkUtils = NetworkUtils.getByPlatform();
    Consumer<GreengrassLogMessage> logListener = m -> {
        String message = m.getMessage();
        if (UPDATE_DEPLOYMENT_STATUS_MQTT_ERROR_LOG.equals(message) && m.getCause().getCause() instanceof MqttException || UPDATE_DEPLOYMENT_STATUS_TIMEOUT_ERROR_LOG.equals(message)) {
            connectionInterrupted.countDown();
        }
    };
    try {
        Slf4jLogAdapter.addGlobalListener(logListener);
        networkUtils.disconnectMqtt();
        // Wait for the deployment to finish offline
        assertTrue(jobCompleted.await(5, TimeUnit.MINUTES));
        assertTrue(connectionInterrupted.await(2, TimeUnit.MINUTES));
    } finally {
        networkUtils.recoverMqtt();
        Slf4jLogAdapter.removeGlobalListener(logListener);
    }
    // Wait for DNS Cache to expire
    Thread.sleep(DNS_CACHE_TTL.plus(Duration.ofSeconds(1)).toMillis());
    // Wait for the IoT job to be updated and marked as successful
    // The reason for making the timeout as 7 min is because it has been observed that if the update job status was
    // invoked just before the connection recovers it can block the call for total timeout of 5 mins,
    // without successfully updating the status of the job in cloud. After this timeout expires the status will
    // be updated again as part of the onConnectionResumed callback. Additional 2 mins are for this status
    // to get updated
    IotJobsUtils.waitForJobExecutionStatusToSatisfy(iotClient, jobId, thingInfo.getThingName(), Duration.ofMinutes(7), s -> s.equals(JobExecutionStatus.SUCCEEDED));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) UPDATE_DEPLOYMENT_STATUS_TIMEOUT_ERROR_LOG(com.aws.greengrass.deployment.IotJobsHelper.UPDATE_DEPLOYMENT_STATUS_TIMEOUT_ERROR_LOG) ComponentDeploymentSpecification(software.amazon.awssdk.services.greengrassv2.model.ComponentDeploymentSpecification) JobExecutionStatus(software.amazon.awssdk.services.iot.model.JobExecutionStatus) TimeoutException(java.util.concurrent.TimeoutException) Disabled(org.junit.jupiter.api.Disabled) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) BaseE2ETestCase(com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase) PROCESSED_DEPLOYMENTS_TOPICS(com.aws.greengrass.deployment.DeploymentStatusKeeper.PROCESSED_DEPLOYMENTS_TOPICS) CreateDeploymentResponse(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse) RUNTIME_STORE_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) Duration(java.time.Duration) Map(java.util.Map) NetworkUtils(com.aws.greengrass.integrationtests.e2e.util.NetworkUtils) Tag(org.junit.jupiter.api.Tag) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) DEPLOYMENT_SERVICE_TOPICS(com.aws.greengrass.deployment.DeploymentService.DEPLOYMENT_SERVICE_TOPICS) DEPLOYMENT_ID_KEY_NAME(com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_ID_KEY_NAME) DEPLOYMENT_STATUS_KEY_NAME(com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_STATUS_KEY_NAME) ExceptionLogProtector.ignoreExceptionUltimateCauseOfType(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionUltimateCauseOfType) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) SERVICES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC) ExceptionLogProtector.ignoreExceptionWithMessage(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage) UPDATE_DEPLOYMENT_STATUS_MQTT_ERROR_LOG(com.aws.greengrass.deployment.IotJobsHelper.UPDATE_DEPLOYMENT_STATUS_MQTT_ERROR_LOG) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Topic(com.aws.greengrass.config.Topic) Topics(com.aws.greengrass.config.Topics) Slf4jLogAdapter(com.aws.greengrass.logging.impl.Slf4jLogAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) Utils(com.aws.greengrass.util.Utils) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IotJobsUtils(com.aws.greengrass.integrationtests.e2e.util.IotJobsUtils) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) Timeout(org.junit.jupiter.api.Timeout) CreateDeploymentResponse(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentResponse) Topics(com.aws.greengrass.config.Topics) GreengrassLogMessage(com.aws.greengrass.logging.impl.GreengrassLogMessage) CreateDeploymentRequest(software.amazon.awssdk.services.greengrassv2.model.CreateDeploymentRequest) CountDownLatch(java.util.concurrent.CountDownLatch) NetworkUtils(com.aws.greengrass.integrationtests.e2e.util.NetworkUtils) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) Topic(com.aws.greengrass.config.Topic) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

Topic (com.aws.greengrass.config.Topic)1 Topics (com.aws.greengrass.config.Topics)1 DEPLOYMENT_SERVICE_TOPICS (com.aws.greengrass.deployment.DeploymentService.DEPLOYMENT_SERVICE_TOPICS)1 DEPLOYMENT_ID_KEY_NAME (com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_ID_KEY_NAME)1 DEPLOYMENT_STATUS_KEY_NAME (com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_STATUS_KEY_NAME)1 PROCESSED_DEPLOYMENTS_TOPICS (com.aws.greengrass.deployment.DeploymentStatusKeeper.PROCESSED_DEPLOYMENTS_TOPICS)1 UPDATE_DEPLOYMENT_STATUS_MQTT_ERROR_LOG (com.aws.greengrass.deployment.IotJobsHelper.UPDATE_DEPLOYMENT_STATUS_MQTT_ERROR_LOG)1 UPDATE_DEPLOYMENT_STATUS_TIMEOUT_ERROR_LOG (com.aws.greengrass.deployment.IotJobsHelper.UPDATE_DEPLOYMENT_STATUS_TIMEOUT_ERROR_LOG)1 BaseE2ETestCase (com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase)1 IotJobsUtils (com.aws.greengrass.integrationtests.e2e.util.IotJobsUtils)1 NetworkUtils (com.aws.greengrass.integrationtests.e2e.util.NetworkUtils)1 RUNTIME_STORE_NAMESPACE_TOPIC (com.aws.greengrass.lifecyclemanager.GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC)1 SERVICES_NAMESPACE_TOPIC (com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC)1 GreengrassLogMessage (com.aws.greengrass.logging.impl.GreengrassLogMessage)1 Slf4jLogAdapter (com.aws.greengrass.logging.impl.Slf4jLogAdapter)1 ExceptionLogProtector.ignoreExceptionUltimateCauseOfType (com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionUltimateCauseOfType)1 ExceptionLogProtector.ignoreExceptionWithMessage (com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage)1 GGExtension (com.aws.greengrass.testcommons.testutilities.GGExtension)1 Utils (com.aws.greengrass.util.Utils)1 Duration (java.time.Duration)1