|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0. |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import uuid |
| 9 | +import time |
| 10 | + |
| 11 | +import boto3 |
| 12 | + |
| 13 | +import run_in_ci |
| 14 | +import ci_iot_thing |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + argument_parser = argparse.ArgumentParser( |
| 19 | + description="Run Jobs test in CI") |
| 20 | + argument_parser.add_argument( |
| 21 | + "--config-file", required=True, help="JSON file providing command-line arguments for a test") |
| 22 | + argument_parser.add_argument( |
| 23 | + "--input-uuid", required=False, help="UUID for thing name. UUID will be generated if this option is omit") |
| 24 | + argument_parser.add_argument( |
| 25 | + "--region", required=False, default="us-east-1", help="The name of the region to use") |
| 26 | + parsed_commands = argument_parser.parse_args() |
| 27 | + |
| 28 | + try: |
| 29 | + iot_client = boto3.client('iot', region_name=parsed_commands.region) |
| 30 | + secrets_client = boto3.client("secretsmanager", region_name=parsed_commands.region) |
| 31 | + except Exception as e: |
| 32 | + print(f"ERROR: Could not make Boto3 iot-data client. Credentials likely could not be sourced. Exception: {e}", |
| 33 | + file=sys.stderr) |
| 34 | + return -1 |
| 35 | + |
| 36 | + input_uuid = parsed_commands.input_uuid if parsed_commands.input_uuid else str(uuid.uuid4()) |
| 37 | + |
| 38 | + thing_name = "ServiceTest_Jobs_" + input_uuid |
| 39 | + policy_name = secrets_client.get_secret_value( |
| 40 | + SecretId="ci/JobsServiceClientTest/policy_name")["SecretString"] |
| 41 | + |
| 42 | + # Temporary certificate/key file path. |
| 43 | + certificate_path = os.path.join(os.getcwd(), "tests/JobsExecution/certificate.pem.crt") |
| 44 | + key_path = os.path.join(os.getcwd(), "tests/JobsExecution/private.pem.key") |
| 45 | + |
| 46 | + try: |
| 47 | + ci_iot_thing.create_iot_thing( |
| 48 | + thing_name=thing_name, |
| 49 | + thing_group="CI_ServiceClient_Thing_Group", |
| 50 | + region=parsed_commands.region, |
| 51 | + policy_name=policy_name, |
| 52 | + certificate_path=certificate_path, |
| 53 | + key_path=key_path) |
| 54 | + except Exception as e: |
| 55 | + print(f"ERROR: Failed to create IoT thing: {e}") |
| 56 | + sys.exit(-1) |
| 57 | + |
| 58 | + thing_job = 'ERROR' |
| 59 | + i = 0; |
| 60 | + while 'ERROR' in thing_job and i <= 4: |
| 61 | + try: |
| 62 | + job_id = secrets_client.get_secret_value(SecretId="ci/JobsServiceClientTest/job_id")["SecretString"] |
| 63 | + thing_job = iot_client.describe_job_execution(jobId=job_id, thingName=thing_name) |
| 64 | + print(f'thing job is {thing_job}'); |
| 65 | + if 'ERROR' in thing_job: |
| 66 | + i = i + 1; |
| 67 | + else: |
| 68 | + break; |
| 69 | + except Exception as e: |
| 70 | + print(f"Waiting for a newly created thing to be ready for the Job {e}") |
| 71 | + i = i + 1; |
| 72 | + time.sleep(1); |
| 73 | + |
| 74 | + # Perform Jobs test. If it's successful, the Job execution should be marked as SUCCEEDED for the thing. |
| 75 | + try: |
| 76 | + test_result = run_in_ci.setup_and_launch(parsed_commands.config_file, input_uuid) |
| 77 | + except Exception as e: |
| 78 | + print(f"ERROR: Failed to execute Jobs test: {e}") |
| 79 | + test_result = -1 |
| 80 | + |
| 81 | + # Test reported success, verify that Job was indeed executed by the thing. |
| 82 | + if test_result == 0: |
| 83 | + print("Verifying that Job was executed") |
| 84 | + try: |
| 85 | + job_id = secrets_client.get_secret_value(SecretId="ci/JobsServiceClientTest/job_id")["SecretString"] |
| 86 | + thing_job = iot_client.describe_job_execution(jobId=job_id, thingName=thing_name) |
| 87 | + job_status = thing_job.get('execution', {}).get('status', {}) |
| 88 | + if job_status != 'SUCCEEDED': |
| 89 | + print(f"ERROR: Could not verify Job execution; Job info: {thing_job}") |
| 90 | + test_result = -1 |
| 91 | + except Exception as e: |
| 92 | + print(f"ERROR: Could not verify Job execution: {e}") |
| 93 | + test_result = -1 |
| 94 | + |
| 95 | + if test_result == 0: |
| 96 | + print("Test succeeded") |
| 97 | + |
| 98 | + # Delete a thing created for this test run. |
| 99 | + # NOTE We want to try to delete thing even if test was unsuccessful. |
| 100 | + try: |
| 101 | + ci_iot_thing.delete_iot_thing(thing_name, parsed_commands.region) |
| 102 | + except Exception as e: |
| 103 | + print(f"ERROR: Failed to delete thing: {e}") |
| 104 | + # Fail the test if unable to delete thing, so this won't remain unnoticed. |
| 105 | + test_result = -1 |
| 106 | + |
| 107 | + try: |
| 108 | + if os.path.isfile(certificate_path): |
| 109 | + os.remove(certificate_path) |
| 110 | + if os.path.isfile(key_path): |
| 111 | + os.remove(key_path) |
| 112 | + except Exception as e: |
| 113 | + print(f"WARNING: Failed to delete local files: {e}") |
| 114 | + |
| 115 | + if test_result != 0: |
| 116 | + sys.exit(-1) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments