3 # Copyright (c) 2018 Enea AB 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 cinder_test"""
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 CinderBase(testcase.TestCase):
29 Base class for CinderCheck tests that check volume data persistence
30 between two VMs shared internal network.
31 This class is responsible for creating the image, internal network,
34 # pylint: disable=too-many-instance-attributes
36 def __init__(self, **kwargs):
37 super(CinderBase, self).__init__(**kwargs)
38 self.logger = logging.getLogger(__name__)
39 self.cloud = os_client_config.make_shade()
40 self.ext_net = functest_utils.get_external_network(self.cloud)
41 self.logger.debug("ext_net: %s", self.ext_net)
42 self.guid = '-' + str(uuid.uuid4())
51 def run(self, **kwargs): # pylint: disable=too-many-locals
53 Begins the test execution which should originate from the subclass
57 self.logger.info('Begin virtual environment setup')
59 self.start_time = time.time()
61 "CinderCheck Start Time:'%s'",
62 datetime.fromtimestamp(self.start_time).strftime(
65 image_base_name = '{}-{}'.format(
66 getattr(config.CONF, 'cinder_image_name'), self.guid)
67 self.logger.info("Creating image with name: '%s'", image_base_name)
68 meta = getattr(config.CONF, 'openstack_extra_properties', None)
69 self.logger.info("Image metadata: %s", meta)
70 self.image = self.cloud.create_image(
72 filename=getattr(config.CONF, 'openstack_image_url'),
74 self.logger.debug("image: %s", self.image)
76 private_net_name = getattr(
77 config.CONF, 'cinder_private_net_name') + self.guid
78 private_subnet_name = str(getattr(
79 config.CONF, 'cinder_private_subnet_name') + self.guid)
80 private_subnet_cidr = getattr(
81 config.CONF, 'cinder_private_subnet_cidr')
84 if hasattr(config.CONF, 'cinder_network_type'):
85 provider["network_type"] = getattr(
86 config.CONF, 'cinder_network_type')
87 if hasattr(config.CONF, 'cinder_physical_network'):
88 provider["physical_network"] = getattr(
89 config.CONF, 'cinder_physical_network')
90 if hasattr(config.CONF, 'cinder_segmentation_id'):
91 provider["segmentation_id"] = getattr(
92 config.CONF, 'cinder_segmentation_id')
94 "Creating network with name: '%s'", private_net_name)
95 self.network = self.cloud.create_network(
98 self.logger.debug("network: %s", self.network)
100 self.subnet = self.cloud.create_subnet(
102 subnet_name=private_subnet_name,
103 cidr=private_subnet_cidr,
105 dns_nameservers=[env.get('NAMESERVER')])
106 self.logger.debug("subnet: %s", self.subnet)
108 router_name = getattr(config.CONF, 'cinder_router_name') + self.guid
109 self.logger.info("Creating router with name: '%s'", router_name)
110 self.router = self.cloud.create_router(
112 ext_gateway_net_id=self.ext_net.id)
113 self.logger.debug("router: %s", self.router)
114 self.cloud.add_router_interface(self.router, subnet_id=self.subnet.id)
116 flavor_name = 'cinder-flavor' + self.guid
118 "Creating flavor with name: '%s'", flavor_name)
119 self.flavor = self.cloud.create_flavor(
120 flavor_name, getattr(config.CONF, 'openstack_flavor_ram'),
121 getattr(config.CONF, 'openstack_flavor_vcpus'),
122 getattr(config.CONF, 'openstack_flavor_disk'))
123 self.logger.debug("flavor: %s", self.flavor)
124 self.cloud.set_flavor_specs(
125 self.flavor.id, getattr(config.CONF, 'flavor_extra_specs', {}))
126 volume_name = 'cinder-volume' + self.guid
128 "Creating volume with name: %s", volume_name)
129 self.volume = self.cloud.create_volume(
130 name=volume_name, size='2')
131 self.logger.info("volume: %s", self.volume)
135 Method called by subclasses after environment has been setup
136 :return: the exit code
138 self.logger.info('Begin test execution')
139 self.stop_time = time.time()
140 write_data = self.write_data()
141 if write_data == testcase.TestCase.EX_OK:
142 result = self.read_data()
144 result = testcase.TestCase.EX_RUN_ERROR
145 if result != testcase.TestCase.EX_OK:
147 return testcase.TestCase.EX_RUN_ERROR
149 return testcase.TestCase.EX_OK
153 Cleanup all OpenStack objects. Should be called on completion
157 self.cloud.delete_image(self.image)
158 self.cloud.remove_router_interface(self.router, self.subnet.id)
159 self.cloud.delete_router(self.router.id)
160 self.cloud.delete_network(self.network.id)
161 self.cloud.delete_flavor(self.flavor.id)
162 self.cloud.delete_volume(self.volume.id)
164 def write_data(self):
166 Method to be implemented by subclasses
167 Begins the real test after the OpenStack environment has been setup
170 raise NotImplementedError('cinder test execution is not implemented')
174 Method to be implemented by subclasses
175 Begins the real test after the OpenStack environment has been setup
178 raise NotImplementedError('cinder test execution is not implemented')