1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
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 ##############################################################################
12 from oslo_config import cfg
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
22 help='the name of flavor',
24 cfg.IntOpt('instance_count',
25 default=os.environ.get('VM_COUNT', 1),
26 help='the count of instance',
28 cfg.StrOpt('instance_basename',
30 help='the base name of instance',
35 class Instance(object):
37 def __init__(self, conf, log):
40 self.auth = get_identity_auth(username=self.conf.doctor_user,
41 password=self.conf.doctor_passwd,
42 project=self.conf.doctor_project)
44 nova_client(conf.nova_version,
45 get_session(auth=self.auth))
46 self.neutron = neutron_client(get_session(auth=self.auth))
51 self.log.info('instance create start......')
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']}
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,
69 self.servers[vm_name] = server
72 self.log.info('instance create end......')
75 self.log.info('instance delete start.......')
77 for vm_name in self.vm_names:
78 if vm_name in self.servers:
79 self.nova.servers.delete(self.servers[vm_name])
82 # check that all vms are deleted
83 while self.nova.servers.list():
88 self.log.info('instance delete end.......')
90 def wait_for_vm_launch(self):
91 self.log.info('wait for vm launch start......')
95 while count < wait_time:
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:
102 elif 'error' == server_status:
103 raise Exception('vm launched with error state')
108 if active_count == self.conf.instance_count:
109 self.log.info('wait for vm launch end......')
113 raise Exception('time out for vm launch')