Add maintenance test code
[doctor.git] / doctor_tests / inspector / sample.py
index fadfd3d..a55a12b 100644 (file)
@@ -13,6 +13,7 @@ import json
 import time
 from threading import Thread
 import requests
+import yaml
 
 from doctor_tests.common import utils
 from doctor_tests.identity_auth import get_identity_auth
@@ -105,11 +106,47 @@ class SampleInspector(BaseInspector):
                 if self.conf.inspector.update_neutron_port_dp_status:
                     thr3.join()
 
+    def _alarm_data_decoder(self, data):
+        if "[" in data or "{" in data:
+            # string to list or dict removing unicode
+            data = yaml.load(data.replace("u'", "'"))
+        return data
+
+    def _alarm_traits_decoder(self, data):
+        return ({str(t[0]): self._alarm_data_decoder(str(t[2]))
+                for t in data['reason_data']['event']['traits']})
+
+    def maintenance(self, data):
+        try:
+            payload = self._alarm_traits_decoder(data)
+        except:
+            payload = ({t[0]: t[2] for t in
+                       data['reason_data']['event']['traits']})
+            self.log.error('cannot parse alarm data: %s' % payload)
+            raise Exception('sample inspector cannot parse alarm.'
+                            'Possibly trait data over 256 char')
+        self.log.info('sample inspector received data = %s' % payload)
+
+        state = payload['state']
+        host = payload['host']
+
+        if state == 'IN_MAINTENANCE':
+            self.log.info("sample inspector: disable %s automatic fault "
+                          "management" % host)
+        elif state == 'MAINTENANCE_COMPLETE':
+            self.log.info("sample inspector: enable %s automatic fault "
+                          "management" % host)
+        else:
+            raise("sample inspector couldn't handle state: %s" % state)
+
     @utils.run_async
     def _disable_compute_host(self, hostname):
         self.nova.services.force_down(hostname, 'nova-compute', True)
+
+        hostdown_time = time.time()
+        self.host_down_time = hostdown_time
         self.log.info('doctor mark host(%s) down at %s'
-                      % (hostname, time.time()))
+                      % (hostname, hostdown_time))
 
     @utils.run_async
     def _vms_reset_state(self, state, hostname):
@@ -117,8 +154,10 @@ class SampleInspector(BaseInspector):
         @utils.run_async
         def _vm_reset_state(nova, server, state):
             nova.servers.reset_state(server, state)
+            vmdown_time = time.time()
+            self.vm_down_time = vmdown_time
             self.log.info('doctor mark vm(%s) error at %s'
-                          % (server, time.time()))
+                          % (server, vmdown_time))
 
         thrs = []
         for nova, server in zip(self.novaclients, self.servers[hostname]):
@@ -168,7 +207,12 @@ class InspectorApp(Thread):
             self.inspector.handle_events(events)
             return "OK"
 
-        @app.route('/shutdown', methods=['POST'])
+        @app.route('/maintenance', methods=['POST'])
+        def maintenance():
+            self.inspector.maintenance(request.json)
+            return "OK"
+
+        @app.route('/events/shutdown', methods=['POST'])
         def shutdown():
             self.log.info('shutdown inspector app server at %s' % time.time())
             func = request.environ.get('werkzeug.server.shutdown')