2 # -*- coding: utf-8 -*-
5 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
6 # This file is part of openmano
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: nfvlabs@tid.es
26 Module to test openmanoclient class and indirectly the whole openmano
27 It allows both python 2 and python 3
29 __author__
="Alfonso Tierno"
30 __date__
="$09-Mar-2016 09:09:48$"
32 version_date
="May 2016"
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
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
)))
49 if __name__
=="__main__":
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'
68 #import openmanoclient from relative path
69 module_info
= imp
.find_module("openmanoclient", [".."] )
70 Client
= imp
.load_module("Client", *module_info
)
72 streamformat
= "%(asctime)s %(name)s %(levelname)s: %(message)s"
73 logging
.basicConfig(format
=streamformat
)
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]))
82 url
= "http://localhost:9090/openmano"
85 test_datacenter
= None
86 test_vim_tenant
= None
89 if o
in ("-v", "--verbose"):
91 elif o
in ("--version"):
92 print ("{} version".format(sys
.argv
[0]), __version__
, version_date
)
93 print ("(c) Copyright Telefonica")
95 elif o
in ("-h", "--help"):
98 elif o
in ("-d", "--debug"):
100 elif o
in ("-u", "--url"):
102 elif o
in ("-t", "--tenant"):
104 elif o
in ("--datacenter"):
106 elif o
in ("--image"):
109 assert False, "Unhandled option"
113 client
= Client
.openmanoclient(
115 tenant_name
=test_tenant
,
116 datacenter_name
= test_datacenter
,
123 print(" {}. TEST create_tenant".format(test_number
))
125 long_name
= _get_random_name(60)
127 tenant
= client
.create_tenant(name
=long_name
, description
=long_name
)
128 if verbose
: print(tenant
)
130 print(" {}. TEST list_tenants".format(test_number
))
132 tenants
= client
.list_tenants()
133 if verbose
: print(tenants
)
135 print(" {}. TEST list_tenans filter by name".format(test_number
))
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_
)
142 print(" {}. TEST get_tenant by UUID".format(test_number
))
144 tenant
= client
.get_tenant(uuid
=tenants_
["tenants"][0]["uuid"])
145 if verbose
: print(tenant
)
147 print(" {}. TEST delete_tenant by name".format(test_number
))
149 tenant
= client
.delete_tenant(name
= long_name
)
150 if verbose
: print(tenant
)
153 print(" {}. TEST create_tenant for remaining tests".format(test_number
))
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
161 to_delete_list
.insert(0,{"item": "tenant", "function": client
.delete_tenant
, "params":{"name": test_tenant
} })
164 print(" {}. TEST create_datacenter".format(test_number
))
166 long_name
= _get_random_name(60)
168 datacenter
= client
.create_datacenter(name
=long_name
, vim_url
="http://fakeurl/fake")
169 if verbose
: print(datacenter
)
171 print(" {}. TEST list_datacenters".format(test_number
))
173 datacenters
= client
.list_datacenters(all_tenants
=True)
174 if verbose
: print(datacenters
)
176 print(" {}. TEST list_tenans filter by name".format(test_number
))
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_
)
183 print(" {}. TEST get_datacenter by UUID".format(test_number
))
185 datacenter
= client
.get_datacenter(uuid
=datacenters_
["datacenters"][0]["uuid"], all_tenants
=True)
186 if verbose
: print(datacenter
)
188 print(" {}. TEST delete_datacenter by name".format(test_number
))
190 datacenter
= client
.delete_datacenter(name
=long_name
)
191 if verbose
: print(datacenter
)
193 if not test_datacenter
:
194 print(" {}. TEST create_datacenter for remaining tests".format(test_number
))
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
,
203 "name": test_datacenter
207 print(" {}. TEST datacenter new tenenat".format(test_number
))
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
,
219 "datacenter_name": test_datacenter
,
221 "uuid": vim_tenant
["tenant"]["id"]
225 print(" {}. TEST datacenter attach".format(test_number
))
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
} })
232 client
["datacenter_name"] = test_datacenter
235 print(" {}. TEST create_wim".format(test_number
))
237 long_name
= _get_random_name(60)
239 wim
= client
.create_wim(name
=long_name
, wim_url
="http://fakeurl/fake")
240 if verbose
: print(wim
)
242 print(" {}. TEST list_wims".format(test_number
))
244 wims
= client
.list_wims(all_tenants
=True)
245 if verbose
: print(wims
)
247 print(" {}. TEST list_tenans filter by name".format(test_number
))
249 wims_
= client
.list_wims(all_tenants
=True, name
=long_name
)
250 if not wims_
["wims"]:
251 raise Exception("Text error, no TENANT found with name")
252 if verbose
: print(wims_
)
254 print(" {}. TEST get_wim by UUID".format(test_number
))
256 wim
= client
.get_wim(uuid
=wims_
["wims"][0]["uuid"], all_tenants
=True)
257 if verbose
: print(wim
)
259 print(" {}. TEST delete_wim by name".format(test_number
))
261 wim
= client
.delete_wim(name
=long_name
)
262 if verbose
: print(wim
)
264 print(" {}. TEST create_wim for remaining tests".format(test_number
))
266 test_wim
= "test-wim " + \
267 ''.join(random
.choice('abcdefghijklmnopqrstuvwxyz') for _
in range(40))
268 wim
= client
.create_wim(name
=test_wim
, vim_url
="http://127.0.0.1:9080/odl")
269 if verbose
: print(wim
)
270 to_delete_list
.insert(0,
272 "item": "wim", "function": client
.delete_wim
,
279 test_wim_tenant
= "test-wimtenant " + \
280 ''.join(random
.choice('abcdefghijklmnopqrstuvwxyz') for _
in range(40))
282 # print(" {}. TEST datacenter new tenenat".format(test_number))
284 # test_vim_tenant = "test-vimtenant " + \
285 # ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(40))
286 # vim_tenant = client.vim_action("create", "tenants", datacenter_name=test_datacenter, all_tenants=True,
287 # name=test_vim_tenant)
288 # if verbose: print(vim_tenant)
289 # client["datacenter_name"] = test_datacenter
290 # to_delete_list.insert(0, {"item": "vim_tenant",
291 # "function": client.vim_action,
293 # "action": "delete",
295 # "datacenter_name": test_datacenter,
296 # "all_tenants": True,
297 # "uuid": vim_tenant["tenant"]["id"]
301 print(" {}. TEST wim attach".format(test_number
))
303 wim
= client
.attach_wim(name
=test_wim
, wim_tenant_name
=test_wim_tenant
)
304 if verbose
: print(wim
)
305 to_delete_list
.insert(0, {"item": "wim-detach", "function": client
.detach_wim
,
306 "params": {"name": test_wim
}})
309 print(" {}. TEST create_VIM_tenant".format(test_number
))
311 long_name
= _get_random_name(60)
313 tenant
= client
.vim_action("create", "tenants", name
=long_name
)
314 if verbose
: print(tenant
)
315 tenant_uuid
= tenant
["tenant"]["id"]
317 print(" {}. TEST list_VIM_tenants".format(test_number
))
319 tenants
= client
.vim_action("list", "tenants")
320 if verbose
: print(tenants
)
322 print(" {}. TEST get_VIM_tenant by UUID".format(test_number
))
324 tenant
= client
.vim_action("show", "tenants", uuid
=tenant_uuid
)
325 if verbose
: print(tenant
)
327 print(" {}. TEST delete_VIM_tenant by id".format(test_number
))
329 tenant
= client
.vim_action("delete", "tenants", uuid
= tenant_uuid
)
330 if verbose
: print(tenant
)
332 print(" {}. TEST create_VIM_network".format(test_number
))
334 long_name
= _get_random_name(60)
336 network
= client
.vim_action("create", "networks", name
=long_name
)
337 if verbose
: print(network
)
338 network_uuid
= network
["network"]["id"]
340 print(" {}. TEST list_VIM_networks".format(test_number
))
342 networks
= client
.vim_action("list", "networks")
343 if verbose
: print(networks
)
345 print(" {}. TEST get_VIM_network by UUID".format(test_number
))
347 network
= client
.vim_action("show", "networks", uuid
=network_uuid
)
348 if verbose
: print(network
)
350 print(" {}. TEST delete_VIM_network by id".format(test_number
))
352 network
= client
.vim_action("delete", "networks", uuid
= network_uuid
)
353 if verbose
: print(network
)
355 print(" {}. TEST create_vnf".format(test_number
))
357 test_vnf_name
= _get_random_name(255)
359 test_vnf_path
= test_image
361 test_vnf_path
= "/random/path/" + "".join(random
.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ') for _
in range(20))
363 vnf_descriptor
={'vnf': {'name': test_vnf_name
,
364 'VNFC': [{'description': _get_random_name(255),
366 'VNFC image': test_vnf_path
,
369 'bridge-ifaces': [{'name': 'eth0'}]
371 'description': _get_random_name(255),
373 'external-connections': [{'name': 'eth0',
374 'local_iface_name': 'eth0',
379 vnf
= client
.create_vnf(descriptor
=vnf_descriptor
)
380 if verbose
: print(vnf
)
381 to_delete_list
.insert(0,{"item": "vnf", "function": client
.delete_vnf
, "params":{"name": test_vnf_name
} })
383 print(" {}. TEST list_vnfs".format(test_number
))
385 vnfs
= client
.list_vnfs()
386 if verbose
: print(vnfs
)
388 print(" {}. TEST list_vnfs filter by name".format(test_number
))
390 vnfs_
= client
.list_vnfs(name
=test_vnf_name
)
391 if not vnfs_
["vnfs"]:
392 raise Exception("Text error, no VNF found with name")
393 if verbose
: print(vnfs_
)
395 print(" {}. TEST get_vnf by UUID".format(test_number
))
397 vnf
= client
.get_vnf(uuid
=vnfs_
["vnfs"][0]["uuid"])
398 if verbose
: print(vnf
)
401 print(" {}. TEST create_scenario".format(test_number
))
403 test_scenario_name
= _get_random_name(255)
405 scenario_descriptor
={ 'schema_version': 2,
407 'name': test_scenario_name
,
408 'description': _get_random_name(255),
412 'vnf_name': test_vnf_name
426 scenario
= client
.create_scenario(descriptor
=scenario_descriptor
)
427 if verbose
: print(scenario
)
428 to_delete_list
.insert(0,{"item": "scenario", "function": client
.delete_scenario
, "params":{"name": test_scenario_name
} })
430 print(" {}. TEST list_scenarios".format(test_number
))
432 scenarios
= client
.list_scenarios()
433 if verbose
: print(scenarios
)
435 print(" {}. TEST list_scenarios filter by name".format(test_number
))
437 scenarios_
= client
.list_scenarios(name
=test_scenario_name
)
438 if not scenarios_
["scenarios"]:
439 raise Exception("Text error, no VNF found with name")
440 if verbose
: print(scenarios_
)
442 print(" {}. TEST get_scenario by UUID".format(test_number
))
444 scenario
= client
.get_scenario(uuid
=scenarios_
["scenarios"][0]["uuid"])
445 if verbose
: print(scenario
)
450 print(" {}. TEST create_instance".format(test_number
))
452 test_instance_name
= _get_random_name(255)
454 instance_descriptor
={ 'schema_version': 2,
456 'name': test_instance_name
,
457 'description': _get_random_name(255),
461 'vnf_name': test_vnf_name
475 instance
= client
.create_instance(scenario_name
=test_scenario_name
, name
=test_instance_name
)
476 if verbose
: print(instance
)
477 to_delete_list
.insert(0,{"item": "instance", "function": client
.delete_instance
, "params":{"name": test_instance_name
} })
479 print(" {}. TEST list_instances".format(test_number
))
481 instances
= client
.list_instances()
482 if verbose
: print(instances
)
484 print(" {}. TEST list_instances filter by name".format(test_number
))
486 instances_
= client
.list_instances(name
=test_instance_name
)
487 if not instances_
["instances"]:
488 raise Exception("Text error, no VNF found with name")
489 if verbose
: print(instances_
)
491 print(" {}. TEST get_instance by UUID".format(test_number
))
493 instance
= client
.get_instance(uuid
=instances_
["instances"][0]["uuid"])
494 if verbose
: print(instance
)
499 #DELETE Create things
500 for item
in to_delete_list
:
501 print(" {}. TEST delete_{}".format(test_number
, item
["item"]))
503 response
= item
["function"](**item
["params"])
504 if verbose
: print(response
)