def test_filter_by_infrastructure_ocp(self): """Test that filter by infrastructure for ocp not on aws.""" data_generator = OCPReportDataGenerator(self.tenant, current_month_only=True) data_generator.add_data_to_tenant() query_params = { 'filter': { 'resolution': 'monthly', 'time_scope_value': -1, 'time_scope_units': 'month', 'infrastructures': ['AWS'] }, } query_string = '?filter[resolution]=monthly&' + \ 'filter[time_scope_value]=-1&' + \ 'filter[time_scope_units]=month&' + \ 'filter[infrastructures]=aws' handler = OCPReportQueryHandler(query_params, query_string, self.tenant, **{'report_type': 'cpu'}) query_data = handler.execute_query() for entry in query_data.get('data', []): for value in entry.get('values', []): self.assertEqual(value.get('usage').get('value'), 0) self.assertEqual(value.get('request').get('value'), 0) data_generator.remove_data_from_tenant()
def test_get_cluster_capacity_daily_resolution_group_by_clusters(self): """Test that cluster capacity returns daily capacity by cluster.""" query_params = { 'filter': { 'resolution': 'daily', 'time_scope_value': -1, 'time_scope_units': 'month' }, 'group_by': { 'cluster': ['*'] }, } query_string = '?filter[resolution]=daily&' + \ 'filter[time_scope_value]=-1&' + \ 'filter[time_scope_units]=month&' + \ 'group_by[cluster]=*' handler = OCPReportQueryHandler(query_params, query_string, self.tenant, **{'report_type': 'cpu'}) query_data = handler.execute_query() daily_capacity_by_cluster = defaultdict(dict) total_capacity = Decimal(0) query_filter = handler.query_filter query_group_by = ['usage_start', 'cluster_id'] annotations = {'capacity': Max('cluster_capacity_cpu_core_hours')} cap_key = list(annotations.keys())[0] q_table = handler._mapper.query_table query = q_table.objects.filter(query_filter) with tenant_context(self.tenant): cap_data = query.values(*query_group_by).annotate(**annotations) for entry in cap_data: date = handler.date_to_string(entry.get('usage_start')) cluster_id = entry.get('cluster_id', '') if cluster_id in daily_capacity_by_cluster[date]: daily_capacity_by_cluster[date][cluster_id] += entry.get( cap_key, 0) else: daily_capacity_by_cluster[date][cluster_id] = entry.get( cap_key, 0) total_capacity += entry.get(cap_key, 0) for entry in query_data.get('data', []): date = entry.get('date') for cluster in entry.get('clusters', []): cluster_name = cluster.get('cluster', '') capacity = cluster.get('values')[0].get('capacity', {}).get('value') self.assertEqual(capacity, daily_capacity_by_cluster[date][cluster_name]) self.assertEqual( query_data.get('total', {}).get('capacity', {}).get('value'), total_capacity)
def test_get_cluster_capacity_monthly_resolution_group_by_cluster(self): """Test that cluster capacity returns capacity by cluster.""" # Add data for a second cluster OCPReportDataGenerator(self.tenant).add_data_to_tenant() query_params = { 'filter': { 'resolution': 'monthly', 'time_scope_value': -1, 'time_scope_units': 'month' }, 'group_by': { 'cluster': ['*'] }, } query_string = '?filter[resolution]=monthly&' + \ 'filter[time_scope_value]=-1&' + \ 'filter[time_scope_units]=month&' + \ 'group_by[cluster]=*' handler = OCPReportQueryHandler(query_params, query_string, self.tenant, **{'report_type': 'cpu'}) query_data = handler.execute_query() capacity_by_cluster = defaultdict(Decimal) total_capacity = Decimal(0) query_filter = handler.query_filter query_group_by = ['usage_start', 'cluster_id'] annotations = {'capacity': Max('cluster_capacity_cpu_core_hours')} cap_key = list(annotations.keys())[0] q_table = handler._mapper.provider_map.get('tables').get('query') query = q_table.objects.filter(query_filter) with tenant_context(self.tenant): cap_data = query.values(*query_group_by).annotate(**annotations) for entry in cap_data: cluster_id = entry.get('cluster_id', '') capacity_by_cluster[cluster_id] += entry.get(cap_key, 0) total_capacity += entry.get(cap_key, 0) for entry in query_data.get('data', []): for cluster in entry.get('clusters', []): cluster_name = cluster.get('cluster', '') capacity = cluster.get('values')[0].get('capacity', {}).get('value') self.assertEqual(capacity, capacity_by_cluster[cluster_name]) self.assertEqual( query_data.get('total', {}).get('capacity', {}).get('value'), total_capacity)
def test_execute_sum_query_costs(self): """Test that the sum query runs properly for the costs endpoint.""" query_params = {} handler = OCPReportQueryHandler(query_params, '', self.tenant, **{'report_type': 'costs'}) aggregates = handler._mapper.report_type_map.get('aggregates') current_totals = self.get_totals_costs_by_time_scope( aggregates, self.ten_day_filter) 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('value'), current_totals.get('cost'))
def test_execute_sum_query_charge(self): """Test that the sum query runs properly for the charge endpoint.""" query_params = {} handler = OCPReportQueryHandler(query_params, '', self.tenant, **{'report_type': 'charge'}) aggregates = handler._mapper._report_type_map.get('aggregates') current_totals = self.get_totals_by_time_scope(aggregates) 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('charge').quantize(Decimal('0.001')), current_totals.get('charge').quantize(Decimal('0.001')))
def test_execute_sum_query(self): """Test that the sum query runs properly.""" handler = OCPReportQueryHandler({}, '', self.tenant, report_type='cpu') aggregates = handler._mapper.report_type_map.get('aggregates') current_totals = self.get_totals_by_time_scope(aggregates) 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('usage', {}).get('value'), current_totals.get('usage')) self.assertEqual(total.get('request', {}).get('value'), current_totals.get('request')) self.assertEqual(total.get('charge', {}).get('value'), current_totals.get('charge')) self.assertEqual(total.get('limit', {}).get('value'), current_totals.get('limit'))
def test_get_cluster_capacity_daily_resolution(self): """Test that total capacity is returned daily resolution.""" query_params = { 'filter': { 'resolution': 'daily', 'time_scope_value': -1, 'time_scope_units': 'month' } } handler = OCPReportQueryHandler( query_params, '', self.tenant, **{'report_type': 'cpu'} ) query_data = handler.execute_query() daily_capacity = defaultdict(Decimal) total_capacity = Decimal(0) query_filter = handler.query_filter query_group_by = ['usage_start'] annotations = {'capacity': Max('total_capacity_cpu_core_hours')} cap_key = list(annotations.keys())[0] q_table = handler._mapper.provider_map.get('tables').get('query') query = q_table.objects.filter(query_filter) with tenant_context(self.tenant): cap_data = query.values(*query_group_by).annotate(**annotations) for entry in cap_data: date = handler.date_to_string(entry.get('usage_start')) daily_capacity[date] += entry.get(cap_key, 0) # This is a hack because the total capacity in the test data # is artificial but the total should still be a sum of # cluster capacities annotations = {'capacity': Max('cluster_capacity_cpu_core_hours')} cap_data = query.values(*query_group_by).annotate(**annotations) for entry in cap_data: total_capacity += entry.get(cap_key, 0) self.assertEqual(query_data.get('total', {}).get('capacity', {}).get('value'), total_capacity) for entry in query_data.get('data', []): date = entry.get('date') values = entry.get('values') if values: capacity = values[0].get('capacity', {}).get('value') self.assertEqual(capacity, daily_capacity[date])