def setUpClass(cls): if not sys.warnoptions: import warnings warnings.simplefilter("ignore") cls.logger = ConsoleLogger() cls.resource_stack = get_resource_stack( TESTED_ACTION, create_resource_stack_func=cls.create_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=region()) assert (cls.resource_stack is not None) cls.instance_id = cls.resource_stack.stack_outputs["InstanceId0"] cls.root_volume = Ec2(region()).get_root_volume(cls.instance_id) cls.data_volumes = [ cls.resource_stack.stack_outputs["VolumeId0"], cls.resource_stack.stack_outputs["VolumeId1"] ] cls.task_runner = get_task_runner(TESTED_ACTION, KEEP_AND_USE_EXISTING_ACTION_STACK) cls.ec2 = Ec2(region())
def setUpClass(cls): if not sys.warnoptions: import warnings warnings.simplefilter("ignore") cls.logger = ConsoleLogger() cls.resource_stack = get_resource_stack( TESTED_ACTION, create_resource_stack_func=cls.create_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=region()) assert (cls.resource_stack is not None) cls.instance_no_cpu_load = cls.resource_stack.stack_outputs[ "InstanceNoCPULoad"] cls.instance_cpu_load = cls.resource_stack.stack_outputs[ "InstanceCPULoad"] cls.task_runner = get_task_runner( TESTED_ACTION, KEEP_AND_USE_EXISTING_ACTION_STACK, interval="0/{} * * * ?".format(INTERVAL_MINUTES)) cls.ec2 = Ec2(region()) testing.tags.set_ec2_tag_to_delete( ec2_client=cls.ec2, resource_ids=[cls.instance_no_cpu_load, cls.instance_cpu_load]) cls.metrics_client = CloudwatchMetrics(region())
def setUpClass(cls): if not sys.warnoptions: import warnings warnings.simplefilter("ignore") cls.logger = ConsoleLogger() cls.task_runner = get_task_runner(TESTED_ACTION, KEEP_AND_USE_EXISTING_ACTION_STACK) cls.resource_stack = get_resource_stack( TESTED_ACTION, create_resource_stack_func=cls.create_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=region()) assert (cls.resource_stack is not None) cls.ec2 = Ec2(region()) cls.volume_unencrypted = cls.resource_stack.stack_outputs[ "VolumeIdEncryptedUnencrypted"] cls.volume_encrypted_default = cls.resource_stack.stack_outputs[ "VolumeIdEncryptedDefault"] cls.volume_encrypted_custom = cls.resource_stack.stack_outputs[ "VolumeIdEncryptedCustom"] cls.custom_key_arn = cls.resource_stack.stack_outputs[ "EncryptionKeyArn"]
def setUpClass(cls): cls.ec2 = Ec2(region()) cls.elb = Elb(region()) cls.elbv2 = ElbV2(region()) cls.logger = ConsoleLogger() cls.resource_stack = get_resource_stack(TESTED_ACTION, create_resource_stack_func=cls.create_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=region()) assert (cls.resource_stack is not None) cls.v1_elb_name = cls.resource_stack.stack_outputs["V1LoadBalancerName"] cls.v2_target_group_arn = cls.resource_stack.stack_outputs["V2TargetGroupArn"] cls.instance_profile = cls.resource_stack.stack_outputs["InstanceProfile"] # noinspection PyPep8,PyBroadException try: cls.ec2.ec2_service.get(services.ec2_service.KEY_PAIRS, KeyNames=[TESTED_ACTION], region=region()) cls.key_pair = TESTED_ACTION except: cls.key_pair = cls.ec2.ec2_client.create_key_pair(KeyName=TESTED_ACTION).get("KeyName") cls.ami_single_volume = cls.ec2.latest_aws_linux_image["ImageId"] cls.task_runner = get_task_runner(TESTED_ACTION, KEEP_AND_USE_EXISTING_ACTION_STACK)
def test_copy_unencrypted_snapshot_other_region(self): test_method = inspect.stack()[0][3] self.cleanup_leftover_source_snapshots(test_method) destination_ec2 = Ec2(region=remote_region()) remote_snapshot_copy_id = None try: self.logger.test("Creating source snapshot") source_snapshot_unencrypted = self.ec2.create_snapshot(self.volume_unencrypted, tags={ "Name": "Ec2CopySnapshot_{}".format(test_method), "copied-tag": "copied-value", "not-copied-tag": "not-copied-value", tasklist_tagname(TESTED_ACTION): test_method }, description="Snapshot for testing Ec2CopySnapshot : {}".format(test_method)) self.snapshots.append(source_snapshot_unencrypted["SnapshotId"]) parameters = { copy_snapshot.PARAM_DESTINATION_REGION: remote_region(), copy_snapshot.PARAM_ACCOUNTS_VOLUME_CREATE_PERMISSIONS: ["123456789012"], copy_snapshot.PARAM_COPIED_SNAPSHOT_TAGS: "copied-tag", copy_snapshot.PARAM_SNAPSHOT_DESCRIPTION: "{{{}}}".format(copy_snapshot.TAG_PLACEHOLDER_SOURCE_DESCRIPTION), copy_snapshot.PARAM_SNAPSHOT_TAGS: testing.tags.common_placeholder_tags( test_delete=False, placeholders=[ copy_snapshot.TAG_PLACEHOLDER_SOURCE_REGION, copy_snapshot.TAG_PLACEHOLDER_SOURCE_SNAPSHOT_ID, copy_snapshot.TAG_PLACEHOLDER_SOURCE_VOLUME, copy_snapshot.TAG_PLACEHOLDER_OWNER_ACCOUNT]), copy_snapshot.PARAM_SOURCE_TAGS: testing.tags.common_placeholder_tags([ copy_snapshot.TAG_PLACEHOLDER_COPIED_SNAPSHOT_ID, copy_snapshot.TAG_PLACEHOLDER_COPIED_REGION ]) } self.logger.test("Running task") self.task_runner.run(parameters, task_name=test_method, complete_check_polling_interval=10) self.assertTrue(self.task_runner.success(), "Task executed successfully") remote_snapshot_copy_id = self.task_runner.results[0].result["copy-snapshot-id"] self.logger.test("[X] Task completed") self.logger.test("Checking snapshot copy") snapshot_copy = destination_ec2.get_snapshot(remote_snapshot_copy_id) self.assertIsNotNone(snapshot_copy, "Snapshot created in destination region") self.check_copy_snapshot(remote_snapshot_copy_id, source_snapshot_unencrypted, remote_region()) self.check_source_snapshot(source_snapshot_unencrypted["SnapshotId"], remote_snapshot_copy_id, test_method, remote_region()) finally: if remote_snapshot_copy_id is not None: destination_ec2.delete_snapshots([remote_snapshot_copy_id])
def setUpClass(cls): if not sys.warnoptions: import warnings warnings.simplefilter("ignore") cls.logger = ConsoleLogger() cls.resource_stack = get_resource_stack( TESTED_ACTION, create_resource_stack_func=cls.create_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=region()) assert (cls.resource_stack is not None) cls.ec2 = Ec2(region()) cls.instance_id = cls.resource_stack.stack_outputs["InstanceId"] if KEEP_AND_USE_EXISTING_ACTION_STACK: cls.ec2.start_instance(cls.instance_id) testing.tags.set_ec2_tag_to_delete(ec2_client=cls.ec2, resource_ids=[cls.instance_id]) cls.task_runner = get_task_runner(TESTED_ACTION, KEEP_AND_USE_EXISTING_ACTION_STACK) tags = cls.ec2.get_instance_tags(cls.instance_id) cls.original_tags = { t: tags[t] for t in tags if not t.startswith("aws:") }
def check_copy_snapshot(self, snapshot_id, source_snapshot, destination): ec2_destination = Ec2(region=destination) self.logger.test("Checking copied snapshot") snapshot_copy = ec2_destination.get_snapshot(snapshot_id) self.assertIsNotNone(snapshot_copy, "Snapshot copy does exist") snapshot_tags = snapshot_copy.get("Tags", {}) self.assertTrue( TagFilterExpression("copied-tag=copied-value").is_match( snapshot_tags), "Source snapshot tag copied") self.assertFalse( TagFilterExpression("not-copied-tag=*").is_match(snapshot_tags), "Source snapshot tag not copied") self.logger.test("[X] Expected source snapshot tag copied") snapshot_placeholders = { copy_snapshot.TAG_PLACEHOLDER_SOURCE_REGION: source_snapshot["Region"], copy_snapshot.TAG_PLACEHOLDER_SOURCE_SNAPSHOT_ID: source_snapshot["SnapshotId"], copy_snapshot.TAG_PLACEHOLDER_SOURCE_VOLUME: source_snapshot["VolumeId"], copy_snapshot.TAG_PLACEHOLDER_OWNER_ACCOUNT: source_snapshot["AwsAccount"] } self.assertTrue( testing.tags.verify_placeholder_tags(snapshot_tags, snapshot_placeholders), "All placeholder tags set on snapshot {}".format(snapshot_id)) self.logger.test("[X] Placeholder tags created") self.assertTrue( TagFilterExpression("{}={}".format( actions.marker_snapshot_tag_source_source_volume_id(), source_snapshot["VolumeId"])).is_match(snapshot_tags), "Source volume tag set") self.logger.test("[X] Source volume tag created") self.assertTrue( TagFilterExpression("{}={}".format( copy_snapshot.Ec2CopySnapshotAction. marker_tag_source_snapshot_id(), source_snapshot["SnapshotId"])).is_match(snapshot_tags), "Source snapshot tag set") self.logger.test("[X] Source snapshot tag created") self.assertEqual( ec2_destination.get_snapshot_create_volume_permission_users( snapshot_id), ["123456789012"], "Create volume permissions set") self.logger.test("[X] Volume create permission set") self.assertEqual(snapshot_copy["Description"], source_snapshot["Description"], "Description is copied") self.logger.test("[X] Description is copied")
def set_snapshot_copied_tag(snapshot_id, task_name, destination_region, copy_snapshot_id, region_name=None): copied_tag_name = Ec2CopySnapshotAction.marker_tag_copied_to(task_name) tags = { copied_tag_name: safe_json( {"region": destination_region, "snapshot-id": copy_snapshot_id}) } ec2 = Ec2(region_name) ec2.create_tags(snapshot_id, tags)
def set_snapshot_sources_tags(snapshot_id, source_volume_id=None, source_snapshot_id=None, region_name=None): tags = {} if source_volume_id is not None: tags[actions.marker_snapshot_tag_source_source_volume_id()] = source_volume_id if source_snapshot_id is not None: tags[Ec2CopySnapshotAction.marker_tag_source_snapshot_id()] = source_snapshot_id if len(tags) > 0: ec2 = Ec2(region_name) ec2.create_tags([snapshot_id], tags)
def test_copy_snapshot_encrypt_with_custom_key_other_region(self): destination_ec2 = Ec2(region=remote_region()) self.logger.test("Creating stack in destination region with custom encryption key") remote_stack = get_resource_stack(TESTED_ACTION, create_resource_stack_func=self.create_remote_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=remote_region()) remote_snapshot_copy_id = None try: remote_custom_key_arn = remote_stack.stack_outputs["EncryptionKeyArn"] test_method = inspect.stack()[0][3] self.cleanup_leftover_source_snapshots(test_method) self.logger.test("Creating source snapshot") source_snapshot_unencrypted = self.ec2.create_snapshot(self.volume_unencrypted, tags={ "Name": "Ec2CopySnapshot_{}".format(test_method), tasklist_tagname(TESTED_ACTION): test_method }, description="Snapshot for testing Ec2CopySnapshot : {}".format(test_method)) assert (source_snapshot_unencrypted is not None) self.snapshots.append(source_snapshot_unencrypted["SnapshotId"]) parameters = { copy_snapshot.PARAM_DESTINATION_REGION: remote_region(), copy_snapshot.PARAM_ENCRYPTED: True, copy_snapshot.PARAM_KMS_KEY_ID: remote_custom_key_arn } self.snapshots.append(source_snapshot_unencrypted["SnapshotId"]) self.logger.test("Running task") self.task_runner.run(parameters, task_name=test_method, complete_check_polling_interval=10) self.assertTrue(self.task_runner.success(), "Task executed successfully") remote_snapshot_copy_id = self.task_runner.results[0].result["copy-snapshot-id"] self.logger.test("[X] Task completed") self.logger.test("Checking snapshot copy") snapshot_copy = destination_ec2.get_snapshot(remote_snapshot_copy_id) self.assertIsNotNone(snapshot_copy, "Snapshot created in destination region") self.assertTrue(snapshot_copy["Encrypted"], "Snapshot is encrypted") self.assertEqual(snapshot_copy["KmsKeyId"], remote_custom_key_arn, "Custom encryption key is used") self.logger.test("[X] Snapshot created and encrypted") finally: if not KEEP_AND_USE_EXISTING_RESOURCES_STACK and remote_stack is not None: remote_stack.delete_stack(600) if remote_snapshot_copy_id is not None: destination_ec2.delete_snapshots([remote_snapshot_copy_id])
def setUpClass(cls): cls.logger = ConsoleLogger() cls.resource_stack = get_resource_stack(TESTED_ACTION, create_resource_stack_func=cls.create_resource_stack, use_existing=KEEP_AND_USE_EXISTING_RESOURCES_STACK, region_name=region()) assert (cls.resource_stack is not None) cls.volume_id = cls.resource_stack.stack_outputs["VolumeId0"] cls.ec2 = Ec2(region()) cls.task_runner = get_task_runner(TESTED_ACTION, KEEP_AND_USE_EXISTING_ACTION_STACK)
def create_resource_stack(cls, resource_stack_name): try: cls.logger.test("Creating test resources stack {}", resource_stack_name) ami = Ec2(region()).latest_aws_linux_image["ImageId"] resource_stack = Stack(resource_stack_name, region=region()) resource_stack.create_stack(template_file=template_path(__file__, TEST_RESOURCES_TEMPLATE), timeout=1200, iam_capability=True, params={ "InstanceAmi": ami, "InstanceType": TEST_INSTANCE_TYPES[0] }) return resource_stack except Exception as ex: cls.logger.test("Error creating stack {}, {}", resource_stack_name, ex) return None
def create_resource_stack(cls, resource_stack_name): try: cls.logger.test("Creating test resources stack {}", resource_stack_name) ami = Ec2(region()).latest_aws_linux_image["ImageId"] resource_stack = Stack(resource_stack_name, region=region()) resource_stack.create_stack(template_file=template_path(__file__, TEST_RESOURCES_TEMPLATE), iam_capability=True, params={ "InstanceAmi": ami, "InstanceType": "t2.micro", "TaskListTagName": tasklist_tagname(TESTED_ACTION), "TaskListTagValue": ",".join(cls.get_methods()) }) return resource_stack except Exception as ex: cls.logger.test("Error creating stack {}, {}", resource_stack_name, ex) return None