diff options
-rw-r--r-- | func/driver.py | 38 | ||||
-rw-r--r-- | func/fetchimg.py | 58 | ||||
-rw-r--r-- | func/spawn_vm.py | 561 | ||||
-rw-r--r-- | test-requirements.txt | 1 | ||||
-rw-r--r-- | tests/driver_test.py | 65 | ||||
-rw-r--r-- | tests/fetchimg_test.py | 22 | ||||
-rw-r--r-- | tests/spawn_vm_test.py | 71 |
7 files changed, 484 insertions, 332 deletions
diff --git a/func/driver.py b/func/driver.py index 48c09c5d..33dbe320 100644 --- a/func/driver.py +++ b/func/driver.py @@ -6,13 +6,11 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## - - - import os import json from collections import defaultdict + class Driver: def __init__(self): @@ -21,8 +19,8 @@ class Driver: print os.environ['PWD'] self.dic_json = defaultdict() - def drive_bench(self, benchmark, roles, benchmark_fname, benchmark_detail = None, pip_dict = None, proxy_info = None): - roles= sorted(roles) + def drive_bench(self, benchmark, roles, benchmark_fname, benchmark_detail=None, pip_dict=None, proxy_info=None): + roles = sorted(roles) pip_dict = sorted(pip_dict) result_dir = 'results' benchmark_name = benchmark + '.yaml' @@ -33,30 +31,30 @@ class Driver: self.dic_json['workingdir'] = str(os.environ['PWD']) self.dic_json['fname'] = str(benchmark_fname) self.dic_json['username'] = str('root') - + for key in proxy_info.keys(): self.dic_json[key] = proxy_info[key] - + if os.environ['INSTALLER_TYPE'] == str('joid'): - self.dic_json['username']=str('ubuntu') + self.dic_json['username'] = str('ubuntu') if os.environ['INSTALLER_TYPE'] == str('apex'): - self.dic_json['username']=str('heat-admin') - for k,v in benchmark_detail: - self.dic_json[k]=v + self.dic_json['username'] = str('heat-admin') + for k, v in benchmark_detail: + self.dic_json[k] = v for k, v in roles: - self.dic_json['role']=k - index=1 - if benchmark_detail != None: + self.dic_json['role'] = k + index = 1 + if benchmark_detail is not None: for values in v: - if k == '1-server': + if k == '1-server': print values, 'saving IP' - self.dic_json['ip'+str(index)]= str(values) + self.dic_json['ip' + str(index)] = str(values) if pip_dict[0][1][0]: - self.dic_json['privateip'+str(index)] = pip_dict[0][1] + self.dic_json['privateip' + str(index)] = pip_dict[0][1] if not pip_dict[0][1][0]: - self.dic_json['privateip'+str(index)] = 'NONE' - index= index+1 + self.dic_json['privateip' + str(index)] = 'NONE' + index = index + 1 dic_json = json.dumps(dict(self.dic_json.items())) print dic_json run_play = 'ansible-playbook ./benchmarks/playbooks/{0} --private-key=./data/QtipKey -i ./data/hosts --extra-vars \'{1}\''.format(benchmark_name, dic_json) - status = os.system(run_play) + os.system(run_play) diff --git a/func/fetchimg.py b/func/fetchimg.py index 1c621a3b..6cae426e 100644 --- a/func/fetchimg.py +++ b/func/fetchimg.py @@ -1,30 +1,28 @@ -############################################################################## -# Copyright (c) 2015 Dell Inc 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 - - -class FetchImg: - - def __init__(self): - print 'Fetching Image!' - print 'Fetching QTIP_VM Image' - - def download(self): - time.sleep(2) - os.system( - 'mkdir -p Temp_Img && wget http://artifacts.opnfv.org/qtip/QTIP_CentOS.qcow2 -P Temp_Img') - - filepath = './Temp_Img/QTIP_CentOS.qcow2' - while not os.path.isfile(filepath): - time.sleep(10) - print 'Download Completed!' +##############################################################################
+# Copyright (c) 2015 Dell Inc 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
+
+
+class FetchImg:
+
+ def __init__(self):
+ print 'Fetching Image!'
+ print 'Fetching QTIP_VM Image'
+
+ @staticmethod
+ def download():
+ time.sleep(2)
+ os.system(
+ 'mkdir -p Temp_Img && wget http://artifacts.opnfv.org/qtip/QTIP_CentOS.qcow2 -P Temp_Img')
+
+ filepath = './Temp_Img/QTIP_CentOS.qcow2'
+ while not os.path.isfile(filepath):
+ time.sleep(10)
+ print 'Download Completed!'
diff --git a/func/spawn_vm.py b/func/spawn_vm.py index 94aa424a..ec63230c 100644 --- a/func/spawn_vm.py +++ b/func/spawn_vm.py @@ -1,282 +1,279 @@ -############################################################################## -# Copyright (c) 2015 Dell Inc 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 sys -from collections import defaultdict -from func.env_setup import Env_setup -from func.fetchimg import FetchImg -import yaml -import heatclient.client -import keystoneclient -import glanceclient -from novaclient import client -import time -import json -from func.create_zones import create_zones - -class SpawnVM(Env_setup): - vm_role_ip_dict = defaultdict(list) - installer = '' - - def __init__(self, vm_info): - print 'SpawnVM Class initiated' - vm_role_ip_dict = vm_info.copy() - print 'Generating Heat Template\n' - self._keystone_client = None - self._heat_client = None - self._glance_client = None - self._nova_client = None - nova =self. _get_nova_client() - azoneobj = create_zones() - azoneobj.create_agg(vm_info['availability_zone']) - installer= self.get_installer_type() - self.Heat_template1 = self.HeatTemplate_vm(vm_info,installer) - self.create_stack(vm_role_ip_dict, self.Heat_template1) - - def get_installer_type(self): - print 'Getting Installer Name' - return os.environ['INSTALLER_TYPE'] - - def get_public_network(self,installer_detected): - - ''' - TODO: GET THE NAMES OF THE PUBLIC NETWORKS for OTHER PROJECTS - ''' - print 'Getting Public Network' - if installer_detected.lower() == 'fuel': - return 'admin_floating_net' - if installer_detected.lower() == 'apex': - return 'external' - if installer_detected.lower() == 'compass': - return 'ext-net' - if installer_detected.lower() == 'joid': - return 'ext-net' - - def HeatTemplate_vm(self, vm_params, installer): - try: - Heat_Dic='' - with open('./heat/SampleHeat.yaml', 'r+') as H_temp: - Heat_Dic = yaml.load(H_temp) - except yaml.YAMLError as exc: - if hasattr(exc, 'problem_mark'): - mark = exc.problem_mark - print 'Error in qtip/heat/SampleHeat.yaml at: (%s,%s)' % (mark.line + 1, mark.column + 1) - print 'EXITING PROGRAM. Correct File and restart' - sys.exit(0) - fopen = open('./data/QtipKey.pub', 'r') - fopenstr = fopen.read() - fopenstr = fopenstr.rstrip() - scriptcmd = '#!/bin/bash \n echo {0} >> foo.txt \n echo {1} >> /root/.ssh/authorized_keys'.format( - fopenstr, fopenstr) - - netName = self.get_public_network(installer) - print netName - Heat_Dic['heat_template_version'] = '2014-10-16' - Heat_Dic['resources']['KeyPairSavePrivate'] = { - 'type': 'OS::Nova::KeyPair', - 'properties': { - 'save_private_key': 'true', - 'name': 'my_key' - } - } - Heat_Dic['parameters']['public_network'] = { - 'type': 'string', - #'default': vm_params['public_network'][0] - 'default': netName - } - for x in range(1, len(vm_params['availability_zone']) + 1): - avail_zone = vm_params['availability_zone'][x - 1] - img = vm_params['OS_image'][x - 1] - flavor = vm_params['flavor'][x - 1] - - Heat_Dic['parameters']['availability_zone_' +str(x)] = { - 'description': 'Availability Zone of the instance', - 'default': avail_zone, - 'type': 'string' - - } - - Heat_Dic['resources']['public_port_' +str(x)] = { - 'type': 'OS::Neutron::Port', - 'properties': { - 'network': {'get_resource': 'private_network'}, - 'security_groups': [{ 'get_resource': 'demo1_security_Group'}], - 'fixed_ips': [ - {'subnet_id': {'get_resource': 'private_subnet'}}]}} - - Heat_Dic['resources']['floating_ip_' + str(x)] = { - 'type': 'OS::Neutron::FloatingIP', - 'properties': { - 'floating_network': {'get_param': 'public_network'}}} - - Heat_Dic['resources']['floating_ip_assoc_' + str(x)] = { - 'type': 'OS::Neutron::FloatingIPAssociation', - 'properties': { - 'floatingip_id': {'get_resource': 'floating_ip_' + str(x)}, - 'port_id': {'get_resource': 'public_port_' + str(x)}}} - - Heat_Dic['resources']['my_instance_' + str(x)] = { - 'type': 'OS::Nova::Server', - 'properties': { - 'image': img, - 'networks':[ - {'port': {'get_resource': 'public_port_' + str(x)}}], - 'flavor': flavor, - 'availability_zone': avail_zone, - 'name': 'instance' + str(x), - 'key_name': {'get_resource': 'KeyPairSavePrivate'}, - 'user_data_format': 'RAW', - 'user_data': scriptcmd}} - - Heat_Dic['resources']['demo1_security_Group'] = { - 'type': 'OS::Neutron::SecurityGroup', - 'properties': { - 'name': 'demo1_security_Group', - 'rules': [{ - 'protocol': 'tcp', - 'port_range_min': 22, - 'port_range_max': 5201}, - {'protocol': 'udp', - 'port_range_min': 22, - 'port_range_max': 5201}, - {'protocol': 'icmp'}]}} - - Heat_Dic['outputs']['instance_PIP_' +str(x)] = { - 'description': 'IP address of the instance', - 'value': {'get_attr': ['my_instance_' + str(x), 'first_address']}} - Heat_Dic['outputs']['instance_ip_' +str(x)] = { - 'description': 'IP address of the instance', - 'value': {'get_attr': ['floating_ip_' + str(x), 'floating_ip_address']}} - - Heat_Dic['outputs']['availability_instance_' + str(x)] = { - 'description': 'Availability Zone of the Instance', - 'value': { 'get_param': 'availability_zone_'+str(x)}} - - - Heat_Dic['outputs']['KeyPair_PublicKey'] = { - 'description': 'Private Key', - 'value': {'get_attr': ['KeyPairSavePrivate', 'private_key']} - } - del Heat_Dic['outputs']['description'] - return Heat_Dic - - def _get_keystone_client(self): - '''returns a keystone client instance''' - - if self._keystone_client is None: - self._keystone_client = keystoneclient.v2_0.client.Client( - auth_url=os.environ.get('OS_AUTH_URL'), - username=os.environ.get('OS_USERNAME'), - password=os.environ.get('OS_PASSWORD'), - tenant_name=os.environ.get('OS_TENANT_NAME')) - return self._keystone_client - - def _get_nova_client(self): - if self._nova_client is None: - keystone = self._get_keystone_client() - self._nova_client = client.Client('2', token=keystone.auth_token) - return self._nova_client - - def _get_heat_client(self): - '''returns a heat client instance''' - if self._heat_client is None: - keystone = self._get_keystone_client() - heat_endpoint = keystone.service_catalog.url_for( - service_type='orchestration') - self._heat_client = heatclient.client.Client( - '1', endpoint=heat_endpoint, token=keystone.auth_token) - return self._heat_client - - def _get_glance_client(self): - if self._glance_client is None: - keystone = self._get_keystone_client() - glance_endpoint = keystone.service_catalog.url_for( - service_type='image') - self._glance_client = glanceclient.Client( - '2', glance_endpoint, token=keystone.auth_token) - return self._glance_client - - def create_stack(self, vm_role_ip_dict, Heat_template): - - stackname = 'QTIP' - heat = self._get_heat_client() - glance = self._get_glance_client() - - available_images = [] - for image_list in glance.images.list(): - - available_images.append(image_list.name) - - if 'QTIP_CentOS' in available_images: - print 'Image Present' - - elif 'QTIP_CentOS' not in available_images: - fetchImage = FetchImg() - fetchImage.download() - print 'Uploading Image to Glance. Please wait' - qtip_image = glance.images.create( - name='QTIP_CentOS', - visibility='public', - disk_format='qcow2', - container_format='bare') - qtip_image = glance.images.upload( - qtip_image.id, open('./Temp_Img/QTIP_CentOS.qcow2')) - json_temp = json.dumps(Heat_template) - - for checks in range(3): - for prev_stacks in heat.stacks.list(): - - if prev_stacks.stack_name == 'QTIP': - print 'QTIP Stacks exists.\nDeleting Existing Stack' - heat.stacks.delete('QTIP') - time.sleep(10) - - print '\nStack Creating Started\n' - - try: - heat.stacks.create(stack_name=stackname, template=Heat_template) - except: - print 'Create Failed :( ' - - cluster_detail = heat.stacks.get(stackname) - while(cluster_detail.status != 'COMPLETE'): - if cluster_detail.status == 'IN_PROGRESS': - print 'Stack Creation in Progress' - cluster_detail = heat.stacks.get(stackname) - time.sleep(10) - print 'Stack Created' - print 'Getting Public IP(s)' - zone = [] - s=0 - for vm in range(len(vm_role_ip_dict['OS_image'])): - - for I in cluster_detail.outputs: - availabilityKey = 'availability_instance_'+str(vm+1) - - if I['output_key'] == availabilityKey: - zone.insert(s,str(I['output_value'])) - s=s+1 - for i in cluster_detail.outputs: - instanceKey = "instance_ip_" + str(vm + 1) - privateIPkey = 'instance_PIP_' + str(vm +1) - if i['output_key'] == instanceKey: - Env_setup.roles_dict[vm_role_ip_dict['role'][vm]].append( - str(i['output_value'])) - Env_setup.ip_pw_list.append((str(i['output_value']),'')) - - if i['output_key'] == privateIPkey: - Env_setup.ip_pw_dict[vm_role_ip_dict['role'][vm]]=str(i['output_value']) - if i['output_key'] == 'KeyPair_PublicKey': - sshkey = str(i['output_value']) - - with open('./data/my_key.pem', 'w') as fopen: - fopen.write(sshkey) - fopen.close() - print Env_setup.ip_pw_list +##############################################################################
+# Copyright (c) 2015 Dell Inc 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 sys
+from collections import defaultdict
+from func.env_setup import Env_setup
+from func.fetchimg import FetchImg
+import yaml
+import heatclient.client
+import keystoneclient
+import glanceclient
+from novaclient import client
+import time
+from func.create_zones import create_zones
+
+
+class SpawnVM(Env_setup):
+ vm_role_ip_dict = defaultdict(list)
+ installer = ''
+
+ def __init__(self, vm_info):
+ Env_setup.__init__(self)
+ print 'SpawnVM Class initiated'
+ vm_role_ip_dict = vm_info.copy()
+ print 'Generating Heat Template\n'
+ self._keystone_client = None
+ self._heat_client = None
+ self._glance_client = None
+ self._nova_client = None
+ self. _get_nova_client()
+ azoneobj = create_zones()
+ azoneobj.create_agg(vm_info['availability_zone'])
+ installer = self.get_installer_type()
+ self.Heat_template1 = self.heat_template_vm(vm_info, installer)
+ self.create_stack(vm_role_ip_dict, self.Heat_template1)
+
+ @staticmethod
+ def get_installer_type():
+ print 'Getting Installer Name'
+ return os.environ['INSTALLER_TYPE']
+
+ @staticmethod
+ def get_public_network(installer_detected):
+
+ """
+ TODO: GET THE NAMES OF THE PUBLIC NETWORKS for OTHER PROJECTS
+ """
+ print 'Getting Public Network'
+ if installer_detected.lower() == 'fuel':
+ return 'admin_floating_net'
+ if installer_detected.lower() == 'apex':
+ return 'external'
+ if installer_detected.lower() == 'compass':
+ return 'ext-net'
+ if installer_detected.lower() == 'joid':
+ return 'ext-net'
+
+ def heat_template_vm(self, vm_params, installer):
+ Heat_Dic = {}
+ try:
+ with open('./heat/SampleHeat.yaml', 'r+') as H_temp:
+ Heat_Dic = yaml.load(H_temp)
+ except yaml.YAMLError as exc:
+ if hasattr(exc, 'problem_mark'):
+ mark = exc.problem_mark
+ print 'Error in qtip/heat/SampleHeat.yaml at: (%s,%s)' % (mark.line + 1, mark.column + 1)
+ print 'EXITING PROGRAM. Correct File and restart'
+ sys.exit(0)
+ fopen = open('./data/QtipKey.pub', 'r')
+ fopenstr = fopen.read()
+ fopenstr = fopenstr.rstrip()
+ scriptcmd = '#!/bin/bash \n echo {0} >> foo.txt \n echo {1} >> /root/.ssh/authorized_keys'.format(
+ fopenstr, fopenstr)
+
+ netName = self.get_public_network(installer)
+ print netName
+ Heat_Dic['heat_template_version'] = '2014-10-16'
+ Heat_Dic['resources']['KeyPairSavePrivate'] = {
+ 'type': 'OS::Nova::KeyPair',
+ 'properties': {
+ 'save_private_key': 'true',
+ 'name': 'my_key'
+ }
+ }
+ Heat_Dic['parameters']['public_network'] = {
+ 'type': 'string',
+ 'default': netName
+ }
+ for x in range(1, len(vm_params['availability_zone']) + 1):
+ avail_zone = vm_params['availability_zone'][x - 1]
+ img = vm_params['OS_image'][x - 1]
+ flavor = vm_params['flavor'][x - 1]
+
+ Heat_Dic['parameters']['availability_zone_' + str(x)] = \
+ {'description': 'Availability Zone of the instance',
+ 'default': avail_zone,
+ 'type': 'string'}
+
+ Heat_Dic['resources']['public_port_' + str(x)] = \
+ {'type': 'OS::Neutron::Port',
+ 'properties': {'network': {'get_resource': 'private_network'},
+ 'security_groups': [{'get_resource': 'demo1_security_Group'}],
+ 'fixed_ips': [{'subnet_id':
+ {'get_resource': 'private_subnet'}}]}}
+
+ Heat_Dic['resources']['floating_ip_' + str(x)] = {
+ 'type': 'OS::Neutron::FloatingIP',
+ 'properties': {
+ 'floating_network': {'get_param': 'public_network'}}}
+
+ Heat_Dic['resources']['floating_ip_assoc_' + str(x)] = {
+ 'type': 'OS::Neutron::FloatingIPAssociation',
+ 'properties': {
+ 'floatingip_id': {'get_resource': 'floating_ip_' + str(x)},
+ 'port_id': {'get_resource': 'public_port_' + str(x)}}}
+
+ Heat_Dic['resources']['my_instance_' + str(x)] = \
+ {'type': 'OS::Nova::Server',
+ 'properties': {'image': img,
+ 'networks':
+ [{'port': {'get_resource': 'public_port_' + str(x)}}],
+ 'flavor': flavor,
+ 'availability_zone': avail_zone,
+ 'name': 'instance' + str(x),
+ 'key_name': {'get_resource': 'KeyPairSavePrivate'},
+ 'user_data_format': 'RAW',
+ 'user_data': scriptcmd}}
+
+ Heat_Dic['resources']['demo1_security_Group'] = {
+ 'type': 'OS::Neutron::SecurityGroup',
+ 'properties': {
+ 'name': 'demo1_security_Group',
+ 'rules': [{
+ 'protocol': 'tcp',
+ 'port_range_min': 22,
+ 'port_range_max': 5201},
+ {'protocol': 'udp',
+ 'port_range_min': 22,
+ 'port_range_max': 5201},
+ {'protocol': 'icmp'}]}}
+
+ Heat_Dic['outputs']['instance_PIP_' + str(x)] = {
+ 'description': 'IP address of the instance',
+ 'value': {'get_attr': ['my_instance_' + str(x), 'first_address']}}
+ Heat_Dic['outputs']['instance_ip_' + str(x)] = {
+ 'description': 'IP address of the instance',
+ 'value': {'get_attr': ['floating_ip_' + str(x), 'floating_ip_address']}}
+
+ Heat_Dic['outputs']['availability_instance_' + str(x)] = {
+ 'description': 'Availability Zone of the Instance',
+ 'value': {'get_param': 'availability_zone_' + str(x)}}
+
+ Heat_Dic['outputs']['KeyPair_PublicKey'] = {
+ 'description': 'Private Key',
+ 'value': {'get_attr': ['KeyPairSavePrivate', 'private_key']}
+ }
+ del Heat_Dic['outputs']['description']
+ print Heat_Dic
+ return Heat_Dic
+
+ def _get_keystone_client(self):
+ """returns a keystone client instance"""
+
+ if self._keystone_client is None:
+ self._keystone_client = keystoneclient.v2_0.client.Client(
+ auth_url=os.environ.get('OS_AUTH_URL'),
+ username=os.environ.get('OS_USERNAME'),
+ password=os.environ.get('OS_PASSWORD'),
+ tenant_name=os.environ.get('OS_TENANT_NAME'))
+ return self._keystone_client
+
+ def _get_nova_client(self):
+ if self._nova_client is None:
+ keystone = self._get_keystone_client()
+ self._nova_client = client.Client('2', token=keystone.auth_token)
+ return self._nova_client
+
+ def _get_heat_client(self):
+ """returns a heat client instance"""
+ if self._heat_client is None:
+ keystone = self._get_keystone_client()
+ heat_endpoint = keystone.service_catalog.url_for(
+ service_type='orchestration')
+ self._heat_client = heatclient.client.Client(
+ '1', endpoint=heat_endpoint, token=keystone.auth_token)
+ return self._heat_client
+
+ def _get_glance_client(self):
+ if self._glance_client is None:
+ keystone = self._get_keystone_client()
+ glance_endpoint = keystone.service_catalog.url_for(
+ service_type='image')
+ self._glance_client = glanceclient.Client(
+ '2', glance_endpoint, token=keystone.auth_token)
+ return self._glance_client
+
+ def create_stack(self, vm_role_ip_dict, heat_template):
+
+ global sshkey
+ stackname = 'QTIP'
+ heat = self._get_heat_client()
+ glance = self._get_glance_client()
+
+ available_images = []
+ for image_list in glance.images.list():
+
+ available_images.append(image_list.name)
+
+ if 'QTIP_CentOS' in available_images:
+ print 'Image Present'
+
+ elif 'QTIP_CentOS' not in available_images:
+ fetchImage = FetchImg()
+ fetchImage.download()
+ print 'Uploading Image to Glance. Please wait'
+ qtip_image = glance.images.create(
+ name='QTIP_CentOS',
+ visibility='public',
+ disk_format='qcow2',
+ container_format='bare')
+ glance.images.upload(
+ qtip_image.id, open('./Temp_Img/QTIP_CentOS.qcow2'))
+ for checks in range(3):
+ print "Try to delete heats %s" % checks
+ for prev_stacks in heat.stacks.list():
+ if prev_stacks.stack_name == 'QTIP':
+ print 'QTIP Stacks exists.\nDeleting Existing Stack'
+ heat.stacks.delete('QTIP')
+ time.sleep(10)
+
+ print '\nStack Creating Started\n'
+
+ try:
+ heat.stacks.create(stack_name=stackname, template=heat_template)
+ except Exception:
+ print 'Create Failed :( '
+
+ cluster_detail = heat.stacks.get(stackname)
+ while cluster_detail.status != 'COMPLETE':
+ if cluster_detail.status == 'IN_PROGRESS':
+ print 'Stack Creation in Progress'
+ cluster_detail = heat.stacks.get(stackname)
+ time.sleep(10)
+ print 'Stack Created'
+ print 'Getting Public IP(s)'
+ zone = []
+ s = 0
+ for vm in range(len(vm_role_ip_dict['OS_image'])):
+
+ for I in cluster_detail.outputs:
+ availabilityKey = 'availability_instance_' + str(vm + 1)
+
+ if I['output_key'] == availabilityKey:
+ zone.insert(s, str(I['output_value']))
+ s = s + 1
+ for i in cluster_detail.outputs:
+ instanceKey = "instance_ip_" + str(vm + 1)
+ privateIPkey = 'instance_PIP_' + str(vm + 1)
+ if i['output_key'] == instanceKey:
+ Env_setup.roles_dict[vm_role_ip_dict['role'][vm]] \
+ .append(str(i['output_value']))
+ Env_setup.ip_pw_list.append((str(i['output_value']), ''))
+
+ if i['output_key'] == privateIPkey:
+ Env_setup.ip_pw_dict[vm_role_ip_dict['role'][vm]] = str(i['output_value'])
+ if i['output_key'] == 'KeyPair_PublicKey':
+ sshkey = str(i['output_value'])
+
+ with open('./data/my_key.pem', 'w') as fopen:
+ fopen.write(sshkey)
+ fopen.close()
+ print Env_setup.ip_pw_list
diff --git a/test-requirements.txt b/test-requirements.txt index cdac2380..31581503 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,3 +4,4 @@ pytest pykwalify +mock diff --git a/tests/driver_test.py b/tests/driver_test.py new file mode 100644 index 00000000..39adc939 --- /dev/null +++ b/tests/driver_test.py @@ -0,0 +1,65 @@ +import pytest +import mock +import os +import json +from func.driver import Driver + + +class TestClass: + @pytest.mark.parametrize("test_input, expected", [ + (["iperf", + [('host', ['10.20.0.13', '10.20.0.15'])], + "iperf_bm.yaml", + [('duration', 20), ('protocol', 'tcp'), ('bandwidthGbps', 0)], + [("10.20.0.13", [None]), ("10.20.0.15", [None])], + {'http_proxy': 'http://10.20.0.1:8118', + 'https_proxy': 'http://10.20.0.1:8118', + 'no_proxy': 'localhost,127.0.0.1,10.20.*,192.168.*'}, + 'fuel'], + {'Dest_dir': 'results', + 'ip1': '', + 'ip2': '', + 'installer': 'fuel', + 'workingdir': '/home', + 'fname': 'iperf_bm.yaml', + 'username': 'root', + 'http_proxy': 'http://10.20.0.1:8118', + 'https_proxy': 'http://10.20.0.1:8118', + 'no_proxy': 'localhost,127.0.0.1,10.20.*,192.168.*', + 'duration': 20, + 'protocol': 'tcp', + 'bandwidthGbps': 0, + "role": "host"}), + (["iperf", + [('1-server', ['10.20.0.13']), ('2-host', ['10.20.0.15'])], + "iperf_vm.yaml", + [('duration', 20), ('protocol', 'tcp'), ('bandwidthGbps', 0)], + [("10.20.0.13", [None]), ("10.20.0.15", [None])], + {}, + 'joid'], + {'Dest_dir': 'results', + 'ip1': '10.20.0.13', + 'ip2': '', + 'installer': 'joid', + "privateip1": "NONE", + 'workingdir': '/home', + 'fname': 'iperf_vm.yaml', + 'username': 'ubuntu', + 'duration': 20, + 'protocol': 'tcp', + 'bandwidthGbps': 0, + "role": "2-host"}) + ]) + @mock.patch('func.driver.os.system') + def test_driver_success(self, mock_system, test_input, expected): + mock_system.return_value = True + k = mock.patch.dict(os.environ, {'INSTALLER_TYPE': test_input[6], 'PWD': '/home'}) + k.start() + dri = Driver() + dri.drive_bench(test_input[0], test_input[1], test_input[2], test_input[3], test_input[4], test_input[5]) + call = mock_system.call_args + k.stop() + call_args, call_kwargs = call + real_call = call_args[0].split('extra-vars \'')[1] + real_call = real_call[0: len(real_call) - 1] + assert json.loads(real_call) == json.loads(json.dumps(expected)) diff --git a/tests/fetchimg_test.py b/tests/fetchimg_test.py new file mode 100644 index 00000000..683c9701 --- /dev/null +++ b/tests/fetchimg_test.py @@ -0,0 +1,22 @@ +import mock +from func.fetchimg import FetchImg + + +class TestClass: + @mock.patch('func.fetchimg.os') + @mock.patch('func.fetchimg.os.path') + def test_fetch_img_success(self, mock_path, mock_os): + mock_os.system.return_value = True + mock_path.isfile.return_value = True + img = FetchImg() + img.download() + + @mock.patch('func.fetchimg.time') + @mock.patch('func.fetchimg.os.system') + @mock.patch('func.fetchimg.os.path') + def test_fetch_img_fail(self, mock_path, mock_system, mock_time): + img = FetchImg() + mock_system.return_value = True + mock_path.isfile.side_effect = [False, True] + img.download() + assert mock_time.sleep.call_count == 2 diff --git a/tests/spawn_vm_test.py b/tests/spawn_vm_test.py new file mode 100644 index 00000000..eb843ad9 --- /dev/null +++ b/tests/spawn_vm_test.py @@ -0,0 +1,71 @@ +import pytest +import mock +from mock import Mock, MagicMock +import os +from func.spawn_vm import SpawnVM + + +class KeystoneMock(MagicMock): + auth_token = Mock() + v2_0 = Mock() + + +class ImageMock(MagicMock): + name = 'QTIP_CentOS' + + +class ImagesMock(MagicMock): + def list(self): + return [ImageMock()] + + +class StackMock(MagicMock): + status = 'COMPLETE' + outputs = [{'output_key': 'availability_instance_1', + 'output_value': 'output_value_1'}, + {'output_key': 'instance_ip_1', + "output_value": "172.10.0.154"}, + {"output_key": "instance_PIP_1", + "output_value": "10.10.17.5"}, + {'output_key': 'KeyPair_PublicKey', + "output_value": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpwIBAAKCAQEAqCiHcrLBXtxG0LhnKndU7VIVpYxORmv0d4tvujkWOkYuagiW\nU/MTRk0zhRvFQDVPEs0Jrj/BIecqm6fjjT6dZ/H7JLYGaqJitRkoupKgBsMSIqUz\nrR0ekOlfXZ6N+Ud8k6s+qjc7BO4b1ezz78jHisC5o0GCkUV0ECx64Re1fO+oKs1c\nfL9aaexahJUYN3J48pazQz+imc2x/G9nuqHX3cqEszmxnT4jwv//In1GjHy2AyXw\n1oA5F6wZoQCSrXc2BditU+1tlVhEkPFt5JgiHUpY8T8mYbroT7JH6xjcGSKUN+HG\nN8PXNUTD1VAQfwHpkfsGMfDyzjytCXsoTEOqnwIDAQABAoIBAAEL/4vfQQTuKiKy\ngzHofEbd8/SL4xDdKzBzVca7BEBon3FZjFYJdV1CrcduXNQBgPSFAkJrczBa2BEQ\nAoKmmSREhWO9Hl0blbG67l36+7QPEtXUYXX6cG5Ghal3izq6DzR8JG+62Es3kETM\nrNgZT+S1PnKdvcpZvFc9b6ZnF2InuTbrmNVBZKrhdWOJ5tCwRGKKUl6BHoJH3yu0\nT5hUW277e1LYHx+hZtoZ98ToC+LGe6/M8a8y6VLYpcQlX2AtVXeGDalomunF+p3f\nuY6din6s4lq1gSJz03PTpUbwiuhYCTe8Xkseu74Y+XYYJXPHopFju0Ewd6p0Db9Q\nJzzxCoECggCBAM2ox9zyrDc/Vlc0bb9SciFGUd/nEJF89+UHy98bAkpo22zNZIDg\nfacSgkg/6faZD+KrOU0I5W7m2B5t6w2fNHHik6NYGSLQ1JhgbXELGV7X/qECDL02\nctPaf+8o+dYoZja2LdJNASq2nmEmPI3LSHhzAt4dWY4W+geXiHt4iWVHAoIAgQDR\nUdN09xv4U+stWqNcSfgjtx6boEUE8Ky7pyj+LrZKG0L61Jy9cSDP0x0rCtkW9vVR\n6RjidWM/DHQ5cl6aq+7pPy20/OqtqttFYT4R+C3AoAnRSaNzPD9a80C2gjv7WEz0\nPPFstWkI1gsN71KKRx7e6NIa9CNn5x9iE+SGfjgb6QKCAIBXylzG7LCnRNpOj4rp\nyP//RE1fDvv7nyUTF6jnrFfl+6zvXR4yBaKd10DWJrJxGhW15PGo+Ms39EL9el6E\nihmRI+9yIwFX411dToxpXRuPaRTBFmbpvnx2Ayfpp8w+pzA62rnktApzeVFSl0fy\nH3zoLfBjcJPyG8zPwNf6HRJJsQKCAIAE2S5asTaWo+r4m/bYtmXm/eDZnfa7TI/T\nsOWELbTPNp5wjOgsgyhNaAhu7MtmesXn5cxLwohP94vhoMKMNptMD8iRPqJ471Iw\n4zW62NLGeW6AyIHes3CMPMIs+AtHoR33MkotSG5sY/jRk8+HoGoYo6/qK+l+CJ5z\neR579wR5sQKCAIAvPWq+bvcPTDKUU1Fe/Y/GyWoUA+uSqmCdORBkK38lALFGphxj\nfDz9dXskimqW+A9hOPOS8dm8YcVvi/TLXVE5Vsx9VkOg6z6AZBQpgNXGfOgpju4W\nbjER7bQaASatuWQyCxbA9oNlAUdSeOhGTxeFLkLj7hNMd6tLjfd8w7A/hA==\n-----END RSA PRIVATE KEY-----\n"}] + + +class HeatMock(MagicMock): + def list(self): + return [] + + def get(self, stackname): + return StackMock() + + def create(self, stack_name, template): + pass + + +class TestClass: + @pytest.mark.parametrize("test_input, expected", [ + ({'availability_zone': ['compute1', 'compute1'], + 'OS_image': ['QTIP_CentOS', 'QTIP_CentOS'], + 'public_network': ['admin-floating_net', 'admin-floating_net'], + 'flavor': ['m1.large', 'm1.large'], + 'role': ['1-server', '2-host']}, + [('172.10.0.154', '')]), + ]) + @mock.patch('func.spawn_vm.Env_setup') + @mock.patch('func.spawn_vm.FetchImg') + @mock.patch('func.spawn_vm.create_zones') + @mock.patch('func.spawn_vm.client', autospec=True) + @mock.patch('func.spawn_vm.glanceclient', autospec=True) + @mock.patch('func.spawn_vm.keystoneclient.v2_0', autospec=True) + @mock.patch('func.spawn_vm.heatclient.client', autospec=True) + def test_create_zones_success(self, mock_heat, mock_keystone, mock_glance, + mock_nova_client, mock_zone, mock_fetch, + mock_setup, test_input, expected): + mock_glance.Client.return_value = Mock(images=ImagesMock()) + mock_nova_client.Client.return_value = Mock() + mock_heat.Client.return_value = Mock(stacks=HeatMock()) + k = mock.patch.dict(os.environ, {'INSTALLER_TYPE': 'fuel'}) + k.start() + SpawnVM(test_input) + k.stop() + mock_setup.ip_pw_list.append.assert_called_with(expected[0]) |