|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-3.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 get_shadow_attrs(config_file): |
| 18 | + with open(config_file) as f: |
| 19 | + json_data = json.load(f) |
| 20 | + shadow_name = next((json_arg["data"] for json_arg in json_data["arguments"] if json_arg.get("name", "") == "--shadow_name"), "") |
| 21 | + shadow_property = next((json_arg["data"] for json_arg in json_data["arguments"] if json_arg.get("name", "") == "--shadow_property"), "") |
| 22 | + shadow_desired_value = next((json_arg["data"] for json_arg in json_data["arguments"] if json_arg.get("name", "") == "--shadow_value"), "") |
| 23 | + return [shadow_name, shadow_property, shadow_desired_value] |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + argument_parser = argparse.ArgumentParser( |
| 28 | + description="Run Shadow test in CI") |
| 29 | + argument_parser.add_argument( |
| 30 | + "--config-file", required=True, |
| 31 | + help="JSON file providing command-line arguments for a test") |
| 32 | + argument_parser.add_argument( |
| 33 | + "--input-uuid", required=False, help="UUID for thing name. UUID will be generated if this option is omit") |
| 34 | + argument_parser.add_argument( |
| 35 | + "--region", required=False, default="us-east-1", help="The name of the region to use") |
| 36 | + parsed_commands = argument_parser.parse_args() |
| 37 | + |
| 38 | + [shadow_name, shadow_property, shadow_desired_value] = get_shadow_attrs(parsed_commands.config_file) |
| 39 | + print(f"Shadow name: '{shadow_name}'") |
| 40 | + print(f"Shadow property: '{shadow_property}'") |
| 41 | + print(f"Shadow desired value: '{shadow_desired_value}'") |
| 42 | + |
| 43 | + try: |
| 44 | + iot_data_client = boto3.client('iot-data', region_name=parsed_commands.region) |
| 45 | + secrets_client = boto3.client("secretsmanager", region_name=parsed_commands.region) |
| 46 | + except Exception as e: |
| 47 | + print(f"ERROR: Could not make Boto3 iot-data client. Credentials likely could not be sourced. Exception: {e}", |
| 48 | + file=sys.stderr) |
| 49 | + return -1 |
| 50 | + |
| 51 | + input_uuid = parsed_commands.input_uuid if parsed_commands.input_uuid else str(uuid.uuid4()) |
| 52 | + |
| 53 | + thing_name = "ServiceTest_Shadow_" + input_uuid |
| 54 | + policy_name = secrets_client.get_secret_value( |
| 55 | + SecretId="ci/ShadowServiceClientTest/policy_name")["SecretString"] |
| 56 | + |
| 57 | + # Temporary certificate/key file path. |
| 58 | + certificate_path = os.path.join(os.getcwd(), "tests/ShadowUpdate/certificate.pem.crt") |
| 59 | + key_path = os.path.join(os.getcwd(), "tests/ShadowUpdate/private.pem.key") |
| 60 | + |
| 61 | + try: |
| 62 | + ci_iot_thing.create_iot_thing( |
| 63 | + thing_name=thing_name, |
| 64 | + region=parsed_commands.region, |
| 65 | + policy_name=policy_name, |
| 66 | + certificate_path=certificate_path, |
| 67 | + key_path=key_path) |
| 68 | + except Exception as e: |
| 69 | + print(f"ERROR: Failed to create IoT thing: {e}") |
| 70 | + sys.exit(-1) |
| 71 | + |
| 72 | + # Perform Shadow test. If it's successful, a shadow should appear for a specified thing. |
| 73 | + try: |
| 74 | + test_result = run_in_ci.setup_and_launch(parsed_commands.config_file, input_uuid) |
| 75 | + except Exception as e: |
| 76 | + print(f"ERROR: Failed to create shadow test: {e}") |
| 77 | + test_result = -1 |
| 78 | + |
| 79 | + # Test reported success, verify that shadow was indeed updated. |
| 80 | + if test_result == 0: |
| 81 | + print("Verifying that shadow was updated") |
| 82 | + shadow_value = None |
| 83 | + try: |
| 84 | + if shadow_name: |
| 85 | + thing_shadow = iot_data_client.get_thing_shadow(thingName=thing_name, shadowName=shadow_name) |
| 86 | + else: |
| 87 | + thing_shadow = iot_data_client.get_thing_shadow(thingName=thing_name) |
| 88 | + |
| 89 | + payload = thing_shadow['payload'].read() |
| 90 | + data = json.loads(payload) |
| 91 | + shadow_value = data.get('state', {}).get('reported', {}).get(shadow_property, None) |
| 92 | + if shadow_value != shadow_desired_value: |
| 93 | + print(f"ERROR: Could not verify thing shadow: {shadow_property} is not set to desired value " |
| 94 | + f"'{shadow_desired_value}'; shadow actual state: {data}") |
| 95 | + test_result = -1 |
| 96 | + except KeyError as e: |
| 97 | + print(f"ERROR: Could not verify thing shadow: key {e} does not exist in shadow response: {thing_shadow}") |
| 98 | + test_result = -1 |
| 99 | + except Exception as e: |
| 100 | + print(f"ERROR: Could not verify thing shadow: {e}") |
| 101 | + test_result = -1 |
| 102 | + |
| 103 | + if test_result == 0: |
| 104 | + print("Test succeeded") |
| 105 | + |
| 106 | + # Delete a thing created for this test run. |
| 107 | + # NOTE We want to try to delete thing even if test was unsuccessful. |
| 108 | + try: |
| 109 | + ci_iot_thing.delete_iot_thing(thing_name, parsed_commands.region) |
| 110 | + except Exception as e: |
| 111 | + print(f"ERROR: Failed to delete thing: {e}") |
| 112 | + # Fail the test if unable to delete thing, so this won't remain unnoticed. |
| 113 | + test_result = -1 |
| 114 | + |
| 115 | + try: |
| 116 | + if os.path.isfile(certificate_path): |
| 117 | + os.remove(certificate_path) |
| 118 | + if os.path.isfile(key_path): |
| 119 | + os.remove(key_path) |
| 120 | + except Exception as e: |
| 121 | + print(f"WARNING: Failed to delete local files: {e}") |
| 122 | + |
| 123 | + if test_result != 0: |
| 124 | + sys.exit(-1) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments