3 # Copyright (c) 2017 Cable Television Laboratories, Inc. and others.
5 # This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 """Define the parent class of vping_ssh and vping_userdata testcases."""
13 from datetime import datetime
18 from functest.core import testcase
19 from functest.opnfv_tests.openstack.snaps import snaps_utils
20 from functest.utils.constants import CONST
22 from snaps.config.flavor import FlavorConfig
23 from snaps.config.network import NetworkConfig, SubnetConfig
24 from snaps.config.router import RouterConfig
25 from snaps.openstack import create_flavor
26 from snaps.openstack.create_flavor import OpenStackFlavor
27 from snaps.openstack.tests import openstack_tests
28 from snaps.openstack.utils import deploy_utils
31 class VPingBase(testcase.TestCase):
34 Base class for vPing tests that check connectivity between two VMs shared
36 This class is responsible for creating the image, internal network.
38 # pylint: disable=too-many-instance-attributes
40 def __init__(self, **kwargs):
41 super(VPingBase, self).__init__(**kwargs)
42 self.logger = logging.getLogger(__name__)
43 self.os_creds = kwargs.get('os_creds') or snaps_utils.get_credentials()
44 self.creators = list()
45 self.image_creator = None
46 self.network_creator = None
47 self.vm1_creator = None
48 self.vm2_creator = None
49 self.router_creator = None
52 self.guid = '-' + str(uuid.uuid4())
54 self.router_name = getattr(CONST, 'vping_router_name') + self.guid
55 self.vm1_name = getattr(CONST, 'vping_vm_name_1') + self.guid
56 self.vm2_name = getattr(CONST, 'vping_vm_name_2') + self.guid
58 self.vm_boot_timeout = getattr(CONST, 'vping_vm_boot_timeout')
59 self.vm_delete_timeout = getattr(CONST, 'vping_vm_delete_timeout')
60 self.vm_ssh_connect_timeout = getattr(
61 CONST, 'vping_vm_ssh_connect_timeout')
62 self.ping_timeout = getattr(CONST, 'vping_ping_timeout')
63 self.flavor_name = 'vping-flavor' + self.guid
65 # Move this configuration option up for all tests to leverage
66 if hasattr(CONST, 'snaps_images_cirros'):
67 self.cirros_image_config = getattr(CONST, 'snaps_images_cirros')
69 self.cirros_image_config = None
71 def run(self, **kwargs): # pylint: disable=too-many-locals
73 Begins the test execution which should originate from the subclass
75 self.logger.info('Begin virtual environment setup')
77 self.start_time = time.time()
79 "vPing Start Time:'%s'",
80 datetime.fromtimestamp(self.start_time).strftime(
83 image_base_name = '{}-{}'.format(
84 getattr(CONST, 'vping_image_name'),
86 os_image_settings = openstack_tests.cirros_image_settings(
87 image_base_name, image_metadata=self.cirros_image_config)
88 self.logger.info("Creating image with name: '%s'", image_base_name)
90 self.image_creator = deploy_utils.create_image(
91 self.os_creds, os_image_settings)
92 self.creators.append(self.image_creator)
94 private_net_name = getattr(CONST, 'vping_private_net_name') + self.guid
95 private_subnet_name = getattr(
96 CONST, 'vping_private_subnet_name') + self.guid
97 private_subnet_cidr = getattr(CONST, 'vping_private_subnet_cidr')
99 vping_network_type = None
100 vping_physical_network = None
101 vping_segmentation_id = None
103 if hasattr(CONST, 'vping_network_type'):
104 vping_network_type = getattr(CONST, 'vping_network_type')
105 if hasattr(CONST, 'vping_physical_network'):
106 vping_physical_network = getattr(CONST, 'vping_physical_network')
107 if hasattr(CONST, 'vping_segmentation_id'):
108 vping_segmentation_id = getattr(CONST, 'vping_segmentation_id')
111 "Creating network with name: '%s'", private_net_name)
112 self.network_creator = deploy_utils.create_network(
115 name=private_net_name,
116 network_type=vping_network_type,
117 physical_network=vping_physical_network,
118 segmentation_id=vping_segmentation_id,
119 subnet_settings=[SubnetConfig(
120 name=private_subnet_name,
121 cidr=private_subnet_cidr)]))
122 self.creators.append(self.network_creator)
124 # Creating router to external network
125 log = "Creating router with name: '%s'" % self.router_name
126 self.logger.info(log)
127 ext_net_name = snaps_utils.get_ext_net_name(self.os_creds)
128 self.router_creator = deploy_utils.create_router(
131 name=self.router_name,
132 external_gateway=ext_net_name,
133 internal_subnets=[private_subnet_name]))
134 self.creators.append(self.router_creator)
137 "Creating flavor with name: '%s'", self.flavor_name)
138 scenario = getattr(CONST, 'DEPLOY_SCENARIO')
139 flavor_metadata = None
141 if 'ovs' in scenario or 'fdio' in scenario:
142 flavor_metadata = create_flavor.MEM_PAGE_SIZE_LARGE
144 flavor_creator = OpenStackFlavor(
146 FlavorConfig(name=self.flavor_name, ram=flavor_ram, disk=1,
147 vcpus=1, metadata=flavor_metadata))
148 flavor_creator.create()
149 self.creators.append(flavor_creator)
153 Method called by subclasses after environment has been setup
154 :return: the exit code
156 self.logger.info('Begin test execution')
158 test_ip = self.vm1_creator.get_port_ip(
159 self.vm1_creator.instance_settings.port_settings[0].name)
161 if self.vm1_creator.vm_active(
162 block=True) and self.vm2_creator.vm_active(block=True):
163 result = self._do_vping(self.vm2_creator, test_ip)
165 raise Exception('VMs never became active')
167 self.stop_time = time.time()
169 if result != testcase.TestCase.EX_OK:
171 return testcase.TestCase.EX_RUN_ERROR
174 return testcase.TestCase.EX_OK
178 Cleanup all OpenStack objects. Should be called on completion
181 if getattr(CONST, 'vping_cleanup_objects') == 'True':
182 for creator in reversed(self.creators):
185 except Exception as error: # pylint: disable=broad-except
186 self.logger.error('Unexpected error cleaning - %s', error)
188 def _do_vping(self, vm_creator, test_ip):
190 Method to be implemented by subclasses
191 Begins the real test after the OpenStack environment has been setup
192 :param vm_creator: the SNAPS VM instance creator object
193 :param test_ip: the IP to which the VM needs to issue the ping
196 raise NotImplementedError('vping execution is not implemented')