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
25 class VPingBase(testcase.TestCase):
28 Base class for vPing tests that check connectivity between two VMs shared
30 This class is responsible for creating the image, internal network.
32 # pylint: disable=too-many-instance-attributes
34 def __init__(self, **kwargs):
35 super(VPingBase, self).__init__(**kwargs)
36 self.logger = logging.getLogger(__name__)
37 self.cloud = os_client_config.make_shade()
38 self.ext_net = self.cloud.get_network("ext-net")
39 self.logger.debug("ext_net: %s", self.ext_net)
40 self.guid = '-' + str(uuid.uuid4())
48 def run(self, **kwargs): # pylint: disable=too-many-locals
50 Begins the test execution which should originate from the subclass
54 self.logger.info('Begin virtual environment setup')
56 self.start_time = time.time()
58 "vPing Start Time:'%s'",
59 datetime.fromtimestamp(self.start_time).strftime(
62 image_base_name = '{}-{}'.format(
63 getattr(config.CONF, 'vping_image_name'), self.guid)
64 self.logger.info("Creating image with name: '%s'", image_base_name)
65 self.image = self.cloud.create_image(
67 filename=getattr(config.CONF, 'openstack_image_url'))
68 self.logger.debug("image: %s", self.image)
70 private_net_name = getattr(
71 config.CONF, 'vping_private_net_name') + self.guid
72 private_subnet_name = str(getattr(
73 config.CONF, 'vping_private_subnet_name') + self.guid)
74 private_subnet_cidr = getattr(config.CONF, 'vping_private_subnet_cidr')
77 if hasattr(config.CONF, 'vping_network_type'):
78 provider["network_type"] = getattr(
79 config.CONF, 'vping_network_type')
80 if hasattr(config.CONF, 'vping_physical_network'):
81 provider["physical_network"] = getattr(
82 config.CONF, 'vping_physical_network')
83 if hasattr(config.CONF, 'vping_segmentation_id'):
84 provider["segmentation_id"] = getattr(
85 config.CONF, 'vping_segmentation_id')
87 "Creating network with name: '%s'", private_net_name)
88 self.network = self.cloud.create_network(
91 self.logger.debug("network: %s", self.network)
93 self.subnet = self.cloud.create_subnet(
95 subnet_name=private_subnet_name,
96 cidr=private_subnet_cidr,
98 dns_nameservers=[env.get('NAMESERVER')])
99 self.logger.debug("subnet: %s", self.subnet)
101 router_name = getattr(config.CONF, 'vping_router_name') + self.guid
102 self.logger.info("Creating router with name: '%s'", router_name)
103 self.router = self.cloud.create_router(
105 ext_gateway_net_id=self.ext_net.id)
106 self.logger.debug("router: %s", self.router)
107 self.cloud.add_router_interface(self.router, subnet_id=self.subnet.id)
109 flavor_name = 'vping-flavor' + self.guid
111 "Creating flavor with name: '%s'", flavor_name)
112 self.flavor = self.cloud.create_flavor(
113 flavor_name, getattr(config.CONF, 'openstack_flavor_ram'),
114 getattr(config.CONF, 'openstack_flavor_vcpus'),
115 getattr(config.CONF, 'openstack_flavor_disk'))
116 self.logger.debug("flavor: %s", self.flavor)
117 self.cloud.set_flavor_specs(
118 self.flavor.id, getattr(config.CONF, 'flavor_extra_specs', {}))
120 vm1_name = getattr(config.CONF, 'vping_vm_name_1') + self.guid
122 "Creating VM 1 instance with name: '%s'", vm1_name)
123 self.vm1 = self.cloud.create_server(
124 vm1_name, image=self.image.id,
125 flavor=self.flavor.id,
126 auto_ip=False, wait=True,
127 timeout=getattr(config.CONF, 'vping_vm_boot_timeout'),
128 network=self.network.id)
129 self.logger.debug("vm1: %s", self.vm1)
130 self.vm1 = self.cloud.wait_for_server(self.vm1, auto_ip=False)
134 Method called by subclasses after environment has been setup
135 :return: the exit code
137 self.logger.info('Begin test execution')
138 result = self._do_vping()
139 self.stop_time = time.time()
140 if result != testcase.TestCase.EX_OK:
142 return testcase.TestCase.EX_RUN_ERROR
144 return testcase.TestCase.EX_OK
148 Cleanup all OpenStack objects. Should be called on completion
152 self.cloud.delete_server(self.vm1, wait=True)
153 self.cloud.delete_image(self.image)
154 self.cloud.remove_router_interface(self.router, self.subnet.id)
155 self.cloud.delete_router(self.router.id)
156 self.cloud.delete_network(self.network.id)
157 self.cloud.delete_flavor(self.flavor.id)
161 Method to be implemented by subclasses
162 Begins the real test after the OpenStack environment has been setup
165 raise NotImplementedError('vping execution is not implemented')