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