Remove id from monasca metric name list

Since returned metric names are distinct, we can remove the id from
each element. Also change the returned metric name into alphabetical
order.

Change-Id: Id981dafd00778a6d4a376b9ceab011231e94c0c6
This commit is contained in:
Kaiyan Sheng 2016-09-12 14:17:54 -06:00
parent fe59447db7
commit 9a592c6f4b
1 changed files with 108 additions and 40 deletions

View File

@ -1,4 +1,4 @@
# (C) Copyright 2015 Hewlett Packard Enterprise Development Company LP
# (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@ -14,13 +14,12 @@
import time
from oslo_utils import timeutils
from monasca_tempest_tests.tests.api import base
from monasca_tempest_tests.tests.api import constants
from monasca_tempest_tests.tests.api import helpers
from tempest.common.utils import data_utils
from tempest import test
from urllib import urlencode
class TestMetricsNames(base.BaseMonascaTest):
@ -28,27 +27,41 @@ class TestMetricsNames(base.BaseMonascaTest):
@classmethod
def resource_setup(cls):
super(TestMetricsNames, cls).resource_setup()
name = data_utils.rand_name()
name1 = data_utils.rand_name('name1')
name2 = data_utils.rand_name('name2')
name3 = data_utils.rand_name('name3')
key = data_utils.rand_name()
key1 = data_utils.rand_name()
value = data_utils.rand_name()
cls._param = key + ':' + value
metric = helpers.create_metric(name=name,
dimensions={key: value})
cls._test_metric = metric
cls.monasca_client.create_metrics(metric)
value1 = data_utils.rand_name()
start_time = str(timeutils.iso8601_from_timestamp(
metric['timestamp'] / 1000.0))
query_params = '?name=' + str(cls._test_metric['name']) +\
'&start_time=' + start_time
timestamp = int(round(time.time() * 1000))
time_iso = helpers.timestamp_to_iso(timestamp)
metric1 = helpers.create_metric(name=name1,
dimensions={key: value})
metric2 = helpers.create_metric(name=name2,
dimensions={key1: value1})
metric3 = helpers.create_metric(name=name3,
dimensions={key: value})
cls._test_metric_names = {name1, name2, name3}
cls._expected_names_list = list(cls._test_metric_names)
cls._expected_names_list.sort()
cls._test_metric_names_with_same_dim = [name1, name3]
cls._test_metrics = [metric1, metric2, metric3]
cls._dimensions_param = key + ':' + value
cls.monasca_client.create_metrics(cls._test_metrics)
query_param = '?start_time=' + time_iso
returned_name_set = set()
for i in xrange(constants.MAX_RETRIES):
resp, response_body = cls.monasca_client.list_metrics(
query_params)
resp, response_body = cls.monasca_client.list_metrics(query_param)
elements = response_body['elements']
for element in elements:
if str(element['name']) == cls._test_metric['name']:
return
returned_name_set.add(str(element['name']))
if cls._test_metric_names.issubset(returned_name_set):
return
time.sleep(constants.RETRY_WAIT_SECS)
assert False, 'Unable to initialize metrics'
@ -60,35 +73,90 @@ class TestMetricsNames(base.BaseMonascaTest):
@test.attr(type='gate')
def test_list_metrics_names(self):
resp, response_body = self.monasca_client.list_metrics_names()
self.assertEqual(200, resp.status)
self.assertTrue(set(['links', 'elements']) == set(response_body))
if self._is_name_in_list(response_body):
return
self.fail('Metric name not found')
metric_names = self._verify_response(resp, response_body)
self.assertEqual(metric_names, self._expected_names_list)
@test.attr(type='gate')
def test_list_metrics_names_with_dimensions(self):
query_params = '?dimensions=' + self._param
query_params = '?dimensions=' + self._dimensions_param
resp, response_body = self.monasca_client.list_metrics_names(
query_params)
self.assertEqual(200, resp.status)
self.assertTrue(set(['links', 'elements']) == set(response_body))
if self._is_name_in_list(response_body):
return
self.fail('Metric name not found')
metric_names = self._verify_response(resp, response_body)
self.assertEqual(metric_names,
self._test_metric_names_with_same_dim)
@test.attr(type='gate')
def test_list_metrics_names_with_limit_offset(self):
# Can not test list_metrics_names_with_limit_offset for now because
# list_metrics_names returns a list of metric names with no
# duplicates. But the limit and offset are using the original list
# with duplicates as reference.
self.skipException('list_metrics_names_with_limit_offset')
def _is_name_in_list(self, response_body):
resp, response_body = self.monasca_client.list_metrics_names()
self.assertEqual(200, resp.status)
elements = response_body['elements']
for element in elements:
self.assertTrue(set(['id', 'name']) == set(element))
if str(element['name']) == self._test_metric['name']:
return True
return False
num_names = len(elements)
for limit in xrange(1, num_names):
start_index = 0
params = [('limit', limit)]
offset = None
while True:
num_expected_elements = limit
if (num_expected_elements + start_index) > num_names:
num_expected_elements = num_names - start_index
these_params = list(params)
# If not the first call, use the offset returned by the last
# call
if offset:
these_params.extend([('offset', str(offset))])
query_params = '?' + urlencode(these_params)
resp, response_body = \
self.monasca_client.list_metrics_names(query_params)
new_elements = self._verify_response(resp, response_body)
self.assertEqual(num_expected_elements, len(new_elements))
expected_elements = elements[start_index:start_index+limit]
expected_names = \
[expected_elements[i]['name'] for i in xrange(
len(expected_elements))]
self.assertEqual(expected_names, new_elements)
start_index += num_expected_elements
if start_index >= num_names:
break
# Get the next set
offset = self._get_offset(response_body)
@test.attr(type='gate')
def test_list_metrics_names_with_offset_not_in_metrics_names_list(self):
offset1 = 'tempest-abc'
offset2 = 'tempest-name111'
offset3 = 'tempest-name4-random'
query_param1 = '?' + urlencode([('offset', offset1)])
query_param2 = '?' + urlencode([('offset', offset2)])
query_param3 = '?' + urlencode([('offset', offset3)])
resp, response_body = self.monasca_client.list_metrics_names(
query_param1)
metric_names = self._verify_response(resp, response_body)
self.assertEqual(metric_names, self._expected_names_list[:])
resp, response_body = self.monasca_client.list_metrics_names(
query_param2)
metric_names = self._verify_response(resp, response_body)
self.assertEqual(metric_names, self._expected_names_list[1:])
resp, response_body = self.monasca_client.list_metrics_names(
query_param3)
self.assertEqual(response_body['elements'], [])
def _verify_response(self, resp, response_body):
self.assertEqual(200, resp.status)
self.assertTrue(set(['links', 'elements']) == set(response_body))
response_names_length = len(response_body['elements'])
if response_names_length == 0:
self.fail("No metric names returned")
metric_names = [str(response_body['elements'][i]['name']) for i in
xrange(response_names_length)]
return metric_names