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