예제 #1
0
    def test_set_tag_filters(self):
        """Test that tag filters are created properly."""
        filters = QueryFilterCollection()

        # '?'
        query_params = FakeQueryParameters({}, tenant=self.tenant)
        handler = OCPTagQueryHandler(query_params.mock_qp)
        tag_keys = handler.get_tag_keys(filters=False)

        filter_key = tag_keys[0]

        filter_value = 'filter'
        group_by_key = tag_keys[1]

        group_by_value = 'group_By'

        # '?filter[tag:some_key]=some_value&group_by[tag:some_key]=some_value'
        params = {
            'filter': {
                filter_key: [filter_value]
            },
            'group_by': {
                group_by_key: [group_by_value]
            }
        }
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tag_keys=tag_keys,
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        filters = handler._set_tag_filters(filters)

        expected = f"""<class 'api.query_filter.QueryFilterCollection'>: (AND: ('pod_labels__{filter_key}__icontains', '{filter_value}')), (AND: ('pod_labels__{group_by_key}__icontains', '{group_by_value}')), """  # noqa: E501

        self.assertEqual(repr(filters), expected)
예제 #2
0
    def test_get_tag_group_by_keys(self):
        """Test that group_by params with tag keys are returned."""
        # '?'
        query_params = FakeQueryParameters({}, tenant=self.tenant)
        handler = OCPTagQueryHandler(query_params.mock_qp)
        tag_keys = handler.get_tag_keys(filters=False)

        params = {'group_by': {tag_keys[0]: ['*']}}
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tag_keys=tag_keys,
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        results = handler.get_tag_group_by_keys()
        self.assertEqual(results, [tag_keys[0]])
예제 #3
0
    def test_strip_label_column_name(self):
        """Test that the tag column name is stripped from results."""
        # '?'
        query_params = FakeQueryParameters({},
                                           report_type='cpu',
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        tag_column = handler._mapper.provider_map.get('tag_column')
        data = [{
            f'{tag_column}__tag_key1': 'value'
        }, {
            f'{tag_column}__tag_key2': 'value'
        }]
        group_by = [
            'date', f'{tag_column}__tag_key1', f'{tag_column}__tag_key2'
        ]

        expected_data = [{'tag_key1': 'value'}, {'tag_key2': 'value'}]
        expected_group_by = ['date', 'tag_key1', 'tag_key2']

        result_data, result_group_by = handler.strip_label_column_name(
            data, group_by)

        self.assertEqual(result_data, expected_data)
        self.assertEqual(result_group_by, expected_group_by)
    def test_get_tag_keys_filter_false(self):
        """Test that all tag keys are returned with no filter."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-2&filter[resolution]=monthly'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -2,
                'time_scope_units': 'month'
            }
        }
        query_params = FakeQueryParameters(params, tenant=self.tenant)
        handler = AzureTagQueryHandler(query_params.mock_qp)

        tag_keys = set()
        with tenant_context(self.tenant):
            tags = AzureCostEntryLineItemDailySummary.objects\
                .annotate(tag_keys=JSONBObjectKeys('tags'))\
                .values('tags')\
                .distinct()\
                .all()

            for tag in tags:
                for key in tag.get('tags').keys():
                    tag_keys.add(key)

        result = handler.get_tag_keys(filters=False)
        self.assertEqual(sorted(result), sorted(list(tag_keys)))
예제 #5
0
    def test_ocp_cpu_query_group_by_cluster(self):
        """Test that group by cluster includes cluster and cluster_alias."""
        for _ in range(1, 5):
            OCPReportDataGenerator(self.tenant).add_data_to_tenant()

        # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[cluster]=*'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -1,
                'time_scope_units': 'month',
                'limit': 3
            },
            'group_by': {
                'cluster': ['*']
            }
        }
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)

        query_data = handler.execute_query()
        for data in query_data.get('data'):
            self.assertIn('clusters', data)
            for cluster_data in data.get('clusters'):
                self.assertIn('cluster', cluster_data)
                self.assertIn('values', cluster_data)
                for cluster_value in cluster_data.get('values'):
                    self.assertIn('cluster', cluster_value)
                    self.assertIn('cluster_alias', cluster_value)
                    self.assertIsNotNone('cluster', cluster_value)
                    self.assertIsNotNone('cluster_alias', cluster_value)
예제 #6
0
    def test_get_tag_type_filter_storage(self):
        """Test that all storage tags are returned with storage type filter."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-2&filter[resolution]=monthly&filter[type]=storage'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -2,
                'time_scope_units': 'month',
                'type': 'storage'
            }
        }
        query_params = FakeQueryParameters(params, tenant=self.tenant)
        handler = OCPTagQueryHandler(query_params.mock_qp)

        with tenant_context(self.tenant):
            storage_tag_keys = OCPUsageLineItemDailySummary.objects\
                .annotate(tag_keys=JSONBObjectKeys('volume_labels'))\
                .values('tag_keys')\
                .distinct()\
                .all()
            storage_tag_keys = [
                tag.get('tag_keys') for tag in storage_tag_keys
            ]
            tag_keys = storage_tag_keys

        result = handler.get_tag_keys(filters=False)
        self.assertEqual(sorted(result), sorted(tag_keys))
