def test_query_by_partial_filtered_service(self):
        """Test execute_query monthly breakdown by filtered service."""
        url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[service]=eC2"  # noqa: E501
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        query_output = handler.execute_query()
        data = query_output.get("data")
        self.assertIsNotNone(data)
        self.assertIsNotNone(query_output.get("total"))
        total = query_output.get("total")
        self.assertIsNotNone(total.get("cost"))
        filt = copy.deepcopy(self.this_month_filter)
        filt["product_code__icontains"] = "ec2"
        aggregates = handler._mapper.report_type_map.get("aggregates")
        current_totals = self.get_totals_by_time_scope(aggregates, filt)
        self.assertEqual(
            total.get("cost", {}).get("total", {}).get("value", 0),
            current_totals.get("cost_total", 1))

        cmonth_str = DateHelper().this_month_start.strftime("%Y-%m")
        for data_item in data:
            month_val = data_item.get("date")
            month_data = data_item.get("services")
            self.assertEqual(month_val, cmonth_str)
            self.assertIsInstance(month_data, list)
            for month_item in month_data:
                compute = month_item.get("service")
                self.assertEqual(compute, "AmazonEC2")
                self.assertIsInstance(month_item.get("values"), list)
Exemplo n.º 2
0
    def test_execute_query_current_month_by_account(self):
        """Test execute_query for current month on monthly breakdown by account."""
        url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[account]=*"  # noqa: E501
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        query_output = handler.execute_query()
        data = query_output.get("data")
        self.assertIsNotNone(data)
        self.assertIsNotNone(query_output.get("total"))
        total = query_output.get("total")
        self.assertIsNotNone(total.get("cost"))

        aggregates = handler._mapper.report_type_map.get("aggregates")
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(
            total.get("cost", {}).get("value", 0),
            current_totals.get("cost", 1))

        cmonth_str = DateHelper().this_month_start.strftime("%Y-%m")
        for data_item in data:
            month_val = data_item.get("date", "not-a-date")
            month_data = data_item.get("accounts", "not-a-list")
            self.assertEqual(month_val, cmonth_str)
            self.assertIsInstance(month_data, list)
            for month_item in month_data:
                self.assertIsInstance(month_item.get("values"), list)
Exemplo n.º 3
0
    def test_execute_query_by_account_by_service(self):
        """Test execute_query for current month breakdown by account by service."""
        url = '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[account]=*&group_by[service]=*'  # noqa: E501
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        query_output = handler.execute_query()
        data = query_output.get('data')
        self.assertIsNotNone(data)
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(
            total.get('cost', {}).get('value'), current_totals.get('cost'))

        cmonth_str = DateHelper().this_month_start.strftime('%Y-%m')
        for data_item in data:
            month_val = data_item.get('date')
            month_data = data_item.get('accounts')
            self.assertEqual(month_val, cmonth_str)
            self.assertIsInstance(month_data, list)
            for month_item in month_data:
                self.assertIsInstance(month_item.get('services'), list)
Exemplo n.º 4
0
    def test_execute_query_by_account_by_service(self):
        """Test execute_query for current month breakdown by account by service."""
        query_params = {'filter':
                        {'resolution': 'monthly', 'time_scope_value': -1,
                         'time_scope_units': 'month'},
                        'group_by': {'account': ['*'],
                                     'service': ['*']}}
        query_string = '?group_by[account]=*&group_by[service]=*'
        handler = OCPAWSReportQueryHandler(query_params, query_string,
                                           self.tenant,
                                           **{'report_type': 'costs'})
        query_output = handler.execute_query()
        data = query_output.get('data')
        self.assertIsNotNone(data)
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(total.get('cost', {}).get('value'), current_totals.get('cost'))

        cmonth_str = DateHelper().this_month_start.strftime('%Y-%m')
        for data_item in data:
            month_val = data_item.get('date')
            month_data = data_item.get('accounts')
            self.assertEqual(month_val, cmonth_str)
            self.assertIsInstance(month_data, list)
            for month_item in month_data:
                self.assertIsInstance(month_item.get('services'), list)
