Simplify vping_ssh
[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         self.sec1 = None
49
50     def run(self, **kwargs):  # pylint: disable=too-many-locals
51         """
52         Begins the test execution which should originate from the subclass
53         """
54         assert self.cloud
55         assert self.ext_net
56         self.logger.info('Begin virtual environment setup')
57
58         self.start_time = time.time()
59         self.logger.info(
60             "vPing Start Time:'%s'",
61             datetime.fromtimestamp(self.start_time).strftime(
62                 '%Y-%m-%d %H:%M:%S'))
63
64         image_base_name = '{}-{}'.format(
65             getattr(config.CONF, 'vping_image_name'), self.guid)
66         self.logger.info("Creating image with name: '%s'", image_base_name)
67         self.image = self.cloud.create_image(
68             image_base_name,
69             filename=getattr(config.CONF, 'openstack_image_url'))
70         self.logger.debug("image: %s", self.image)
71
72         private_net_name = getattr(
73             config.CONF, 'vping_private_net_name') + self.guid
74         private_subnet_name = str(getattr(
75             config.CONF, 'vping_private_subnet_name') + self.guid)
76         private_subnet_cidr = getattr(config.CONF, 'vping_private_subnet_cidr')
77
78         provider = {}
79         if hasattr(config.CONF, 'vping_network_type'):
80             provider["network_type"] = getattr(
81                 config.CONF, 'vping_network_type')
82         if hasattr(config.CONF, 'vping_physical_network'):
83             provider["physical_network"] = getattr(
84                 config.CONF, 'vping_physical_network')
85         if hasattr(config.CONF, 'vping_segmentation_id'):
86             provider["segmentation_id"] = getattr(
87                 config.CONF, 'vping_segmentation_id')
88         self.logger.info(
89             "Creating network with name: '%s'", private_net_name)
90         self.network = self.cloud.create_network(
91             private_net_name,
92             provider=provider)
93         self.logger.debug("network: %s", self.network)
94
95         self.subnet = self.cloud.create_subnet(
96             self.network.id,
97             subnet_name=private_subnet_name,
98             cidr=private_subnet_cidr,
99             enable_dhcp=True,
100             dns_nameservers=[env.get('NAMESERVER')])
101         self.logger.debug("subnet: %s", self.subnet)
102
103         router_name = getattr(config.CONF, 'vping_router_name') + self.guid
104         self.logger.info("Creating router with name: '%s'", router_name)
105         self.router = self.cloud.create_router(
106             name=router_name,
107             ext_gateway_net_id=self.ext_net.id)
108         self.logger.debug("router: %s", self.router)
109         self.cloud.add_router_interface(self.router, subnet_id=self.subnet.id)
110
111         flavor_name = 'vping-flavor' + self.guid
112         self.logger.info(
113             "Creating flavor with name: '%s'", flavor_name)
114         self.flavor = self.cloud.create_flavor(
115             flavor_name, getattr(config.CONF, 'openstack_flavor_ram'),
116             getattr(config.CONF, 'openstack_flavor_vcpus'),
117             getattr(config.CONF, 'openstack_flavor_disk'))
118         self.logger.debug("flavor: %s", self.flavor)
119         self.cloud.set_flavor_specs(
120             self.flavor.id, getattr(config.CONF, 'flavor_extra_specs', {}))
121
122         self.sec1 = self.cloud.create_security_group(
123             getattr(config.CONF, 'vping_sg_name') + self.guid,
124             getattr(config.CONF, 'vping_sg_desc'))
125         self.cloud.create_security_group_rule(
126             self.sec1.id, protocol='icmp', direction='ingress')
127
128         vm1_name = getattr(config.CONF, 'vping_vm_name_1') + self.guid
129         self.logger.info(
130             "Creating VM 1 instance with name: '%s'", vm1_name)
131         self.vm1 = self.cloud.create_server(
132             vm1_name, image=self.image.id,
133             flavor=self.flavor.id,
134             auto_ip=False, wait=True,
135             timeout=getattr(config.CONF, 'vping_vm_boot_timeout'),
136             network=self.network.id,
137             security_groups=[self.sec1.id])
138         self.logger.debug("vm1: %s", self.vm1)
139         self.vm1 = self.cloud.wait_for_server(self.vm1, auto_ip=False)
140         p_console = self.cloud.get_server_console(self.vm1.id)
141         self.logger.debug("vm1 console: \n%s", p_console)
142
143     def _execute(self):
144         """
145         Method called by subclasses after environment has been setup
146         :return: the exit code
147         """
148         self.logger.info('Begin test execution')
149         result = self._do_vping()
150         self.stop_time = time.time()
151         if result != testcase.TestCase.EX_OK:
152             self.result = 0
153             return testcase.TestCase.EX_RUN_ERROR
154         self.result = 100
155         return testcase.TestCase.EX_OK
156
157     def clean(self):
158         """
159         Cleanup all OpenStack objects. Should be called on completion
160         :return:
161         """
162         assert self.cloud
163         self.cloud.delete_server(self.vm1, wait=True)
164         self.cloud.delete_security_group(self.sec1.id)
165         self.cloud.delete_image(self.image)
166         self.cloud.remove_router_interface(self.router, self.subnet.id)
167         self.cloud.delete_router(self.router.id)
168         self.cloud.delete_network(self.network.id)
169         self.cloud.delete_flavor(self.flavor.id)
170
171     def _do_vping(self):
172         """
173         Method to be implemented by subclasses
174         Begins the real test after the OpenStack environment has been setup
175         :return: T/F
176         """
177         raise NotImplementedError('vping execution is not implemented')