예제 #7
0
 def test_execute_query_no_query_parameters(self):
     """Test that the execute query runs properly with no query."""
     # '?'
     handler = OCPTagQueryHandler(
         FakeQueryParameters({}, tenant=self.tenant).mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'day')
     self.assertEqual(handler.time_scope_value, -10)
예제 #8
0
    def test_get_exclusions(self):
        """Test that exclusions are properly set."""
        # '?'
        query_params = FakeQueryParameters({}, tenant=self.tenant)
        handler = OCPTagQueryHandler(query_params.mock_qp)
        tag_keys = handler.get_tag_keys(filters=False)

        group_by_key = tag_keys[0]
        group_by_value = 'group_By'
        # '?group_by[tag:some_key]=some_value'
        params = {'group_by': {group_by_key: [group_by_value]}}
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tag_keys=tag_keys,
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        exclusions = handler._get_exclusions()
        expected = f"<Q: (AND: ('pod_labels__{group_by_key}__isnull', True))>"
        self.assertEqual(repr(exclusions), expected)
예제 #9
0
 def test_add_deltas_current_month(self, mock_current_deltas, mock_deltas):
     """Test that the current month method is called for deltas."""
     # '?'
     query_params = FakeQueryParameters({},
                                        report_type='cpu',
                                        tenant=self.tenant)
     handler = OCPReportQueryHandler(query_params.mock_qp)
     handler._delta = 'usage__request'
     handler.add_deltas([], [])
     mock_current_deltas.assert_called()
     mock_deltas.assert_not_called()
