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 import os_client_config
19 from xtesting.core import testcase
21 from functest.utils import config
22 from functest.utils import env
23 from functest.utils import functest_utils
26 class VPingBase(testcase.TestCase):
29 Base class for vPing tests that check connectivity between two VMs shared
31 This class is responsible for creating the image, internal network.
33 # pylint: disable=too-many-instance-attributes
35 def __init__(self, **kwargs):
36 super(VPingBase, self).__init__(**kwargs)
37 self.logger = logging.getLogger(__name__)
38 self.cloud = os_client_config.make_shade()
39 self.ext_net = functest_utils.get_external_network(self.cloud)
40 self.logger.debug("ext_net: %s", self.ext_net)
41 self.guid = '-' + str(uuid.uuid4())
49 def run(self, **kwargs): # pylint: disable=too-many-locals
51 Begins the test execution which should originate from the subclass
55 self.logger.info('Begin virtual environment setup')
57 self.start_time = time.time()
59 "vPing Start Time:'%s'",
60 datetime.fromtimestamp(self.start_time).strftime(
63 image_base_name = '{}-{}'.format(
64 getattr(config.CONF, 'vping_image_name'), self.guid)
65 self.logger.info("Creating image with name: '%s'", image_base_name)
66 self.image = self.cloud.create_image(
68 filename=getattr(config.CONF, 'openstack_image_url'))
69 self.logger.debug("image: %s", self.image)
71 private_net_name = getattr(
72 config.CONF, 'vping_private_net_name') + self.guid
73 private_subnet_name = str(getattr(
74 config.CONF, 'vping_private_subnet_name') + self.guid)
75 private_subnet_cidr = getattr(config.CONF, 'vping_private_subnet_cidr')
78 if hasattr(config.CONF, 'vping_network_type'):
79 provider["network_type"] = getattr(
80 config.CONF, 'vping_network_type')
81 if hasattr(config.CONF, 'vping_physical_network'):
82 provider["physical_network"] = getattr(
83 config.CONF, 'vping_physical_network')
84 if hasattr(config.CONF, 'vping_segmentation_id'):
85 provider["segmentation_id"] = getattr(
86 config.CONF, 'vping_segmentation_id')
88 "Creating network with name: '%s'", private_net_name)
89 self.network = self.cloud.create_network(
92 self.logger.debug("network: %s", self.network)
94 self.subnet = self.cloud.create_subnet(
96 subnet_name=private_subnet_name,
97 cidr=private_subnet_cidr,
99 dns_nameservers=[env.get('NAMESERVER')])
100 self.logger.debug("subnet: %s", self.subnet)
102 router_name = getattr(config.CONF, 'vping_router_name') + self.guid
103 self.logger.info("Creating router with name: '%s'", router_name)
104 self.router = self.cloud.create_router(
106 ext_gateway_net_id=self.ext_net.id)
107 self.logger.debug("router: %s", self.router)
108 self.cloud.add_router_interface(self.router, subnet_id=self.subnet.id)
110 flavor_name = 'vping-flavor' + self.guid
112 "Creating flavor with name: '%s'", flavor_name)
113 self.flavor = self.cloud.create_flavor(
114 flavor_name, getattr(config.CONF, 'openstack_flavor_ram'),
115 getattr(config.CONF, 'openstack_flavor_vcpus'),
116 getattr(config.CONF, 'openstack_flavor_disk'))
117 self.logger.debug("flavor: %s", self.flavor)
118 self.cloud.set_flavor_specs(
119 self.flavor.id, getattr(config.CONF, 'flavor_extra_specs', {}))
121 vm1_name = getattr(config.CONF, 'vping_vm_name_1') + self.guid
123 "Creating VM 1 instance with name: '%s'", vm1_name)
124 self.vm1 = self.cloud.create_server(
125 vm1_name, image=self.image.id,
126 flavor=self.flavor.id,
127 auto_ip=False, wait=True,
128 timeout=getattr(config.CONF, 'vping_vm_boot_timeout'),
129 network=self.network.id)
130 self.logger.debug("vm1: %s", self.vm1)
131 self.vm1 = self.cloud.wait_for_server(self.vm1, auto_ip=False)
135 Method called by subclasses after environment has been setup
136 :return: the exit code
138 self.logger.info('Begin test execution')
139 result = self._do_vping()
140 self.stop_time = time.time()
141 if result != testcase.TestCase.EX_OK:
143 return testcase.TestCase.EX_RUN_ERROR
145 return testcase.TestCase.EX_OK
149 Cleanup all OpenStack objects. Should be called on completion
153 self.cloud.delete_server(self.vm1, wait=True)
154 self.cloud.delete_image(self.image)
155 self.cloud.remove_router_interface(self.router, self.subnet.id)
156 self.cloud.delete_router(self.router.id)
157 self.cloud.delete_network(self.network.id)
158 self.cloud.delete_flavor(self.flavor.id)
162 Method to be implemented by subclasses
163 Begins the real test after the OpenStack environment has been setup
166 raise NotImplementedError('vping execution is not implemented')