def test_post_settings_ocp_tag_enabled(self): """Test setting OCP tags as enabled.""" url = ( "?filter[time_scope_units]=month&filter[time_scope_value]=-1" "&filter[resolution]=monthly&key_only=True&filter[enabled]=False") query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) query_output = handler.execute_query() tag = query_output.get("data")[0] body = { "api": { "settings": { "openshift": { "tag-management": { "enabled": [tag] } } } } } response = self.post_settings(body) self.assertEqual(response.status_code, status.HTTP_200_OK) response = self.get_settings() self.assertEqual(response.status_code, status.HTTP_200_OK) duallist = self.get_duallist_from_response(response) enabled = duallist.get("initialValue") self.assertIn(tag, enabled)
def test_execute_query_for_project(self): """Test that the execute query runs properly with project query.""" namespace = None with tenant_context(self.tenant): namespace_obj = OCPUsageLineItemDailySummary.objects\ .values('namespace')\ .first() namespace = namespace_obj.get('namespace') query_params = { 'filter': { 'resolution': 'daily', 'time_scope_value': -10, 'time_scope_units': 'day', 'project': namespace }, } query_string = '?filter[resolution]=daily&' + \ 'filter[time_scope_value]=-10&' + \ 'filter[time_scope_units]=day&' + \ 'filter[project]={}'.format(namespace) handler = OCPTagQueryHandler(query_params, query_string, self.tenant, **{}) 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)
def _obtain_tag_keys(self): """ Collect the available tag keys for the customer. Returns: (List) - List of available tag keys objects (List) - List of enabled tag keys strings """ url = ( "?filter[time_scope_units]=month&filter[time_scope_value]=-1" "&filter[resolution]=monthly&key_only=True&filter[enabled]=False") tag_request = self.factory.get(url) tag_request.user = self.request.user query_params = QueryParameters(tag_request, OCPTagView) handler = OCPTagQueryHandler(query_params) query_output = handler.execute_query() avail_data = query_output.get("data") all_tags_set = set(avail_data) enabled = [] with schema_context(self.schema): enabled_tags = OCPEnabledTagKeys.objects.all() enabled = [enabled_tag.key for enabled_tag in enabled_tags] all_tags_set.update(enabled) return all_tags_set, enabled
def test_execute_query_no_query_parameters(self): """Test that the execute query runs properly with no query.""" url = "?" query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) 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)
def test_execute_query_10_day_parameters_only_keys(self): """Test that the execute query runs properly with 10 day query.""" url = "?filter[time_scope_units]=day&filter[time_scope_value]=-10&filter[resolution]=daily&key_only=True" query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) 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)
def test_execute_query_month_parameters(self): """Test that the execute query runs properly with single month query.""" url = '?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly' query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) 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)
def test_execute_query_30_day_parameters(self): """Test that the execute query runs properly with 30 day query.""" url = '?filter[time_scope_units]=day&filter[time_scope_value]=-30&filter[resolution]=daily' query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) 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_query_two_month_parameters(self): """Test that the execute query runs properly with two month query.""" url = "?filter[time_scope_units]=month&filter[time_scope_value]=-2&filter[resolution]=monthly" query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) 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_no_query_parameters(self): """Test that the execute query runs properly with no query.""" query_params = {} handler = OCPTagQueryHandler(query_params, '', self.tenant, **{}) 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)
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)
def test_execute_query_for_project(self): """Test that the execute query runs properly with project query.""" namespace = None with tenant_context(self.tenant): namespace_obj = OCPUsageLineItemDailySummary.objects.values("namespace").first() namespace = namespace_obj.get("namespace") url = f"?filter[time_scope_units]=day&filter[time_scope_value]=-10&filter[resolution]=daily&filter[project]={namespace}" # noqa: E501 query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) 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)
def test_get_settings_ocp_tag_enabled(self): """Test that a GET settings call returns expected format.""" response = self.get_settings() self.assertEqual(response.status_code, status.HTTP_200_OK) duallist = self.get_duallist_from_response(response) all_keys = duallist.get("options") self.assertIsNotNone(all_keys) all_key_values = [key_obj.get("value") for key_obj in all_keys] url = ( "?filter[time_scope_units]=month&filter[time_scope_value]=-1" "&filter[resolution]=monthly&key_only=True&filter[enabled]=False") query_params = self.mocked_query_params(url, OCPTagView) handler = OCPTagQueryHandler(query_params) query_output = handler.execute_query() tag = query_output.get("data")[0] self.assertIn(tag, all_key_values)
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)
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_query_two_month_parameters(self): """Test that the execute query runs properly with two month query.""" query_params = { 'filter': { 'resolution': 'monthly', 'time_scope_value': -2, 'time_scope_units': 'month' }, } query_string = '?filter[resolution]=monthly&' + \ 'filter[time_scope_value]=-2&' + \ 'filter[time_scope_units]=month&' handler = OCPTagQueryHandler(query_params, query_string, self.tenant, **{}) 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_30_day_parameters(self): """Test that the execute query runs properly with 30 day query.""" query_params = { 'filter': { 'resolution': 'daily', 'time_scope_value': -30, 'time_scope_units': 'day' }, } query_string = '?filter[resolution]=daily&' + \ 'filter[time_scope_value]=-30&' + \ 'filter[time_scope_units]=day&' handler = OCPTagQueryHandler(query_params, query_string, self.tenant, **{}) 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_query_for_project(self): """Test that the execute query runs properly with project query.""" namespace = None with tenant_context(self.tenant): namespace_obj = OCPUsageLineItemDailySummary.objects\ .values('namespace')\ .first() namespace = namespace_obj.get('namespace') # '?filter[time_scope_units]=day&filter[time_scope_value]=-10&filter[resolution]=daily&filter[project]=some_project' params = { 'filter': { 'resolution': 'daily', 'time_scope_value': -10, 'time_scope_units': 'day', 'project': namespace } } 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, -10)