Beispiel #1
0
    def test_single_rhel_run_no_result(self):
        """
        Test with a single RHEL instance requesting results in futures.

        Also assert that no ConcurrentUsage is saved to the database because the
        requested date is in the future, and we cannot know that data yet.
        """
        rhel_instance = api_helper.generate_instance(self.user1account1,
                                                     image=self.image_rhel)
        api_helper.generate_single_run(
            rhel_instance,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 0, 0),
            ),
            image=rhel_instance.machine_image,
            calculate_concurrent_usage=False,
        )
        request_date = datetime.date(2019, 5, 4)
        expected_date = request_date

        self.assertEqual(
            models.ConcurrentUsage.objects.filter(
                date=request_date, user_id=self.user1.id).count(),
            0,
        )
        concurrent_usage = get_max_concurrent_usage(request_date,
                                                    user_id=self.user1.id)
        self.assertEqual(
            models.ConcurrentUsage.objects.filter(
                date=request_date, user_id=self.user1.id).count(),
            0,
        )
        self.assertEqual(concurrent_usage.date, expected_date)
        self.assertEqual(len(concurrent_usage.maximum_counts), 0)
Beispiel #2
0
    def test_generate_aws_events_with_some_times(self):
        """Assert generation of InstanceEvents with some times."""
        account = helper.generate_aws_account()
        instance = helper.generate_aws_instance(account)
        powered_times = (
            (None, util_helper.utc_dt(2017, 1, 1)),
            (util_helper.utc_dt(2017, 1, 2), util_helper.utc_dt(2017, 1, 3)),
            (util_helper.utc_dt(2017, 1, 4), None),
        )
        events = helper.generate_aws_instance_events(instance, powered_times)

        self.assertEqual(len(events), 4)
        self.assertEqual(events[0].occurred_at, powered_times[0][1])
        self.assertEqual(events[1].occurred_at, powered_times[1][0])
        self.assertEqual(events[2].occurred_at, powered_times[1][1])
        self.assertEqual(events[3].occurred_at, powered_times[2][0])

        self.assertEqual(events[0].event_type, InstanceEvent.TYPE.power_off)
        self.assertEqual(events[1].event_type, InstanceEvent.TYPE.power_on)
        self.assertEqual(events[2].event_type, InstanceEvent.TYPE.power_off)
        self.assertEqual(events[3].event_type, InstanceEvent.TYPE.power_on)

        self.assertIsNotNone(events[0].machineimage)
        self.assertIsNotNone(events[0].instance_type)
        self.assertIsNotNone(events[0].subnet)

        # Assert they all have the same AMI, subnet, and instance type.
        for event in events[1:]:
            self.assertEqual(event.machineimage, events[0].machineimage)
            self.assertEqual(event.instance_type, events[0].instance_type)
            self.assertEqual(event.subnet, events[0].subnet)
Beispiel #3
0
    def test_generate_aws_events_with_args_and_some_times(self):
        """Assert generation of InstanceEvents with all specified args."""
        account = helper.generate_cloud_account()
        ec2_ami_id = util_helper.generate_dummy_image_id()
        image = helper.generate_image(ec2_ami_id=ec2_ami_id)
        instance = helper.generate_instance(account, image=image)
        powered_times = (
            (None, util_helper.utc_dt(2017, 1, 1)),
            (util_helper.utc_dt(2017, 1, 2), util_helper.utc_dt(2017, 1, 3)),
            (util_helper.utc_dt(2017, 1, 4), None),
        )
        instance_type = util_helper.get_random_instance_type()
        subnet = str(uuid.uuid4())
        events = helper.generate_instance_events(
            instance,
            powered_times,
            instance_type=instance_type,
            subnet=subnet,
        )

        self.assertEqual(len(events), 4)
        # We don't care to check *everything* in the events since that should
        # already be covered by ``test_generate_events_with_some_times``.
        # Here we only care that argument values were set correctly.
        for event in events:
            if event.event_type != event.TYPE.power_off:
                self.assertEqual(
                    event.instance.machine_image.content_object.ec2_ami_id, ec2_ami_id
                )
            self.assertEqual(event.content_object.instance_type, instance_type)
            self.assertEqual(event.content_object.subnet, subnet)