Exemplo n.º 5
0
 def test_execute_sum_query_instance_types(self):
     """Test that the sum query runs properly for instance-types."""
     query_params = {}
     handler = OCPAWSReportQueryHandler(
         query_params,
         '',
         self.tenant,
         **{'report_type': 'instance_type'}
     )
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertIsNotNone(query_output.get('total'))
     total = query_output.get('total')
     self.assertIsNotNone(total.get('cost'))
     self.assertIsInstance(total.get('cost'), dict)
     self.assertEqual(total.get('cost').get('value'), 0)
     self.assertEqual(total.get('cost').get('units'), 'USD')
     self.assertIsNotNone(total.get('usage'))
     self.assertIsInstance(total.get('usage'), dict)
     self.assertEqual(total.get('usage').get('value'), 0)
     self.assertEqual(total.get('usage').get('units'), 'Hrs')
     self.assertIsNotNone(total.get('count'))
     self.assertIsInstance(total.get('count'), dict)
     self.assertEqual(total.get('count').get('value'), 0)
     self.assertEqual(total.get('count').get('units'), 'instances')
Exemplo n.º 6
0
    def test_query_by_partial_filtered_service(self):
        """Test execute_query monthly breakdown by filtered service."""
        query_params = {'filter':
                        {'resolution': 'monthly', 'time_scope_value': -1,
                         'time_scope_units': 'month'},
                        'group_by': {'service': ['eC2']}}
        handler = OCPAWSReportQueryHandler(query_params, '?group_by[service]=eC2',
                                           self.tenant,
                                           **{'report_type': 'costs'})
        query_output = handler.execute_query()
        data = query_output.get('data')
        self.assertIsNotNone(data)
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(total.get('cost', {}).get('value'), current_totals.get('cost'))

        cmonth_str = DateHelper().this_month_start.strftime('%Y-%m')
        for data_item in data:
            month_val = data_item.get('date')
            month_data = data_item.get('services')
            self.assertEqual(month_val, cmonth_str)
            self.assertIsInstance(month_data, list)
            for month_item in month_data:
                compute = month_item.get('service')
                self.assertEqual(compute, 'AmazonEC2')
                self.assertIsInstance(month_item.get('values'), list)
Exemplo n.º 7
0
 def test_source_uuid_mapping(self):  # noqa: C901
     """Test source_uuid is mapped to the correct source."""
     endpoints = [OCPAWSCostView, OCPAWSInstanceTypeView, OCPAWSStorageView]
     with tenant_context(self.tenant):
         expected_source_uuids = list(AWSCostEntryBill.objects.distinct().values_list("provider_id", flat=True))
     source_uuid_list = []
     for endpoint in endpoints:
         urls = ["?"]
         if endpoint == OCPAWSCostView:
             urls.extend(["?group_by[account]=*", "?group_by[service]=*", "?group_by[region]=*"])
         for url in urls:
             query_params = self.mocked_query_params(url, endpoint)
             handler = OCPAWSReportQueryHandler(query_params)
             query_output = handler.execute_query()
             for dictionary in query_output.get("data"):
                 for _, value in dictionary.items():
                     if isinstance(value, list):
                         for item in value:
                             if isinstance(item, dict):
                                 if "values" in item.keys():
                                     value = item["values"][0]
                                     source_uuid_list.extend(value.get("source_uuid"))
     self.assertNotEquals(source_uuid_list, [])
     for source_uuid in source_uuid_list:
         self.assertIn(source_uuid, expected_source_uuids)
Exemplo n.º 8
0
 def test_execute_sum_query_storage(self):
     """Test that the sum query runs properly."""
     url = '?'
     query_params = self.mocked_query_params(url, OCPAWSStorageView)
     handler = OCPAWSReportQueryHandler(query_params)
     filt = {'product_family__contains': 'Storage'}
     filt.update(self.ten_day_filter)
     aggregates = handler._mapper.report_type_map.get('aggregates')
     current_totals = self.get_totals_by_time_scope(aggregates, filt)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertIsNotNone(query_output.get('total'))
     total = query_output.get('total')
     self.assertEqual(total.get('total'), current_totals.get('total'))
