refactor the monitor
[doctor.git] / 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
13 from alarm import Alarm
14 import config
15 from image import Image
16 from instance import Instance
17 from inspector import get_inspector
18 import logger as doctor_log
19 from user import User
20 from network import Network
21 from monitor import get_monitor
22
23
24 LOG = doctor_log.Logger('doctor').getLogger()
25
26
27 class DoctorTest(object):
28
29     def __init__(self, conf):
30         self.conf = conf
31         self.image = Image(self.conf, LOG)
32         self.user = User(self.conf, LOG)
33         self.network = Network(self.conf, LOG)
34         self.instance = Instance(self.conf, LOG)
35         self.alarm = Alarm(self.conf, LOG)
36         self.inspector = get_inspector(self.conf, LOG)
37         self.monitor = get_monitor(self.conf,
38                                    self.inspector.get_inspector_url(),
39                                    LOG)
40
41     def setup(self):
42         # prepare the cloud env
43
44         # preparing VM image...
45         self.image.create()
46
47         # creating test user...
48         self.user.create()
49         self.user.update_quota()
50
51         # creating VM...
52         self.network.create()
53         self.instance.create()
54         self.instance.wait_for_vm_launch()
55
56         # creating alarm...
57         self.alarm.create()
58
59         # starting doctor sample components...
60         self.inspector.start()
61         self.monitor.start()
62
63     def run(self):
64         """run doctor test"""
65         try:
66             LOG.info('doctor test starting.......')
67
68             self.setup()
69
70             # injecting host failure...
71
72             # verify the test results
73
74         except Exception as e:
75             LOG.error('doctor test failed, Exception=%s' % e)
76             sys.exit(1)
77         finally:
78             self.cleanup()
79
80     def cleanup(self):
81         self.alarm.delete()
82         self.instance.delete()
83         self.network.delete()
84         self.image.delete()
85         self.user.delete()
86         self.inspector.stop()
87         self.monitor.stop()
88
89
90 def main():
91     """doctor main"""
92     doctor_root_dir = os.path.dirname(sys.path[0])
93     config_file_dir = '{0}/{1}'.format(doctor_root_dir, 'etc/')
94     config_files = [join(config_file_dir, f) for f in os.listdir(config_file_dir)
95                     if isfile(join(config_file_dir, f))]
96
97     conf = config.prepare_conf(args=sys.argv[1:],
98                                config_files=config_files)
99
100     doctor = DoctorTest(conf)
101     doctor.run()
102
103
104 if __name__ == '__main__':
105     sys.exit(main())