Beispiel #4
0
    def test_normalize_one_instance_on_on_off_off(self):
        """
        Test normalize_runs one instance with "overlapping" ons and offs.

        In this special case, the mock data simulates receiving information
        that would indicate two potentially overlapping runs. However, since
        we discard subsequent on and off events, we only generate one run.
        """
        powered_times = (
            (
                util_helper.utc_dt(2019, 1, 9, 0, 0, 0),
                util_helper.utc_dt(2019, 1, 15, 0, 0, 0),
            ),
            (
                util_helper.utc_dt(2019, 1, 10, 0, 0, 0),
                util_helper.utc_dt(2019, 1, 19, 0, 0, 0),
            ),
        )
        events = api_helper.generate_instance_events(self.instance_plain,
                                                     powered_times)
        random.shuffle(events)

        runs = util.normalize_runs(events)
        self.assertEquals(len(runs), 1)
        run = runs[0]
        self.assertEquals(run.start_time, powered_times[0][0])
        self.assertEquals(run.end_time, powered_times[0][1])
        self.assertEquals(run.instance_id, self.instance_plain.id)
        self.assertEquals(run.image_id, self.image_plain.id)
Beispiel #5
0
    def test_overlapping_rhel_runs_within_day_with_user_filter(self):
        """
        Test with two overlapping RHEL instances run within the day.

        Because a user filter is applied, only one instance's data is seen.
        """
        rhel_instance1 = api_helper.generate_instance(
            self.user1account1, image=self.image_rhel3
        )
        rhel_instance2 = api_helper.generate_instance(
            self.user2account1, image=self.image_rhel4
        )
        api_helper.generate_single_run(
            rhel_instance1,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 0, 0),
            ),
            image=rhel_instance1.machine_image,
        )
        # This second instance run should be filtered away.
        api_helper.generate_single_run(
            rhel_instance2,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 30, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 30, 0),
            ),
            image=rhel_instance2.machine_image,
        )
        request_date = datetime.date(2019, 5, 1)
        expected_date = request_date
        expected_instances = 1

        usage = calculate_max_concurrent_usage(request_date, user_id=self.user1.id)
        self.assertMaxConcurrentUsage(usage, expected_date, expected_instances)
Beispiel #6
0
    def test_normalize_one_instance_multiple_on_off(self):
        """Test normalize_runs one instance having multiple on-off cycles."""
        powered_times = (
            (
                util_helper.utc_dt(2019, 1, 9, 0, 0, 0),
                util_helper.utc_dt(2019, 1, 10, 0, 0, 0),
            ),
            (
                util_helper.utc_dt(2019, 1, 11, 0, 0, 0),
                util_helper.utc_dt(2019, 1, 12, 0, 0, 0),
            ),
            (util_helper.utc_dt(2019, 1, 13, 0, 0, 0), None),
        )
        events = api_helper.generate_instance_events(self.instance_plain,
                                                     powered_times)
        random.shuffle(events)

        runs = util.normalize_runs(events)
        self.assertEquals(len(runs), len(powered_times))
        sorted_runs = sorted(runs, key=lambda r: r.start_time)
        for index, run in enumerate(sorted_runs):
            self.assertEquals(run.start_time, powered_times[index][0])
            self.assertEquals(run.end_time, powered_times[index][1])
            self.assertEquals(run.instance_id, self.instance_plain.id)
            self.assertEquals(run.image_id, self.image_plain.id)
Beispiel #7
0
    def test_normalize_one_instance_on_off_off_off(self):
        """
        Test normalize_runs one instance with multiple on and one off.

        In this special case, only one run should be created. The first off
        event is the only one relevant to that run.
        """
        powered_times = (
            (
                util_helper.utc_dt(2019, 1, 9, 0, 0, 0),
                util_helper.utc_dt(2019, 1, 10, 0, 0, 0),
            ),
            (None, util_helper.utc_dt(2019, 1, 12, 0, 0, 0)),
            (None, util_helper.utc_dt(2019, 1, 14, 0, 0, 0)),
        )
        events = api_helper.generate_instance_events(self.instance_plain,
                                                     powered_times)
        random.shuffle(events)

        runs = util.normalize_runs(events)
        self.assertEquals(len(runs), 1)
        run = runs[0]
        self.assertEquals(run.start_time, powered_times[0][0])
        self.assertEquals(run.end_time, powered_times[0][1])
        self.assertEquals(run.instance_id, self.instance_plain.id)
        self.assertEquals(run.image_id, self.image_plain.id)
