Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
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)
Exemplo n.º 8
0
    def generate(self):
        """Generate the usage report and return the results."""
        start = self.validated_data['start']
        end = self.validated_data['end']
        name_pattern = self.validated_data.get('name_pattern', None)

        user = self.context['request'].user
        user_id = user.id
        account_id = self.validated_data.get('account_id', None)

        if user.is_superuser:
            user_id = self.validated_data.get('user_id', user.id)

        return reports.get_daily_usage(user_id, start, end, name_pattern,
                                       account_id)
Exemplo n.º 9
0
    def test_usage_on_before_off_never(self):
        """
        Assert usage for all 31 days powered in the period.

        This test asserts counting when there was a power-on event before
        the report window starts and nothing else.

        The instance's running time in the window would look like:
            [###############################]
        """
        powered_times = ((util_helper.utc_dt(2017, 1, 1), None),)
        self.generate_events(powered_times)
        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=DAYS_31)
        self.assertDaysSeen(results, rhel=31)
        self.assertInstancesSeen(results, rhel=1)
Exemplo n.º 10
0
    def test_usage_on_in(self):
        """
        Assert usage for 5 hours powered in the period.

        This test asserts counting when there was only a power-on event 5 hours
        before the report window ends.

        The instance's running time in the window would look like:
            [                             ##]
        """
        powered_times = ((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_5)
        self.assertDaysSeen(results, rhel=1)
        self.assertInstancesSeen(results, rhel=1)
Exemplo n.º 11
0
    def test_usage_on_over_multiple_days_then_off(self):
        """
        Assert usage when powered on over multiple days.

        This test asserts counting when there was a power-on event at the start
        of the reporting window start, several days pass, and then a power-off
        event 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, 6, 5, 0, 0)
            ),
        )
        self.generate_events(powered_times)
        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=DAY * 5 + HOURS_5)
        self.assertDaysSeen(results, rhel=6)
        self.assertInstancesSeen(results, rhel=1)
Exemplo n.º 12
0
    def test_usage_on_in_on_in_off_in(self):
        """
        Assert usage for 5 hours powered in the period.

        This test asserts counting when there's a both power-on event, a second
        bogus power-on even 1 hour later, and a power-off event 4 more hours
        later, all inside the report window.

        The instance's running time in the window would look like:
            [        ##                     ]
        """
        powered_times = (
            (util_helper.utc_dt(2018, 1, 10, 0, 0, 0), None),
            (
                util_helper.utc_dt(2018, 1, 10, 1, 0, 0),
                util_helper.utc_dt(2018, 1, 10, 5, 0, 0)
            ),
        )
        self.generate_events(powered_times)
        results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
        self.assertTotalRunningTimes(results, rhel=HOURS_5)
        self.assertDaysSeen(results, rhel=1)
        self.assertInstancesSeen(results, rhel=1)
Exemplo n.º 13
0
    def test_several_instances_with_whole_days(self):
        """
        Assert correct report for instances with various run times.

        The RHEL-only running times over the month would look like:

            [ ####      ##                  ]
            [  ##                ##         ]

        The plain running times over the month would look like:

            [  #####                        ]

        The OpenShift-only running times over the month would look like:

            [                  ###          ]

        The RHEL+OpenShift running times over the month would look like:

            [        #          ##          ]
        """
        powered_times_1 = (
            (
                util_helper.utc_dt(2018, 1, 2, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 6, 0, 0, 0)
            ),
            (
                util_helper.utc_dt(2018, 1, 12, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 14, 0, 0, 0)
            ),
        )
        account_helper.generate_aws_instance_events(
            self.instance_1,
            powered_times_1,
            ec2_ami_id=self.image_rhel.ec2_ami_id,
        )

        powered_times_2 = (
            (
                util_helper.utc_dt(2018, 1, 3, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 5, 0, 0, 0)
            ),
            (
                util_helper.utc_dt(2018, 1, 21, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 23, 0, 0, 0)
            ),
        )
        account_helper.generate_aws_instance_events(
            self.instance_2,
            powered_times_2,
            ec2_ami_id=self.image_rhel.ec2_ami_id,
        )

        powered_times_3 = (
            (
                util_helper.utc_dt(2018, 1, 3, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 8, 0, 0, 0)
            ),
        )
        account_helper.generate_aws_instance_events(
            self.instance_3,
            powered_times_3,
            ec2_ami_id=self.image_plain.ec2_ami_id,
        )

        powered_times_4 = (
            (
                util_helper.utc_dt(2018, 1, 19, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 22, 0, 0, 0)
            ),
        )
        account_helper.generate_aws_instance_events(
            self.instance_4,
            powered_times_4,
            ec2_ami_id=self.image_ocp.ec2_ami_id,
        )

        powered_times_5 = (
            (
                util_helper.utc_dt(2018, 1, 9, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 10, 0, 0, 0)
            ),
            (
                util_helper.utc_dt(2018, 1, 20, 0, 0, 0),
                util_helper.utc_dt(2018, 1, 22, 0, 0, 0)
            ),
        )
        account_helper.generate_aws_instance_events(
            self.instance_5,
            powered_times_5,
            ec2_ami_id=self.image_rhel_ocp.ec2_ami_id,
        )

        results = reports.get_daily_usage(
            self.account_1.user_id,
            self.start,
            self.end,
        )

        self.assertEqual(len(results['daily_usage']), 31)
        self.assertInstancesSeen(results, rhel=3, openshift=2)

        # total of rhel seconds should be 13 days worth of seconds.
        # total of openshift seconds should be 6 days worth of seconds.
        self.assertTotalRunningTimes(results, rhel=DAY * 13, openshift=DAY * 6)

        # number of individual days in which we saw anything rhel is 10
        # number of individual days in which we saw anything openshift is 4
        self.assertDaysSeen(results, rhel=10, openshift=4)

        # number of days in which we saw 2 rhel running all day is 3
        self.assertEqual(sum((
            1 for day in results['daily_usage']
            if day['rhel_runtime_seconds'] == DAY * 2
        )), 3)

        # number of days in which we saw 1 rhel running all day is 7
        self.assertEqual(sum((
            1 for day in results['daily_usage']
            if day['rhel_runtime_seconds'] == DAY
        )), 7)

        # number of days in which we saw 1 openshift running all day is 2
        self.assertEqual(sum((
            1 for day in results['daily_usage']
            if day['openshift_runtime_seconds'] == DAY * 2
        )), 2)

        # number of days in which we saw 2 openshift running all day is 2
        self.assertEqual(sum((
            1 for day in results['daily_usage']
            if day['openshift_runtime_seconds'] == DAY * 2
        )), 2)
Exemplo n.º 14
0
 def test_no_events(self):
     """Assert empty report when no events exist."""
     results = reports.get_daily_usage(self.user_1.id, self.start, self.end)
     self.assertNoActivityFound(results)