fix the path of log file
[doctor.git] / doctor_tests / instance.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 import time
11
12 from oslo_config import cfg
13
14 from doctor_tests.identity_auth import get_identity_auth
15 from doctor_tests.identity_auth import get_session
16 from doctor_tests.os_clients import neutron_client
17 from doctor_tests.os_clients import nova_client
18
19 OPTS = [
20     cfg.StrOpt('flavor',
21                default='m1.tiny',
22                help='the name of flavor',
23                required=True),
24     cfg.IntOpt('instance_count',
25                default=os.environ.get('VM_COUNT', 1),
26                help='the count of instance',
27                required=True),
28     cfg.StrOpt('instance_basename',
29                default='doctor_vm',
30                help='the base name of instance',
31                required=True),
32 ]
33
34
35 class Instance(object):
36
37     def __init__(self, conf, log):
38         self.conf = conf
39         self.log = log
40         self.auth = get_identity_auth(username=self.conf.doctor_user,
41                                       password=self.conf.doctor_passwd,
42                                       project=self.conf.doctor_project)
43         self.nova = \
44             nova_client(conf.nova_version,
45                         get_session(auth=self.auth))
46         self.neutron = neutron_client(get_session(auth=self.auth))
47         self.servers = {}
48         self.vm_names = []
49
50     def create(self):
51         self.log.info('instance create start......')
52
53         # get flavor, image and network for vm boot
54         flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
55         flavor = flavors.get(self.conf.flavor)
56         image = self.nova.glance.find_image(self.conf.image_name)
57         network = self.neutron.list_networks(name=self.conf.net_name)['networks'][0]
58         nics = {'net-id': network['id']}
59
60         self.servers = \
61             {getattr(server, 'name'): server
62              for server in self.nova.servers.list()}
63         for i in range(0, self.conf.instance_count):
64             vm_name = "%s%d"%(self.conf.instance_basename, i)
65             self.vm_names.append(vm_name)
66             if vm_name not in self.servers:
67                 server = self.nova.servers.create(vm_name, image,
68                                                   flavor, nics=[nics])
69                 self.servers[vm_name] = server
70                 time.sleep(0.1)
71
72         self.log.info('instance create end......')
73
74     def delete(self):
75         self.log.info('instance delete start.......')
76
77         for vm_name in self.vm_names:
78             if vm_name in self.servers:
79                 self.nova.servers.delete(self.servers[vm_name])
80                 time.sleep(0.1)
81
82         # check that all vms are deleted
83         while self.nova.servers.list():
84             time.sleep(0.1)
85         self.servers.clear()
86         del self.vm_names[:]
87
88         self.log.info('instance delete end.......')
89
90     def wait_for_vm_launch(self):
91         self.log.info('wait for vm launch start......')
92
93         wait_time = 60
94         count = 0
95         while count < wait_time:
96             active_count = 0
97             for vm_name in self.vm_names:
98                 server = self.nova.servers.get(self.servers[vm_name])
99                 server_status = getattr(server, 'status').lower()
100                 if 'active' == server_status:
101                     active_count += 1
102                 elif 'error' == server_status:
103                     raise Exception('vm launched with error state')
104                 else:
105                     time.sleep(2)
106                     count += 1
107                     continue
108             if active_count == self.conf.instance_count:
109                 self.log.info('wait for vm launch end......')
110                 return
111             count += 1
112             time.sleep(2)
113         raise Exception('time out for vm launch')
114