Beispiel #8
0
    def test_generate_aws_events_with_args_and_some_times(self):
        """Assert generation of InstanceEvents with all specified args."""
        account = helper.generate_aws_account()
        instance = helper.generate_aws_instance(account)
        powered_times = (
            (None, util_helper.utc_dt(2017, 1, 1)),
            (util_helper.utc_dt(2017, 1, 2), util_helper.utc_dt(2017, 1, 3)),
            (util_helper.utc_dt(2017, 1, 4), None),
        )
        ec2_ami_id = util_helper.generate_dummy_image_id()
        instance_type = random.choice(util_helper.SOME_EC2_INSTANCE_TYPES)
        subnet = str(uuid.uuid4())
        events = helper.generate_aws_instance_events(
            instance,
            powered_times,
            ec2_ami_id=ec2_ami_id,
            instance_type=instance_type,
            subnet=subnet,
        )

        self.assertEqual(len(events), 4)
        # We don't care to check *everything* in the events since that should
        # already be covered by ``test_generate_events_with_some_times``.
        # Here we only care that argument values were set correctly.
        for event in events:
            self.assertEqual(event.machineimage.ec2_ami_id, ec2_ami_id)
            self.assertEqual(event.instance_type, instance_type)
            self.assertEqual(event.subnet, subnet)
Beispiel #9
0
 def test_get_cloud_account_overview_with_openshift_and_rhel_image(self):
     """Assert an account overview reports openshift and rhel correctly."""
     powered_times = (
         (
             util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
             util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
         ),
     )
     account_helper.generate_aws_instance_events(
         self.instance_1, powered_times,
         self.windows_image.ec2_ami_id
     )
     # in addition to instance_1's events, we are creating an event for
     # instance_2 with a rhel & openshift_image
     account_helper.generate_single_aws_instance_event(
         self.instance_2, self.start, InstanceEvent.TYPE.power_on,
         self.openshift_and_rhel_image.ec2_ami_id)
     overview = reports.get_account_overview(
         self.account, self.start, self.end)
     # we expect to find 2 total images, 2 total instances, 1 rhel instance
     # and 1 openshift instance
     self.assertExpectedAccountOverview(overview, self.account,
                                        images=2, instances=2,
                                        rhel_instances=1,
                                        openshift_instances=1)
Beispiel #10
0
    def test_usage_on_times_overlapping(self):
        """
        Assert overlapping RHEL and OpenShift times are reported separately.

        This test asserts counting when the RHEL instance was on for 5 hours
        and the OpenShift instance was on for 5 hours at a another time that
        overlaps with the first by 2.5 hours.

        The instances' running times in the window would look like:
            [        ##                     ]
            [         ##                    ]
        """
        powered_times = (
            (
                util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
            ),
        )
        self.generate_events(powered_times, self.instance_1, self.image_rhel)

        powered_times = (
            (
                util_helper.utc_dt(2018, 1, 10, 2, 30, 0),
                util_helper.utc_dt(2018, 1, 10, 7, 30, 0)
            ),
        )
        self.generate_events(powered_times, self.instance_2, self.image_ocp)

        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=HOURS_5, openshift=HOURS_5)
        self.assertDaysSeen(results, rhel=1, openshift=1)
        self.assertInstancesSeen(results, rhel=1, openshift=1)
Beispiel #11
0
    def test_usage_on_times_not_overlapping(self):
        """
        Assert usage for 5 hours RHEL and 5 hours OpenShift in the period.

        This test asserts counting when the RHEL instance was on for 5 hours
        and the OpenShift instance was on for 5 hours at a different time.

        The instances' running times in the window would look like:
            [        ##                     ]
            [                    ##         ]
        """
        powered_times = (
            (
                util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
            ),
        )
        self.generate_events(powered_times, self.instance_1, self.image_rhel)

        powered_times = (
            (
                util_helper.utc_dt(2018, 1, 20, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 20, 5, 0, 0)
            ),
        )
        self.generate_events(powered_times, self.instance_2, self.image_ocp)

        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=HOURS_5, openshift=HOURS_5)
        self.assertDaysSeen(results, rhel=1, openshift=1)
        self.assertInstancesSeen(results, rhel=1, openshift=1)