Exemplo n.º 9
0
    def test_execute_query_current_month_monthly(self):
        """Test execute_query for current month on monthly breakdown."""
        url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=daily"
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        query_output = handler.execute_query()
        self.assertIsNotNone(query_output.get("data"))
        self.assertIsNotNone(query_output.get("total"))
        total = query_output.get("total")
        self.assertIsNotNone(total.get("cost"))

        aggregates = handler._mapper.report_type_map.get("aggregates")
        current_totals = self.get_totals_by_time_scope(aggregates, self.this_month_filter)
        self.assertEqual(total.get("cost", {}).get("total", {}).get("value", 0), current_totals.get("cost_total", 1))
Exemplo n.º 10
0
 def test_execute_sum_query_storage(self):
     """Test that the sum query runs properly."""
     url = "?"
     query_params = self.mocked_query_params(url, OCPAWSStorageView)
     handler = OCPAWSReportQueryHandler(query_params)
     filt = {"product_family__contains": "Storage"}
     filt.update(self.ten_day_filter)
     aggregates = handler._mapper.report_type_map.get("aggregates")
     current_totals = self.get_totals_by_time_scope(aggregates, filt)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get("data"))
     self.assertIsNotNone(query_output.get("total"))
     total = query_output.get("total")
     self.assertEqual(total.get("cost", {}).get("total", {}).get("value", 0), current_totals.get("cost_total", 1))
Exemplo n.º 11
0
    def test_execute_query_current_month_daily(self):
        """Test execute_query for current month on daily breakdown."""
        url = '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=daily'
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        query_output = handler.execute_query()
        self.assertIsNotNone(query_output.get('data'))
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(
            total.get('cost', {}).get('value'), current_totals.get('cost'))
Exemplo n.º 12
0
    def test_execute_query_current_month_monthly(self):
        """Test execute_query for current month on monthly breakdown."""
        query_params = {'filter':
                        {'resolution': 'monthly', 'time_scope_value': -1,
                         'time_scope_units': 'month'}}
        handler = OCPAWSReportQueryHandler(query_params, '', self.tenant,
                                           **{'report_type': 'costs'})
        query_output = handler.execute_query()
        self.assertIsNotNone(query_output.get('data'))
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(total.get('cost', {}).get('value'), current_totals.get('cost'))
Exemplo n.º 13
0
 def test_execute_sum_query_instance_types(self):
     """Test that the sum query runs properly for instance-types."""
     url = "?"
     query_params = self.mocked_query_params(url, OCPAWSInstanceTypeView)
     handler = OCPAWSReportQueryHandler(query_params)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get("data"))
     self.assertIsNotNone(query_output.get("total"))
     total = query_output.get("total")
     self.assertIsNotNone(total.get("cost"))
     self.assertIsInstance(total.get("cost"), dict)
     self.assertEqual(total.get("cost").get("value"), 0)
     self.assertEqual(total.get("cost").get("units"), "USD")
     self.assertIsNotNone(total.get("usage"))
     self.assertIsInstance(total.get("usage"), dict)
     self.assertEqual(total.get("usage").get("value"), 0)
     self.assertEqual(total.get("usage").get("units"), "Hrs")
     self.assertIsNotNone(total.get("count"))
     self.assertIsInstance(total.get("count"), dict)
     self.assertEqual(total.get("count").get("value"), 0)
     self.assertEqual(total.get("count").get("units"), "instances")
