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("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", "not-a-date") month_data = data_item.get("accounts", "not-a-string") self.assertEqual(month_val, cmonth_str) self.assertIsInstance(month_data, list) for month_item in month_data: self.assertIsInstance(month_item.get("services"), list)
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)
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)
def test_ocp_aws_date_order_by_cost_desc(self): """Test that order of every other date matches the order of the `order_by` date.""" yesterday = self.dh.yesterday.date() url = f"?order_by[cost]=desc&order_by[date]={yesterday}&group_by[service]=*" query_params = self.mocked_query_params(url, OCPAWSCostView) handler = OCPAWSReportQueryHandler(query_params) query_output = handler.execute_query() data = query_output.get("data") svc_annotations = handler.annotations.get("service") cost_annotation = handler.report_annotations.get("cost_total") with tenant_context(self.tenant): expected = list( OCPAWSCostSummaryByServiceP.objects.filter(usage_start=str(yesterday)) .annotate(service=svc_annotations) .values("service") .annotate(cost=cost_annotation) .order_by("-cost") ) tested = False ranking_map = {} count = 1 for service in expected: ranking_map[service.get("service")] = count count += 1 for element in data: previous = 0 for service in element.get("services"): service_name = service.get("service") if service_name in ranking_map.keys(): self.assertGreaterEqual(ranking_map[service_name], previous) previous = ranking_map[service_name] tested = True self.assertTrue(tested)
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))
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))
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.assertNotEqual(total.get("cost").get("total", {}).get("value"), 0) self.assertEqual(total.get("cost").get("total", {}).get("units"), "USD") self.assertIsNotNone(total.get("usage")) self.assertIsInstance(total.get("usage"), dict) self.assertNotEqual(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.assertNotEqual(total.get("count").get("value"), 0) self.assertEqual(total.get("count").get("units"), "instances")