Merge "Add CRUD tests for telemetry alarming"
diff --git a/tempest/api/telemetry/base.py b/tempest/api/telemetry/base.py
index 1f661a6..c4614c6 100644
--- a/tempest/api/telemetry/base.py
+++ b/tempest/api/telemetry/base.py
@@ -10,7 +10,9 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.common.utils import data_utils
 from tempest import config
+from tempest import exceptions
 import tempest.test
 
 CONF = config.CONF
@@ -22,6 +24,28 @@
 
     @classmethod
     def setUpClass(cls):
-        super(BaseTelemetryTest, cls).setUpClass()
         if not CONF.service_available.ceilometer:
             raise cls.skipException("Ceilometer support is required")
+        super(BaseTelemetryTest, cls).setUpClass()
+        os = cls.get_client_manager()
+        cls.telemetry_client = os.telemetry_client
+        cls.alarm_ids = []
+
+    @classmethod
+    def create_alarm(cls, **kwargs):
+        resp, body = cls.telemetry_client.create_alarm(
+            name=data_utils.rand_name('telemetry_alarm'),
+            type='threshold', **kwargs)
+        if resp['status'] == '201':
+            cls.alarm_ids.append(body['alarm_id'])
+        return resp, body
+
+    @classmethod
+    def tearDownClass(cls):
+        for alarm_id in cls.alarm_ids:
+            try:
+                cls.telemetry_client.delete_alarm(alarm_id)
+            except exceptions.NotFound:
+                pass
+        cls.clear_isolated_creds()
+        super(BaseTelemetryTest, cls).tearDownClass()
diff --git a/tempest/api/telemetry/test_telemetry_alarming_api.py b/tempest/api/telemetry/test_telemetry_alarming_api.py
new file mode 100644
index 0000000..907d3d0
--- /dev/null
+++ b/tempest/api/telemetry/test_telemetry_alarming_api.py
@@ -0,0 +1,43 @@
+#    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
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from tempest.api.telemetry import base
+from tempest import exceptions
+from tempest.test import attr
+
+
+class TelemetryAlarmingAPITestJSON(base.BaseTelemetryTest):
+    _interface = 'json'
+
+    @attr(type="gate")
+    def test_alarm_list(self):
+        resp, _ = self.telemetry_client.list_alarms()
+        self.assertEqual(int(resp['status']), 200)
+
+    @attr(type="gate")
+    def test_create_alarm(self):
+        rules = {'meter_name': 'cpu_util',
+                 'comparison_operator': 'gt',
+                 'threshold': 80.0,
+                 'period': 70}
+        resp, body = self.create_alarm(threshold_rule=rules)
+        self.alarm_id = body['alarm_id']
+        self.assertEqual(int(resp['status']), 201)
+        self.assertDictContainsSubset(rules, body['threshold_rule'])
+        resp, body = self.telemetry_client.get_alarm(self.alarm_id)
+        self.assertEqual(int(resp['status']), 200)
+        self.assertDictContainsSubset(rules, body['threshold_rule'])
+        resp, _ = self.telemetry_client.delete_alarm(self.alarm_id)
+        self.assertEqual(int(resp['status']), 204)
+        self.assertRaises(exceptions.NotFound,
+                          self.telemetry_client.get_alarm,
+                          self.alarm_id)
diff --git a/tempest/services/telemetry/json/telemetry_client.py b/tempest/services/telemetry/json/telemetry_client.py
index 747d7c1..e666475 100644
--- a/tempest/services/telemetry/json/telemetry_client.py
+++ b/tempest/services/telemetry/json/telemetry_client.py
@@ -29,10 +29,6 @@
     def serialize(self, body):
         return json.dumps(body)
 
-    def create_alarm(self, **kwargs):
-        uri = "%s/alarms" % self.uri_prefix
-        return self.post(uri, kwargs)
-
     def add_sample(self, sample_list, meter_name, meter_unit, volume,
                    sample_type, resource_id, **kwargs):
         sample = {"counter_name": meter_name, "counter_unit": meter_unit,
diff --git a/tempest/services/telemetry/telemetry_client_base.py b/tempest/services/telemetry/telemetry_client_base.py
index 200c94a..a35a1ab 100644
--- a/tempest/services/telemetry/telemetry_client_base.py
+++ b/tempest/services/telemetry/telemetry_client_base.py
@@ -77,7 +77,7 @@
         return self.rest_client.put(uri, body, self.headers)
 
     def get(self, uri):
-        resp, body = self.rest_client.get(uri)
+        resp, body = self.rest_client.get(uri, self.headers)
         body = self.deserialize(body)
         return resp, body
 
@@ -124,9 +124,13 @@
         return self.get(uri)
 
     def get_alarm(self, alarm_id):
-        uri = '%s/meter/%s' % (self.uri_prefix, alarm_id)
+        uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id)
         return self.get(uri)
 
     def delete_alarm(self, alarm_id):
         uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
         return self.delete(uri)
+
+    def create_alarm(self, **kwargs):
+        uri = "%s/alarms" % self.uri_prefix
+        return self.post(uri, kwargs)