Beispiel #12
0
    def test_usage_on_times_overlapping(self):
        """
        Assert usage for 10 hours powered in the period.

        This test asserts counting when one instance was on for 5 hours and
        another instance was on for 5 hours at a another time that overlaps
        with the first by 2.5 hours.

        The instances' running times in the window would look like:
            [        ##                     ]
            [         ##                    ]
        """
        powered_times = (
            (
                util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
            ),
        )
        self.generate_events(powered_times, self.instance_1)

        powered_times = (
            (
                util_helper.utc_dt(2018, 1, 10, 2, 30, 0),
                util_helper.utc_dt(2018, 1, 10, 7, 30, 0)
            ),
        )
        self.generate_events(powered_times, self.instance_2)

        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=HOURS_10)
        self.assertDaysSeen(results, rhel=1)
        self.assertInstancesSeen(results, rhel=2)
Beispiel #13
0
    def test_multiple_requests_use_precalculated_data(self):
        """Test multiple requests use the same saved pre-calculated data."""
        rhel_instance = api_helper.generate_instance(self.user1account1,
                                                     image=self.image_rhel)
        api_helper.generate_single_run(
            rhel_instance,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 0, 0),
            ),
            image=rhel_instance.machine_image,
        )
        request_date = datetime.date(2019, 5, 1)
        expected_date = request_date
        expected_instances = 1

        concurrent_usage = get_max_concurrent_usage(request_date,
                                                    user_id=self.user1.id)
        concurrent_usage_2 = get_max_concurrent_usage(request_date,
                                                      user_id=self.user1.id)
        concurrent_usage_3 = get_max_concurrent_usage(request_date,
                                                      user_id=self.user1.id)
        self.assertEqual(concurrent_usage, concurrent_usage_2)
        self.assertEqual(concurrent_usage, concurrent_usage_3)
        self.assertEqual(concurrent_usage.date, expected_date)
        self.assertEqual(len(concurrent_usage.maximum_counts), 24)
        for single_day_counts in concurrent_usage.maximum_counts:
            self.assertEqual(single_day_counts["instances_count"],
                             expected_instances)
Beispiel #14
0
    def test_normalize_instance_on_off(self):
        """Test normalize_runs for one plain instance."""
        powered_times = ((
            util_helper.utc_dt(2019, 1, 9, 0, 0, 0),
            util_helper.utc_dt(2019, 1, 10, 0, 0, 0),
        ), )
        events = api_helper.generate_instance_events(self.instance_plain,
                                                     powered_times)
        runs = util.normalize_runs(events)
        self.assertEquals(len(runs), 1)
        run = runs[0]
        self.assertEquals(run.start_time, powered_times[0][0])
        self.assertEquals(run.end_time, powered_times[0][1])
        self.assertEquals(run.instance_id, self.instance_plain.id)
        self.assertEquals(run.image_id, self.image_plain.id)

        # special flags
        self.assertFalse(run.is_cloud_access)
        self.assertFalse(run.is_encrypted)
        self.assertFalse(run.is_marketplace)

        # rhel detection
        self.assertFalse(run.rhel)
        self.assertFalse(run.rhel_detected)

        # openshift detection
        self.assertFalse(run.openshift)
        self.assertFalse(run.openshift_detected)
Beispiel #15
0
    def test_overlapping_rhel_runs_within_day(self):
        """
        Test with two overlapping RHEL instances run within the day.

        Because no account filter is applied, both instances are seen.
        """
        rhel_instance1 = api_helper.generate_instance(
            self.user1account1, image=self.image_rhel1
        )
        rhel_instance2 = api_helper.generate_instance(
            self.user1account1, image=self.image_rhel2
        )
        api_helper.generate_single_run(
            rhel_instance1,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 0, 0),
            ),
            image=rhel_instance1.machine_image,
        )
        api_helper.generate_single_run(
            rhel_instance2,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 30, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 30, 0),
            ),
            image=rhel_instance2.machine_image,
        )
        expected_date = request_date = datetime.date(2019, 5, 1)

        usage = calculate_max_concurrent_usage(request_date, user_id=self.user1.id)
        self.assertEqual(usage.date, expected_date)
        self.assertEqual(len(usage.maximum_counts), 32)
