Add maintenance test code
[doctor.git] / doctor_tests / main.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import os
10 from os.path import isfile, join
11 import sys
12 import time
13 from traceback import format_exc
14
15 from doctor_tests import config
16 from doctor_tests.identity_auth import get_identity_auth
17 from doctor_tests.identity_auth import get_session
18 from doctor_tests.image import Image
19 from doctor_tests.installer import get_installer
20 import doctor_tests.logger as doctor_log
21 from doctor_tests.scenario.fault_management import FaultManagement
22 from doctor_tests.os_clients import nova_client
23 from doctor_tests.scenario.maintenance import Maintenance
24 from doctor_tests.user import User
25
26
27 Logger = doctor_log.Logger('doctor')
28 LOG = Logger.getLogger()
29 LogFile = Logger.getLogFilename()
30
31
32 class DoctorTest(object):
33
34     def __init__(self, conf):
35         self.conf = conf
36         self.image = Image(self.conf, LOG)
37         self.user = User(self.conf, LOG)
38         self.installer = get_installer(self.conf, LOG)
39         auth = get_identity_auth(project=self.conf.doctor_project)
40         self.nova = nova_client(self.conf.nova_version,
41                                 get_session(auth=auth))
42
43     def setup(self):
44         # prepare the cloud env
45         self.installer.setup()
46
47         # preparing VM image...
48         self.image.create()
49
50         # creating test user...
51         self.user.create()
52
53     def test_fault_management(self):
54         try:
55             LOG.info('doctor fault management test starting.......')
56
57             self.fault_management = \
58                 FaultManagement(self.conf, self.installer, self.user, LOG)
59
60             # prepare test env
61             self.fault_management.setup()
62
63             # wait for aodh alarms are updated in caches for event evaluator,
64             # sleep time should be larger than event_alarm_cache_ttl
65             # (default 60)
66             # (tojuvone) Fraser currently needs 120
67             time.sleep(120)
68
69             # injecting host failure...
70             # NOTE (umar) add INTERFACE_NAME logic to host injection
71             self.fault_management.start()
72             time.sleep(30)
73
74             # verify the test results
75             # NOTE (umar) copy remote monitor.log file when monitor=collectd
76             self.fault_management.check_host_status('down')
77             self.fault_management.check_notification_time()
78
79         except Exception as e:
80             LOG.error('doctor fault management test failed, '
81                       'Exception=%s' % e)
82             sys.exit(1)
83         finally:
84             self.fault_management.cleanup()
85
86     def _amount_compute_nodes(self):
87         services = self.nova.services.list(binary='nova-compute')
88         return len(services)
89
90     def test_maintenance(self):
91         cnodes = self._amount_compute_nodes()
92         if cnodes < 3:
93             # need 2 compute for redundancy and one spare to migrate
94             LOG.info('not enough compute nodes, skipping doctor '
95                      'maintenance test')
96             return
97         elif self.conf.installer.type != 'apex':
98             LOG.info('not supported installer, skipping doctor '
99                      'maintenance test')
100             return
101         try:
102             LOG.info('doctor maintenance test starting.......')
103             trasport_url = self.installer.get_transport_url()
104             maintenance = Maintenance(trasport_url, self.conf, LOG)
105             maintenance.setup_maintenance(self.user)
106
107             # wait for aodh alarms are updated in caches for event evaluator,
108             # sleep time should be larger than event_alarm_cache_ttl
109             # (default 60)
110             LOG.info('wait aodh for 120s.......')
111             time.sleep(120)
112
113             session_id = maintenance.start_maintenance()
114             maintenance.wait_maintenance_complete(session_id)
115
116             LOG.info('doctor maintenance complete.......')
117
118         except Exception as e:
119             LOG.error('doctor maintenance test failed, Exception=%s' % e)
120             LOG.error(format_exc())
121             sys.exit(1)
122         finally:
123             maintenance.cleanup_maintenance()
124
125     def run(self):
126         """run doctor tests"""
127         try:
128             LOG.info('doctor test starting.......')
129
130             # prepare common test env
131             self.setup()
132
133             if self.conf.test_case == 'all':
134                 self.test_fault_management()
135                 self.test_maintenance()
136             else:
137                 function = 'test_%s' % self.conf.test_case
138                 if hasattr(self, function):
139                     getattr(self, function)()
140                 else:
141                     raise Exception('Can not find function <%s> in'
142                                     'DoctorTest, see config manual'
143                                     % function)
144         except Exception as e:
145             LOG.error('doctor test failed, Exception=%s' % e)
146             sys.exit(1)
147         finally:
148             self.cleanup()
149
150     def cleanup(self):
151         self.installer.cleanup()
152         self.image.delete()
153         self.user.delete()
154
155
156 def main():
157     """doctor main"""
158     test_dir = os.path.split(os.path.realpath(__file__))[0]
159     doctor_root_dir = os.path.dirname(test_dir)
160
161     config_file_dir = '{0}/{1}'.format(doctor_root_dir, 'etc/')
162     config_files = [join(config_file_dir, f)
163                     for f in os.listdir(config_file_dir)
164                     if isfile(join(config_file_dir, f))]
165
166     conf = config.prepare_conf(args=sys.argv[1:],
167                                config_files=config_files)
168
169     doctor = DoctorTest(conf)
170     doctor.run()