| tierno | f8fa9f0 | 2016-12-28 09:11:15 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | ## |
| 5 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 6 | # This file is part of openmano |
| 7 | # All Rights Reserved. |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | # For those usages not covered by the Apache License, Version 2.0 please |
| 22 | # contact with: nfvlabs@tid.es |
| 23 | ## |
| 24 | |
| 25 | ''' |
| 26 | Module to test openmanoclient class and indirectly the whole openmano |
| 27 | It allows both python 2 and python 3 |
| 28 | ''' |
| 29 | __author__="Alfonso Tierno" |
| 30 | __date__ ="$09-Mar-2016 09:09:48$" |
| 31 | __version__="0.0.2" |
| 32 | version_date="May 2016" |
| 33 | |
| 34 | import logging |
| 35 | import imp |
| 36 | |
| 37 | |
| 38 | |
| 39 | def _get_random_name(maxLength): |
| 40 | '''generates a string with random craracters from space (ASCCI 32) to ~(ASCCI 126) |
| 41 | with a random length up to maxLength |
| 42 | ''' |
| 43 | long_name = "testing up to {} size name: ".format(maxLength) |
| 44 | #long_name += ''.join(chr(random.randint(32,126)) for _ in range(random.randint(20, maxLength-len(long_name)))) |
| 45 | long_name += ''.join(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ') for _ in range(20, maxLength-len(long_name))) |
| 46 | return long_name |
| 47 | |
| 48 | |
| 49 | if __name__=="__main__": |
| 50 | import getopt |
| 51 | #import os |
| 52 | import sys |
| 53 | |
| 54 | |
| 55 | |
| 56 | usage =\ |
| 57 | """Make a test against an openmano server.\nUsage: test_openmanoclient [options] |
| 58 | -v|--verbose: prints more info in the test |
| 59 | --version: shows current version |
| 60 | -h|--help: shows this help |
| 61 | -d|--debug: set logs to debug level |
| 62 | -t|--tenant: set the tenant name to test. By default creates one |
| 63 | --datacenter: set the datacenter name to test. By default creates one at http://localhost:9080/openvim |
| 64 | -u|--url: set the openmano server url. By default 'http://localhost:9090/openmano' |
| 65 | --image: use this image path for testing a VNF. By default a fake one is generated, valid for VIM in test mode' |
| 66 | """ |
| 67 | |
| 68 | #import openmanoclient from relative path |
| 69 | module_info = imp.find_module("openmanoclient", [".."] ) |
| 70 | Client = imp.load_module("Client", *module_info) |
| 71 | |
| 72 | streamformat = "%(asctime)s %(name)s %(levelname)s: %(message)s" |
| 73 | logging.basicConfig(format=streamformat) |
| 74 | try: |
| 75 | opts, args = getopt.getopt(sys.argv[1:], "t:u:dhv", ["url=", "tenant=", "debug", "help", "version", "verbose", "datacenter=", "image="]) |
| 76 | except getopt.GetoptError as err: |
| 77 | print ("Error: {}\n Try '{} --help' for more information".format(str(err), sys.argv[0])) |
| 78 | sys.exit(2) |
| 79 | |
| 80 | debug = False |
| 81 | verbose = False |
| 82 | url = "http://localhost:9090/openmano" |
| 83 | to_delete_list=[] |
| 84 | test_tenant = None |
| 85 | test_datacenter = None |
| 86 | test_vim_tenant = None |
| 87 | test_image = None |
| 88 | for o, a in opts: |
| 89 | if o in ("-v", "--verbose"): |
| 90 | verbose = True |
| 91 | elif o in ("--version"): |
| 92 | print ("{} version".format(sys.argv[0]), __version__, version_date) |
| 93 | print ("(c) Copyright Telefonica") |
| 94 | sys.exit() |
| 95 | elif o in ("-h", "--help"): |
| 96 | print(usage) |
| 97 | sys.exit() |
| 98 | elif o in ("-d", "--debug"): |
| 99 | debug = True |
| 100 | elif o in ("-u", "--url"): |
| 101 | url = a |
| 102 | elif o in ("-t", "--tenant"): |
| 103 | test_tenant = a |
| 104 | elif o in ("--datacenter"): |
| 105 | test_datacenter = a |
| 106 | elif o in ("--image"): |
| 107 | test_image = a |
| 108 | else: |
| 109 | assert False, "Unhandled option" |
| 110 | |
| 111 | |
| 112 | |
| 113 | client = Client.openmanoclient( |
| 114 | endpoint_url=url, |
| 115 | tenant_name=test_tenant, |
| 116 | datacenter_name = test_datacenter, |
| 117 | debug = debug) |
| 118 | |
| 119 | import random |
| 120 | test_number=1 |
| 121 | |
| 122 | #TENANTS |
| 123 | print(" {}. TEST create_tenant".format(test_number)) |
| 124 | test_number += 1 |
| 125 | long_name = _get_random_name(60) |
| 126 | |
| 127 | tenant = client.create_tenant(name=long_name, description=long_name) |
| 128 | if verbose: print(tenant) |
| 129 | |
| 130 | print(" {}. TEST list_tenants".format(test_number)) |
| 131 | test_number += 1 |
| 132 | tenants = client.list_tenants() |
| 133 | if verbose: print(tenants) |
| 134 | |
| 135 | print(" {}. TEST list_tenans filter by name".format(test_number)) |
| 136 | test_number += 1 |
| 137 | tenants_ = client.list_tenants(name=long_name) |
| 138 | if not tenants_["tenants"]: |
| 139 | raise Exception("Text error, no TENANT found with name") |
| 140 | if verbose: print(tenants_) |
| 141 | |
| 142 | print(" {}. TEST get_tenant by UUID".format(test_number)) |
| 143 | test_number += 1 |
| 144 | tenant = client.get_tenant(uuid=tenants_["tenants"][0]["uuid"]) |
| 145 | if verbose: print(tenant) |
| 146 | |
| 147 | print(" {}. TEST delete_tenant by name".format(test_number)) |
| 148 | test_number += 1 |
| 149 | tenant = client.delete_tenant(name = long_name) |
| 150 | if verbose: print(tenant) |
| 151 | |
| 152 | if not test_tenant: |
| 153 | print(" {}. TEST create_tenant for remaining tests".format(test_number)) |
| 154 | test_number += 1 |
| 155 | test_tenant = "test-tenant "+\ |
| 156 | ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(40)) |
| 157 | tenant = client.create_tenant(name = test_tenant) |
| 158 | if verbose: print(tenant) |
| 159 | client["tenant_name"] = test_tenant |
| 160 | |
| 161 | to_delete_list.insert(0,{"item": "tenant", "function": client.delete_tenant, "params":{"name": test_tenant} }) |
| 162 | |
| 163 | #DATACENTERS |
| 164 | print(" {}. TEST create_datacenter".format(test_number)) |
| 165 | test_number += 1 |
| 166 | long_name = _get_random_name(60) |
| 167 | |
| 168 | datacenter = client.create_datacenter(name=long_name, vim_url="http://fakeurl/fake") |
| 169 | if verbose: print(datacenter) |
| 170 | |
| 171 | print(" {}. TEST list_datacenters".format(test_number)) |
| 172 | test_number += 1 |
| 173 | datacenters = client.list_datacenters(all_tenants=True) |
| 174 | if verbose: print(datacenters) |
| 175 | |
| 176 | print(" {}. TEST list_tenans filter by name".format(test_number)) |
| 177 | test_number += 1 |
| 178 | datacenters_ = client.list_datacenters(all_tenants=True, name=long_name) |
| 179 | if not datacenters_["datacenters"]: |
| 180 | raise Exception("Text error, no TENANT found with name") |
| 181 | if verbose: print(datacenters_) |
| 182 | |
| 183 | print(" {}. TEST get_datacenter by UUID".format(test_number)) |
| 184 | test_number += 1 |
| 185 | datacenter = client.get_datacenter(uuid=datacenters_["datacenters"][0]["uuid"], all_tenants=True) |
| 186 | if verbose: print(datacenter) |
| 187 | |
| 188 | print(" {}. TEST delete_datacenter by name".format(test_number)) |
| 189 | test_number += 1 |
| 190 | datacenter = client.delete_datacenter(name=long_name) |
| 191 | if verbose: print(datacenter) |
| 192 | |
| 193 | if not test_datacenter: |
| 194 | print(" {}. TEST create_datacenter for remaining tests".format(test_number)) |
| 195 | test_number += 1 |
| 196 | test_datacenter = "test-datacenter "+\ |
| 197 | ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(40)) |
| 198 | datacenter = client.create_datacenter(name=test_datacenter, vim_url="http://127.0.0.1:9080/openvim") |
| 199 | if verbose: print(datacenter) |
| 200 | client["datacenter_name"] = test_datacenter |
| 201 | to_delete_list.insert(0,{"item": "datacenter", "function": client.delete_datacenter, |
| 202 | "params":{ |
| 203 | "name": test_datacenter |
| 204 | } |
| 205 | }) |
| 206 | |
| 207 | print(" {}. TEST datacenter new tenenat".format(test_number)) |
| 208 | test_number += 1 |
| 209 | test_vim_tenant = "test-vimtenant "+\ |
| 210 | ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(40)) |
| 211 | vim_tenant = client.vim_action("create", "tenants", datacenter_name=test_datacenter, all_tenants=True, name=test_vim_tenant) |
| 212 | if verbose: print(vim_tenant) |
| 213 | client["datacenter_name"] = test_datacenter |
| 214 | to_delete_list.insert(0,{"item": "vim_tenant", |
| 215 | "function": client.vim_action, |
| 216 | "params":{ |
| 217 | "action":"delete", |
| 218 | "item":"tenants", |
| 219 | "datacenter_name": test_datacenter, |
| 220 | "all_tenants": True, |
| 221 | "uuid": vim_tenant["tenant"]["id"] |
| 222 | } |
| 223 | }) |
| 224 | |
| 225 | print(" {}. TEST datacenter attach".format(test_number)) |
| 226 | test_number += 1 |
| 227 | datacenter = client.attach_datacenter(name=test_datacenter, vim_tenant_name=test_vim_tenant) |
| 228 | if verbose: print(datacenter) |
| 229 | client["datacenter_name"] = test_datacenter |
| 230 | to_delete_list.insert(0,{"item": "datacenter-detach", "function": client.detach_datacenter, "params":{"name": test_datacenter} }) |
| 231 | |
| 232 | client["datacenter_name"] = test_datacenter |
| 233 | |
| 234 | |
| 235 | #VIM_ACTIONS |
| 236 | print(" {}. TEST create_VIM_tenant".format(test_number)) |
| 237 | test_number += 1 |
| 238 | long_name = _get_random_name(60) |
| 239 | |
| 240 | tenant = client.vim_action("create", "tenants", name=long_name) |
| 241 | if verbose: print(tenant) |
| 242 | tenant_uuid = tenant["tenant"]["id"] |
| 243 | |
| 244 | print(" {}. TEST list_VIM_tenants".format(test_number)) |
| 245 | test_number += 1 |
| 246 | tenants = client.vim_action("list", "tenants") |
| 247 | if verbose: print(tenants) |
| 248 | |
| 249 | print(" {}. TEST get_VIM_tenant by UUID".format(test_number)) |
| 250 | test_number += 1 |
| 251 | tenant = client.vim_action("show", "tenants", uuid=tenant_uuid) |
| 252 | if verbose: print(tenant) |
| 253 | |
| 254 | print(" {}. TEST delete_VIM_tenant by id".format(test_number)) |
| 255 | test_number += 1 |
| 256 | tenant = client.vim_action("delete", "tenants", uuid = tenant_uuid) |
| 257 | if verbose: print(tenant) |
| 258 | |
| 259 | print(" {}. TEST create_VIM_network".format(test_number)) |
| 260 | test_number += 1 |
| 261 | long_name = _get_random_name(60) |
| 262 | |
| 263 | network = client.vim_action("create", "networks", name=long_name) |
| 264 | if verbose: print(network) |
| 265 | network_uuid = network["network"]["id"] |
| 266 | |
| 267 | print(" {}. TEST list_VIM_networks".format(test_number)) |
| 268 | test_number += 1 |
| 269 | networks = client.vim_action("list", "networks") |
| 270 | if verbose: print(networks) |
| 271 | |
| 272 | print(" {}. TEST get_VIM_network by UUID".format(test_number)) |
| 273 | test_number += 1 |
| 274 | network = client.vim_action("show", "networks", uuid=network_uuid) |
| 275 | if verbose: print(network) |
| 276 | |
| 277 | print(" {}. TEST delete_VIM_network by id".format(test_number)) |
| 278 | test_number += 1 |
| 279 | network = client.vim_action("delete", "networks", uuid = network_uuid) |
| 280 | if verbose: print(network) |
| 281 | #VNFS |
| 282 | print(" {}. TEST create_vnf".format(test_number)) |
| 283 | test_number += 1 |
| 284 | test_vnf_name = _get_random_name(255) |
| 285 | if test_image: |
| 286 | test_vnf_path = test_image |
| 287 | else: |
| 288 | test_vnf_path = "/random/path/" + "".join(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ') for _ in range(20)) |
| 289 | |
| 290 | vnf_descriptor={'vnf': {'name': test_vnf_name, |
| 291 | 'VNFC': [{'description': _get_random_name(255), |
| 292 | 'name': 'linux-VM', |
| 293 | 'VNFC image': test_vnf_path, |
| 294 | 'ram': 1024, |
| 295 | 'vcpus': 1, |
| 296 | 'bridge-ifaces': [{'name': 'eth0'}] |
| 297 | }], |
| 298 | 'description': _get_random_name(255), |
| 299 | 'nets': [], |
| 300 | 'external-connections': [{'name': 'eth0', |
| 301 | 'local_iface_name': 'eth0', |
| 302 | 'VNFC': 'linux-VM', |
| 303 | 'type': 'bridge'}], |
| 304 | 'public': False}} |
| 305 | |
| 306 | vnf = client.create_vnf(descriptor=vnf_descriptor) |
| 307 | if verbose: print(vnf) |
| 308 | to_delete_list.insert(0,{"item": "vnf", "function": client.delete_vnf, "params":{"name": test_vnf_name} }) |
| 309 | |
| 310 | print(" {}. TEST list_vnfs".format(test_number)) |
| 311 | test_number += 1 |
| 312 | vnfs = client.list_vnfs() |
| 313 | if verbose: print(vnfs) |
| 314 | |
| 315 | print(" {}. TEST list_vnfs filter by name".format(test_number)) |
| 316 | test_number += 1 |
| 317 | vnfs_ = client.list_vnfs(name=test_vnf_name) |
| 318 | if not vnfs_["vnfs"]: |
| 319 | raise Exception("Text error, no VNF found with name") |
| 320 | if verbose: print(vnfs_) |
| 321 | |
| 322 | print(" {}. TEST get_vnf by UUID".format(test_number)) |
| 323 | test_number += 1 |
| 324 | vnf = client.get_vnf(uuid=vnfs_["vnfs"][0]["uuid"]) |
| 325 | if verbose: print(vnf) |
| 326 | |
| 327 | #SCENARIOS |
| 328 | print(" {}. TEST create_scenario".format(test_number)) |
| 329 | test_number += 1 |
| 330 | test_scenario_name = _get_random_name(255) |
| 331 | |
| 332 | scenario_descriptor={ 'schema_version': 2, |
| 333 | 'scenario': { |
| 334 | 'name': test_scenario_name, |
| 335 | 'description': _get_random_name(255), |
| 336 | 'public': True, |
| 337 | 'vnfs':{ |
| 338 | 'vnf1': { |
| 339 | 'vnf_name': test_vnf_name |
| 340 | } |
| 341 | }, |
| 342 | 'networks':{ |
| 343 | 'net1':{ |
| 344 | 'external': True, |
| 345 | 'interfaces': [ |
| 346 | {'vnf1': 'eth0'} |
| 347 | ] |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | scenario = client.create_scenario(descriptor=scenario_descriptor) |
| 354 | if verbose: print(scenario) |
| 355 | to_delete_list.insert(0,{"item": "scenario", "function": client.delete_scenario, "params":{"name": test_scenario_name} }) |
| 356 | |
| 357 | print(" {}. TEST list_scenarios".format(test_number)) |
| 358 | test_number += 1 |
| 359 | scenarios = client.list_scenarios() |
| 360 | if verbose: print(scenarios) |
| 361 | |
| 362 | print(" {}. TEST list_scenarios filter by name".format(test_number)) |
| 363 | test_number += 1 |
| 364 | scenarios_ = client.list_scenarios(name=test_scenario_name) |
| 365 | if not scenarios_["scenarios"]: |
| 366 | raise Exception("Text error, no VNF found with name") |
| 367 | if verbose: print(scenarios_) |
| 368 | |
| 369 | print(" {}. TEST get_scenario by UUID".format(test_number)) |
| 370 | test_number += 1 |
| 371 | scenario = client.get_scenario(uuid=scenarios_["scenarios"][0]["uuid"]) |
| 372 | if verbose: print(scenario) |
| 373 | |
| 374 | |
| 375 | |
| 376 | #INSTANCES |
| 377 | print(" {}. TEST create_instance".format(test_number)) |
| 378 | test_number += 1 |
| 379 | test_instance_name = _get_random_name(255) |
| 380 | |
| 381 | instance_descriptor={ 'schema_version': 2, |
| 382 | 'instance': { |
| 383 | 'name': test_instance_name, |
| 384 | 'description': _get_random_name(255), |
| 385 | 'public': True, |
| 386 | 'vnfs':{ |
| 387 | 'vnf1': { |
| 388 | 'vnf_name': test_vnf_name |
| 389 | } |
| 390 | }, |
| 391 | 'networks':{ |
| 392 | 'net1':{ |
| 393 | 'external': True, |
| 394 | 'interfaces': [ |
| 395 | {'vnf1': 'eth0'} |
| 396 | ] |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | instance = client.create_instance(scenario_name=test_scenario_name, name=test_instance_name ) |
| 403 | if verbose: print(instance) |
| 404 | to_delete_list.insert(0,{"item": "instance", "function": client.delete_instance, "params":{"name": test_instance_name} }) |
| 405 | |
| 406 | print(" {}. TEST list_instances".format(test_number)) |
| 407 | test_number += 1 |
| 408 | instances = client.list_instances() |
| 409 | if verbose: print(instances) |
| 410 | |
| 411 | print(" {}. TEST list_instances filter by name".format(test_number)) |
| 412 | test_number += 1 |
| 413 | instances_ = client.list_instances(name=test_instance_name) |
| 414 | if not instances_["instances"]: |
| 415 | raise Exception("Text error, no VNF found with name") |
| 416 | if verbose: print(instances_) |
| 417 | |
| 418 | print(" {}. TEST get_instance by UUID".format(test_number)) |
| 419 | test_number += 1 |
| 420 | instance = client.get_instance(uuid=instances_["instances"][0]["uuid"]) |
| 421 | if verbose: print(instance) |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | #DELETE Create things |
| 427 | for item in to_delete_list: |
| 428 | print(" {}. TEST delete_{}".format(test_number, item["item"])) |
| 429 | test_number += 1 |
| 430 | response = item["function"](**item["params"]) |
| 431 | if verbose: print(response) |
| 432 | |