def _generate_ocp_on_aws_data(self, cluster_id=None):
        """Generate OCP and AWS data."""
        if not cluster_id:
            cluster_id = self.ocp_provider_resource_name
        creator = ReportObjectCreator(self.schema, self.column_map)

        bill_ids = []

        today = DateAccessor().today_with_timezone("UTC")
        last_month = today - relativedelta(months=1)
        resource_id = "i-12345"

        for cost_entry_date in (today, last_month):
            bill = creator.create_cost_entry_bill(
                provider_uuid=self.aws_provider_uuid,
                bill_date=cost_entry_date)
            bill_ids.append(str(bill.id))
            cost_entry = creator.create_cost_entry(bill, cost_entry_date)
            product = creator.create_cost_entry_product("Compute Instance")
            pricing = creator.create_cost_entry_pricing()
            reservation = creator.create_cost_entry_reservation()
            creator.create_cost_entry_line_item(bill,
                                                cost_entry,
                                                product,
                                                pricing,
                                                reservation,
                                                resource_id=resource_id)

        with AWSReportDBAccessor(self.schema, self.column_map) as aws_accessor:
            aws_accessor.populate_line_item_daily_table(
                last_month.date(), today.date(), bill_ids)

        provider_uuid = self.ocp_provider_uuid

        for cost_entry_date in (today, last_month):
            period = creator.create_ocp_report_period(
                provider_uuid=provider_uuid,
                period_date=cost_entry_date,
                cluster_id=cluster_id)
            report = creator.create_ocp_report(period, cost_entry_date)
            creator.create_ocp_usage_line_item(period,
                                               report,
                                               resource_id=resource_id)
        cluster_id = get_cluster_id_from_provider(self.ocp_test_provider_uuid)
        with OCPReportDBAccessor(self.schema, self.column_map) as ocp_accessor:
            ocp_accessor.populate_line_item_daily_table(
                last_month.date(), today.date(), cluster_id)
    def _generate_ocp_on_aws_data(self):
        """Test that the OCP on AWS cost summary table is populated."""
        creator = ReportObjectCreator(self.schema, self.column_map)

        bill_ids = []

        today = DateAccessor().today_with_timezone('UTC')
        last_month = today - relativedelta.relativedelta(months=1)
        resource_id = 'i-12345'

        for cost_entry_date in (today, last_month):
            bill = creator.create_cost_entry_bill(
                provider_id=self.aws_provider.id, bill_date=cost_entry_date)
            bill_ids.append(str(bill.id))
            cost_entry = creator.create_cost_entry(bill, cost_entry_date)
            product = creator.create_cost_entry_product('Compute Instance')
            pricing = creator.create_cost_entry_pricing()
            reservation = creator.create_cost_entry_reservation()
            creator.create_cost_entry_line_item(bill,
                                                cost_entry,
                                                product,
                                                pricing,
                                                reservation,
                                                resource_id=resource_id)

        with AWSReportDBAccessor(self.schema, self.column_map) as aws_accessor:
            aws_accessor.populate_line_item_daily_table(
                last_month.date(), today.date(), bill_ids)

        cluster_id = self.ocp_provider_resource_name
        provider_id = self.ocp_provider.id

        for cost_entry_date in (today, last_month):
            period = creator.create_ocp_report_period(cost_entry_date,
                                                      provider_id=provider_id,
                                                      cluster_id=cluster_id)
            report = creator.create_ocp_report(period, cost_entry_date)
            creator.create_ocp_usage_line_item(period,
                                               report,
                                               resource_id=resource_id)
        cluster_id = get_cluster_id_from_provider(self.ocp_test_provider_uuid)
        with OCPReportDBAccessor(self.schema, self.column_map) as ocp_accessor:
            ocp_accessor.populate_line_item_daily_table(
                last_month.date(), today.date(), cluster_id)
Пример #3
0
    def test_update_summary_tables_without_manifest(self, mock_daily, mock_sum,
                                                    mock_storage_daily,
                                                    mock_storage_summary,
                                                    mock_node_daily):
        """Test that summary tables are properly run without a manifest."""
        # Create an updater that doesn't have a manifest
        updater = OCPReportSummaryUpdater(self.schema, self.provider, None)
        start_date = DateAccessor().today_with_timezone("UTC")
        end_date = start_date + datetime.timedelta(days=1)
        bill_date = start_date.replace(day=1).date()

        with schema_context(self.schema):
            period = self.accessor.get_usage_periods_by_date(bill_date)[0]
            period.summary_data_updated_datetime = start_date
            period.save()

        start_date_str = start_date.strftime("%Y-%m-%d")
        end_date_str = end_date.strftime("%Y-%m-%d")

        updater.update_daily_tables(start_date_str, end_date_str)
        mock_node_daily.assert_called_with(start_date.date(), end_date.date(),
                                           self.cluster_id)
        mock_daily.assert_called_with(start_date.date(), end_date.date(),
                                      self.cluster_id)
        mock_storage_daily.assert_called_with(start_date.date(),
                                              end_date.date(), self.cluster_id)
        mock_sum.assert_not_called()
        mock_storage_summary.assert_not_called()

        updater.update_summary_tables(start_date_str, end_date_str)
        mock_sum.assert_called_with(start_date.date(), end_date.date(),
                                    self.cluster_id, self.provider.uuid)
        mock_storage_summary.assert_called_with(start_date.date(),
                                                end_date.date(),
                                                self.cluster_id,
                                                self.provider.uuid)

        with OCPReportDBAccessor(self.schema) as accessor:
            period = accessor.get_usage_periods_by_date(bill_date)[0]
            self.assertIsNotNone(period.summary_data_creation_datetime)
            self.assertGreater(period.summary_data_updated_datetime,
                               self.today)
Пример #4
0
 def test_get_usage_periods_by_date(self):
     """Test that report periods are returned by date filter."""
     period_start = DateAccessor().today_with_timezone('UTC').replace(day=1)
     prev_period_start = period_start - relativedelta.relativedelta(months=1)
     reporting_period = self.creator.create_ocp_report_period(period_start)
     prev_reporting_period = self.creator.create_ocp_report_period(
         prev_period_start
     )
     periods = self.accessor.get_usage_periods_by_date(period_start.date())
     self.assertIn(reporting_period, periods)
     periods = self.accessor.get_usage_periods_by_date(prev_period_start.date())
     self.assertIn(prev_reporting_period, periods)
Пример #5
0
 def test_get_usage_period_by_dates_and_cluster(self):
     """Test that report periods are returned by dates & cluster filter."""
     period_start = DateAccessor().today_with_timezone("UTC").replace(day=1)
     period_end = period_start + relativedelta.relativedelta(months=1)
     prev_period_start = period_start - relativedelta.relativedelta(months=1)
     prev_period_end = prev_period_start + relativedelta.relativedelta(months=1)
     reporting_period = self.creator.create_ocp_report_period(
         self.ocp_provider_uuid, period_date=period_start, cluster_id="0001"
     )
     prev_reporting_period = self.creator.create_ocp_report_period(
         self.ocp_provider_uuid, period_date=prev_period_start, cluster_id="0002"
     )
     with schema_context(self.schema):
         periods = self.accessor.get_usage_period_by_dates_and_cluster(
             period_start.date(), period_end.date(), "0001"
         )
         self.assertEqual(reporting_period, periods)
         periods = self.accessor.get_usage_period_by_dates_and_cluster(
             prev_period_start.date(), prev_period_end.date(), "0002"
         )
         self.assertEqual(prev_reporting_period, periods)