Beispiel #1
0
    def test_copy_ami_snapshot_success_with_reference(self, mock_aws):
        """Assert the snapshot copy task succeeds using a reference AMI ID."""
        mock_session = mock_aws.boto3.Session.return_value
        mock_account_id = mock_aws.get_session_account_id.return_value

        account = account_helper.generate_aws_account()
        arn = account.account_arn

        region = random.choice(util_helper.SOME_AWS_REGIONS)
        new_image_id = util_helper.generate_dummy_image_id()
        mock_image = util_helper.generate_mock_image(new_image_id)
        block_mapping = mock_image.block_device_mappings
        mock_snapshot_id = block_mapping[0]['Ebs']['SnapshotId']
        mock_snapshot = util_helper.generate_mock_snapshot(
            mock_snapshot_id, owner_id=mock_account_id)
        mock_new_snapshot_id = util_helper.generate_dummy_snapshot_id()

        # This is the original ID of a private/shared image.
        # It would have been saved to our DB upon initial discovery.
        reference_image = account_helper.generate_aws_image(account=account)
        reference_image_id = reference_image.ec2_ami_id

        mock_aws.get_session.return_value = mock_session
        mock_aws.get_ami.return_value = mock_image
        mock_aws.get_ami_snapshot_id.return_value = mock_snapshot_id
        mock_aws.get_snapshot.return_value = mock_snapshot
        mock_aws.copy_snapshot.return_value = mock_new_snapshot_id

        with patch.object(tasks, 'create_volume') as mock_create_volume, \
                patch.object(tasks, 'remove_snapshot_ownership') as \
                mock_remove_snapshot_ownership:
            tasks.copy_ami_snapshot(
                arn,
                new_image_id,
                region,
                reference_image_id,
            )
            # arn, customer_snapshot_id, snapshot_region, snapshot_copy_id
            mock_remove_snapshot_ownership.delay.assert_called_with(
                arn,
                mock_snapshot_id,
                region,
                mock_new_snapshot_id,
            )
            mock_create_volume.delay.assert_called_with(
                reference_image_id,
                mock_new_snapshot_id,
            )

        mock_aws.get_session.assert_called_with(arn)
        mock_aws.get_ami.assert_called_with(mock_session, new_image_id, region)
        mock_aws.get_ami_snapshot_id.assert_called_with(mock_image)
        mock_aws.add_snapshot_ownership.assert_called_with(mock_snapshot)
        mock_aws.copy_snapshot.assert_called_with(mock_snapshot_id, region)

        # Verify that the copy object was stored correctly to reference later.
        copied_image = AwsMachineImageCopy.objects.get(ec2_ami_id=new_image_id)
        self.assertIsNotNone(copied_image)
        self.assertEqual(copied_image.reference_awsmachineimage.ec2_ami_id,
                         reference_image_id)
Beispiel #2
0
 def setUp(self):
     """Set up commonly used data for each test."""
     # set up start & end dates and images
     self.start = util_helper.utc_dt(2018, 1, 1, 0, 0, 0)
     self.end = util_helper.utc_dt(2018, 2, 1, 0, 0, 0)
     self.account = account_helper.generate_aws_account()
     self.account.created_at = util_helper.utc_dt(2017, 1, 1, 0, 0, 0)
     self.account.save()
     # set up an account created after the end date for testing
     self.account_after_end = account_helper.generate_aws_account()
     self.account_after_end.created_at = \
         util_helper.utc_dt(2018, 3, 1, 0, 0, 0)
     self.account_after_end.save()
     # set up an account created on the end date for testing
     self.account_on_end = account_helper.generate_aws_account()
     self.account_on_end.created_at = self.end
     self.account_on_end.save()
     self.windows_image = account_helper.generate_aws_image(
         self.account,
         is_encrypted=False,
         is_windows=True,
     )
     self.rhel_image = account_helper.generate_aws_image(
         self.account,
         is_encrypted=False,
         is_windows=False,
         ec2_ami_id=None,
         is_rhel=True,
         is_openshift=False)
     self.openshift_image = account_helper.generate_aws_image(
         self.account,
         is_encrypted=False,
         is_windows=False,
         ec2_ami_id=None,
         is_rhel=False,
         is_openshift=True)
     self.openshift_and_rhel_image = account_helper.generate_aws_image(
         self.account,
         is_encrypted=False,
         is_windows=False,
         ec2_ami_id=None,
         is_rhel=True,
         is_openshift=True)
     self.instance_1 = account_helper.generate_aws_instance(self.account)
     self.instance_2 = account_helper.generate_aws_instance(self.account)
Beispiel #3
0
    'super_spaghetti':
    account_helper.generate_aws_account(user=users['super'],
                                        name='knees weak arms spaghetti'),
}
for account in accounts.values():
    account.created_at = util_helper.utc_dt(2018, 1, 1, 0, 0, 0)
    account.save()
    print(f'account:'
          f'id={account.id};'
          f'user_id={account.user_id};'
          f'name={account.name};')

images = {
    'normie_greatest': {
        'plain':
        account_helper.generate_aws_image(accounts['normie_greatest'],
                                          ec2_ami_id='ami-plain'),
        'rhel7':
        account_helper.generate_aws_image(accounts['normie_greatest'],
                                          is_rhel=True,
                                          ec2_ami_id='ami-rhel7'),
        'rhel8':
        account_helper.generate_aws_image(accounts['normie_greatest'],
                                          is_rhel=True,
                                          ec2_ami_id='ami-rhel8'),
    },
    'normie_another': {
        'ocp':
        account_helper.generate_aws_image(accounts['normie_another'],
                                          is_openshift=True,
                                          ec2_ami_id='ami-ocp'),
    },
                                        name='my favorite account'),
}
for account in accounts.values():
    account.created_at = util_helper.utc_dt(2018, 1, 1, 0, 0, 0)
    account.save()
    print(f'account:'
          f'id={account.id};'
          f'user_id={account.user_id};'
          f'name={account.name};')