Beispiel #16
0
    def test_non_overlapping_rhel_runs_within_day(self):
        """
        Test with two non-overlapping RHEL instances run within the day.

        Two instances of different size run at different times, and this test
        should see the *larger* of the two matching the max values.
        """
        rhel_instance1 = api_helper.generate_instance(
            self.user1account1, image=self.image_rhel4
        )
        rhel_instance2 = api_helper.generate_instance(
            self.user1account2, image=self.image_rhel5
        )
        api_helper.generate_single_run(
            rhel_instance1,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 0, 0),
            ),
            image=rhel_instance1.machine_image,
        )
        api_helper.generate_single_run(
            rhel_instance2,
            (
                util_helper.utc_dt(2019, 5, 1, 3, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 4, 0, 0),
            ),
            image=rhel_instance2.machine_image,
        )
        request_date = datetime.date(2019, 5, 1)
        expected_date = request_date
        expected_instances = 1

        usage = calculate_max_concurrent_usage(request_date, user_id=self.user1.id)
        self.assertMaxConcurrentUsage(usage, expected_date, expected_instances)
Beispiel #17
0
    def test_report_with_timezones_specified(self):
        """Test that start/end dates with timezones shift correctly to UTC."""
        cloud_provider = AWS_PROVIDER_STRING
        cloud_account_id = util_helper.generate_dummy_aws_account_id()
        start_no_tz = '2018-01-01T00:00:00-05'
        end_no_tz = '2018-02-01T00:00:00+04'
        request_data = {
            'cloud_provider': cloud_provider,
            'cloud_account_id': str(cloud_account_id),
            'start': start_no_tz,
            'end': end_no_tz,
        }
        expected_start = util_helper.utc_dt(2018, 1, 1, 5, 0)
        expected_end = util_helper.utc_dt(2018, 1, 31, 20, 0)

        with patch.object(reports, 'get_time_usage') as mock_get_hourly:
            serializer = ReportSerializer(data=request_data)
            serializer.is_valid(raise_exception=True)
            results = serializer.generate()
            mock_get_hourly.assert_called_with(
                cloud_provider=cloud_provider,
                cloud_account_id=cloud_account_id,
                start=expected_start,
                end=expected_end)
            self.assertEqual(results, mock_get_hourly.return_value)
Beispiel #18
0
    def test_save_with_concurrent_usages(self):
        """Test that save deletes the related concurrent_usages."""
        user = util_helper.generate_test_user()
        aws_account_id = util_helper.generate_dummy_aws_account_id()
        account = api_helper.generate_cloud_account(
            aws_account_id=aws_account_id,
            user=user,
        )
        image = api_helper.generate_image(
            owner_aws_account_id=aws_account_id,
            rhel_detected=True,
        )
        instance = api_helper.generate_instance(account, image=image)
        api_helper.generate_single_run(
            instance,
            (
                util_helper.utc_dt(2019, 5, 1, 1, 0, 0),
                util_helper.utc_dt(2019, 5, 1, 2, 0, 0),
            ),
            image=instance.machine_image,
        )
        request_date = datetime.date(2019, 5, 1)
        calculate_max_concurrent_usage(request_date, user_id=user.id)

        self.assertEquals(1, ConcurrentUsage.objects.count())
        image.rhel_detected_by_tag = True
        image.save()
        self.assertEquals(0, ConcurrentUsage.objects.count())
Beispiel #19
0
    def test_usage_on_before_off_in_on_in_off_in_on_in(self):
        """
        Assert usage for 15 hours powered in the period.

        This test asserts counting when there was a power-on event before the
        reporting window start, a power-off event 5 hours into the window, a
        power-on event in the middle of the window, a power-off event 5 hours
        later, and a power-on event 5 hours before the window ends

        The instance's running time in the window would look like:
            [##            ##             ##]
        """
        powered_times = (
            (
                util_helper.utc_dt(2017, 1, 1, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 1, 5, 0, 0)
            ),
            (
                util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
            ),
            (util_helper.utc_dt(2018, 1, 31, 19, 0, 0), None),
        )
        self.generate_events(powered_times)
        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=HOURS_15)
        self.assertDaysSeen(results, rhel=3)
        self.assertInstancesSeen(results, rhel=1)
