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