Fix 1475 - Incorrect description in instantiating error
[osm/RO.git] / RO / test / test_osconnector.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 ##
5 # Copyright 2015 Telefonica Investigacion 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 test_osconnector.py makes a test over osconnector.py (openstack connector)
27 credentiasl must be provided with environment bash variables or arguments
28 '''
29 __author__="Alfonso Tierno, Gerardo Garcia"
30 __date__ ="$22-jun-2014 11:19:29$"
31
32
33 import os
34 import sys
35 import getopt
36 #import yaml
37 #from jsonschema import validate as js_v, exceptions as js_e
38
39 #load osconnector, insert openmano directory in the path
40 r=sys.argv[0].rfind('/')
41 if r<0:
42 osconnector_path=".."
43 else:
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')
47 import osconnector
48
49 version="0.1"
50
51 def usage():
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")
61 return
62
63 def delete_items():
64 global myvim
65 global rollback_list
66 print("Making rollback, deleting items")
67 for i in range(len(rollback_list)-1, -1, -1):
68 item,name,id_ = rollback_list[i]
69 if item=="creds":
70 print("changing credentials {}='{}'".format(name, id_)).ljust(50),
71 else:
72 print("deleting {} '{}'".format(item, name)).ljust(50),
73 sys.stdout.flush()
74 if item=="flavor":
75 result,message=myvim.delete_tenant_flavor(id_)
76 elif item=="image":
77 result,message=myvim.delete_tenant_image(id_)
78 elif item=="tenant":
79 result,message=myvim.delete_tenant(id_)
80 elif item=="user":
81 result,message=myvim.delete_user(id_)
82 elif item=="network":
83 result,message=myvim.delete_tenant_network(id_)
84 elif item=="vm":
85 result,message=myvim.delete_tenant_vminstance(id_)
86 elif item=="creds":
87 try:
88 myvim[name]=id_
89 result=1
90 except Exception as e:
91 result=-1
92 message= " " + str(type(e))[6:-1] + ": "+ str(e)
93 else:
94 print("Internal error unknown item rollback {},{},{}".format(item,name,id_))
95 continue
96 if result<0:
97 print(" Fail")
98 print(" VIM response:", message)
99 continue
100 else:
101 print(" Ok")
102
103 if __name__=="__main__":
104 global myvim
105 global rollback_list
106 #print("(c) Copyright Telefonica"
107 rollback_list=[]
108 try:
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")
114 usage()
115 sys.exit(2)
116
117 creds = {}
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"
125 for o, a in opts:
126 if o in ("-h", "--help"):
127 usage()
128 sys.exit()
129 elif o in ("-v", "--version"):
130 creds['version']=a
131 elif o in ("-u", "--username"):
132 creds['username']=a
133 elif o in ("-p", "--password"):
134 creds['password']=a
135 elif o in ("-U", "--auth_url"):
136 creds['auth_url']=a
137 elif o in ("-t", "--tenant_name"):
138 creds['tenant_name']=a
139 elif o in ("-i", "--image"):
140 image_path=a
141 elif o=="--skip-admin-tests":
142 skip_admin_tests=True
143 else:
144 assert False, "Unhandled option"
145
146 if creds['auth_url']==None:
147 print("you must provide openstack url with -U or bash OS_AUTH_URL")
148 sys.exit()
149 print("creds:", creds)
150
151
152 try:
153 print('load osconnector class'.ljust(50))
154 sys.stdout.flush()
155 try:
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'} )
160 print(" Ok")
161 except Exception as e:
162 print(" Fail")
163 print(str(type(e))[6:-1] + ": "+ str(e))
164 exit(-1)
165
166 if not skip_admin_tests:
167 tenant_name="tos-tenant"
168 print("creating new tenant '{}'".format(tenant_name)).ljust(50),
169 sys.stdout.flush()
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.")
171 if result<0:
172 print(" Fail")
173 print(" you can skip tenant creation with param'--skip-admin-tests'")
174 print(" VIM response:", new_tenant)
175 exit(-1)
176 else:
177 print(" Ok", new_tenant)
178 rollback_list.append(("tenant",tenant_name,new_tenant))
179
180 user_name="tos-user"
181 print("creating new user '{}'".format(user_name).ljust(50), end="")
182 sys.stdout.flush()
183 result,new_user=myvim.new_user(user_name, user_name, tenant_id=new_tenant)
184 if result<0:
185 print(" Fail")
186 print(" VIM response:", new_user)
187 exit(-1)
188 else:
189 print(" Ok", new_user)
190 rollback_list.append(("user",user_name,new_user))
191
192 name="tos-fl1"
193 print("creating new flavor '{}'".format(name)).ljust(50),
194 sys.stdout.flush()
195 flavor={}
196 flavor['name']=name
197 result,new_flavor1=myvim.new_tenant_flavor(flavor, True)
198 if result<0:
199 print(" Fail")
200 print(" VIM response:", new_flavor1)
201 exit(-1)
202 else:
203 print(" Ok", new_flavor1)
204 rollback_list.append(("flavor",name,new_flavor1))
205
206 name="tos-cirros"
207 print("creating new image '{}'".format(name).ljust(50))
208 sys.stdout.flush()
209 image={}
210 image['name']=name
211 image['location']=image_path #"/home/atierno/cirros-0.3.3-x86_64-disk.img"
212 result,new_image1=myvim.new_tenant_image(image)
213 if result<0:
214 print(" Fail")
215 print(" VIM response:", new_image1)
216 exit(-1)
217 else:
218 print(" Ok", new_image1)
219 rollback_list.append(("image",name, new_image1))
220
221 if not skip_admin_tests:
222 try:
223 print('changing credentials to new tenant'.ljust(50))
224 sys.stdout.flush()
225 myvim['tenant'] =tenant_name
226 myvim['user']=user_name
227 myvim['passwd']=user_name
228 print(" Ok")
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:
233 print(" Fail")
234 print(" Error setting osconnector to new tenant:", str(type(e))[6:-1] + ": "+ str(e))
235 exit(-1)
236
237 name="tos-net-bridge"
238 print("creating new net '{}'".format(name)).ljust(50),
239 sys.stdout.flush()
240 result,new_net1=myvim.new_tenant_network(name, "bridge")
241 if result<0:
242 print(" Fail")
243 print(" VIM response:", new_net1)
244 exit(-1)
245 else:
246 print(" Ok", new_net1)
247 rollback_list.append(("network",name, new_net1))
248
249 name="tos-vm-cloud"
250 print("creating new VM '{}'".format(name).ljust(50))
251 sys.stdout.flush()
252 result,new_vm1=myvim.new_tenant_vminstance(name, "vm-cloud-description", False,new_image1,new_flavor1,
253 [{"net_id":new_net1, "type":"virtio"}] )
254 if result<0:
255 print(" Fail")
256 print(" VIM response:", new_vm1)
257 exit(-1)
258 else:
259 print(" Ok", new_vm1)
260 rollback_list.append(("vm",name, new_vm1))
261
262
263 print('DONE Ok')
264 print("Type ENTER to delete items")
265 input('> ')
266 exit()
267
268 except KeyboardInterrupt:
269 print(" Canceled!")
270 except SystemExit:
271 pass
272 if len(rollback_list):
273 delete_items()
274