-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend_to_end.rb
372 lines (332 loc) · 9.67 KB
/
end_to_end.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
require 'rest-client'
require 'json'
@tenor = ARGV[0]
if ARGV[0].nil?
@tenor = "localhost:4000"
end
@OPENSTACK_HOST = ENV['OPENSTACK_HOST']
@OPENSTACK_USER = ENV['OPENSTACK_USER']
@OPENSTACK_PASS = ENV['OPENSTACK_PASS']
@OPENSTACK_TENANT_NAME = ENV['OPENSTACK_TENANT_NAME']
@OPENSTACK_DNS = ENV['OPENSTACK_DNS']
if @OPENSTACK_HOST.nil?
puts "Execute the environment script! (. ./env_end_to_end.sh)"
exit
end
#host = "IP...."
#user = "admin"
#password = "pass"
#tenant_name = "admin"
#dns = "8.8.8.8"
@e2e = {
:vnfd_id => nil,
:nsd_id => nil,
:pops => [],
:instances => []
}
def end_to_end_script()
host = @OPENSTACK_HOST
user = @OPENSTACK_USER
password = @OPENSTACK_PASS
tenant_name = @OPENSTACK_TENANT_NAME
dns = @OPENSTACK_DNS
puts "Removing PoPs if exists...."
pops_names = ["admin_v2", "admin_v3", "non_admin_v2", "non_admin_v3"]
pops_names.each do |pop|
#remove_pops_if_exists(pop)
end
puts "All PoPs are removed\n"
puts "Create a PoP with Admin role v2"
obj = {
name: "admin_v2",
host: host,
user: user,
password: password,
tenant_name: tenant_name,
is_admin: true,
description: "",
extra_info: "keystone-endpoint=http://#{host}:35357/v2.0 orch-endpoint=http://#{host}:8004/v1 compute-endpoint=http://#{host}:8774/v2.1 neutron-endpoint=http://#{host}:9696/v2.0 dns=#{dns}"
}
pop_id, errors = create_pop(obj)
recover_state(errors) if errors
@e2e[:pops] << {:name => obj[:name], :id => pop_id}
puts "Create a PoP with Admin role v3"
obj = {
name: "admin_v3",
host: host,
user: user,
password: password,
tenant_name: tenant_name,
is_admin: true,
description: "",
extra_info: "keystone-endpoint=http://#{host}:35357/v3 orch-endpoint=http://#{host}:8004/v1 compute-endpoint=http://#{host}:8774/v2.1 neutron-endpoint=http://#{host}:9696/v2.0 dns=#{dns}"
}
pop_id, errors = create_pop(obj)
recover_state(errors) if errors
#@e2e[:pops] << {:name => obj[:name], :id => pop_id}
puts "Create a PoP without Admin role v2"
obj = {
name: "non_admin_v2",
host: host,
user: user,
password: password,
tenant_name: tenant_name,
is_admin: false,
description: "",
extra_info: "keystone-endpoint=http://#{host}:35357/v2.0 orch-endpoint=http://#{host}:8004/v1 compute-endpoint=http://#{host}:8774/v2.1 neutron-endpoint=http://#{host}:9696/v2.0 dns=#{dns}"
}
pop_id, errors = create_pop(obj)
recover_state(errors) if errors
@e2e[:pops] << {:name => obj[:name], :id => pop_id}
puts "Create a PoP without Admin role v3"
obj = {
name: "non_admin_v3",
host: host,
user: user,
password: password,
tenant_name: tenant_name,
is_admin: false,
description: "",
extra_info: "keystone-endpoint=http://#{host}:35357/v3 orch-endpoint=http://#{host}:8004/v1 compute-endpoint=http://#{host}:8774/v2.1 neutron-endpoint=http://#{host}:9696/v2.0 dns=#{dns}"
}
pop_id, errors = create_pop(obj)
recover_state(errors) if errors
#@e2e[:pops] << {:name => obj[:name], :id => pop_id}
#create basic descriptors
#check_if_descriptors()
descriptors, errors = create_descriptors()
recover_state(errors) if errors
@e2e[:vnfd_id] = "2910"
@e2e[:nsd_id] = "578e2db5e4b0356a4eb0d2b1"
puts "Instantiate services .............................................."
#instantiate to each PoP created
@e2e[:pops].each do |pop|
ns_instance_id, errors = create_instance(pop[:id])
puts errors if errors
recover_state(errors) if errors
@e2e[:instances] << {:id => ns_instance_id, :status => "INIT"}
break
end
pop = @e2e[:pops].find { |p| p[:name] == "admin_v2"}
#ns_instance, errors = create_instance(pop[:id])
pop = @e2e[:pops].find { |p| p[:name] == "admin_v3"}
#ns_instance = create_instance(pop[:id])
pop = @e2e[:pops].find { |p| p[:name] == "non_admin_v2"}
#ns_instance = create_instance(pop[:id])
pop = @e2e[:pops].find { |p| p[:name] == "non_admin_v3"}
#ns_instance = create_instance(pop[:id])
#wait until instance is created
puts "Waiting 30secs..."
sleep(30)
counter = 0
final_status = 'INIT'
while counter < 30 && final_status != 'INSTANTIATED' do
@e2e[:instances].each do |instance|
status, error = get_instance_status(instance[:id])
instance[:status] = status
puts "Counter: #{counter}. Instance: #{instance[:id].to_s}. Status: #{instance[:status].to_s}"
recover_state("Error creating...") if error
if status == 'INSTANTIATED'
if @e2e[:instances].find {|ins| ins[:status] != "INSTANTIATED"}
next
else
puts "All instances are INSTANTIATED"
counter = 30
break
end
elsif status == 'ERROR_CREATING'
recover_state("Error creating...")
end
end
sleep(20)
counter +=1
end
puts "All instances created correctly..."
puts "Start removing of instances..."
@e2e[:instances].each do |instances|
delete_instance(instances[:id])
end
puts "Waiting 60 secs before remove PoPs..."
sleep(60)
@e2e[:pops].each do |pop|
# delete_pop(pop[:id])
end
delete_descriptors()
end
def delete_instance(id)
begin
response = RestClient.delete "#{@tenor}/ns-instances/#{id}", :content_type => :json
rescue => e
puts e
return 400, e
end
response
end
def get_instance_status(id)
begin
response = RestClient.get "#{@tenor}/ns-instances/#{id}/status", :content_type => :json
rescue => e
puts e
return 400, e
end
response
end
def create_instance(pop_id)
instance = {"ns_id": @e2e[:nsd_id],"callbackUrl":"https://httpbin.org/post","pop_id": pop_id,"flavour":"basic"}
begin
response = JSON.parse(RestClient.post "#{@tenor}/ns-instances", instance.to_json, :content_type => :json)
rescue => e
puts e
return 400, e
end
response['id']
end
def check_if_descriptors(vnf_id, ns_id)
begin
response = JSON.parse(RestClient.get "#{@tenor}/vnfs/{vnf_id}", :content_type => :json)
rescue RestClient::ExceptionWithResponse => e
puts e
response = {'vnfd' => {}}
response['vnfd']['id'] = JSON.parse(vnfd)['vnfd']['id']
rescue => e
puts e
puts e.response
puts e.response.code
puts e.response.body
return 400, "Error creating VNF descriptor"
end
@e2e[:vnfd_id] = vnf_id
begin
response = JSON.parse(RestClient.get "#{@tenor}/network-services/{ns_id}", :content_type => :json)
rescue RestClient::ExceptionWithResponse => e
puts e
response = {'vnfd' => {}}
response['vnfd']['id'] = JSON.parse(vnfd)['vnfd']['id']
rescue => e
puts e
puts e.response
puts e.response.code
puts e.response.body
return 400, "Error creating VNF descriptor"
end
@e2e[:nsd_id] = ns_id
end
def create_descriptors()
puts "Creating descriptors"
vnfd = File.read('vnfd-validator/assets/samples/vnfd_example.json')
puts
begin
response = JSON.parse(RestClient.post "#{@tenor}/vnfs", vnfd, :content_type => :json)
rescue RestClient::ExceptionWithResponse => e
puts e
puts "Using the created VNFD"
response = {'vnfd' => {}}
response['vnfd']['id'] = JSON.parse(vnfd)['vnfd']['id']
rescue => e
puts e
puts e.response
puts e.response.code
puts e.response.body
return 400, "Error creating VNF descriptor"
end
vnfd_id = response['vnfd']['id']
@e2e[:vnfd_id] = response['vnfd']['id']
nsd = File.read('nsd-validator/assets/samples/nsd_example.json')
begin
response = JSON.parse(RestClient.post "#{@tenor}/network-services", nsd, :content_type => :json)
rescue RestClient::ExceptionWithResponse => e
puts e
puts "Using the created NSD"
response = {'nsd' => {}}
response['nsd']['id'] = JSON.parse(nsd)['nsd']['id']
rescue => e
puts "Error...."
puts e
puts e.response
puts e.response.code
puts e.response.body
return 400, "Error creating VNF descriptor"
end
nsd_id = response['nsd']['id']
@e2e[:nsd_id] = response['nsd']['id']
response = {:vnfd_id => vnfd_id, :nsd_id => nsd_id }
puts response
return response
end
def delete_descriptors()
if !@e2e[:nsd_id].nil?
begin
response = RestClient.delete "#{@tenor}/network-services/" + @e2e[:nsd_id]
rescue => e
puts "Error removing network-service..."
puts e
# puts e.response
# puts e.response.code
#puts e.response.body
return 400, "Error removing the NS descriptor"
end
end
puts "Remving VNFD...."
if !@e2e[:vnfd_id].nil?
begin
response = RestClient.delete "#{@tenor}/vnfs/" + @e2e[:vnfd_id].to_s
rescue => e
puts e
puts e.response
puts e.response.code
puts e.response.body
return 400, "Error creating VNF descriptor"
end
end
puts "Descriptors removed correctly."
end
def create_pop(obj)
begin
response = JSON.parse(RestClient.post "#{@tenor}/pops/dc", obj.to_json, :content_type => :json)
rescue => e
puts "ERROR: "
puts e
begin
response = JSON.parse(RestClient.get "#{@tenor}/pops/dc/name/" + obj[:name])
rescue => e
puts "Failed getting PoP."
return 400, e
end
end
return response['id']
end
def remove_pops_if_exists(pop_name)
begin
response = RestClient.get "#{@tenor}/pops/dc/name/#{pop_name}"
rescue => e
puts e
return 400, e
end
pop = JSON.parse(response)
delete_pop(pop['id'])
end
def delete_pop(pop_id)
response, errors = RestClient.delete "#{@tenor}/pops/dc/#{pop_id}"
puts errors if errors
return 400, errors if errors
puts "Removing PoP.... #{pop_id}"
end
def recover_state(error)
puts "ERROR DETECTED! Recovering initial state...."
puts error
delete_descriptors()
@e2e[:pops].each do |pop|
delete_pop(pop[:id])
end
@e2e[:instances].each do |instances|
delete_instance(instances[:id])
end
exit
return 4000
end
puts "Creating end to end tests..."
response, errors = end_to_end_script()
puts "End to end test not completed." if errors
puts errors if errors
exit
puts "End to end test completed."