bb0e943b4f287f1719f132d35f45746734fc8051
[doctor.git] / doctor_tests / scenario / maintenance.py
1 ##############################################################################
2 # Copyright (c) 2018 Nokia 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 from doctor_tests.common.utils import get_doctor_test_root_dir
10 from doctor_tests.identity_auth import get_identity_auth
11 from doctor_tests.identity_auth import get_session
12 from doctor_tests.os_clients import keystone_client
13 from doctor_tests.os_clients import neutron_client
14 from doctor_tests.os_clients import nova_client
15 from doctor_tests.stack import Stack
16
17
18 class Maintenance(object):
19
20     def __init__(self, conf, log):
21         self.conf = conf
22         self.log = log
23         self.keystone = keystone_client(
24             self.conf.keystone_version, get_session())
25         self.nova = nova_client(conf.nova_version, get_session())
26         auth = get_identity_auth(project=self.conf.doctor_project)
27         self.neutron = neutron_client(get_session(auth=auth))
28         self.stack = Stack(self.conf, self.log)
29
30     def get_external_network(self):
31         ext_net = None
32         networks = self.neutron.list_networks()['networks']
33         for network in networks:
34             if network['router:external']:
35                 ext_net = network['name']
36                 break
37         if ext_net is None:
38             raise Exception("externl network not defined")
39         return ext_net
40
41     def setup_maintenance(self, user):
42         # each hypervisor needs to have same amount of vcpus and they
43         # need to be free before test
44         hvisors = self.nova.hypervisors.list(detailed=True)
45         prev_vcpus = 0
46         prev_hostname = ""
47         self.log.info('checking hypervisors.......')
48         for hvisor in hvisors:
49             vcpus = hvisor.__getattr__("vcpus")
50             vcpus_used = hvisor.__getattr__("vcpus_used")
51             hostname = hvisor.__getattr__("hypervisor_hostname")
52             if vcpus < 2:
53                 raise Exception('not enough vcpus on %s' % hostname)
54             if vcpus_used > 0:
55                 raise Exception('%d vcpus used on %s'
56                                 % (vcpus_used, hostname))
57             if prev_vcpus != 0 and prev_vcpus != vcpus:
58                 raise Exception('%d vcpus on %s does not match to'
59                                 '%d on %s'
60                                 % (vcpus, hostname,
61                                    prev_vcpus, prev_hostname))
62             prev_vcpus = vcpus
63             prev_hostname = hostname
64
65         # maintenance flavor made so that 2 instances take whole node
66         flavor_vcpus = int(vcpus / 2)
67         compute_nodes = len(hvisors)
68         amount_actstdby_instances = 2
69         amount_noredundancy_instances = 2 * compute_nodes - 2
70         self.log.info('testing %d computes with %d vcpus each'
71                       % (compute_nodes, vcpus))
72         self.log.info('testing %d actstdby and %d noredundancy instances'
73                       % (amount_actstdby_instances,
74                          amount_noredundancy_instances))
75         max_instances = (amount_actstdby_instances +
76                          amount_noredundancy_instances)
77         max_cores = compute_nodes * vcpus
78
79         user.update_quota(max_instances, max_cores)
80
81         test_dir = get_doctor_test_root_dir()
82         template_file = '{0}/{1}'.format(test_dir, 'maintenance_hot_tpl.yaml')
83         files, template = self.stack.get_hot_tpl(template_file)
84
85         ext_net = self.get_external_network()
86
87         parameters = {'ext_net': ext_net,
88                       'flavor_vcpus': flavor_vcpus,
89                       'maint_image': self.conf.image_name,
90                       'nonha_intances': amount_noredundancy_instances,
91                       'ha_intances': amount_actstdby_instances}
92
93         self.log.info('creating maintenance stack.......')
94         self.log.info('parameters: %s' % parameters)
95
96         self.stack.create('doctor_test_maintenance',
97                           template,
98                           parameters=parameters,
99                           files=files)
100
101     def cleanup_maintenance(self):
102         self.log.info('stack delete start.......')
103         self.stack.delete()