3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
27 import rift
.auto
.session
28 import rift
.mano
.examples
.ping_pong_nsd
as ping_pong
31 class PackageError(Exception):
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'''
39 @pytest.fixture(scope
='session')
40 def ping_pong_install_dir():
41 '''Fixture containing the location of ping_pong installation
43 install_dir
= os
.path
.join(
44 os
.environ
["RIFT_ROOT"],
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
54 ping_pong_install_dir - location of ping_pong installation
56 ping_pkg_file
= os
.path
.join(
57 ping_pong_install_dir
,
58 "ping_vnfd_with_image.tar.gz",
60 if not os
.path
.exists(ping_pkg_file
):
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
71 ping_pong_install_dir - location of ping_pong installation
73 pong_pkg_file
= os
.path
.join(
74 ping_pong_install_dir
,
75 "pong_vnfd_with_image.tar.gz",
77 if not os
.path
.exists(pong_pkg_file
):
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
88 ping_pong_install_dir - location of ping_pong installation
90 ping_pong_pkg_file
= os
.path
.join(
91 ping_pong_install_dir
,
92 "ping_pong_nsd.tar.gz",
94 if not os
.path
.exists(ping_pong_pkg_file
):
97 return ping_pong_pkg_file
99 @pytest.fixture(scope
='session')
101 ''' Fixture containing a list of directories where images can be found
103 rift_build
= os
.environ
['RIFT_BUILD']
104 rift_root
= os
.environ
['RIFT_ROOT']
108 "modules/core/mano/src/core_mano-build/examples/",
109 "ping_pong_ns/ping_vnfd_with_image/images"
118 @pytest.fixture(scope
='session')
119 def image_paths(image_dirs
):
120 ''' Fixture containing a mapping of image names to their path images
123 image_dirs - a list of directories where images are located
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
})
132 @pytest.fixture(scope
='session')
133 def path_ping_image(image_paths
):
134 ''' Fixture containing the location of the ping image
137 image_paths - mapping of images to their paths
139 return image_paths
["Fedora-x86_64-20-20131211.1-sda-ping.qcow2"]
141 @pytest.fixture(scope
='session')
142 def path_pong_image(image_paths
):
143 ''' Fixture containing the location of the pong image
146 image_paths - mapping of images to their paths
148 return image_paths
["Fedora-x86_64-20-20131211.1-sda-pong.qcow2"]
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
157 def generate_descriptors(self
):
158 '''Return a new set of ping and pong descriptors
161 with
open(path
, mode
='rb') as fd
:
163 for buf
in iter(functools
.partial(fd
.read
, 4096), b
''):
165 return md5
.hexdigest()
167 ping_md5sum
= md5sum(self
.path_ping_image
)
168 pong_md5sum
= md5sum(self
.path_pong_image
)
171 if self
.rsyslog_host
and self
.rsyslog_port
:
174 - "$ActionForwardDefaultTemplate RSYSLOG_ForwardFormat"
175 - "*.* @{host}:{port}"
177 host
=self
.rsyslog_host
,
178 port
=self
.rsyslog_port
,
181 descriptors
= ping_pong
.generate_ping_pong_descriptors(
183 ping_md5sum
=ping_md5sum
,
184 pong_md5sum
=pong_md5sum
,
185 ex_ping_userdata
=ex_userdata
,
186 ex_pong_userdata
=ex_userdata
,
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
195 return PingPongFactory(path_ping_image
, path_pong_image
, rsyslog_host
, rsyslog_port
)
197 @pytest.fixture(scope
='session')
198 def ping_pong_records(ping_pong_factory
):
199 '''Fixture returns the default set of ping_pong descriptors
201 return ping_pong_factory
.generate_descriptors()
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.
210 Tuple: file path for ping vnfd, pong vnfd and ping_pong_nsd
212 ping_vnfd
, pong_vnfd
, ping_pong_nsd
= ping_pong_records
214 tmpdir
= tempfile
.mkdtemp()
215 rift_build
= os
.environ
['RIFT_BUILD']
216 MANO_DIR
= os
.path
.join(
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")
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
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')
234 for descriptor
in [ping_vnfd
, pong_vnfd
, ping_pong_nsd
]:
235 descriptor
.write_to_file(output_format
='xml', outdir
=tmpdir
)
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
))
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
)
246 for dir_name
in [ping_vnfd
.name
, pong_vnfd
.name
, ping_pong_nsd
.name
]:
249 "{rift_install}/usr/rift/toolchain/cmake/bin/generate_descriptor_pkg.sh".format(rift_install
=os
.environ
['RIFT_INSTALL']),
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
)))
257 def haproxy_descriptors():
258 """HAProxy descriptors."""
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")
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()
277 @pytest.fixture(scope
='session')
278 def descriptor_images(request
):
279 def haproxy_images():
280 """HAProxy 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")
289 if request
.config
.option
.network_service
== "haproxy":
290 return haproxy_images()