images = {
    'normie_account': {
        'plain':
        account_helper.generate_aws_image(
            # accounts['normie_account'],
            is_encrypted=False,
            ec2_ami_id='ami-plain',
            name='my-named-plain-image'),
        'rhel7':
        account_helper.generate_aws_image(
            # accounts['normie_account'],
            is_encrypted=False,
            rhel_detected=True,
            ec2_ami_id='ami-rhel7',
            name='my-named-rhel7-image'),
        'ocp':
        account_helper.generate_aws_image(
            # accounts['normie_account'],
            is_encrypted=False,
            openshift_detected=True,
            ec2_ami_id='ami-ocp',
      f'user_1.username={user_1.username};')

account_1 = account_helper.generate_aws_account(
    user=user_1, name=_faker.bs())

print(f'account_1.id={account_1.id};'
      f'account_1.user_id={account_1.user_id};'
      f'account_1.name={account_1.name};')

instance_1 = account_helper.generate_aws_instance(account_1)

print(f'instance_1.id={instance_1.id};'
      f'instance_1.account_id={instance_1.account_id};')

image_rhel = account_helper.generate_aws_image(
    account_1, is_rhel=True,
    ec2_ami_id='ami-rhel')

print(f'image_rhel.id={image_rhel.id};'
      f'image_rhel.ec2_ami_id={image_rhel.ec2_ami_id};'
      )

events = account_helper.generate_aws_instance_events(
    instance_1,
    [(
        util_helper.utc_dt(2018, 1, 11, 5, 0, 0),
        util_helper.utc_dt(2018, 1, 11, 6, 0, 0)
    ),
    (
        util_helper.utc_dt(2018, 1, 11, 7, 0, 0),
        util_helper.utc_dt(2018, 1, 11, 8, 0, 0)
Beispiel #6
0
instance_1 = account_helper.generate_aws_instance(account_1)
instance_2 = account_helper.generate_aws_instance(account_1)
instance_3 = account_helper.generate_aws_instance(account_1)
instance_4 = account_helper.generate_aws_instance(account_2)

print(f'instance_1.id={instance_1.id};'
      f'instance_1.account_id={instance_1.account_id};')
print(f'instance_2.id={instance_2.id};'
      f'instance_2.account_id={instance_2.account_id};')
print(f'instance_3.id={instance_3.id};'
      f'instance_3.account_id={instance_3.account_id};')
print(f'instance_4.id={instance_4.id};'
      f'instance_4.account_id={instance_4.account_id};')

image_plain = account_helper.generate_aws_image(account_1,
                                                ec2_ami_id='ami-plain')
# image_windows = account_helper.generate_aws_image(
#     account_1, is_windows=True)
image_rhel = account_helper.generate_aws_image(account_1,
                                               is_rhel=True,
                                               ec2_ami_id='ami-rhel')
image_ocp = account_helper.generate_aws_image(account_1,
                                              is_openshift=True,
                                              ec2_ami_id='ami-ocp')
image_rhel_ocp = account_helper.generate_aws_image(account_2,
                                                   is_rhel=True,
                                                   is_openshift=True,
                                                   ec2_ami_id='ami-rhel_ocp')

print(
    f'image_plain.id={image_plain.id};'
Beispiel #7
0
    def setUp(self):
        """
        Set up commonly used data for each test.

        From this rich combination of data, we should have enough data for some
        interesting tests! If you think your test needs more, consider adding
        to this based class setup.

        The rough hierarchy of initial objects looks something like this:

            user_1:
                image_plain
                image_rhel
                image_ocp
                image_rhel_ocp
                image_windows
                account_1:
                    instance_1
                    instance_2
                    instance_3
                    instance_4
                    instance_5
                account_2:
                    None

            user_2:
                account_3:
                    None
                account_4:
                    None

            user_super:
                None
        """
        self.user_1 = util_helper.generate_test_user()
        self.user_2 = util_helper.generate_test_user()
        self.user_super = util_helper.generate_test_user(is_superuser=True)

        self.account_1 = account_helper.generate_aws_account(
            user=self.user_1, name=_faker.bs())
        self.account_2 = account_helper.generate_aws_account(
            user=self.user_1, name=_faker.bs())
        self.account_3 = account_helper.generate_aws_account(
            user=self.user_2, name=_faker.bs())
        self.account_4 = account_helper.generate_aws_account(
            user=self.user_2, name=_faker.bs())

        self.instance_1 = account_helper.generate_aws_instance(self.account_1)
        self.instance_2 = account_helper.generate_aws_instance(self.account_1)
        self.instance_3 = account_helper.generate_aws_instance(self.account_1)
        self.instance_4 = account_helper.generate_aws_instance(self.account_1)
        self.instance_5 = account_helper.generate_aws_instance(self.account_1)

        self.image_plain = account_helper.generate_aws_image(self.account_1)
        self.image_windows = account_helper.generate_aws_image(
            self.account_1, is_windows=True)
        self.image_rhel = account_helper.generate_aws_image(
            self.account_1, is_rhel=True)
        self.image_ocp = account_helper.generate_aws_image(
            self.account_1, is_openshift=True)
        self.image_rhel_ocp = account_helper.generate_aws_image(
            self.account_1, is_rhel=True, is_openshift=True)

        # Report on "month of January in 2018"
        self.start = util_helper.utc_dt(2018, 1, 1, 0, 0, 0)
        self.end = util_helper.utc_dt(2018, 2, 1, 0, 0, 0)