예제 #10
0
    def test_get_cluster_capacity_daily_resolution_group_by_clusters(self):
        """Test that cluster capacity returns daily capacity by cluster."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=daily&group_by[cluster]=*'
        params = {
            'filter': {
                'resolution': 'daily',
                'time_scope_value': -1,
                'time_scope_units': 'month'
            },
            'group_by': {
                'cluster': ['*']
            }
        }
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        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)
예제 #11
0
    def test_get_tag_group_by(self):
        """Test that tag based group bys work."""
        # '?'
        query_params = FakeQueryParameters({}, tenant=self.tenant)
        handler = OCPTagQueryHandler(query_params.mock_qp)
        tag_keys = handler.get_tag_keys(filters=False)

        group_by_key = tag_keys[0]
        group_by_value = 'group_by'
        # '?group_by[tag:some_key]=some_value'
        params = {'group_by': {group_by_key: [group_by_value]}}
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tag_keys=tag_keys,
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        group_by = handler._get_tag_group_by()
        group = group_by[0]
        expected = 'pod_labels__' + group_by_key
        self.assertEqual(len(group_by), 1)
        self.assertEqual(group[0], expected)
예제 #12
0
    def test_get_tag_filter_keys(self):
        """Test that filter params with tag keys are returned."""
        # '?'
        query_params = FakeQueryParameters({}, tenant=self.tenant)
        handler = OCPTagQueryHandler(query_params.mock_qp)
        tag_keys = handler.get_tag_keys(filters=False)

        # '?filter[time_scope_value]=-1&filter[resolution]=monthly&filter[time_scope_units]=month&filter[tag:some_tag]=*'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -1,
                'time_scope_units': 'month',
                tag_keys[0]: ['*']
            }
        }
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tag_keys=tag_keys,
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        results = handler.get_tag_filter_keys()
        self.assertEqual(results, [tag_keys[0]])
예제 #13
0
    def test_get_tag_order_by(self):
        """Verify that a propery order by is returned."""
        tag = 'pod_labels__key'
        expected_param = (tag.split('__')[1], )

        # '?'
        query_params = FakeQueryParameters({}, tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        result = handler.get_tag_order_by(tag)
        expression = result.expression

        self.assertIsInstance(result, OrderBy)
        self.assertEqual(expression.sql, 'pod_labels -> %s')
        self.assertEqual(expression.params, expected_param)
예제 #14
0
 def test_execute_sum_query_costs(self):
     """Test that the sum query runs properly for the costs endpoint."""
     # '?'
     query_params = FakeQueryParameters({}, tenant=self.tenant)
     handler = OCPReportQueryHandler(query_params.mock_qp)
     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_query_month_parameters(self):
     """Test that the execute query runs properly with single month query."""
     params = {
         'filter': {
             'resolution': 'monthly',
             'time_scope_value': -1,
             'time_scope_units': 'month'
         }
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = AzureTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'month')
     self.assertEqual(handler.time_scope_value, -1)
예제 #16
0
    def test_get_cluster_capacity_daily_resolution(self):
        """Test that total capacity is returned daily resolution."""
        # '?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,
                                           report_type='cpu',
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        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])
예제 #17
0
 def test_execute_query_30_day_parameters(self):
     """Test that the execute query runs properly with 30 day query."""
     # '?filter[time_scope_units]=day&filter[time_scope_value]=-30&filter[resolution]=daily'
     params = {
         'filter': {
             'resolution': 'daily',
             'time_scope_value': -30,
             'time_scope_units': 'day'
         }
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = OCPTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'day')
     self.assertEqual(handler.time_scope_value, -30)
 def test_execute_sum_query_storage(self):
     """Test that the sum query runs properly."""
     # '?'
     query_params = FakeQueryParameters({},
                                        report_type='storage',
                                        tenant=self.tenant)
     handler = OCPAWSReportQueryHandler(query_params.mock_qp)
     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'))
예제 #19
0
 def test_execute_query_two_month_parameters(self):
     """Test that the execute query runs properly with two month query."""
     # '?filter[time_scope_units]=month&filter[time_scope_value]=-2&filter[resolution]=monthly'
     params = {
         'filter': {
             'resolution': 'monthly',
             'time_scope_value': -2,
             'time_scope_units': 'month'
         }
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = OCPTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'month')
     self.assertEqual(handler.time_scope_value, -2)
예제 #20
0
 def test_10_day(self):
     """Test that the execute_query() succeeds with 10 day parameters."""
     # '?filter[time_scope_units]=day&filter[time_scope_value]=-10&filter[resolution]=daily'
     params = {
         'filter': {
             'resolution': 'daily',
             'time_scope_value': -10,
             'time_scope_units': 'day'
         }
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = OCPAWSTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'day')
     self.assertEqual(handler.time_scope_value, -10)
예제 #21
0
 def test_last_month(self):
     """Test that execute_query() succeeds with last-month parameters."""
     # '?filter[time_scope_units]=month&filter[time_scope_value]=-2&filter[resolution]=monthly'
     params = {
         'filter': {
             'resolution': 'monthly',
             'time_scope_value': -2,
             'time_scope_units': 'month'
         }
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = OCPAWSTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'month')
     self.assertEqual(handler.time_scope_value, -2)
 def test_execute_query_10_day_parameters_only_keys(self):
     """Test that the execute query runs properly with 10 day query."""
     # '?filter[time_scope_units]=day&filter[time_scope_value]=-10&filter[resolution]=daily&key_only=True'
     params = {
         'filter': {
             'resolution': 'daily',
             'time_scope_value': -10,
             'time_scope_units': 'day'
         },
         'key_only': True
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = AzureTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'day')
     self.assertEqual(handler.time_scope_value, -10)
예제 #23
0
 def test_specific_account(self):
     """Test that execute_query() succeeds with account parameter."""
     # '?filter[time_scope_units]=day&filter[time_scope_value]=-10&filter[resolution]=daily&filter[account]=some_account'
     params = {
         'filter': {
             'resolution': 'daily',
             'time_scope_value': -10,
             'time_scope_units': 'day',
             'account': str(self.fake.ean8())
         }
     }
     query_params = FakeQueryParameters(params, tenant=self.tenant)
     handler = OCPAWSTagQueryHandler(query_params.mock_qp)
     query_output = handler.execute_query()
     self.assertIsNotNone(query_output.get('data'))
     self.assertEqual(handler.time_scope_units, 'day')
     self.assertEqual(handler.time_scope_value, -10)
예제 #24
0
    def test_add_current_month_deltas_no_previous_data_wo_query_data(self):
        """Test that current month deltas are calculated with no previous month data."""
        OCPReportDataGenerator(self.tenant).remove_data_from_tenant()
        OCPReportDataGenerator(self.tenant,
                               current_month_only=True).add_data_to_tenant()

        # '?filter[time_scope_value]=-2&filter[resolution]=monthly&filter[time_scope_units]=month&filter[limit]=1&delta=usage__request'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -2,
                'time_scope_units': 'month',
                'limit': 1
            },
            'delta': 'usage__request'
        }
        query_params = FakeQueryParameters(params,
                                           report_type='cpu',
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)

        q_table = handler._mapper.provider_map.get('tables').get('query')
        with tenant_context(self.tenant):
            query = q_table.objects.filter(handler.query_filter)
            query_data = query.annotate(**handler.annotations)
            group_by_value = handler._get_group_by()
            query_group_by = ['date'] + group_by_value
            query_order_by = ('-date', )
            query_order_by += (handler.order, )

            annotations = annotations = handler.report_annotations
            query_data = query_data.values(*query_group_by).annotate(
                **annotations)

            aggregates = handler._mapper.report_type_map.get('aggregates')
            metric_sum = query.aggregate(**aggregates)
            query_sum = {
                key: metric_sum.get(key) if metric_sum.get(key) else Decimal(0)
                for key in aggregates
            }

            result = handler.add_current_month_deltas(query_data, query_sum)

            self.assertEqual(result, query_data)
            self.assertEqual(handler.query_delta['value'], Decimal(0))
            self.assertIsNone(handler.query_delta['percent'])
예제 #25
0
    def test_order_by_null_values(self):
        """Test that order_by returns properly sorted data with null data."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -1,
                'time_scope_units': 'month'
            }
        }
        query_params = FakeQueryParameters(params)
        handler = OCPReportQueryHandler(query_params.mock_qp)

        unordered_data = [{
            'node': None,
            'cluster': 'cluster-1'
        }, {
            'node': 'alpha',
            'cluster': 'cluster-2'
        }, {
            'node': 'bravo',
            'cluster': 'cluster-3'
        }, {
            'node': 'oscar',
            'cluster': 'cluster-4'
        }]

        order_fields = ['node']
        expected = [{
            'node': 'alpha',
            'cluster': 'cluster-2'
        }, {
            'node': 'bravo',
            'cluster': 'cluster-3'
        }, {
            'node': 'no-node',
            'cluster': 'cluster-1'
        }, {
            'node': 'oscar',
            'cluster': 'cluster-4'
        }]
        ordered_data = handler.order_by(unordered_data, order_fields)
        self.assertEqual(ordered_data, expected)
