add create alarm 29/34829/14
authordongwenjuan <dong.wenjuan@zte.com.cn>
Tue, 16 May 2017 07:46:04 +0000 (15:46 +0800)
committerdongwenjuan <dong.wenjuan@zte.com.cn>
Fri, 7 Jul 2017 06:34:23 +0000 (14:34 +0800)
JIRA: DOCTOR-93

Change-Id: I316608c9627ff38e55154257b798244de3f221c5
Signed-off-by: dongwenjuan <dong.wenjuan@zte.com.cn>
test-requirements.txt
tests/alarm.py [new file with mode: 0644]
tests/config.py
tests/consumer/__init__.py [new file with mode: 0644]
tests/main.py
tests/os_clients.py

index 2928e0f..070caa4 100644 (file)
@@ -5,6 +5,7 @@ requests>=2.8.0
 oslo.config==3.22.0 # Apache-2.0
 python-openstackclient==2.3.0
 python-ceilometerclient==2.6.2
+aodhclient==0.7.0
 python-keystoneclient==3.5.0
 python-neutronclient==6.0.0
 python-novaclient==6.0.0
diff --git a/tests/alarm.py b/tests/alarm.py
new file mode 100644 (file)
index 0000000..0582085
--- /dev/null
@@ -0,0 +1,95 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corporation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+from oslo_config import cfg
+
+from identity_auth import get_identity_auth
+from identity_auth import get_session
+from os_clients import aodh_client
+from os_clients import nova_client
+
+OPTS = [
+    cfg.StrOpt('alarm_basename',
+               default='doctor_alarm',
+               help='the base name of alarm',
+               required=True),
+]
+
+
+class Alarm(object):
+
+    def __init__(self, conf, log):
+        self.conf = conf
+        self.log = log
+        self.auth = get_identity_auth(username=self.conf.doctor_user,
+                                      password=self.conf.doctor_passwd,
+                                      project=self.conf.doctor_project)
+        self.aodh = \
+            aodh_client(conf.aodh_version,
+                        get_session(auth=self.auth))
+        self.nova = \
+            nova_client(conf.nova_version,
+                        get_session(auth=self.auth))
+        self._init_alarm_name()
+
+    def _init_alarm_name(self):
+        self.alarm_names = []
+        for i in range(0, self.conf.instance_count):
+            alarm_name = '%s%d' % (self.conf.alarm_basename, i)
+            self.alarm_names.append(alarm_name)
+
+    def create(self):
+        self.log.info('alarm create start......')
+
+        alarms = {alarm['name']: alarm for alarm in self.aodh.alarm.list()}
+        servers = \
+            {getattr(server, 'name'): server
+             for server in self.nova.servers.list()}
+
+        for i in range(0, self.conf.instance_count):
+            alarm_name = self.alarm_names[i]
+            if alarm_name in alarms:
+                continue;
+            vm_name = '%s%d' % (self.conf.instance_basename, i)
+            vm_id = getattr(servers[vm_name], 'id')
+            alarm_request = dict(
+                name=alarm_name,
+                description=u'VM failure',
+                enabled=True,
+                alarm_actions=[u'http://%s:%d/failure'
+                               % (self.conf.consumer.ip,
+                                  self.conf.consumer.port)],
+                repeat_actions=False,
+                severity=u'moderate',
+                type=u'event',
+                event_rule=dict(
+                    event_type=u'compute.instance.update',
+                    query=[
+                        dict(field=u'traits.instance_id',
+                             type='',
+                             op=u'eq',
+                             value=vm_id),
+                        dict(field=u'traits.state',
+                             type='',
+                             op=u'eq',
+                             value=u'error')]))
+            self.aodh.alarm.create(alarm_request)
+
+        self.log.info('alarm create end......')
+
+    def delete(self):
+        self.log.info('alarm delete start.......')
+
+        alarms = {alarm['name']: alarm for alarm in self.aodh.alarm.list()}
+        for alarm_name in self.alarm_names:
+            if alarm_name in alarms:
+                self.aodh.alarm.delete(alarms[alarm_name]['alarm_id'])
+
+        del self.alarm_names[:]
+
+        self.log.info('alarm delete end.......')
index 99f335f..94b85f7 100644 (file)
@@ -10,6 +10,8 @@ import itertools
 \r
 from oslo_config import cfg\r
 \r
+import alarm\r
+import consumer\r
 import image\r
 import instance\r
 import network\r
@@ -19,12 +21,14 @@ import user
 \r
 def list_opts():\r
     return [\r
+        ('consumer', consumer.OPTS),\r
         ('DEFAULT', itertools.chain(\r
             os_clients.OPTS,\r
             image.OPTS,\r
             user.OPTS,\r
             network.OPTS,\r
-            instance.OPTS))\r
+            instance.OPTS,\r
+            alarm.OPTS))\r
     ]\r
 \r
 \r
diff --git a/tests/consumer/__init__.py b/tests/consumer/__init__.py
new file mode 100644 (file)
index 0000000..68cc5dc
--- /dev/null
@@ -0,0 +1,26 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corporation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+from oslo_config import cfg
+
+
+OPTS = [
+    cfg.StrOpt('type',
+               default='sample',
+               choices=['sample'],
+               help='the component of doctor consumer',
+               required=True),
+    cfg.StrOpt('ip',
+               default='127.0.0.1',
+               help='the ip of consumer',
+               required=True),
+    cfg.IntOpt('port',
+               default='12346',
+               help='the port of doctor consumer',
+               required=True),
+]
index 540a924..45683d1 100644 (file)
@@ -10,6 +10,7 @@ import os
 from os.path import isfile, join
 import sys
 
+from alarm import Alarm
 import config
 from image import Image
 from instance import Instance
@@ -28,6 +29,7 @@ class DoctorTest(object):
         self.user = User(self.conf, LOG)
         self.network = Network(self.conf, LOG)
         self.instance = Instance(self.conf, LOG)
+        self.alarm = Alarm(self.conf, LOG)
 
     def setup(self):
         # prepare the cloud env
@@ -44,6 +46,9 @@ class DoctorTest(object):
         self.instance.create()
         self.instance.wait_for_vm_launch()
 
+        # creating alarm...
+        self.alarm.create()
+
     def run(self):
         """run doctor test"""
         try:
@@ -62,6 +67,7 @@ class DoctorTest(object):
             self.cleanup()
 
     def cleanup(self):
+        self.alarm.delete()
         self.instance.delete()
         self.network.delete()
         self.image.delete()
index 5f6d452..2699930 100644 (file)
@@ -8,6 +8,7 @@
 ##############################################################################\r
 from oslo_config import cfg\r
 \r
+import aodhclient.client as aodhclient\r
 import glanceclient.client as glanceclient\r
 from keystoneclient.v2_0 import client as ks_client\r
 from neutronclient.v2_0 import client as neutronclient\r
@@ -17,6 +18,7 @@ import novaclient.client as novaclient
 OPTS = [\r
     cfg.StrOpt('glance_version', default='2', help='glance version'),\r
     cfg.StrOpt('nova_version', default='2.34', help='Nova version'),\r
+    cfg.StrOpt('aodh_version', default='2', help='aodh version'),\r
 ]\r
 \r
 \r
@@ -36,3 +38,7 @@ def nova_client(version, session):
 \r
 def neutron_client(session):\r
     return neutronclient.Client(session=session)\r
+\r
+\r
+def aodh_client(version, session):\r
+    return aodhclient.Client(version, session=session)\r