diff options
author | dongwenjuan <dong.wenjuan@zte.com.cn> | 2017-05-10 15:21:38 +0800 |
---|---|---|
committer | wenjuan dong <dong.wenjuan@zte.com.cn> | 2017-07-07 06:18:42 +0000 |
commit | 183dee7dc0d1c5086ad5d919b7eba955d1d011e9 (patch) | |
tree | 79a1031e47f68a55df6a4e6934f442f9a706b309 /tests | |
parent | 2e35513b691b0319b96182c5d9c39e4ab1a97552 (diff) |
Add create instances
JIRA: DOCTOR-93
Change-Id: I7645c1cc0c76ff86b9fbcc01c19038469fc9b9b4
Signed-off-by: dongwenjuan <dong.wenjuan@zte.com.cn>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/config.py | 6 | ||||
-rw-r--r-- | tests/identity_auth.py | 9 | ||||
-rw-r--r-- | tests/instance.py | 114 | ||||
-rw-r--r-- | tests/main.py | 12 | ||||
-rw-r--r-- | tests/network.py | 68 | ||||
-rw-r--r-- | tests/os_clients.py | 6 |
6 files changed, 209 insertions, 6 deletions
diff --git a/tests/config.py b/tests/config.py index 3cacd0a2..99f335f2 100644 --- a/tests/config.py +++ b/tests/config.py @@ -11,6 +11,8 @@ import itertools from oslo_config import cfg
import image
+import instance
+import network
import os_clients
import user
@@ -20,7 +22,9 @@ def list_opts(): ('DEFAULT', itertools.chain(
os_clients.OPTS,
image.OPTS,
- user.OPTS))
+ user.OPTS,
+ network.OPTS,
+ instance.OPTS))
]
diff --git a/tests/identity_auth.py b/tests/identity_auth.py index ffecc68a..c94893f4 100644 --- a/tests/identity_auth.py +++ b/tests/identity_auth.py @@ -13,13 +13,14 @@ from keystoneauth1 import loading from keystoneauth1 import session -def get_identity_auth(): +def get_identity_auth(username=None, password=None, project=None): auth_url = os.environ['OS_AUTH_URL'] - username = os.environ['OS_USERNAME'] - password = os.environ['OS_PASSWORD'] + username = username or os.environ['OS_USERNAME'] + password = password or os.environ['OS_PASSWORD'] user_domain_name = os.environ.get('OS_USER_DOMAIN_NAME') or 'default' user_domain_id = os.environ.get('OS_USER_DOMAIN_ID') or 'default' - project_name = os.environ.get('OS_PROJECT_NAME') or os.environ.get('OS_TENANT_NAME') + project_name = project or os.environ.get('OS_PROJECT_NAME') \ + or os.environ.get('OS_TENANT_NAME') project_domain_name = os.environ.get('OS_PROJECT_DOMAIN_NAME') or 'default' project_domain_id = os.environ.get('OS_PROJECT_DOMAIN_ID') or 'default' diff --git a/tests/instance.py b/tests/instance.py new file mode 100644 index 00000000..c6acbc3d --- /dev/null +++ b/tests/instance.py @@ -0,0 +1,114 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import os +import time + +from oslo_config import cfg + +from identity_auth import get_identity_auth +from identity_auth import get_session +from os_clients import neutron_client +from os_clients import nova_client + +OPTS = [ + cfg.StrOpt('flavor', + default='m1.tiny', + help='the name of flavor', + required=True), + cfg.IntOpt('instance_count', + default=os.environ.get('VM_COUNT', 1), + help='the count of instance', + required=True), + cfg.StrOpt('instance_basename', + default='doctor_vm', + help='the base name of instance', + required=True), +] + + +class Instance(object): + + def __init__(self, conf, log): + self.conf = conf + self.log = log + self.auth = get_identity_auth(username=self.conf.doctor_user, + password=self.conf.doctor_passwd, + project=self.conf.doctor_project) + self.nova = \ + nova_client(conf.nova_version, + get_session(auth=self.auth)) + self.neutron = neutron_client(get_session(auth=self.auth)) + self.servers = {} + self.vm_names = [] + + def create(self): + self.log.info('instance create start......') + + # get flavor, image and network for vm boot + flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()} + flavor = flavors.get(self.conf.flavor) + image = self.nova.glance.find_image(self.conf.image_name) + network = self.neutron.list_networks(name=self.conf.net_name)['networks'][0] + nics = {'net-id': network['id']} + + self.servers = \ + {getattr(server, 'name'): server + for server in self.nova.servers.list()} + for i in range(0, self.conf.instance_count): + vm_name = "%s%d"%(self.conf.instance_basename, i) + self.vm_names.append(vm_name) + if vm_name not in self.servers: + server = self.nova.servers.create(vm_name, image, + flavor, nics=[nics]) + self.servers[vm_name] = server + time.sleep(0.1) + + self.log.info('instance create end......') + + def delete(self): + self.log.info('instance delete start.......') + + for vm_name in self.vm_names: + if vm_name in self.servers: + self.nova.servers.delete(self.servers[vm_name]) + time.sleep(0.1) + + # check that all vms are deleted + while self.nova.servers.list(): + time.sleep(0.1) + self.servers.clear() + del self.vm_names[:] + + self.log.info('instance delete end.......') + + def wait_for_vm_launch(self): + self.log.info('wait for vm launch start......') + + wait_time = 60 + count = 0 + while count < wait_time: + active_count = 0 + for vm_name in self.vm_names: + server = self.nova.servers.get(self.servers[vm_name]) + server_status = getattr(server, 'status').lower() + if 'active' == server_status: + active_count += 1 + elif 'error' == server_status: + raise Exception('vm launched with error state') + else: + time.sleep(2) + count += 1 + continue + if active_count == self.conf.instance_count: + self.log.info('wait for vm launch end......') + return + count += 1 + time.sleep(2) + raise Exception('time out for vm launch') + diff --git a/tests/main.py b/tests/main.py index bb2c912d..540a9244 100644 --- a/tests/main.py +++ b/tests/main.py @@ -12,9 +12,10 @@ import sys import config from image import Image +from instance import Instance import logger as doctor_log from user import User - +from network import Network LOG = doctor_log.Logger('doctor').getLogger() @@ -25,6 +26,8 @@ class DoctorTest(object): self.conf = conf self.image = Image(self.conf, LOG) self.user = User(self.conf, LOG) + self.network = Network(self.conf, LOG) + self.instance = Instance(self.conf, LOG) def setup(self): # prepare the cloud env @@ -36,6 +39,11 @@ class DoctorTest(object): self.user.create() self.user.update_quota() + # creating VM... + self.network.create() + self.instance.create() + self.instance.wait_for_vm_launch() + def run(self): """run doctor test""" try: @@ -54,6 +62,8 @@ class DoctorTest(object): self.cleanup() def cleanup(self): + self.instance.delete() + self.network.delete() self.image.delete() self.user.delete() diff --git a/tests/network.py b/tests/network.py new file mode 100644 index 00000000..da7ad09d --- /dev/null +++ b/tests/network.py @@ -0,0 +1,68 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +from oslo_config import cfg + +from identity_auth import get_identity_auth +from identity_auth import get_session +from os_clients import neutron_client + + +OPTS = [ + cfg.StrOpt('net_name', + default='doctor_net', + help='the name of test net', + required=True), + cfg.StrOpt('net_cidr', + default='192.168.168.0/24', + help='the cidr of test subnet', + required=True), +] + + +class Network(object): + + def __init__(self, conf, log): + self.conf = conf + self.log = log + self.auth = get_identity_auth(username=self.conf.doctor_user, + password=self.conf.doctor_passwd, + project=self.conf.doctor_project) + self.neutron = neutron_client(get_session(auth=self.auth)) + self.net = None + self.subnet = None + + def create(self): + self.log.info('network create start.......') + net_name = self.conf.net_name + networks = self.neutron.list_networks(name=net_name)['networks'] + self.net = networks[0] if networks \ + else self.neutron.create_network( + {'network': {'name': net_name}})['network'] + self.log.info('network create end.......') + + self.log.info('subnet create start.......') + subnets = self.neutron.list_subnets(network_id=self.net['id'])['subnets'] + subnet_param = {'name': net_name, 'network_id': self.net['id'], + 'cidr': self.conf.net_cidr, 'ip_version': 4, + 'enable_dhcp': False} + self.subnet = subnets[0] if subnets \ + else self.neutron.create_subnet( + {'subnet': subnet_param})['subnet'] + self.log.info('subnet create end.......') + + def delete(self): + self.log.info('subnet delete start.......') + if self.subnet: + self.neutron.delete_subnet(self.subnet['id']) + self.log.info('subnet delete end.......') + + self.log.info('network delete start.......') + if self.net: + self.neutron.delete_network(self.net['id']) + self.log.info('network delete end.......') diff --git a/tests/os_clients.py b/tests/os_clients.py index c9eb0b1d..5f6d452e 100644 --- a/tests/os_clients.py +++ b/tests/os_clients.py @@ -10,8 +10,10 @@ from oslo_config import cfg import glanceclient.client as glanceclient
from keystoneclient.v2_0 import client as ks_client
+from neutronclient.v2_0 import client as neutronclient
import novaclient.client as novaclient
+
OPTS = [
cfg.StrOpt('glance_version', default='2', help='glance version'),
cfg.StrOpt('nova_version', default='2.34', help='Nova version'),
@@ -30,3 +32,7 @@ def keystone_client(session): def nova_client(version, session):
return novaclient.Client(version=version,
session=session)
+
+
+def neutron_client(session):
+ return neutronclient.Client(session=session)
|