예제 #26
0
    def test_add_current_month_deltas(self):
        """Test that current month deltas are calculated."""
        # '?'
        query_params = FakeQueryParameters({},
                                           report_type='cpu',
                                           tenant=self.tenant)
        handler = OCPReportQueryHandler(query_params.mock_qp)
        handler._delta = 'usage__request'

        q_table = handler._mapper.provider_map.get('tables').get('query')
        with tenant_context(self.tenant):
            query = q_table.objects.filter(handler.query_filter)
            query_data = query.annotate(**handler.annotations)
            group_by_value = handler._get_group_by()
            query_group_by = ['date'] + group_by_value
            query_order_by = ('-date', )
            query_order_by += (handler.order, )

            annotations = handler.report_annotations
            query_data = query_data.values(*query_group_by).annotate(
                **annotations)

            aggregates = handler._mapper.report_type_map.get('aggregates')
            metric_sum = query.aggregate(**aggregates)
            query_sum = {key: metric_sum.get(key) for key in aggregates}

            result = handler.add_current_month_deltas(query_data, query_sum)

            delta_field_one, delta_field_two = handler._delta.split('__')
            field_one_total = Decimal(0)
            field_two_total = Decimal(0)
            for entry in result:
                field_one_total += entry.get(delta_field_one, 0)
                field_two_total += entry.get(delta_field_two, 0)
                delta_percent = entry.get('delta_percent')
                expected = (entry.get(delta_field_one, 0) / entry.get(delta_field_two, 0) * 100) \
                    if entry.get(delta_field_two) else 0
                self.assertEqual(delta_percent, expected)

            expected_total = field_one_total / field_two_total * 100 if field_two_total != 0 else 0

            self.assertEqual(handler.query_delta.get('percent'),
                             expected_total)
