1 config = require 'config'
2 assert = require 'assert'
3 forge = require 'yangforge'
4 app = forge.load '!yaml ../promise.yaml', async: false, pkgdir: __dirname
6 # this is javascript promise framework and not related to opnfv-promise
7 promise = require 'promise'
14 # in the future with YF 0.12.x
15 # app = forge.load('..').build('test')
17 # app.use 'proxy', target: x.x.x.x:5050, interface: 'restjson'
19 describe "promise", ->
21 # ensure we have valid OpenStack environment to test against
23 config.get 'openstack.auth.endpoint'
25 throw new Error "missing OpenStack environmental variables"
28 # below 'provider' is used across test suites
31 # Test Scenario 00 (FUTURE)
32 # describe "prepare OpenStack for testing", ->
34 # # ensure we have valid OpenStack environment to test against
36 # config.get 'openstack.auth.url'
38 # throw new Error "missing OpenStack environmental variables"
40 # os = forge.load '!yaml ../openstack.yaml', async: false, pkgdir: __dirname
41 # app.attach 'openstack', os.access 'openstack'
44 # describe "authenticate", ->
45 # it "should retrieve available service catalog", (done) ->
46 # app.access('openstack').invoke 'authenticate'
50 # .catch (err) -> done err
52 # describe "create-tenant", ->
53 # # create a new tenant for testing purposes
55 # describe "upload-image", ->
56 # # upload a new test image
61 describe "register OpenStack into resource pool", ->
65 describe "add-provider", ->
66 it "should add a new OpenStack provider without error", (done) ->
69 auth = config.get 'openstack.auth'
70 auth['provider-type'] = 'openstack'
72 app.access('opnfv-promise').invoke 'add-provider', auth
74 res.get('result').should.equal 'ok'
75 provider = id: res.get('provider-id')
76 # HACK - we delay by a second to allow time for discovering capacity and flavors
78 .catch (err) -> done err
80 it "should update promise.providers with a new entry", ->
81 app.get('opnfv-promise.promise.providers').should.have.length(1)
83 it "should contain a new ResourceProvider record in the store", ->
84 assert provider?.id?, "unable to check without ID"
85 provider = app.access('opnfv-promise').find('ResourceProvider', provider.id)
89 describe "increase-capacity", ->
90 it "should add more capacity to the reservation service without error", (done) ->
91 app.access('opnfv-promise').invoke 'increase-capacity',
99 res.get('result').should.equal 'ok'
100 pool = id: res.get('pool-id')
102 .catch (err) -> done err
104 it "should update promise.pools with a new entry", ->
105 app.get('opnfv-promise.promise.pools').should.have.length(1)
107 it "should contain a ResourcePool record in the store", ->
108 assert pool?.id?, "unable to check without ID"
109 pool = app.access('opnfv-promise').find('ResourcePool', pool.id)
113 describe "query-capacity", ->
114 it "should report total collections and utilizations", (done) ->
115 app.access('opnfv-promise').invoke 'query-capacity',
118 res.get('collections').should.be.Array
119 res.get('collections').length.should.be.above(0)
120 res.get('utilization').should.be.Array
121 res.get('utilization').length.should.be.above(0)
123 .catch (err) -> done err
125 it "should contain newly added capacity pool", (done) ->
126 app.access('opnfv-promise').invoke 'query-capacity',
129 res.get('collections').should.containEql "ResourcePool:#{pool.id}"
131 .catch (err) -> done err
134 describe "allocation without reservation", ->
137 describe "create-instance", ->
138 allocation = undefined
139 instance_id = undefined
142 # XXX - need to determine image and flavor to use in the given provider for this test
144 "unable to execute without registered 'provider'"
146 it "should create a new server in target provider without error", (done) ->
148 test = config.get 'openstack.test'
149 app.access('opnfv-promise').invoke 'create-instance',
150 'provider-id': provider.id
151 name: 'promise-test-no-reservation'
154 networks: [ test.network ]
157 res.get('result').should.equal 'ok'
158 instance_id = res.get('instance-id')
160 .catch (err) -> done err
162 it "should update promise.allocations with a new entry", ->
163 app.get('opnfv-promise.promise.allocations').length.should.be.above(0)
165 it "should contain a new ResourceAllocation record in the store", ->
166 assert instance_id?, "unable to check without ID"
167 allocation = app.access('opnfv-promise').find('ResourceAllocation', instance_id)
170 it "should reference the created server ID from the provider", ->
171 assert allocation?, "unable to check without record"
172 allocation.get('instance-ref').should.have.property('provider')
173 allocation.get('instance-ref').should.have.property('server')
175 it "should have low priority state", ->
176 assert allocation?, "unable to check without record"
177 allocation.get('priority').should.equal 'low'
180 describe "allocation using reservation for immediate use", ->
181 reservation = undefined
184 describe "create-reservation", ->
185 it "should create reservation record (no start/end) without error", (done) ->
186 app.access('opnfv-promise').invoke 'create-reservation',
193 res.get('result').should.equal 'ok'
194 reservation = id: res.get('reservation-id')
196 .catch (err) -> done err
198 it "should update promise.reservations with a new entry", ->
199 app.get('opnfv-promise.promise.reservations').length.should.be.above(0)
201 it "should contain a new ResourceReservation record in the store", ->
202 assert reservation?.id?, "unable to check without ID"
203 reservation = app.access('opnfv-promise').find('ResourceReservation', reservation.id)
207 describe "create-instance", ->
208 allocation = undefined
212 "unable to execute without registered 'provider'"
214 "unable to execute without valid reservation record"
216 it "should create a new server in target provider (with reservation) without error", (done) ->
218 test = config.get 'openstack.test'
219 app.access('opnfv-promise').invoke 'create-instance',
220 'provider-id': provider.id
221 name: 'promise-test-reservation'
224 networks: [ test.network ]
225 'reservation-id': reservation.id
228 res.get('result').should.equal 'ok'
229 allocation = id: res.get('instance-id')
231 .catch (err) -> done err
233 it "should contain a new ResourceAllocation record in the store", ->
234 assert allocation?.id?, "unable to check without ID"
235 allocation = app.access('opnfv-promise').find('ResourceAllocation', allocation.id)
238 it "should be referenced in the reservation record", ->
239 assert reservation? and allocation?, "unable to check without records"
240 reservation.get('allocations').should.containEql allocation.id
242 it "should have high priority state", ->
243 assert allocation?, "unable to check without record"
244 allocation.get('priority').should.equal 'high'
247 describe "reservation for future use", ->
248 reservation = undefined
251 # 7 days in the future
252 start.setTime (start.getTime() + 7*60*60*1000)
253 # 8 days in the future
254 end.setTime (end.getTime() + 8*60*60*1000)
257 describe "create-reservation", ->
258 it "should create reservation record (for future) without error", (done) ->
259 app.access('opnfv-promise').invoke 'create-reservation',
260 start: start.toJSON()
268 res.get('result').should.equal 'ok'
269 reservation = id: res.get('reservation-id')
271 .catch (err) -> done err
273 it "should update promise.reservations with a new entry", ->
274 app.get('opnfv-promise.promise.reservations').length.should.be.above(0)
276 it "should contain a new ResourceReservation record in the store", ->
277 assert reservation?.id?, "unable to check without ID"
278 reservation = app.access('opnfv-promise').find('ResourceReservation', reservation.id)
282 describe "query-reservation", ->
283 it "should contain newly created future reservation", (done) ->
284 app.access('opnfv-promise').invoke 'query-reservation',
286 start: start.toJSON()
289 res.get('reservations').should.containEql reservation.id
291 .catch (err) -> done err
294 describe "update-reservation", ->
295 it "should modify existing reservation without error", (done) ->
296 app.access('opnfv-promise').invoke 'update-reservation',
297 'reservation-id': reservation.id
304 res.get('result').should.equal 'ok'
306 .catch (err) -> done err
309 describe "cancel-reservation", ->
310 it "should modify existing reservation without error", (done) ->
311 app.access('opnfv-promise').invoke 'cancel-reservation',
312 'reservation-id': reservation.id
314 res.get('result').should.equal 'ok'
316 .catch (err) -> done err
318 it "should no longer contain record of the deleted reservation", ->
319 assert reservation?.id?, "unable to check without ID"
320 reservation = app.access('opnfv-promise').find('ResourceReservation', reservation.id)
321 assert not reservation?
324 describe "capacity planning", ->
327 describe "decrease-capacity", ->
330 # 30 days in the future
331 start.setTime (start.getTime() + 30*60*60*1000)
332 # 45 days in the future
333 end.setTime (end.getTime() + 45*60*60*1000)
335 it "should decrease available capacity from a provider in the future", (done) ->
336 app.access('opnfv-promise').invoke 'decrease-capacity',
342 start: start.toJSON()
345 res.get('result').should.equal 'ok'
347 .catch (err) -> done err
350 describe "increase-capacity", ->
353 # 14 days in the future
354 start.setTime (start.getTime() + 14*60*60*1000)
355 # 21 days in the future
356 end.setTime (end.getTime() + 21*60*60*1000)
358 it "should increase available capacity from a provider in the future", (done) ->
359 app.access('opnfv-promise').invoke 'decrease-capacity',
365 start: start.toJSON()
368 res.get('result').should.equal 'ok'
370 .catch (err) -> done err
372 # TC-13 (Should improve this TC)
373 describe "query-capacity", ->
374 it "should report available collections and utilizations", (done) ->
375 app.access('opnfv-promise').invoke 'query-capacity',
376 capacity: 'available'
378 res.get('collections').should.be.Array
379 res.get('collections').length.should.be.above(0)
380 res.get('utilization').should.be.Array
381 res.get('utilization').length.should.be.above(0)
383 .catch (err) -> done err
386 describe "reservation with conflict", ->
388 describe "create-reservation", ->
389 it "should fail to create immediate reservation record with proper error", (done) ->
390 app.access('opnfv-promise').invoke 'create-reservation',
396 res.get('result').should.equal 'conflict'
398 .catch (err) -> done err
400 it "should fail to create future reservation record with proper error", (done) ->
402 # 30 days in the future
403 start.setTime (start.getTime() + 30*60*60*1000)
405 app.access('opnfv-promise').invoke 'create-reservation',
410 start: start.toJSON()
412 res.get('result').should.equal 'conflict'
414 .catch (err) -> done err
417 describe "cleanup test allocations", ->
418 allocations = undefined
420 allocations = app.get('opnfv-promise.promise.allocations')
423 allocations.length.should.be.above(0)
425 describe "destroy-instance", ->
426 it "should successfully destroy all allocations", (done) ->
428 promises = allocations.map (x) ->
429 app.access('opnfv-promise').invoke 'destroy-instance',
435 x.get('result').should.equal 'ok'
437 .catch (err) -> done err