Beispiel #20
0
    def setUp(self):
        """
        Set up a bunch of test data.

        This gets very noisy very quickly because we need users who have
        accounts that have instances that have events that used various image
        types.
        """
        # Users
        self.user1 = util_helper.generate_test_user()
        self.user2 = util_helper.generate_test_user()

        # Accounts for the users
        self.account_u1_1 = api_helper.generate_cloud_account(user=self.user1)
        self.account_u1_2 = api_helper.generate_cloud_account(user=self.user1)
        self.account_u2_1 = api_helper.generate_cloud_account(user=self.user2)
        self.account_u2_2 = api_helper.generate_cloud_account(user=self.user2)

        # Images with various contents
        self.image_plain = api_helper.generate_image()
        self.image_windows = api_helper.generate_image(is_windows=True)
        self.image_rhel = api_helper.generate_image(rhel_detected=True)
        self.image_ocp = api_helper.generate_image(openshift_detected=True,
                                                   architecture="arm64")
        self.image_rhel_ocp = api_helper.generate_image(
            rhel_detected=True,
            openshift_detected=True,
            status=MachineImage.UNAVAILABLE)
        self.inspected_image = api_helper.generate_image(
            status=MachineImage.INSPECTED)

        # Instances for the accounts
        self.instance_u1_1 = api_helper.generate_instance(
            cloud_account=self.account_u1_1, image=self.image_plain)
        self.instance_u1_2 = api_helper.generate_instance(
            cloud_account=self.account_u1_2, image=self.image_rhel)
        self.instance_u2_1 = api_helper.generate_instance(
            cloud_account=self.account_u2_1, image=self.image_ocp)
        self.instance_u2_2 = api_helper.generate_instance(
            cloud_account=self.account_u2_2, image=self.image_rhel_ocp)

        # Some initial event activity spread across the accounts
        powered_times = ((
            util_helper.utc_dt(2018, 1, 9, 0, 0, 0),
            util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
        ), )
        instance_images = (
            (self.instance_u1_1, self.image_plain),
            (self.instance_u1_2, self.image_rhel),
            (self.instance_u2_1, self.image_ocp),
            (self.instance_u2_2, self.image_rhel_ocp),
        )
        for instance, image in instance_images:
            self.generate_events(powered_times, instance, image)

        self.factory = APIRequestFactory()
Beispiel #21
0
    def test_generate_aws_account_with_args(self):
        """Assert generation of an AwsAccount with all specified args."""
        aws_account_id = util_helper.generate_dummy_aws_account_id()
        arn = util_helper.generate_dummy_arn(account_id=aws_account_id)
        user = util_helper.generate_test_user()
        name = _faker.name()
        created_at = util_helper.utc_dt(2017, 1, 1, 0, 0, 0)
        platform_authentication_id = _faker.pyint()
        platform_application_id = _faker.pyint()
        platform_source_id = _faker.pyint()
        is_enabled = False
        enabled_at = util_helper.utc_dt(2017, 1, 2, 0, 0, 0)

        schedule, _ = IntervalSchedule.objects.get_or_create(
            every=settings.SCHEDULE_VERIFY_VERIFY_TASKS_INTERVAL,
            period=IntervalSchedule.SECONDS,
        )
        verify_task, _ = PeriodicTask.objects.get_or_create(
            interval=schedule,
            name=f"Verify {arn}.",
            task="api.clouds.aws.tasks.verify_account_permissions",
            kwargs=json.dumps(
                {
                    "account_arn": arn,
                }
            ),
            defaults={"start_time": created_at},
        )

        account = helper.generate_cloud_account(
            arn,
            aws_account_id,
            user,
            name,
            created_at,
            platform_authentication_id,
            platform_application_id,
            platform_source_id,
            is_enabled,
            enabled_at,
            verify_task,
        )
        self.assertIsInstance(account, CloudAccount)
        self.assertEqual(account.content_object.account_arn, arn)
        self.assertEqual(account.content_object.aws_account_id, aws_account_id)

        self.assertEqual(account.platform_authentication_id, platform_authentication_id)
        self.assertEqual(account.platform_application_id, platform_application_id)
        self.assertEqual(account.platform_source_id, platform_source_id)
        self.assertEqual(account.user, user)
        self.assertEqual(account.name, name)
        self.assertEqual(account.created_at, created_at)
        self.assertFalse(account.is_enabled)
        self.assertEqual(account.enabled_at, enabled_at)
        self.assertEqual(account.content_object.verify_task, verify_task)
