Skip to content

Commit 161835e

Browse files
authored
Updates to AWS Snapshot copy E2E test (#503)
* Updates to AWS Snapshot copy E2E test * Linter appeasement
1 parent dddcc24 commit 161835e

3 files changed

Lines changed: 51 additions & 35 deletions

File tree

libcloudforensics/providers/aws/forensics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ def CopyEBSSnapshotToS3Process(
440440
curr_wait = 0
441441
success = False
442442
prefix = '{0:s}/{1:s}/'.format(object_path, snapshot_id)
443-
files = ['image.bin', 'log.txt', 'hlog.txt', 'mlog.txt']
443+
files = ['image.bin', 'log.txt', 'hlog.txt', 'mlog.txt',
444+
'instance_copy_stdout.txt', 'instance_copy_stderr.txt']
444445

445446
logger.info('Transfer expected to take {0:d} seconds'.
446447
format(snapshot_size * transfer_speed))

libcloudforensics/scripts/ebs_snapshot_copy_aws.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ function ebsCopy {{
1212
snapshot=$1
1313
bucket=$2
1414

15+
echo snapshot: "$snapshot"
16+
echo bucket: "$bucket"
17+
18+
# Install utilities
19+
amazon-linux-extras install epel -y
20+
yum install jq dc3dd -y
21+
1522
# Get details about self
1623
region=$(curl -s http://169.254.169.254/latest/meta-data/placement/region)
1724
az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
1825
instance=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
1926

27+
echo region: "$region"
28+
echo az: "$az"
29+
echo instance: "$instance"
30+
2031
# create the new volume
2132
volume=$(aws ec2 --region $region create-volume --availability-zone $az --snapshot-id $snapshot --tag-specification 'ResourceType=volume,Tags=[{{Key=Name,Value=volumeToCopy}}]' | jq -r .VolumeId)
2233

@@ -44,9 +55,11 @@ function ebsCopy {{
4455
aws ec2 --region $region delete-volume --volume-id $volume
4556
}}
4657

47-
amazon-linux-extras install epel -y
48-
yum install jq dc3dd -y
58+
ebsCopy $snapshot $bucket 2> /tmp/err > /tmp/out
59+
60+
aws s3 cp /tmp/out $bucket/$snapshot/instance_copy_stdout.txt
61+
aws s3 cp /tmp/err $bucket/$snapshot/instance_copy_stderr.txt
4962

50-
ebsCopy $snapshot $bucket
63+
sleep 5
5164

5265
poweroff

tests/providers/aws/e2e.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,33 @@ class CopyEBSSnapshotToS3E2ETest(unittest.TestCase):
286286
guidelines in https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html # pylint: disable=line-too-long
287287
"""
288288

289-
@classmethod
289+
expected_filenames = ['image.bin', 'log.txt', 'hlog.txt', 'mlog.txt',
290+
'instance_copy_stdout.txt', 'instance_copy_stderr.txt']
291+
290292
@typing.no_type_check
291293
@IgnoreWarnings
292-
def setUpClass(cls):
294+
def setUp(self):
293295
try:
294296
project_info = utils.ReadProjectInfo(
295297
['s3_destination', 'zone', 'snapshot_id'])
296298
except (OSError, RuntimeError, ValueError) as exception:
297299
raise unittest.SkipTest(str(exception))
298-
cls.zone = project_info['zone']
299-
cls.aws = account.AWSAccount(cls.zone)
300-
cls.subnet_id = project_info.get('subnet_id', None)
301-
cls.security_group_id = project_info.get('security_group_id', None)
302-
cls.s3_destination = project_info.get('s3_destination', None)
303-
cls.snapshot_id = project_info.get('snapshot_id', None)
300+
self.zone = project_info['zone']
301+
self.aws_account: account.AWSAccount = account.AWSAccount(self.zone)
302+
self.subnet_id = project_info.get('subnet_id', None)
303+
self.security_group_id = project_info.get('security_group_id', None)
304+
self.s3_destination = project_info.get('s3_destination', None)
305+
self.snapshot_id = project_info.get('snapshot_id', None)
306+
307+
if not self.s3_destination.startswith('s3://'):
308+
self.s3_destination = 's3://' + self.s3_destination
309+
path_components = SplitStoragePath(self.s3_destination)
310+
self.bucket: str = path_components[0]
311+
self.object_path: str = path_components[1]
312+
self.directory: str = '{0:s}/{1:s}/'.format(
313+
self.object_path, self.snapshot_id)
314+
315+
super().setUp()
304316

305317
@typing.no_type_check
306318
@IgnoreWarnings
@@ -309,13 +321,6 @@ def testCopyEBSSnapshotToS3(self):
309321
310322
Test copying an EBS snapshot into S3.
311323
"""
312-
313-
if not self.s3_destination.startswith('s3://'):
314-
self.s3_destination = 's3://' + self.s3_destination
315-
path_components = SplitStoragePath(self.s3_destination)
316-
bucket = path_components[0]
317-
object_path = path_components[1]
318-
319324
forensics.CopyEBSSnapshotToS3(
320325
self.s3_destination,
321326
self.snapshot_id,
@@ -325,22 +330,19 @@ def testCopyEBSSnapshotToS3(self):
325330
security_group_id=self.security_group_id,
326331
cleanup_iam=True)
327332

328-
aws_account = account.AWSAccount(self.zone)
329-
directory = '{0:s}/{1:s}/'.format(object_path, self.snapshot_id)
330-
self.assertEqual(
331-
aws_account.s3.CheckForObject(bucket, directory + 'image.bin'), True)
332-
self.assertEqual(
333-
aws_account.s3.CheckForObject(bucket, directory + 'log.txt'), True)
334-
self.assertEqual(
335-
aws_account.s3.CheckForObject(bucket, directory + 'hlog.txt'), True)
336-
self.assertEqual(
337-
aws_account.s3.CheckForObject(bucket, directory + 'mlog.txt'), True)
338-
339-
# Cleanup
340-
aws_account.s3.RmObject(bucket, directory + 'image.bin')
341-
aws_account.s3.RmObject(bucket, directory + 'log.txt')
342-
aws_account.s3.RmObject(bucket, directory + 'hlog.txt')
343-
aws_account.s3.RmObject(bucket, directory + 'mlog.txt')
333+
for file in self.expected_filenames:
334+
self.assertTrue(self.aws_account.s3.CheckForObject(
335+
self.bucket, self.directory + file))
336+
337+
def tearDown(self) -> None:
338+
"""Remove the created resources."""
339+
for file in self.expected_filenames:
340+
try:
341+
self.aws_account.s3.RmObject(
342+
self.bucket, self.directory + file)
343+
except Exception as error: # pylint: disable=broad-exception-caught
344+
logger.exception(
345+
'Error cleaning up %s: {%s}', file, str(error), exc_info=True)
344346

345347

346348
class S3EndToEndTest(unittest.TestCase):

0 commit comments

Comments
 (0)