Exemplo n.º 14
0
 def test_execute_sum_query_instance_types(self):
     """Test that the sum query runs properly for instance-types."""
     url = '?'
     query_params = self.mocked_query_params(url, OCPAWSInstanceTypeView)
     handler = OCPAWSReportQueryHandler(query_params)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertIsNotNone(query_output.get('total'))
     total = query_output.get('total')
     self.assertIsNotNone(total.get('cost'))
     self.assertIsInstance(total.get('cost'), dict)
     self.assertEqual(total.get('cost').get('value'), 0)
     self.assertEqual(total.get('cost').get('units'), 'USD')
     self.assertIsNotNone(total.get('usage'))
     self.assertIsInstance(total.get('usage'), dict)
     self.assertEqual(total.get('usage').get('value'), 0)
     self.assertEqual(total.get('usage').get('units'), 'Hrs')
     self.assertIsNotNone(total.get('count'))
     self.assertIsInstance(total.get('count'), dict)
     self.assertEqual(total.get('count').get('value'), 0)
     self.assertEqual(total.get('count').get('units'), 'instances')
    def test_execute_query_current_month_by_service(self):
        """Test execute_query for current month on monthly breakdown by service."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[service]=*'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -1,
                'time_scope_units': 'month'
            },
            'group_by': {
                'service': ['*']
            }
        }
        query_params = FakeQueryParameters(params, tenant=self.tenant)
        handler = OCPAWSReportQueryHandler(query_params.mock_qp)
        query_output = handler.execute_query()
        data = query_output.get('data')
        self.assertIsNotNone(data)
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(
            total.get('cost', {}).get('value'), current_totals.get('cost'))

        cmonth_str = DateHelper().this_month_start.strftime('%Y-%m')
        for data_item in data:
            month_val = data_item.get('date')
            month_data = data_item.get('services')
            self.assertEqual(month_val, cmonth_str)
            self.assertIsInstance(month_data, list)
            for month_item in month_data:
                compute = month_item.get('service')
                self.assertEqual(compute, 'AmazonEC2')
                self.assertIsInstance(month_item.get('values'), list)
    def test_execute_query_current_month_monthly(self):
        """Test execute_query for current month on monthly breakdown."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=daily'
        params = {
            'filter': {
                'resolution': 'daily',
                'time_scope_value': -1,
                'time_scope_units': 'month'
            }
        }
        query_params = FakeQueryParameters(params, tenant=self.tenant)
        handler = OCPAWSReportQueryHandler(query_params.mock_qp)
        query_output = handler.execute_query()
        self.assertIsNotNone(query_output.get('data'))
        self.assertIsNotNone(query_output.get('total'))
        total = query_output.get('total')
        self.assertIsNotNone(total.get('cost'))

        aggregates = handler._mapper.report_type_map.get('aggregates')
        current_totals = self.get_totals_by_time_scope(aggregates,
                                                       self.this_month_filter)
        self.assertEqual(
            total.get('cost', {}).get('value'), current_totals.get('cost'))
 def test_execute_sum_query_instance_types(self):
     """Test that the sum query runs properly for instance-types."""
     # '?'
     query_params = FakeQueryParameters({},
                                        report_type='instance_type',
                                        tenant=self.tenant)
     handler = OCPAWSReportQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertIsNotNone(query_output.get('total'))
     total = query_output.get('total')
     self.assertIsNotNone(total.get('cost'))
     self.assertIsInstance(total.get('cost'), dict)
     self.assertEqual(total.get('cost').get('value'), 0)
     self.assertEqual(total.get('cost').get('units'), 'USD')
     self.assertIsNotNone(total.get('usage'))
     self.assertIsInstance(total.get('usage'), dict)
     self.assertEqual(total.get('usage').get('value'), 0)
     self.assertEqual(total.get('usage').get('units'), 'Hrs')
     self.assertIsNotNone(total.get('count'))
     self.assertIsInstance(total.get('count'), dict)
     self.assertEqual(total.get('count').get('value'), 0)
     self.assertEqual(total.get('count').get('units'), 'instances')
Exemplo n.º 18
0
    def test_query_table(self):
        """Test that the correct view is assigned by query table property."""
        url = "?"
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSCostSummary)

        url = "?group_by[account]=*"
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSCostSummaryByAccount)

        url = "?group_by[region]=*"
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSCostSummaryByRegion)

        url = "?group_by[service]=*"
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSCostSummaryByService)

        url = "?"
        query_params = self.mocked_query_params(url, OCPAWSInstanceTypeView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSComputeSummary)

        url = "?"
        query_params = self.mocked_query_params(url, OCPAWSStorageView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSStorageSummary)

        url = "?filter[service]=AmazonVPC,AmazonCloudFront,AmazonRoute53,AmazonAPIGateway"
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSNetworkSummary)

        url = (
            "?filter[service]=AmazonRDS,AmazonDynamoDB,AmazonElastiCache,AmazonNeptune,AmazonRedshift,AmazonDocumentDB"
        )
        query_params = self.mocked_query_params(url, OCPAWSCostView)
        handler = OCPAWSReportQueryHandler(query_params)
        self.assertEqual(handler.query_table, OCPAWSDatabaseSummary)