Beispiel #22
0
 def test_events_only_in_future(self):
     """Assert empty report when events exist only in the past."""
     powered_times = (
         (
             util_helper.utc_dt(2019, 1, 9, 0, 0, 0),
             util_helper.utc_dt(2019, 1, 10, 0, 0, 0)
         ),
     )
     self.generate_events(powered_times)
     results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
     self.assertNoActivityFound(results)
Beispiel #23
0
 def test_events_in_other_user_account(self):
     """Assert empty report when events exist only for a different user."""
     powered_times = (
         (
             util_helper.utc_dt(2019, 1, 9, 0, 0, 0),
             util_helper.utc_dt(2019, 1, 10, 0, 0, 0)
         ),
     )
     self.generate_events(powered_times)
     results = reports.get_daily_usage(self.user_2.id, self.start, self.end)
     self.assertNoActivityFound(results)
Beispiel #24
0
 def test_events_not_rhel_not_openshift(self):
     """Assert empty report when events exist only for plain images."""
     powered_times = (
         (
             util_helper.utc_dt(2018, 1, 9, 0, 0, 0),
             util_helper.utc_dt(2018, 1, 10, 0, 0, 0)
         ),
     )
     self.generate_events(powered_times, image=self.image_plain)
     results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
     self.assertNoActivityFound(results)
 def setUp(self):
     """Set up data for an account with a running instance."""
     self.account = account_helper.generate_cloud_account()
     self.instance = account_helper.generate_instance(self.account)
     self.aws_instance = self.instance.content_object
     self.region = self.aws_instance.region
     self.power_on_time = util_helper.utc_dt(2018, 1, 1, 0, 0, 0)
     self.power_off_time = util_helper.utc_dt(2019, 1, 1, 0, 0, 0)
     event = account_helper.generate_single_instance_event(
         self.instance,
         self.power_on_time,
         event_type=InstanceEvent.TYPE.power_on)
     self.process_event(event)
Beispiel #26
0
    def test_delete_run_removes_concurrent_usage(self):
        """Test when a run is deleted, related concurrent usage are deleted."""
        account = helper.generate_cloud_account()

        image = helper.generate_image()
        instance = helper.generate_instance(cloud_account=account, image=image)
        runtime = (
            util_helper.utc_dt(2019, 1, 1, 0, 0, 0),
            util_helper.utc_dt(2019, 1, 2, 0, 0, 0),
        )
        helper.generate_single_run(instance=instance, runtime=runtime)
        self.assertGreater(models.ConcurrentUsage.objects.count(), 0)
        models.Run.objects.all().delete()
        self.assertEqual(models.ConcurrentUsage.objects.count(), 0)
Beispiel #27
0
 def test_get_cloud_account_overview_with_events(self):
     """Assert an account overview reports instances/images correctly."""
     powered_times = (
         (
             util_helper.utc_dt(2018, 1, 10, 0, 0, 0),
             util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
         ),
     )
     account_helper.generate_aws_instance_events(
         self.instance_1, powered_times,
         self.windows_image.ec2_ami_id
     )
     overview = reports.get_account_overview(
         self.account, self.start, self.end)
     self.assertExpectedAccountOverview(overview, self.account,
                                        images=1, instances=1)
