Merge "Functest fail to parse refstack results"
[functest.git] / functest / opnfv_tests / openstack / vping / vping_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Cable Television Laboratories, Inc. and others.
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 """Define the parent class of vping_ssh and vping_userdata testcases."""
12
13 from datetime import datetime
14 import logging
15 import time
16 import uuid
17
18 import os_client_config
19 from xtesting.core import testcase
20
21 from functest.utils import config
22 from functest.utils import env
23 from functest.utils import functest_utils
24
25
26 class VPingBase(testcase.TestCase):
27
28     """
29     Base class for vPing tests that check connectivity between two VMs shared
30     internal network.
31     This class is responsible for creating the image, internal network.
32     """
33     # pylint: disable=too-many-instance-attributes
34
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())
42         self.network = None
43         self.subnet = None
44         self.router = None
45         self.image = None
46         self.flavor = None
47         self.vm1 = None
48
49     def run(self, **kwargs):  # pylint: disable=too-many-locals
50         """
51         Begins the test execution which should originate from the subclass
52         """
53         assert self.cloud
54         assert self.ext_net
55         self.logger.info('Begin virtual environment setup')
56
57         self.start_time = time.time()
58         self.logger.info(
59             "vPing Start Time:'%s'",
60             datetime.fromtimestamp(self.start_time).strftime(
61                 '%Y-%m-%d %H:%M:%S'))
62
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(
67             image_base_name,
68             filename=getattr(config.CONF, 'openstack_image_url'))
69         self.logger.debug("image: %s", self.image)
70
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')
76
77         provider = {}
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')
87         self.logger.info(
88             "Creating network with name: '%s'", private_net_name)
89         self.network = self.cloud.create_network(
90             private_net_name,
91             provider=provider)
92         self.logger.debug("network: %s", self.network)
93
94         self.subnet = self.cloud.create_subnet(
95             self.network.id,
96             subnet_name=private_subnet_name,
97             cidr=private_subnet_cidr,
98             enable_dhcp=True,
99             dns_nameservers=[env.get('NAMESERVER')])
100         self.logger.debug("subnet: %s", self.subnet)
101
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(
105             name=router_name,
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)
109
110         flavor_name = 'vping-flavor' + self.guid
111         self.logger.info(
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', {}))
120
121         vm1_name = getattr(config.CONF, 'vping_vm_name_1') + self.guid
122         self.logger.info(
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)
132
133     def _execute(self):
134         """
135         Method called by subclasses after environment has been setup
136         :return: the exit code
137         """
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:
142             self.result = 0
143             return testcase.TestCase.EX_RUN_ERROR
144         self.result = 100
145         return testcase.TestCase.EX_OK
146
147     def clean(self):
148         """
149         Cleanup all OpenStack objects. Should be called on completion
150         :return:
151         """
152         assert self.cloud
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)
159
160     def _do_vping(self):
161         """
162         Method to be implemented by subclasses
163         Begins the real test after the OpenStack environment has been setup
164         :return: T/F
165         """
166         raise NotImplementedError('vping execution is not implemented')