enlarge the time of link down
[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
14 from doctor_tests import config
15 from doctor_tests.identity_auth import get_identity_auth
16 from doctor_tests.identity_auth import get_session
17 from doctor_tests.image import Image
18 from doctor_tests.installer import get_installer
19 import doctor_tests.logger as doctor_log
20 from doctor_tests.os_clients import nova_client
21 from doctor_tests.scenario.fault_management import FaultManagement
22 from doctor_tests.user import User
23
24
25 Logger = doctor_log.Logger('doctor')
26 LOG = Logger.getLogger()
27 LogFile = Logger.getLogFilename()
28
29
30 class DoctorTest(object):
31
32     def __init__(self, conf):
33         self.conf = conf
34         self.image = Image(self.conf, LOG)
35         self.user = User(self.conf, LOG)
36         self.installer = get_installer(self.conf, LOG)
37         auth = get_identity_auth(project=self.conf.doctor_project)
38         self.nova = nova_client(self.conf.nova_version,
39                                 get_session(auth=auth))
40
41     def setup(self):
42         # prepare the cloud env
43         self.installer.setup()
44
45         # preparing VM image...
46         self.image.create()
47
48         # creating test user...
49         self.user.create()
50
51     def test_fault_management(self):
52         try:
53             LOG.info('doctor fault management test starting.......')
54
55             self.fault_management = \
56                 FaultManagement(self.conf, self.installer, self.user, LOG)
57
58             # prepare test env
59             self.fault_management.setup()
60
61             # wait for aodh alarms are updated in caches for event evaluator,
62             # sleep time should be larger than event_alarm_cache_ttl
63             # (default 60)
64             # (tojuvone) Fraser currently needs 120
65             time.sleep(120)
66
67             # injecting host failure...
68             # NOTE (umar) add INTERFACE_NAME logic to host injection
69             self.fault_management.start()
70             time.sleep(30)
71
72             # verify the test results
73             # NOTE (umar) copy remote monitor.log file when monitor=collectd
74             self.fault_management.check_host_status('down')
75             self.fault_management.check_notification_time()
76
77         except Exception as e:
78             LOG.error('doctor fault management test failed, '
79                       'Exception=%s' % e)
80             sys.exit(1)
81         finally:
82             self.fault_management.cleanup()
83
84     def _amount_compute_nodes(self):
85         services = self.nova.services.list(binary='nova-compute')
86         return len(services)
87
88     def test_maintenance(self):
89         cnodes = self._amount_compute_nodes()
90         if cnodes < 3:
91             # need 2 compute for redundancy and one spare to migrate
92             LOG.info('not enough compute nodes, skipping doctor '
93                      'maintenance test')
94             return
95         try:
96             LOG.info('doctor maintenance test starting.......')
97             # TODO (tojuvone) test setup and actual test
98         except Exception as e:
99             LOG.error('doctor maintenance test failed, Exception=%s' % e)
100             sys.exit(1)
101         # TODO (tojuvone) finally: test case specific cleanup
102
103     def run(self):
104         """run doctor tests"""
105         try:
106             LOG.info('doctor test starting.......')
107             # prepare common test env
108             self.setup()
109             if self.conf.test_case == 'all':
110                 self.test_fault_management()
111                 self.test_maintenance()
112             else:
113                 function = 'test_%s' % self.conf.test_case
114                 if hasattr(self, function):
115                     getattr(self, function)()
116                 else:
117                     raise Exception('Can not find function <%s> in'
118                                     'DoctorTest, see config manual'
119                                     % function)
120         except Exception as e:
121             LOG.error('doctor test failed, Exception=%s' % e)
122             sys.exit(1)
123         finally:
124             self.cleanup()
125
126     def cleanup(self):
127         self.installer.cleanup()
128         self.image.delete()
129         self.user.delete()
130
131
132 def main():
133     """doctor main"""
134     test_dir = os.path.split(os.path.realpath(__file__))[0]
135     doctor_root_dir = os.path.dirname(test_dir)
136
137     config_file_dir = '{0}/{1}'.format(doctor_root_dir, 'etc/')
138     config_files = [join(config_file_dir, f)
139                     for f in os.listdir(config_file_dir)
140                     if isfile(join(config_file_dir, f))]
141
142     conf = config.prepare_conf(args=sys.argv[1:],
143                                config_files=config_files)
144
145     doctor = DoctorTest(conf)
146     doctor.run()