Beispiel #28
0
    def test_enable_failure_too_many_trails(self, mock_notify_sources):
        """Test that enabling an account rolls back if too many trails present."""
        # Normally you shouldn't directly manipulate the is_enabled value,
        # but here we need to force it down to check that it gets set back.
        self.account.is_enabled = False
        self.account.save()
        self.account.refresh_from_db()
        self.assertFalse(self.account.is_enabled)

        enable_date = util_helper.utc_dt(2019, 1, 4, 0, 0, 0)
        with patch.object(
                aws, "configure_cloudtrail"
        ) as mock_cloudtrail, patch.object(
                aws, "verify_account_access"
        ) as mock_verify, patch.object(aws, "get_session"), patch(
                "api.clouds.aws.tasks.initial_aws_describe_instances"
        ) as mock_initial_aws_describe_instances, util_helper.clouditardis(
                enable_date):
            mock_cloudtrail.side_effect = MaximumNumberOfTrailsExceededException(
                "MaximumNumberOfTrailsExceededException", )
            mock_verify.return_value = (True, "")
            with self.assertLogs("api.clouds.aws.util", level="WARNING") as cm:
                self.account.enable()
                self.assertIn(
                    str(mock_cloudtrail.side_effect.detail),
                    cm.records[1].message,
                )
            mock_cloudtrail.assert_called()
            mock_initial_aws_describe_instances.delay.assert_not_called()

        self.account.refresh_from_db()
        self.assertFalse(self.account.is_enabled)
        self.assertEqual(self.account.enabled_at, self.account.created_at)
Beispiel #29
0
    def test_enable_verify_task_existing(
        self,
        mock_describe,
        mock_verify,
        mock_notify_sources,
    ):
        """Verify Task is assigned and re-enabled since it already existed."""
        aws_clount = self.account.content_object
        self.assertIsNone(aws_clount.verify_task)

        schedule, _ = IntervalSchedule.objects.get_or_create(
            every=settings.SCHEDULE_VERIFY_VERIFY_TASKS_INTERVAL,
            period=IntervalSchedule.SECONDS,
        )
        verify_task = PeriodicTask.objects.create(
            interval=schedule,
            name=f"Verify {aws_clount.account_arn}.",
            task="api.clouds.aws.tasks.verify_account_permissions",
            kwargs=json.dumps({
                "account_arn": aws_clount.account_arn,
            }),
            start_time=self.created_at,
        )

        verify_task.enabled = False
        verify_task.save()

        enable_date = util_helper.utc_dt(2019, 1, 4, 0, 0, 0)
        with util_helper.clouditardis(enable_date):
            self.account.enable()

        self.assertIsNotNone(aws_clount.verify_task)
Beispiel #30
0
    def test_command_output_azure(self, mock_call_command, mock_tqdm, mock_random):
        """Test that 'spawndata' correctly calls seeds data for azure account."""
        # Silence tqdm output during the test.
        mock_tqdm.side_effect = lambda iterable, *args, **kwargs: iterable

        # Make all random calculations deterministic.
        mock_random.choice.side_effect = lambda population: population[0]
        mock_random.random.return_value = 0.0
        mock_random.randrange.side_effect = lambda start, stop: (start + stop) / 2
        mock_random.gauss.side_effect = lambda mu, sigma: mu
        mock_random.uniform.side_effect = lambda a, b: (a + b) / 2

        call_command(
            "spawndata",
            "--cloud_type=azure",
            "--account_count=1",
            "--image_count=5",
            "--instance_count=10",
            "--other_owner_chance=0.0",
            "--rhel_chance=1.0",
            "--ocp_chance=1.0",
            "--min_run_hours=1",
            "--mean_run_count=1",
            "--mean_hours_between_runs=1",
            "--confirm",
            "1",
            "2019-04-15",
        )

        mock_call_command.assert_called_with("create_runs", "--confirm")

        self.assertEquals(AzureCloudAccount.objects.count(), 1)
        self.assertEquals(AzureMachineImage.objects.count(), 5)
        for image in MachineImage.objects.all():
            self.assertTrue(image.rhel)
            self.assertTrue(image.openshift)
        self.assertEquals(AzureInstance.objects.count(), 10)
        self.assertEquals(AzureInstanceEvent.objects.count(), 20)

        # Verify all events were created between the start and "now".
        # Remember that clouditardis changed "now" to 2019-04-20.
        start_time_range = util_helper.utc_dt(2019, 4, 15)
        end_time_range = util_helper.utc_dt(2019, 4, 20)
        for event in InstanceEvent.objects.all():
            self.assertGreaterEqual(event.occurred_at, start_time_range)
            self.assertLess(event.occurred_at, end_time_range)