From d9d67ac3c6d0343f16efa9c68ed60e017bbade8f Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Fri, 24 Nov 2023 12:02:38 -0800 Subject: [PATCH] Add jobs service client test (#448) --- .github/workflows/ci.yml | 16 ++ servicetests/test_cases/mqtt3_jobs_cfg.json | 28 ++ servicetests/test_cases/mqtt5_jobs_cfg.json | 28 ++ .../test_cases/test_jobs_execution.py | 103 +++++++ servicetests/tests/jobs_execution/index.ts | 256 ++++++++++++++++++ .../tests/jobs_execution/package.json | 27 ++ .../tests/jobs_execution/tsconfig.json | 62 +++++ 7 files changed, 520 insertions(+) create mode 100644 servicetests/test_cases/mqtt3_jobs_cfg.json create mode 100644 servicetests/test_cases/mqtt5_jobs_cfg.json create mode 100644 servicetests/test_cases/test_jobs_execution.py create mode 100644 servicetests/tests/jobs_execution/index.ts create mode 100644 servicetests/tests/jobs_execution/package.json create mode 100644 servicetests/tests/jobs_execution/tsconfig.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 51ed6637..dd9a0bd9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,7 @@ env: CI_DEVICE_ADVISOR: arn:aws:iam::180635532705:role/CI_DeviceAdvisor_Role CI_MQTT5_ROLE: arn:aws:iam::180635532705:role/CI_MQTT5_Role CI_GREENGRASS_ROLE: arn:aws:iam::180635532705:role/CI_Greengrass_Role + CI_JOBS_SERVICE_CLIENT_ROLE: arn:aws:iam::180635532705:role/CI_JobsServiceClient_Role jobs: @@ -190,6 +191,21 @@ jobs: sudo apt-get update -y sudo apt-get install softhsm -y softhsm2-util --version + - name: configure AWS credentials (Jobs) + uses: aws-actions/configure-aws-credentials@v2 + with: + role-to-assume: ${{ env.CI_JOBS_SERVICE_CLIENT_ROLE }} + aws-region: ${{ env.AWS_DEFAULT_REGION }} + - name: run Jobs service client test for MQTT311 + working-directory: ./aws-iot-device-sdk-js-v2/servicetests + run: | + export PYTHONPATH=${{ github.workspace }}/aws-iot-device-sdk-js-v2/utils + python3 ./test_cases/test_jobs_execution.py --config-file test_cases/mqtt3_jobs_cfg.json + - name: run Jobs service client test for MQTT5 + working-directory: ./aws-iot-device-sdk-js-v2/servicetests + run: | + export PYTHONPATH=${{ github.workspace }}/aws-iot-device-sdk-js-v2/utils + python3 ./test_cases/test_jobs_execution.py --config-file test_cases/mqtt5_jobs_cfg.json - name: configure AWS credentials (Connect and PubSub) uses: aws-actions/configure-aws-credentials@v1 with: diff --git a/servicetests/test_cases/mqtt3_jobs_cfg.json b/servicetests/test_cases/mqtt3_jobs_cfg.json new file mode 100644 index 00000000..86dde70d --- /dev/null +++ b/servicetests/test_cases/mqtt3_jobs_cfg.json @@ -0,0 +1,28 @@ +{ + "language": "Javascript", + "runnable_file": "./tests/jobs_execution", + "runnable_region": "us-east-1", + "runnable_main_class": "", + "arguments": [ + { + "name": "--mqtt_version", + "data": "3" + }, + { + "name": "--endpoint", + "secret": "ci/endpoint" + }, + { + "name": "--cert", + "data": "certificate.pem.crt" + }, + { + "name": "--key", + "data": "private.pem.key" + }, + { + "name": "--thing_name", + "data": "ServiceTest_Jobs_$INPUT_UUID" + } + ] +} diff --git a/servicetests/test_cases/mqtt5_jobs_cfg.json b/servicetests/test_cases/mqtt5_jobs_cfg.json new file mode 100644 index 00000000..4b2f4414 --- /dev/null +++ b/servicetests/test_cases/mqtt5_jobs_cfg.json @@ -0,0 +1,28 @@ +{ + "language": "Javascript", + "runnable_file": "./tests/jobs_execution", + "runnable_region": "us-east-1", + "runnable_main_class": "", + "arguments": [ + { + "name": "--mqtt_version", + "data": "5" + }, + { + "name": "--endpoint", + "secret": "ci/endpoint" + }, + { + "name": "--cert", + "data": "certificate.pem.crt" + }, + { + "name": "--key", + "data": "private.pem.key" + }, + { + "name": "--thing_name", + "data": "ServiceTest_Jobs_$INPUT_UUID" + } + ] +} diff --git a/servicetests/test_cases/test_jobs_execution.py b/servicetests/test_cases/test_jobs_execution.py new file mode 100644 index 00000000..2a8610c8 --- /dev/null +++ b/servicetests/test_cases/test_jobs_execution.py @@ -0,0 +1,103 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0. + +import argparse +import json +import os +import sys +import uuid + +import boto3 + +import run_in_ci +import ci_iot_thing + + +def main(): + argument_parser = argparse.ArgumentParser( + description="Run Jobs test in CI") + argument_parser.add_argument( + "--config-file", required=True, help="JSON file providing command-line arguments for a test") + argument_parser.add_argument( + "--input-uuid", required=False, help="UUID for thing name. UUID will be generated if this option is omit") + argument_parser.add_argument( + "--region", required=False, default="us-east-1", help="The name of the region to use") + parsed_commands = argument_parser.parse_args() + + try: + iot_client = boto3.client('iot', region_name=parsed_commands.region) + secrets_client = boto3.client("secretsmanager", region_name=parsed_commands.region) + except Exception as e: + print(f"ERROR: Could not make Boto3 iot-data client. Credentials likely could not be sourced. Exception: {e}", + file=sys.stderr) + return -1 + + input_uuid = parsed_commands.input_uuid if parsed_commands.input_uuid else str(uuid.uuid4()) + + thing_name = "ServiceTest_Jobs_" + input_uuid + policy_name = secrets_client.get_secret_value( + SecretId="ci/JobsServiceClientTest/policy_name")["SecretString"] + + # Temporary certificate/key file path. + certificate_path = os.path.join(os.getcwd(), "tests/jobs_execution/certificate.pem.crt") + key_path = os.path.join(os.getcwd(), "tests/jobs_execution/private.pem.key") + + try: + ci_iot_thing.create_iot_thing( + thing_name=thing_name, + thing_group="CI_ServiceClient_Thing_Group", + region=parsed_commands.region, + policy_name=policy_name, + certificate_path=certificate_path, + key_path=key_path) + except Exception as e: + print(f"ERROR: Failed to create IoT thing: {e}") + sys.exit(-1) + + # Perform Jobs test. If it's successful, the Job execution should be marked as SUCCEEDED for the thing. + try: + test_result = run_in_ci.setup_and_launch(parsed_commands.config_file, input_uuid) + except Exception as e: + print(f"ERROR: Failed to execute Jobs test: {e}") + test_result = -1 + + # Test reported success, verify that Job was indeed executed by the thing. + if test_result == 0: + print("Verifying that Job was executed") + try: + job_id = secrets_client.get_secret_value(SecretId="ci/JobsServiceClientTest/job_id")["SecretString"] + thing_job = iot_client.describe_job_execution(jobId=job_id, thingName=thing_name) + job_status = thing_job.get('execution', {}).get('status', {}) + if job_status != 'SUCCEEDED': + print(f"ERROR: Could not verify Job execution; Job info: {thing_job}") + test_result = -1 + except Exception as e: + print(f"ERROR: Could not verify Job execution: {e}") + test_result = -1 + + if test_result == 0: + print("Test succeeded") + + # Delete a thing created for this test run. + # NOTE We want to try to delete thing even if test was unsuccessful. + try: + ci_iot_thing.delete_iot_thing(thing_name, parsed_commands.region) + except Exception as e: + print(f"ERROR: Failed to delete thing: {e}") + # Fail the test if unable to delete thing, so this won't remain unnoticed. + test_result = -1 + + try: + if os.path.isfile(certificate_path): + os.remove(certificate_path) + if os.path.isfile(key_path): + os.remove(key_path) + except Exception as e: + print(f"WARNING: Failed to delete local files: {e}") + + if test_result != 0: + sys.exit(-1) + + +if __name__ == "__main__": + main() diff --git a/servicetests/tests/jobs_execution/index.ts b/servicetests/tests/jobs_execution/index.ts new file mode 100644 index 00000000..31ad4479 --- /dev/null +++ b/servicetests/tests/jobs_execution/index.ts @@ -0,0 +1,256 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +import { mqtt, iotjobs } from 'aws-iot-device-sdk-v2'; +import {once} from "events"; + +const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + +type Args = { [index: string]: any }; +const yargs = require('yargs'); + +// The relative path is '../../../samples/util/cli_args' from here, but the compiled javascript file gets put one level +// deeper inside the 'dist' folder +const common_args = require('../../../../samples/util/cli_args'); + +yargs.command('*', false, (yargs: any) => { + common_args.add_direct_connection_establishment_arguments(yargs); + common_args.add_jobs_arguments(yargs); +}, main).parse(); + +var available_jobs : Array = [] +var jobs_data = { + current_job_id: "", + current_execution_number: 0, + current_version_number: 0, +} + +async function on_rejected_error(error?: iotjobs.IotJobsError, response?:iotjobs.model.RejectedErrorResponse) { + if (error) { + console.log("Request rejected error: " + error); + } + console.log("Request rejected: " + response?.code + ": " + response?.message); + process.exit(-1) +} + +async function on_get_pending_job_execution_accepted(error?: iotjobs.IotJobsError, response?: iotjobs.model.GetPendingJobExecutionsResponse) { + if (error) { + console.log("Pending Jobs Error: " + error); + return; + } + + if (response) { + if (response.inProgressJobs || response.queuedJobs) { + console.log("Pending Jobs: ") + if (response.inProgressJobs) { + for (var i = 0; i < response.inProgressJobs.length; i++) { + var job_id = response.inProgressJobs[i].jobId; + var job_date = response.inProgressJobs[i].lastUpdatedAt; + if (typeof(job_date) == 'number') { + // Convert Epoch time format to a Javascript Date + job_date = new Date(job_date * 1000); + } + + if (job_id != undefined && job_date != undefined) { + available_jobs.push(job_id); + console.log(" In Progress: " + response.inProgressJobs[i].jobId + " @ " + job_date.toDateString()) + } + } + } + if (response.queuedJobs) { + for (var i = 0; i < response.queuedJobs.length; i++) { + var job_id = response.queuedJobs[i].jobId; + var job_date = response.queuedJobs[i].lastUpdatedAt; + if (typeof(job_date) == 'number') { + // Convert Epoch time format to a Javascript Date + job_date = new Date(job_date * 1000); + } + if (job_id != undefined && job_date != undefined) { + available_jobs.push(job_id); + console.log(" In Progress: " + response.queuedJobs[i].jobId + " @ " + job_date.toDateString()) + } + } + } + } + else { + console.log("Pending Jobs: None") + } + } +} + +async function on_describe_job_execution_accepted(error? : iotjobs.IotJobsError, response? : iotjobs.model.DescribeJobExecutionResponse) { + if (error) { + console.log("Describe Job Error: " + error); + return; + } + if (response) { + console.log("Describe Job: " + response.execution?.jobId + " version: " + response.execution?.versionNumber) + if (response.execution?.jobDocument) { + console.log(" Job document as JSON: " + JSON.stringify(response.execution.jobDocument, null, 2)) + } + // Print a new line to flush the console + console.log("\n"); + } +} + +async function on_start_next_pending_job_execution_accepted(error? : iotjobs.IotJobsError, response? : iotjobs.model.StartNextJobExecutionResponse) { + if (error) { + console.log("Start Job error: " + error); + return; + } + if (response) { + console.log("Start Job: " + response.execution?.jobId); + if (response.execution) { + if (response.execution.jobId) { + jobs_data.current_job_id = response.execution.jobId; + } + if (response.execution.executionNumber) { + jobs_data.current_execution_number = response.execution?.executionNumber; + } + if (response.execution.versionNumber) { + jobs_data.current_version_number = response.execution?.versionNumber; + } + } + } +} + +async function get_available_jobs(jobs_client: iotjobs.IotJobsClient, argv: Args) { + // Subscribe to necessary topics and get pending jobs + try { + var pending_subscription_request : iotjobs.model.GetPendingJobExecutionsSubscriptionRequest = { + thingName: argv.thing_name + }; + await jobs_client.subscribeToGetPendingJobExecutionsAccepted(pending_subscription_request, mqtt.QoS.AtLeastOnce, on_get_pending_job_execution_accepted); + await jobs_client.subscribeToGetPendingJobExecutionsRejected(pending_subscription_request, mqtt.QoS.AtLeastOnce, on_rejected_error); + + var pending_publish_request : iotjobs.model.GetPendingJobExecutionsRequest = { + thingName: argv.thing_name + }; + await jobs_client.publishGetPendingJobExecutions(pending_publish_request, mqtt.QoS.AtLeastOnce); + + await sleep(500); + } catch (error) { + console.log(error); + process.exit(-1) + } +} + +async function describe_job(jobs_client: iotjobs.IotJobsClient, job_id: string, argv: Args) { + var description_subscription_request : iotjobs.model.DescribeJobExecutionRequest = { + thingName: argv.thing_name, + jobId: job_id + } + await jobs_client.subscribeToDescribeJobExecutionAccepted(description_subscription_request, mqtt.QoS.AtLeastOnce, on_describe_job_execution_accepted); + await jobs_client.subscribeToDescribeJobExecutionRejected(description_subscription_request, mqtt.QoS.AtLeastOnce, on_rejected_error); + + var description_publish_request : iotjobs.model.DescribeJobExecutionRequest = { + thingName: argv.thing_name, + jobId: job_id, + includeJobDocument: true, + executionNumber: 1 + } + await jobs_client.publishDescribeJobExecution(description_publish_request, mqtt.QoS.AtLeastOnce); +} + +async function start_next_pending_job(jobs_client: iotjobs.IotJobsClient, argv: Args) { + var start_next_subscription_request : iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest = { + thingName: argv.thing_name + } + + await jobs_client.subscribeToStartNextPendingJobExecutionAccepted( + start_next_subscription_request, + mqtt.QoS.AtLeastOnce, + on_start_next_pending_job_execution_accepted); + await jobs_client.subscribeToStartNextPendingJobExecutionRejected( + start_next_subscription_request, + mqtt.QoS.AtLeastOnce, + on_rejected_error); + + var start_next_publish_request : iotjobs.model.StartNextPendingJobExecutionRequest = { + thingName: argv.thing_name, + stepTimeoutInMinutes: 15 + } + await jobs_client.publishStartNextPendingJobExecution(start_next_publish_request, mqtt.QoS.AtLeastOnce); +} + +async function update_current_job_status(jobs_client: iotjobs.IotJobsClient, status: iotjobs.model.JobStatus, argv: Args) { + var executing_subscription_request : iotjobs.model.UpdateJobExecutionSubscriptionRequest = { + thingName: argv.thing_name, + jobId: jobs_data.current_job_id + } + await jobs_client.subscribeToUpdateJobExecutionAccepted(executing_subscription_request, mqtt.QoS.AtLeastOnce, + (error?: iotjobs.IotJobsError, response?: iotjobs.model.UpdateJobExecutionResponse) => { + console.log("Marked job " + jobs_data.current_job_id + " " + status); + }); + await jobs_client.subscribeToUpdateJobExecutionRejected(executing_subscription_request, mqtt.QoS.AtLeastOnce, on_rejected_error); + + var executing_publish_request : iotjobs.model.UpdateJobExecutionRequest = { + thingName: argv.thing_name, + jobId: jobs_data.current_job_id, + executionNumber: jobs_data.current_execution_number, + status: status, + expectedVersion: jobs_data.current_version_number++ + }; + await jobs_client.publishUpdateJobExecution(executing_publish_request, mqtt.QoS.AtLeastOnce); +} + +async function main(argv: Args) { + common_args.apply_sample_arguments(argv); + + let connection; + let client5; + let jobs_client; + + console.log("Connecting..."); + if (argv.mqtt_version == 5) { + client5 = common_args.build_mqtt5_client_from_cli_args(argv); + jobs_client = iotjobs.IotJobsClient.newFromMqtt5Client(client5); + + const connectionSuccess = once(client5, "connectionSuccess"); + client5.start(); + await connectionSuccess; + console.log("Connected with Mqtt5 Client!"); + } else { + connection = common_args.build_connection_from_cli_args(argv); + jobs_client = new iotjobs.IotJobsClient(connection); + + await connection.connect() + console.log("Connected with Mqtt3 Client!"); + } + + try { + await get_available_jobs(jobs_client, argv); + + if (available_jobs.length == 0) { + console.log("ERROR: No jobs queued in CI! At least one job should be queued!"); + process.exit(-1); + } + + for (var i = 0; i < available_jobs.length; ++i) { + await describe_job(jobs_client, available_jobs[i], argv); + } + + for (var job_idx = 0; job_idx < available_jobs.length; ++job_idx) { + await start_next_pending_job(jobs_client, argv); + await update_current_job_status(jobs_client, iotjobs.model.JobStatus.IN_PROGRESS, argv); + await sleep(1000); + await update_current_job_status(jobs_client, iotjobs.model.JobStatus.SUCCEEDED, argv); + } + } catch (error) { + console.log(error); + process.exit(-1) + } + + if (connection) { + await connection.disconnect(); + } else { + let stopped = once(client5, "stopped"); + client5.stop(); + await stopped; + client5.close(); + } + // Quit NodeJS + process.exit(0); +} diff --git a/servicetests/tests/jobs_execution/package.json b/servicetests/tests/jobs_execution/package.json new file mode 100644 index 00000000..6b50cce3 --- /dev/null +++ b/servicetests/tests/jobs_execution/package.json @@ -0,0 +1,27 @@ +{ + "name": "jobs_execution", + "version": "1.0.0", + "description": "NodeJS IoT SDK v2 Jobs Service Client Test", + "homepage": "https://github.com/aws/aws-iot-device-sdk-js-v2", + "repository": { + "type": "git", + "url": "git+https://github.com/aws/aws-iot-device-sdk-js-v2.git" + }, + "contributors": [ + "AWS SDK Common Runtime Team " + ], + "license": "Apache-2.0", + "main": "./dist/index.js", + "scripts": { + "tsc": "tsc", + "prepare": "npm run tsc" + }, + "devDependencies": { + "@types/node": "^10.17.50", + "typescript": "^4.7.4" + }, + "dependencies": { + "aws-iot-device-sdk-v2": "../../../", + "yargs": "^16.2.0" + } +} diff --git a/servicetests/tests/jobs_execution/tsconfig.json b/servicetests/tests/jobs_execution/tsconfig.json new file mode 100644 index 00000000..2836361b --- /dev/null +++ b/servicetests/tests/jobs_execution/tsconfig.json @@ -0,0 +1,62 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ + // "lib": [], /* Specify library files to be included in the compilation. */ + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "removeComments": false, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + "strictNullChecks": true, /* Enable strict null checks. */ + "strictFunctionTypes": true, /* Enable strict checking of function types. */ + "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + /* Additional Checks */ + "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + }, + "include": [ + "*.ts" + ], + "exclude": [ + "node_modules", + "dist" + ] +}