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