예제 #27
0
 def test_get_cluster_capacity_monthly_resolution(self):
     """Test that cluster capacity returns a full month's capacity."""
     # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly'
     params = {
         'filter': {
             'resolution': 'monthly',
             'time_scope_value': -1,
             'time_scope_units': 'month'
         }
     }
     query_params = FakeQueryParameters(params,
                                        report_type='cpu',
                                        tenant=self.tenant)
     handler = OCPReportQueryHandler(query_params.mock_qp)
     query_data = [{'row': 1}]
     query_data, total_capacity = handler.get_cluster_capacity(query_data)
     self.assertTrue('capacity' in total_capacity)
     self.assertTrue(isinstance(total_capacity['capacity'], Decimal))
     self.assertTrue('capacity' in query_data[0])
     self.assertEqual(query_data[0].get('capacity'),
                      total_capacity.get('capacity'))
    def test_query_by_partial_filtered_service(self):
        """Test execute_query monthly breakdown by filtered service."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[service]=eC2'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -1,
                'time_scope_units': 'month'
            },
            'group_by': {
                'service': ['eC2']
            }
        }
        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)
예제 #29
0
    def test_get_tag_keys(self):
        """Test that all OCP-on-AWS tag keys are returned."""
        # '?filter[time_scope_units]=month&filter[time_scope_value]=-2&filter[resolution]=monthly'
        params = {
            'filter': {
                'resolution': 'monthly',
                'time_scope_value': -2,
                'time_scope_units': 'month'
            }
        }
        query_params = FakeQueryParameters(params, tenant=self.tenant)
        handler = OCPAWSTagQueryHandler(query_params.mock_qp)

        with tenant_context(self.tenant):
            tag_keys = OCPAWSCostLineItemDailySummary.objects\
                .annotate(tag_keys=JSONBObjectKeys('tags'))\
                .values('tag_keys')\
                .distinct()\
                .all()
            tag_keys = [tag.get('tag_keys') for tag in tag_keys]

        result = handler.get_tag_keys()
        self.assertEqual(sorted(result), sorted(tag_keys))
 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')