| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | # |
| 4 | # Copyright 2016 RIFT.IO Inc |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
| 19 | @file lp_test.py |
| 20 | @author Austin Cormier (Austin.Cormier@riftio.com) |
| 21 | @date 10/15/2015 |
| 22 | @brief Launchpad Module Test |
| 23 | """ |
| 24 | |
| 25 | import json |
| 26 | import logging |
| 27 | import os |
| 28 | import pytest |
| 29 | import shlex |
| 30 | import requests |
| 31 | import subprocess |
| 32 | import time |
| 33 | import uuid |
| 34 | import datetime |
| 35 | |
| 36 | import gi |
| 37 | gi.require_version('RwBaseYang', '1.0') |
| 38 | gi.require_version('RwCloudYang', '1.0') |
| 39 | gi.require_version('RwIwpYang', '1.0') |
| 40 | gi.require_version('RwlogMgmtYang', '1.0') |
| 41 | gi.require_version('RwNsmYang', '1.0') |
| 42 | gi.require_version('RwNsmYang', '1.0') |
| 43 | gi.require_version('RwResourceMgrYang', '1.0') |
| 44 | gi.require_version('RwConmanYang', '1.0') |
| 45 | gi.require_version('RwVnfdYang', '1.0') |
| 46 | |
| 47 | from gi.repository import ( |
| 48 | NsdYang, |
| 49 | NsrYang, |
| 50 | RwBaseYang, |
| 51 | RwCloudYang, |
| 52 | RwIwpYang, |
| 53 | RwlogMgmtYang, |
| 54 | RwNsmYang, |
| 55 | RwNsrYang, |
| 56 | RwResourceMgrYang, |
| 57 | RwConmanYang, |
| 58 | RwVnfdYang, |
| 59 | VldYang, |
| 60 | ) |
| 61 | |
| 62 | logging.basicConfig(level=logging.DEBUG) |
| 63 | |
| 64 | |
| 65 | RW_PING_PONG_PKG_INSTALL_DIR = os.path.join( |
| 66 | os.environ["RIFT_ROOT"], |
| 67 | "images" |
| 68 | ) |
| 69 | |
| 70 | class PackageError(Exception): |
| 71 | pass |
| 72 | |
| 73 | |
| 74 | def raise_package_error(): |
| 75 | raise PackageError("Could not find ns packages") |
| 76 | |
| 77 | |
| 78 | @pytest.fixture(scope='module') |
| 79 | def iwp_proxy(request, mgmt_session): |
| 80 | return mgmt_session.proxy(RwIwpYang) |
| 81 | |
| 82 | |
| 83 | @pytest.fixture(scope='module') |
| 84 | def rwlog_mgmt_proxy(request, mgmt_session): |
| 85 | return mgmt_session.proxy(RwlogMgmtYang) |
| 86 | |
| 87 | |
| 88 | @pytest.fixture(scope='module') |
| 89 | def resource_mgr_proxy(request, mgmt_session): |
| 90 | return mgmt_session.proxy(RwResourceMgrYang) |
| 91 | |
| 92 | |
| 93 | @pytest.fixture(scope='module') |
| 94 | def cloud_proxy(request, mgmt_session): |
| 95 | return mgmt_session.proxy(RwCloudYang) |
| 96 | |
| 97 | |
| 98 | @pytest.fixture(scope='module') |
| 99 | def vnfd_proxy(request, mgmt_session): |
| 100 | return mgmt_session.proxy(RwVnfdYang) |
| 101 | |
| 102 | |
| 103 | @pytest.fixture(scope='module') |
| 104 | def vld_proxy(request, mgmt_session): |
| 105 | return mgmt_session.proxy(VldYang) |
| 106 | |
| 107 | |
| 108 | @pytest.fixture(scope='module') |
| 109 | def nsd_proxy(request, mgmt_session): |
| 110 | return mgmt_session.proxy(NsdYang) |
| 111 | |
| 112 | |
| 113 | @pytest.fixture(scope='module') |
| 114 | def nsr_proxy(request, mgmt_session): |
| 115 | return mgmt_session.proxy(NsrYang) |
| 116 | |
| 117 | |
| 118 | @pytest.fixture(scope='module') |
| 119 | def rwnsr_proxy(request, mgmt_session): |
| 120 | return mgmt_session.proxy(RwNsrYang) |
| 121 | |
| 122 | |
| 123 | @pytest.fixture(scope='module') |
| 124 | def base_proxy(request, mgmt_session): |
| 125 | return mgmt_session.proxy(RwBaseYang) |
| 126 | |
| 127 | |
| 128 | @pytest.fixture(scope='module') |
| 129 | def so_proxy(request, mgmt_session): |
| 130 | return mgmt_session.proxy(RwConmanYang) |
| 131 | |
| 132 | |
| 133 | @pytest.fixture(scope='module') |
| 134 | def nsm_proxy(request, mgmt_session): |
| 135 | return mgmt_session.proxy(RwNsmYang) |
| 136 | |
| 137 | |
| 138 | @pytest.fixture(scope='session') |
| 139 | def ping_vnfd_package_file(): |
| 140 | ping_pkg_file = os.path.join( |
| 141 | RW_PING_PONG_PKG_INSTALL_DIR, |
| 142 | "ping_vnfd_with_image.tar.gz", |
| 143 | ) |
| 144 | if not os.path.exists(ping_pkg_file): |
| 145 | raise_package_error() |
| 146 | |
| 147 | return ping_pkg_file |
| 148 | |
| 149 | |
| 150 | @pytest.fixture(scope='session') |
| 151 | def pong_vnfd_package_file(): |
| 152 | pong_pkg_file = os.path.join( |
| 153 | RW_PING_PONG_PKG_INSTALL_DIR, |
| 154 | "pong_vnfd_with_image.tar.gz", |
| 155 | ) |
| 156 | if not os.path.exists(pong_pkg_file): |
| 157 | raise_package_error() |
| 158 | |
| 159 | return pong_pkg_file |
| 160 | |
| 161 | |
| 162 | @pytest.fixture(scope='session') |
| 163 | def ping_pong_nsd_package_file(): |
| 164 | ping_pong_pkg_file = os.path.join( |
| 165 | RW_PING_PONG_PKG_INSTALL_DIR, |
| 166 | "ping_pong_nsd.tar.gz", |
| 167 | ) |
| 168 | if not os.path.exists(ping_pong_pkg_file): |
| 169 | raise_package_error() |
| 170 | |
| 171 | return ping_pong_pkg_file |
| 172 | |
| 173 | |
| 174 | def create_nsr_from_nsd_id(nsd_id): |
| 175 | nsr = RwNsrYang.YangData_Nsr_NsInstanceConfig_Nsr() |
| 176 | nsr.id = str(uuid.uuid4()) |
| 177 | nsr.name = "pingpong_{}".format(datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) |
| 178 | nsr.short_name = "nsr_short_name" |
| 179 | nsr.description = "This is a description" |
| 180 | nsr.nsd_ref = nsd_id |
| 181 | nsr.admin_status = "ENABLED" |
| 182 | nsr.cloud_account = "openstack" |
| 183 | |
| 184 | param = NsrYang.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter() |
| 185 | param.xpath = '/nsd:nsd-catalog/nsd:nsd/nsd:vendor' |
| 186 | param.value = "rift-o-matic" |
| 187 | |
| 188 | nsr.input_parameter.append(param) |
| 189 | |
| 190 | return nsr |
| 191 | |
| 192 | |
| 193 | def upload_descriptor(logger, descriptor_file, host="127.0.0.1"): |
| 194 | curl_cmd = 'curl -F "descriptor=@{file}" http://{host}:4567/api/upload'.format( |
| 195 | file=descriptor_file, |
| 196 | host=host, |
| 197 | ) |
| 198 | logger.debug("Uploading descriptor %s using cmd: %s", descriptor_file, curl_cmd) |
| 199 | stdout = subprocess.check_output(shlex.split(curl_cmd), universal_newlines=True) |
| 200 | |
| 201 | json_out = json.loads(stdout) |
| 202 | transaction_id = json_out["transaction_id"] |
| 203 | |
| 204 | return transaction_id |
| 205 | |
| 206 | |
| 207 | class DescriptorOnboardError(Exception): |
| 208 | pass |
| 209 | |
| 210 | |
| 211 | def wait_unboard_transaction_finished(logger, transaction_id, timeout_secs=600, host="127.0.0.1"): |
| 212 | logger.info("Waiting for onboard trans_id %s to complete", |
| 213 | transaction_id) |
| 214 | start_time = time.time() |
| 215 | while (time.time() - start_time) < timeout_secs: |
| 216 | r = requests.get( |
| 217 | 'http://{host}:4567/api/upload/{t_id}/state'.format( |
| 218 | host=host, t_id=transaction_id |
| 219 | ) |
| 220 | ) |
| 221 | state = r.json() |
| 222 | if state["status"] == "pending": |
| 223 | time.sleep(1) |
| 224 | continue |
| 225 | |
| 226 | elif state["status"] == "success": |
| 227 | logger.info("Descriptor onboard was successful") |
| 228 | return |
| 229 | |
| 230 | else: |
| 231 | raise DescriptorOnboardError(state) |
| 232 | |
| 233 | if state["status"] != "success": |
| 234 | raise DescriptorOnboardError(state) |
| 235 | |
| 236 | |
| 237 | @pytest.mark.incremental |
| 238 | class TestLaunchpadStartStop(object): |
| 239 | def test_configure_logging(self, rwlog_mgmt_proxy): |
| 240 | logging = RwlogMgmtYang.Logging.from_dict({ |
| 241 | "console": { |
| 242 | "on": True, |
| 243 | "filter": { |
| 244 | "category": [{ |
| 245 | "name": "rw-generic", |
| 246 | "severity": "error" |
| 247 | }], |
| 248 | } |
| 249 | } |
| 250 | }) |
| 251 | rwlog_mgmt_proxy.merge_config("/rwlog-mgmt:logging", logging) |
| 252 | |
| 253 | def test_configure_cloud_account(self, cloud_proxy, logger): |
| 254 | cloud_account = RwCloudYang.CloudAccount() |
| 255 | # cloud_account.name = "cloudsim_proxy" |
| 256 | # cloud_account.account_type = "cloudsim_proxy" |
| 257 | cloud_account.name = "openstack" |
| 258 | cloud_account.account_type = "openstack" |
| 259 | cloud_account.openstack.key = 'pluto' |
| 260 | cloud_account.openstack.secret = 'mypasswd' |
| 261 | cloud_account.openstack.auth_url = 'http://10.96.4.2:5000/v3/' |
| 262 | cloud_account.openstack.tenant = 'mano1' |
| 263 | cloud_account.openstack.mgmt_network = 'private1' |
| 264 | |
| 265 | cloud_proxy.merge_config("/rw-cloud:cloud/account", cloud_account) |
| 266 | |
| 267 | def test_onboard_ping_vnfd(self, logger, vnfd_proxy, ping_vnfd_package_file): |
| 268 | logger.info("Onboarding ping_vnfd package: %s", ping_vnfd_package_file) |
| 269 | trans_id = upload_descriptor(logger, ping_vnfd_package_file) |
| 270 | wait_unboard_transaction_finished(logger, trans_id) |
| 271 | |
| 272 | catalog = vnfd_proxy.get_config('/vnfd-catalog') |
| 273 | vnfds = catalog.vnfd |
| 274 | assert len(vnfds) == 1, "There should only be a single vnfd" |
| 275 | vnfd = vnfds[0] |
| 276 | assert vnfd.name == "ping_vnfd" |
| 277 | |
| 278 | def test_onboard_pong_vnfd(self, logger, vnfd_proxy, pong_vnfd_package_file): |
| 279 | logger.info("Onboarding pong_vnfd package: %s", pong_vnfd_package_file) |
| 280 | trans_id = upload_descriptor(logger, pong_vnfd_package_file) |
| 281 | wait_unboard_transaction_finished(logger, trans_id) |
| 282 | |
| 283 | catalog = vnfd_proxy.get_config('/vnfd-catalog') |
| 284 | vnfds = catalog.vnfd |
| 285 | assert len(vnfds) == 2, "There should be two vnfds" |
| 286 | assert "pong_vnfd" in [vnfds[0].name, vnfds[1].name] |
| 287 | |
| 288 | def test_onboard_ping_pong_nsd(self, logger, nsd_proxy, ping_pong_nsd_package_file): |
| 289 | logger.info("Onboarding ping_pong_nsd package: %s", ping_pong_nsd_package_file) |
| 290 | trans_id = upload_descriptor(logger, ping_pong_nsd_package_file) |
| 291 | wait_unboard_transaction_finished(logger, trans_id) |
| 292 | |
| 293 | catalog = nsd_proxy.get_config('/nsd-catalog') |
| 294 | nsds = catalog.nsd |
| 295 | assert len(nsds) == 1, "There should only be a single nsd" |
| 296 | nsd = nsds[0] |
| 297 | assert nsd.name == "ping_pong_nsd" |
| 298 | |
| 299 | def test_instantiate_ping_pong_nsr(self, logger, nsd_proxy, nsr_proxy, rwnsr_proxy, base_proxy): |
| 300 | catalog = nsd_proxy.get_config('/nsd-catalog') |
| 301 | nsd = catalog.nsd[0] |
| 302 | |
| 303 | nsr = create_nsr_from_nsd_id(nsd.id) |
| 304 | rwnsr_proxy.merge_config('/ns-instance-config', nsr) |
| 305 | |
| 306 | nsr_opdata = rwnsr_proxy.get('/ns-instance-opdata') |
| 307 | nsrs = nsr_opdata.nsr |
| 308 | assert len(nsrs) == 1 |
| 309 | assert nsrs[0].ns_instance_config_ref == nsr.id |
| 310 | |
| 311 | # logger.info("Waiting up to 30 seconds for ping and pong components to show " |
| 312 | # "up in show tasklet info") |
| 313 | |
| 314 | # start_time = time.time() |
| 315 | # while (time.time() - start_time) < 30: |
| 316 | # vcs_info = base_proxy.get('/vcs/info') |
| 317 | # components = vcs_info.components.component_info |
| 318 | |
| 319 | # def find_component_by_name(name): |
| 320 | # for component in components: |
| 321 | # if name in component.component_name: |
| 322 | # return component |
| 323 | |
| 324 | # logger.warning("Did not find %s component name in show tasklet info", |
| 325 | # name) |
| 326 | |
| 327 | # return None |
| 328 | |
| 329 | # """ |
| 330 | # ping_cluster_component = find_component_by_name( |
| 331 | # "rw_ping_vnfd:rwping_cluster" |
| 332 | # ) |
| 333 | # if ping_cluster_component is None: |
| 334 | # continue |
| 335 | |
| 336 | # pong_cluster_component = find_component_by_name( |
| 337 | # "rw_pong_vnfd:rwpong_cluster" |
| 338 | # ) |
| 339 | # if pong_cluster_component is None: |
| 340 | # continue |
| 341 | # """ |
| 342 | |
| 343 | # ping_vm_component = find_component_by_name( |
| 344 | # "rw_ping_vnfd:rwping_vm" |
| 345 | # ) |
| 346 | # if ping_vm_component is None: |
| 347 | # continue |
| 348 | |
| 349 | # pong_vm_component = find_component_by_name( |
| 350 | # "rw_pong_vnfd:rwpong_vm" |
| 351 | # ) |
| 352 | # if pong_vm_component is None: |
| 353 | # continue |
| 354 | |
| 355 | # ping_proc_component = find_component_by_name( |
| 356 | # "rw_ping_vnfd:rwping_proc" |
| 357 | # ) |
| 358 | # if ping_proc_component is None: |
| 359 | # continue |
| 360 | |
| 361 | # pong_proc_component = find_component_by_name( |
| 362 | # "rw_pong_vnfd:rwpong_proc" |
| 363 | # ) |
| 364 | # if pong_proc_component is None: |
| 365 | # continue |
| 366 | |
| 367 | # ping_tasklet_component = find_component_by_name( |
| 368 | # "rw_ping_vnfd:rwping_tasklet" |
| 369 | # ) |
| 370 | # if ping_tasklet_component is None: |
| 371 | # continue |
| 372 | |
| 373 | # pong_tasklet_component = find_component_by_name( |
| 374 | # "rw_pong_vnfd:rwpong_tasklet" |
| 375 | # ) |
| 376 | # if pong_tasklet_component is None: |
| 377 | # continue |
| 378 | |
| 379 | # logger.info("TEST SUCCESSFUL: All ping and pong components were found in show tasklet info") |
| 380 | # break |
| 381 | |
| 382 | # else: |
| 383 | # assert False, "Did not find all ping and pong component in time" |
| 384 | |
| 385 | #def test_terminate_ping_pong_ns(self, logger, nsd_proxy, nsr_proxy, rwnsr_proxy, base_proxy): |
| 386 | # nsr_configs = nsr_proxy.get_config('/ns-instance-config') |
| 387 | # nsr = nsr_configs.nsr[0] |
| 388 | # nsr_id = nsr.id |
| 389 | |
| 390 | # nsr_configs = nsr_proxy.delete_config("/ns-instance-config/nsr[id='{}']".format(nsr_id)) |