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 test_osconnector.py makes a test over osconnector.py (openstack connector)
27 credentiasl must be provided with environment bash variables or arguments
29 __author__
="Alfonso Tierno, Gerardo Garcia"
30 __date__
="$22-jun-2014 11:19:29$"
37 #from jsonschema import validate as js_v, exceptions as js_e
39 #load osconnector, insert openmano directory in the path
40 r
=sys
.argv
[0].rfind('/')
44 osconnector_path
=sys
.argv
[0][:r
+1]+".."
45 sys
.path
.insert(0, osconnector_path
)
46 #sys.path.insert(0, '/home/atierno/workspace/openmano/openmano')
52 print("Usage: ", sys
.argv
[0], "[options]")
53 print(" -v|--version openstack version (by default 2)")
54 print(" -u|--username USER user to authenticate (by default bash:OS_USERNAME)")
55 print(" -p|--password PASSWD password to authenticate (by default bash:OS_PASSWORD)")
56 print(" -U|--auth_url URL url of authentication over keystone (by default bash:OS_AUTH_URL)")
57 print(" -t|--tenant_name TENANT password to authenticate (by default bash:OS_TENANT_NAME)")
58 print(" -i|--image IMAGE use this local path or url for loading image (by default cirros)")
59 print(" --skip-admin-tests skip tests that requires administrative permissions, like create tenants")
60 print(" -h|--help shows this help")
66 print("Making rollback, deleting items")
67 for i
in range(len(rollback_list
)-1, -1, -1):
68 item
,name
,id_
= rollback_list
[i
]
70 print("changing credentials {}='{}'".format(name
, id_
)).ljust(50),
72 print("deleting {} '{}'".format(item
, name
)).ljust(50),
75 result
,message
=myvim
.delete_tenant_flavor(id_
)
77 result
,message
=myvim
.delete_tenant_image(id_
)
79 result
,message
=myvim
.delete_tenant(id_
)
81 result
,message
=myvim
.delete_user(id_
)
83 result
,message
=myvim
.delete_tenant_network(id_
)
85 result
,message
=myvim
.delete_tenant_vminstance(id_
)
90 except Exception as e
:
92 message
= " " + str(type(e
))[6:-1] + ": "+ str(e
)
94 print("Internal error unknown item rollback {},{},{}".format(item
,name
,id_
))
98 print(" VIM response:", message
)
103 if __name__
=="__main__":
106 #print("(c) Copyright Telefonica"
109 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hv:u:U:p:t:i:",
110 ["username=", "help", "version=", "password=", "tenant=", "url=","skip-admin-tests",'image='])
111 except getopt
.GetoptError
as err
:
112 # print(help information and exit:
113 print("Error:", err
) # will print something like "option -a not recognized")
118 creds
['version'] = os
.environ
.get('OS_VERSION', '2')
119 creds
['username'] = os
.environ
.get('OS_USERNAME')
120 creds
['password'] = os
.environ
.get('OS_PASSWORD')
121 creds
['auth_url'] = os
.environ
.get('OS_AUTH_URL')
122 creds
['tenant_name'] = os
.environ
.get('OS_TENANT_NAME')
123 skip_admin_tests
=False
124 image_path
="http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img"
126 if o
in ("-h", "--help"):
129 elif o
in ("-v", "--version"):
131 elif o
in ("-u", "--username"):
133 elif o
in ("-p", "--password"):
135 elif o
in ("-U", "--auth_url"):
137 elif o
in ("-t", "--tenant_name"):
138 creds
['tenant_name']=a
139 elif o
in ("-i", "--image"):
141 elif o
=="--skip-admin-tests":
142 skip_admin_tests
=True
144 assert False, "Unhandled option"
146 if creds
['auth_url']==None:
147 print("you must provide openstack url with -U or bash OS_AUTH_URL")
149 print("creds:", creds
)
153 print('load osconnector class'.ljust(50))
156 myvim
=osconnector
.osconnector(uuid
=None, name
='test-openstack', tenant
=creds
['tenant_name'],
157 url
=creds
['auth_url'], url_admin
=None,
158 user
=creds
['username'], passwd
=creds
['password'],
159 debug
= False, config
={'network_vlan_ranges':'physnet_sriov'} )
161 except Exception as e
:
163 print(str(type(e
))[6:-1] + ": "+ str(e
))
166 if not skip_admin_tests
:
167 tenant_name
="tos-tenant"
168 print("creating new tenant '{}'".format(tenant_name
)).ljust(50),
170 result
,new_tenant
=myvim
.new_tenant(tenant_name
, "test tenant_description, trying a long description to get the limit. 2 trying a long description to get the limit. 3. trying a long description to get the limit.")
173 print(" you can skip tenant creation with param'--skip-admin-tests'")
174 print(" VIM response:", new_tenant
)
177 print(" Ok", new_tenant
)
178 rollback_list
.append(("tenant",tenant_name
,new_tenant
))
181 print("creating new user '{}'".format(user_name
).ljust(50), end
="")
183 result
,new_user
=myvim
.new_user(user_name
, user_name
, tenant_id
=new_tenant
)
186 print(" VIM response:", new_user
)
189 print(" Ok", new_user
)
190 rollback_list
.append(("user",user_name
,new_user
))
193 print("creating new flavor '{}'".format(name
)).ljust(50),
197 result
,new_flavor1
=myvim
.new_tenant_flavor(flavor
, True)
200 print(" VIM response:", new_flavor1
)
203 print(" Ok", new_flavor1
)
204 rollback_list
.append(("flavor",name
,new_flavor1
))
207 print("creating new image '{}'".format(name
).ljust(50))
211 image
['location']=image_path
#"/home/atierno/cirros-0.3.3-x86_64-disk.img"
212 result
,new_image1
=myvim
.new_tenant_image(image
)
215 print(" VIM response:", new_image1
)
218 print(" Ok", new_image1
)
219 rollback_list
.append(("image",name
, new_image1
))
221 if not skip_admin_tests
:
223 print('changing credentials to new tenant'.ljust(50))
225 myvim
['tenant'] =tenant_name
226 myvim
['user']=user_name
227 myvim
['passwd']=user_name
229 rollback_list
.append(("creds", "tenant", creds
["tenant_name"]))
230 rollback_list
.append(("creds", "user", creds
["username"]))
231 rollback_list
.append(("creds", "passwd", creds
["password"]))
232 except Exception as e
:
234 print(" Error setting osconnector to new tenant:", str(type(e
))[6:-1] + ": "+ str(e
))
237 name
="tos-net-bridge"
238 print("creating new net '{}'".format(name
)).ljust(50),
240 result
,new_net1
=myvim
.new_tenant_network(name
, "bridge")
243 print(" VIM response:", new_net1
)
246 print(" Ok", new_net1
)
247 rollback_list
.append(("network",name
, new_net1
))
250 print("creating new VM '{}'".format(name
).ljust(50))
252 result
,new_vm1
=myvim
.new_tenant_vminstance(name
, "vm-cloud-description", False,new_image1
,new_flavor1
,
253 [{"net_id":new_net1
, "type":"virtio"}] )
256 print(" VIM response:", new_vm1
)
259 print(" Ok", new_vm1
)
260 rollback_list
.append(("vm",name
, new_vm1
))
264 print("Type ENTER to delete items")
268 except KeyboardInterrupt:
272 if len(rollback_list
):