Fix AodhAlarmTest to use gnocchi

This changes the test to use AodhGnocchiAggregationByMetricsAlarm
and add measures using gnocchi api.

Change-Id: I0b2fe154b93d9882b703e95a0b09f139697ceb9a
Closes-Bug: #1727637
diff --git a/heat_tempest_plugin/common/test.py b/heat_tempest_plugin/common/test.py
index 64fd1a0..39a090c 100644
--- a/heat_tempest_plugin/common/test.py
+++ b/heat_tempest_plugin/common/test.py
@@ -109,7 +109,7 @@
         self.network_client = self.manager.network_client
         self.volume_client = self.manager.volume_client
         self.object_client = self.manager.object_client
-        self.metering_client = self.manager.metering_client
+        self.metric_client = self.manager.metric_client
 
         self.client = self.orchestration_client
 
diff --git a/heat_tempest_plugin/services/clients.py b/heat_tempest_plugin/services/clients.py
index ffd5b78..ca79374 100644
--- a/heat_tempest_plugin/services/clients.py
+++ b/heat_tempest_plugin/services/clients.py
@@ -12,8 +12,8 @@
 
 import os
 
-from ceilometerclient import client as ceilometer_client
 from cinderclient import client as cinder_client
+from gnocchiclient import client as gnocchi_client
 from heatclient import client as heat_client
 from keystoneauth1 import exceptions as kc_exceptions
 from keystoneauth1.identity.generic import password
@@ -64,7 +64,7 @@
     CINDERCLIENT_VERSION = '2'
     HEATCLIENT_VERSION = '1'
     NOVA_API_VERSION = '2.1'
-    CEILOMETER_VERSION = '2'
+    GNOCCHI_VERSION = '1'
 
     def __init__(self, conf, admin_credentials=False):
         self.conf = conf
@@ -82,7 +82,7 @@
         self.network_client = self._get_network_client()
         self.volume_client = self._get_volume_client()
         self.object_client = self._get_object_client()
-        self.metering_client = self._get_metering_client()
+        self.metric_client = self._get_metric_client()
 
     def _username(self):
         if self.admin_credentials:
@@ -182,18 +182,13 @@
         }
         return swift_client.Connection(**args)
 
-    def _get_metering_client(self):
-        try:
-            endpoint = self.identity_client.get_endpoint_url('metering',
-                                                             self.conf.region)
-        except kc_exceptions.EndpointNotFound:
-            return None
-        else:
-            args = {
-                'session': self.identity_client.session,
-                'region_name': self.conf.region,
-                'endpoint_type': 'publicURL',
-                'service_type': 'metering',
-            }
-            return ceilometer_client.Client(self.CEILOMETER_VERSION,
-                                            endpoint, **args)
+    def _get_metric_client(self):
+
+        adapter_options = {'interface': 'public',
+                           'region_name': self.conf.region}
+        args = {
+            'session': self.identity_client.session,
+            'adapter_options': adapter_options
+        }
+        return gnocchi_client.Client(version=self.GNOCCHI_VERSION,
+                                     **args)
diff --git a/heat_tempest_plugin/tests/scenario/templates/test_aodh_alarm.yaml b/heat_tempest_plugin/tests/scenario/templates/test_aodh_alarm.yaml
index d4c9745..0dc6a27 100644
--- a/heat_tempest_plugin/tests/scenario/templates/test_aodh_alarm.yaml
+++ b/heat_tempest_plugin/tests/scenario/templates/test_aodh_alarm.yaml
@@ -1,4 +1,7 @@
 heat_template_version: 2013-05-23
+parameters:
+  metric_id:
+    type: string
 resources:
   asg:
     type: OS::Heat::AutoScalingGroup
@@ -15,22 +18,20 @@
       cooldown: 0
       scaling_adjustment: 1
   alarm:
-    type: OS::Aodh::Alarm
+    type: OS::Aodh::GnocchiAggregationByMetricsAlarm
     properties:
-      description: Scale-up if the average CPU > 50% for 1 minute
-      meter_name: test_meter
-      statistic: count
+      metrics:
+        - {get_param: metric_id}
       comparison_operator: ge
-      threshold: 1
-      period: 60
       evaluation_periods: 1
+      granularity: 60
+      aggregation_method: mean
+      threshold: 10
       alarm_actions:
         - str_replace:
             template: trust+url
             params:
               url: {get_attr: [scaleup_policy, signal_url]}
-      matching_metadata:
-        metadata.metering.stack_id: {get_param: "OS::stack_id"}
 outputs:
   asg_size:
     value: {get_attr: [asg, current_size]}
diff --git a/heat_tempest_plugin/tests/scenario/test_aodh_alarm.py b/heat_tempest_plugin/tests/scenario/test_aodh_alarm.py
index ec436de..97e5c8d 100644
--- a/heat_tempest_plugin/tests/scenario/test_aodh_alarm.py
+++ b/heat_tempest_plugin/tests/scenario/test_aodh_alarm.py
@@ -10,6 +10,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import datetime
+from heat.common import timeutils
 from oslo_log import log as logging
 
 from heat_tempest_plugin.common import test
@@ -36,22 +38,27 @@
 
     def test_alarm(self):
         """Confirm we can create an alarm and trigger it."""
+        # create metric
+        metric = self.metric_client.metric.create({
+            'name': 'my_metric',
+            'archive_policy_name': 'high',
+        })
 
-        # 1. create the stack
-        stack_identifier = self.stack_create(template=self.template)
+        # create the stack
+        parameters = {'metric_id': metric['id']}
+        stack_identifier = self.stack_create(template=self.template,
+                                             parameters=parameters)
+        measures = [{'timestamp': timeutils.isotime(datetime.datetime.now()),
+                     'value': 100}, {'timestamp': timeutils.isotime(
+                         datetime.datetime.now() + datetime.timedelta(
+                             minutes=1)), 'value': 100}]
+        # send measures(should cause the alarm to fire)
+        self.metric_client.metric.add_measures(metric['id'], measures)
 
-        # 2. send ceilometer a metric (should cause the alarm to fire)
-        sample = {}
-        sample['counter_type'] = 'gauge'
-        sample['counter_name'] = 'test_meter'
-        sample['counter_volume'] = 1
-        sample['counter_unit'] = 'count'
-        sample['resource_metadata'] = {'metering.stack_id':
-                                       stack_identifier.split('/')[-1]}
-        sample['resource_id'] = 'shouldnt_matter'
-        self.metering_client.samples.create(**sample)
-
-        # 3. confirm we get a scaleup.
+        # confirm we get a scaleup.
         # Note: there is little point waiting more than 60s+time to scale up.
         self.assertTrue(test.call_until_true(
             120, 2, self.check_instance_count, stack_identifier, 2))
+
+        # cleanup metric
+        self.metric_client.metric.delete(metric['id'])
diff --git a/requirements.txt b/requirements.txt
index 83d86fd..098082e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,8 +6,8 @@
 oslo.config>=4.6.0 # Apache-2.0
 oslo.log>=3.30.0 # Apache-2.0
 paramiko>=2.0.0 # LGPLv2.1+
-python-ceilometerclient>=2.5.0 # Apache-2.0
 python-cinderclient>=3.2.0 # Apache-2.0
+gnocchiclient>=3.3.1 # Apache-2.0
 python-heatclient>=1.10.0 # Apache-2.0
 python-neutronclient>=6.3.0 # Apache-2.0
 python-novaclient>=9.1.0 # Apache-2.0