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