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