a1fa4461a40e20669db1dd424fb5b518bf8b2867
[osm/SO.git] / rwlaunchpad / ra / pytest / ns / conftest.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import functools
19 import hashlib
20 import pytest
21 import os
22 import tempfile
23 import shutil
24 import subprocess
25
26 import gi
27 import rift.auto.session
28 import rift.mano.examples.ping_pong_nsd as ping_pong
29 import rift.vcs.vcs
30
31 class PackageError(Exception):
32 pass
33
34 @pytest.fixture(scope='session', autouse=True)
35 def cloud_account_name(request):
36 '''fixture which returns the name used to identify the cloud account'''
37 return 'cloud-0'
38
39 @pytest.fixture(scope='session')
40 def ping_pong_install_dir():
41 '''Fixture containing the location of ping_pong installation
42 '''
43 install_dir = os.path.join(
44 os.environ["RIFT_ROOT"],
45 "images"
46 )
47 return install_dir
48
49 @pytest.fixture(scope='session')
50 def ping_vnfd_package_file(ping_pong_install_dir):
51 '''Fixture containing the location of the ping vnfd package
52
53 Arguments:
54 ping_pong_install_dir - location of ping_pong installation
55 '''
56 ping_pkg_file = os.path.join(
57 ping_pong_install_dir,
58 "ping_vnfd_with_image.tar.gz",
59 )
60 if not os.path.exists(ping_pkg_file):
61 raise_package_error()
62
63 return ping_pkg_file
64
65
66 @pytest.fixture(scope='session')
67 def pong_vnfd_package_file(ping_pong_install_dir):
68 '''Fixture containing the location of the pong vnfd package
69
70 Arguments:
71 ping_pong_install_dir - location of ping_pong installation
72 '''
73 pong_pkg_file = os.path.join(
74 ping_pong_install_dir,
75 "pong_vnfd_with_image.tar.gz",
76 )
77 if not os.path.exists(pong_pkg_file):
78 raise_package_error()
79
80 return pong_pkg_file
81
82
83 @pytest.fixture(scope='session')
84 def ping_pong_nsd_package_file(ping_pong_install_dir):
85 '''Fixture containing the location of the ping_pong_nsd package
86
87 Arguments:
88 ping_pong_install_dir - location of ping_pong installation
89 '''
90 ping_pong_pkg_file = os.path.join(
91 ping_pong_install_dir,
92 "ping_pong_nsd.tar.gz",
93 )
94 if not os.path.exists(ping_pong_pkg_file):
95 raise_package_error()
96
97 return ping_pong_pkg_file
98
99 @pytest.fixture(scope='session')
100 def image_dirs():
101 ''' Fixture containing a list of directories where images can be found
102 '''
103 rift_build = os.environ['RIFT_BUILD']
104 rift_root = os.environ['RIFT_ROOT']
105 image_dirs = [
106 os.path.join(
107 rift_build,
108 "modules/core/mano/src/core_mano-build/examples/",
109 "ping_pong_ns/ping_vnfd_with_image/images"
110 ),
111 os.path.join(
112 rift_root,
113 "images"
114 )
115 ]
116 return image_dirs
117
118 @pytest.fixture(scope='session')
119 def image_paths(image_dirs):
120 ''' Fixture containing a mapping of image names to their path images
121
122 Arguments:
123 image_dirs - a list of directories where images are located
124 '''
125 image_paths = {}
126 for image_dir in image_dirs:
127 if os.path.exists(image_dir):
128 names = os.listdir(image_dir)
129 image_paths.update({name:os.path.join(image_dir, name) for name in names})
130 return image_paths
131
132 @pytest.fixture(scope='session')
133 def path_ping_image(image_paths):
134 ''' Fixture containing the location of the ping image
135
136 Arguments:
137 image_paths - mapping of images to their paths
138 '''
139 return image_paths["Fedora-x86_64-20-20131211.1-sda-ping.qcow2"]
140
141 @pytest.fixture(scope='session')
142 def path_pong_image(image_paths):
143 ''' Fixture containing the location of the pong image
144
145 Arguments:
146 image_paths - mapping of images to their paths
147 '''
148 return image_paths["Fedora-x86_64-20-20131211.1-sda-pong.qcow2"]
149
150 class PingPongFactory:
151 def __init__(self, path_ping_image, path_pong_image, rsyslog_host, rsyslog_port):
152 self.path_ping_image = path_ping_image
153 self.path_pong_image = path_pong_image
154 self.rsyslog_host = rsyslog_host
155 self.rsyslog_port = rsyslog_port
156
157 def generate_descriptors(self):
158 '''Return a new set of ping and pong descriptors
159 '''
160 def md5sum(path):
161 with open(path, mode='rb') as fd:
162 md5 = hashlib.md5()
163 for buf in iter(functools.partial(fd.read, 4096), b''):
164 md5.update(buf)
165 return md5.hexdigest()
166
167 ping_md5sum = md5sum(self.path_ping_image)
168 pong_md5sum = md5sum(self.path_pong_image)
169
170 ex_userdata = None
171 if self.rsyslog_host and self.rsyslog_port:
172 ex_userdata = '''
173 rsyslog:
174 - "$ActionForwardDefaultTemplate RSYSLOG_ForwardFormat"
175 - "*.* @{host}:{port}"
176 '''.format(
177 host=self.rsyslog_host,
178 port=self.rsyslog_port,
179 )
180
181 descriptors = ping_pong.generate_ping_pong_descriptors(
182 pingcount=1,
183 ping_md5sum=ping_md5sum,
184 pong_md5sum=pong_md5sum,
185 ex_ping_userdata=ex_userdata,
186 ex_pong_userdata=ex_userdata,
187 )
188
189 return descriptors
190
191 @pytest.fixture(scope='session')
192 def ping_pong_factory(path_ping_image, path_pong_image, rsyslog_host, rsyslog_port):
193 '''Fixture returns a factory capable of generating ping and pong descriptors
194 '''
195 return PingPongFactory(path_ping_image, path_pong_image, rsyslog_host, rsyslog_port)
196
197 @pytest.fixture(scope='session')
198 def ping_pong_records(ping_pong_factory):
199 '''Fixture returns the default set of ping_pong descriptors
200 '''
201 return ping_pong_factory.generate_descriptors()
202
203
204 @pytest.fixture(scope='session')
205 def descriptors(request, ping_pong_records):
206 def pingpong_descriptors(with_images=True):
207 """Generated the VNFDs & NSD files for pingpong NS.
208
209 Returns:
210 Tuple: file path for ping vnfd, pong vnfd and ping_pong_nsd
211 """
212 ping_vnfd, pong_vnfd, ping_pong_nsd = ping_pong_records
213
214 tmpdir = tempfile.mkdtemp()
215 rift_build = os.environ['RIFT_BUILD']
216 MANO_DIR = os.path.join(
217 rift_build,
218 "modules/core/mano/src/core_mano-build/examples/ping_pong_ns")
219 ping_img = os.path.join(MANO_DIR, "ping_vnfd_with_image/images/Fedora-x86_64-20-20131211.1-sda-ping.qcow2")
220 pong_img = os.path.join(MANO_DIR, "pong_vnfd_with_image/images/Fedora-x86_64-20-20131211.1-sda-pong.qcow2")
221
222 """ grab cached copies of these files if not found. They may not exist
223 because our git submodule dependency mgmt
224 will not populate these because they live in .build, not .install
225 """
226 if not os.path.exists(ping_img):
227 ping_img = os.path.join(
228 os.environ['RIFT_ROOT'],
229 'images/Fedora-x86_64-20-20131211.1-sda-ping.qcow2')
230 pong_img = os.path.join(
231 os.environ['RIFT_ROOT'],
232 'images/Fedora-x86_64-20-20131211.1-sda-pong.qcow2')
233
234 for descriptor in [ping_vnfd, pong_vnfd, ping_pong_nsd]:
235 descriptor.write_to_file(output_format='xml', outdir=tmpdir)
236
237 ping_img_path = os.path.join(tmpdir, "{}/images/".format(ping_vnfd.name))
238 pong_img_path = os.path.join(tmpdir, "{}/images/".format(pong_vnfd.name))
239
240 if with_images:
241 os.makedirs(ping_img_path)
242 os.makedirs(pong_img_path)
243 shutil.copy(ping_img, ping_img_path)
244 shutil.copy(pong_img, pong_img_path)
245
246 for dir_name in [ping_vnfd.name, pong_vnfd.name, ping_pong_nsd.name]:
247 subprocess.call([
248 "sh",
249 "{rift_install}/usr/rift/toolchain/cmake/bin/generate_descriptor_pkg.sh".format(rift_install=os.environ['RIFT_INSTALL']),
250 tmpdir,
251 dir_name])
252
253 return (os.path.join(tmpdir, "{}.tar.gz".format(ping_vnfd.name)),
254 os.path.join(tmpdir, "{}.tar.gz".format(pong_vnfd.name)),
255 os.path.join(tmpdir, "{}.tar.gz".format(ping_pong_nsd.name)))
256
257 def haproxy_descriptors():
258 """HAProxy descriptors."""
259 files = [
260 os.path.join(os.getenv('RIFT_BUILD'), "modules/ext/vnfs/src/ext_vnfs-build/http_client/http_client_vnfd.tar.gz"),
261 os.path.join(os.getenv('RIFT_BUILD'), "modules/ext/vnfs/src/ext_vnfs-build/httpd/httpd_vnfd.tar.gz"),
262 os.path.join(os.getenv('RIFT_BUILD'), "modules/ext/vnfs/src/ext_vnfs-build/haproxy/haproxy_vnfd.tar.gz"),
263 os.path.join(os.getenv('RIFT_BUILD'), "modules/ext/vnfs/src/ext_vnfs-build/waf/waf_vnfd.tar.gz"),
264 os.path.join(os.getenv('RIFT_BUILD'), "modules/ext/vnfs/src/ext_vnfs-build/haproxy_waf_httpd_nsd/haproxy_waf_httpd_nsd.tar.gz")
265 ]
266
267 return files
268
269 if request.config.option.network_service == "pingpong":
270 return pingpong_descriptors()
271 elif request.config.option.network_service == "pingpong_noimg":
272 return pingpong_descriptors(with_images=False)
273 elif request.config.option.network_service == "haproxy":
274 return haproxy_descriptors()
275
276
277 @pytest.fixture(scope='session')
278 def descriptor_images(request):
279 def haproxy_images():
280 """HAProxy images."""
281 images = [
282 os.path.join(os.getenv('RIFT_ROOT'), "images/haproxy-v03.qcow2"),
283 os.path.join(os.getenv('RIFT_ROOT'), "images/web-app-firewall-v02.qcow2"),
284 os.path.join(os.getenv('RIFT_ROOT'), "images/web-server-v02.qcow2")
285 ]
286
287 return images
288
289 if request.config.option.network_service == "haproxy":
290 return haproxy_images()
291
292 return []