def test_CleanBackups(self): ebs_backups_engine = ShelveryEBSBackup() try: backups = ebs_backups_engine.create_backups() except Exception as e: print(e) print(f"Failed with {e}") traceback.print_exc(file=sys.stdout) raise e ec2client = boto3.client('ec2') valid = False # validate there is for backup in backups: if backup.entity_id == self.volume['VolumeId']: snapshot_id = backup.backup_id snapshots = ec2client.describe_snapshots(SnapshotIds=[snapshot_id])['Snapshots'] self.assertEqual(len(snapshots), 1) ec2client.create_tags( Resources=[snapshot_id], Tags=[{'Key': f"{RuntimeConfig.get_tag_prefix()}:date_created", 'Value': datetime(1990, 1, 1).strftime(BackupResource.TIMESTAMP_FORMAT) }] ) ebs_backups_engine.clean_backups() with self.assertRaises(botocore.exceptions.ClientError) as context: ec2client.describe_snapshots(SnapshotIds=[snapshot_id])['Snapshots'] self.assertTrue('does not exist' in context.exception.response['Error']['Message']) self.assertEqual('InvalidSnapshot.NotFound', context.exception.response['Error']['Code']) valid = True self.assertTrue(valid)
def test_CleanBackupData(self): ebs_backups_engine = ShelveryEBSBackup() try: backups = ebs_backups_engine.create_backups() except Exception as e: print(e) print(f"Failed with {e}") traceback.print_exc(file=sys.stdout) raise e ec2client = boto3.client('ec2') valid = False # validate there is for backup in backups: if backup.entity_id == self.volume['VolumeId']: snapshot_id = backup.backup_id snapshots = ec2client.describe_snapshots(SnapshotIds=[snapshot_id])['Snapshots'] self.assertEqual(len(snapshots), 1) ec2client.create_tags( Resources=[snapshot_id], Tags=[{'Key': f"{RuntimeConfig.get_tag_prefix()}:date_created", 'Value': datetime(2000, 1, 1).strftime(BackupResource.TIMESTAMP_FORMAT) }] ) ebs_backups_engine.clean_backups() with self.assertRaises(botocore.exceptions.ClientError) as context: ec2client.describe_snapshots(SnapshotIds=[snapshot_id])['Snapshots'] self.assertTrue('does not exist' in context.exception.response['Error']['Message']) self.assertEqual('InvalidSnapshot.NotFound', context.exception.response['Error']['Code']) account_id = ebs_backups_engine.account_id s3path = f"{S3_DATA_PREFIX}/{ebs_backups_engine.get_engine_type()}/removed/{backup.name}.yaml" s3bucket = ShelveryEngine.get_local_bucket_name() bucket = boto3.resource('s3').Bucket(s3bucket) object = bucket.Object(s3path) content = object.get()['Body'].read() restored_br = yaml.load(content) self.assertEquals(restored_br.backup_id, backup.backup_id) self.assertEquals(restored_br.name, backup.name) self.assertEquals(restored_br.region, backup.region) self.assertIsNotNone(restored_br.date_deleted) self.assertEquals(restored_br.date_created, datetime(2000, 1, 1)) valid = True self.assertTrue(valid)