summaryrefslogtreecommitdiffstats
path: root/source/test/promise-intents.coffee
blob: db7e8d6eb49336c9273717754aaf4a7331082bfc (plain)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#
# Author: Peter K. Lee (peter@corenova.com)
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#
config = require 'config'
assert = require 'assert'
forge  = require 'yangforge'
app = forge.load '!yaml ../promise.yaml', async: false, pkgdir: __dirname

# this is javascript promise framework and not related to opnfv-promise
promise = require 'promise'

if process.env.DEBUG
  debug = console.log
else
  debug = ->

# in the future with YF 0.12.x
# app = forge.load('..').build('test')
# app.set config
# app.use 'proxy', target: x.x.x.x:5050, interface: 'restjson'

describe "promise", ->
  before ->
    # ensure we have valid OpenStack environment to test against
    try
      config.get 'openstack.auth.endpoint'
    catch e
      throw new Error "missing OpenStack environmental variables"


  # below 'provider' is used across test suites
  provider = undefined

  # Test Scenario 00 (FUTURE)
  # describe "prepare OpenStack for testing", ->
  #   before (done) ->
  #     # ensure we have valid OpenStack environment to test against
  #     try
  #       config.get 'openstack.auth.url'
  #     catch e
  #       throw new Error "missing OpenStack environmental variables"

  #     os = forge.load '!yaml ../openstack.yaml', async: false, pkgdir: __dirname
  #     app.attach 'openstack', os.access 'openstack'
  #     app.set config

  #   describe "authenticate", ->
  #     it "should retrieve available service catalog", (done) ->
  #       app.access('openstack').invoke 'authenticate'
  #       .then (res) ->

  #         done()
  #       .catch (err) -> done err

  #   describe "create-tenant", ->
  #     # create a new tenant for testing purposes

  #   describe "upload-image", ->
  #     # upload a new test image



  # Test Scenario 01
  describe "register OpenStack into resource pool", ->
    pool = undefined

    # TC-01
    describe "add-provider", ->
      it "should add a new OpenStack provider without error", (done) ->
        @timeout 5000

        auth = config.get 'openstack.auth'
        auth['provider-type'] = 'openstack'

        app.access('opnfv-promise').invoke 'add-provider', auth
        .then (res) ->
          res.get('result').should.equal 'ok'
          provider = id: res.get('provider-id')
          # HACK - we delay by a second to allow time for discovering capacity and flavors
          setTimeout done, 1000
        .catch (err) -> done err

      it "should update promise.providers with a new entry", ->
        app.get('opnfv-promise.promise.providers').should.have.length(1)

      it "should contain a new ResourceProvider record in the store", ->
        assert provider?.id?, "unable to check without ID"
        provider = app.access('opnfv-promise').find('ResourceProvider', provider.id)
        assert provider?

    # TC-02
    describe "increase-capacity", ->
      it "should add more capacity to the reservation service without error", (done) ->
        app.access('opnfv-promise').invoke 'increase-capacity',
          source: provider
          capacity:
            cores: 20
            ram: 51200
            instances: 10
            addresses: 10
        .then (res) ->
          res.get('result').should.equal 'ok'
          pool = id: res.get('pool-id')
          done()
        .catch (err) -> done err

      it "should update promise.pools with a new entry", ->
        app.get('opnfv-promise.promise.pools').should.have.length(1)

      it "should contain a ResourcePool record in the store", ->
        assert pool?.id?, "unable to check without ID"
        pool = app.access('opnfv-promise').find('ResourcePool', pool.id)
        assert pool?

    # TC-03
    describe "query-capacity", ->
      it "should report total collections and utilizations", (done) ->
        app.access('opnfv-promise').invoke 'query-capacity',
          capacity: 'total'
        .then (res) ->
          res.get('collections').should.be.Array
          res.get('collections').length.should.be.above(0)
          res.get('utilization').should.be.Array
          res.get('utilization').length.should.be.above(0)
          done()
        .catch (err) -> done err

      it "should contain newly added capacity pool", (done) ->
        app.access('opnfv-promise').invoke 'query-capacity',
          capacity: 'total'
        .then (res) ->
          res.get('collections').should.containEql "ResourcePool:#{pool.id}"
          done()
        .catch (err) -> done err

  # Test Scenario 02
  describe "allocation without reservation", ->

    # TC-04
    describe "create-instance", ->
      allocation = undefined
      instance_id = undefined

      before ->
        # XXX - need to determine image and flavor to use in the given provider for this test
        assert provider?,
          "unable to execute without registered 'provider'"

      it "should create a new server in target provider without error", (done) ->
        @timeout 5000
        test = config.get 'openstack.test'
        app.access('opnfv-promise').invoke 'create-instance',
          'provider-id': provider.id
          name: 'promise-test-no-reservation'
          image:   test.image
          flavor:  test.flavor
          networks: [ test.network ]
        .then (res) ->
          debug res.get()
          res.get('result').should.equal 'ok'
          instance_id = res.get('instance-id')
          done()
        .catch (err) -> done err

      it "should update promise.allocations with a new entry", ->
        app.get('opnfv-promise.promise.allocations').length.should.be.above(0)

      it "should contain a new ResourceAllocation record in the store", ->
        assert instance_id?, "unable to check without ID"
        allocation = app.access('opnfv-promise').find('ResourceAllocation', instance_id)
        assert allocation?

      it "should reference the created server ID from the provider", ->
        assert allocation?, "unable to check without record"
        allocation.get('instance-ref').should.have.property('provider')
        allocation.get('instance-ref').should.have.property('server')

      it "should have low priority state", ->
        assert allocation?, "unable to check without record"
        allocation.get('priority').should.equal 'low'

  # Test Scenario 03
  describe "allocation using reservation for immediate use", ->
    reservation = undefined

    # TC-05
    describe "create-reservation", ->
      it "should create reservation record (no start/end) without error", (done) ->
        app.access('opnfv-promise').invoke 'create-reservation',
          capacity:
            cores: 5
            ram: 25600
            addresses: 3
            instances: 3
        .then (res) ->
          res.get('result').should.equal 'ok'
          reservation = id: res.get('reservation-id')
          done()
        .catch (err) -> done err

      it "should update promise.reservations with a new entry", ->
        app.get('opnfv-promise.promise.reservations').length.should.be.above(0)

      it "should contain a new ResourceReservation record in the store", ->
        assert reservation?.id?, "unable to check without ID"
        reservation = app.access('opnfv-promise').find('ResourceReservation', reservation.id)
        assert reservation?

    # TC-06
    describe "create-instance", ->
      allocation = undefined

      before ->
        assert provider?,
          "unable to execute without registered 'provider'"
        assert reservation?,
          "unable to execute without valid reservation record"

      it "should create a new server in target provider (with reservation) without error", (done) ->
        @timeout 5000
        test = config.get 'openstack.test'
        app.access('opnfv-promise').invoke 'create-instance',
          'provider-id': provider.id
          name: 'promise-test-reservation'
          image:  test.image
          flavor: test.flavor
          networks: [ test.network ]
          'reservation-id': reservation.id
        .then (res) ->
          debug res.get()
          res.get('result').should.equal 'ok'
          allocation = id: res.get('instance-id')
          done()
        .catch (err) -> done err

      it "should contain a new ResourceAllocation record in the store", ->
        assert allocation?.id?, "unable to check without ID"
        allocation = app.access('opnfv-promise').find('ResourceAllocation', allocation.id)
        assert allocation?

      it "should be referenced in the reservation record", ->
        assert reservation? and allocation?, "unable to check without records"
        reservation.get('allocations').should.containEql allocation.id

      it "should have high priority state", ->
        assert allocation?, "unable to check without record"
        allocation.get('priority').should.equal 'high'

  # Test Scenario 04
  describe "reservation for future use", ->
    reservation = undefined
    start = new Date
    end   = new Date
    # 7 days in the future
    start.setTime (start.getTime() + 7*60*60*1000)
    # 8 days in the future
    end.setTime (end.getTime() + 8*60*60*1000)

    # TC-07
    describe "create-reservation", ->
      it "should create reservation record (for future) without error", (done) ->
        app.access('opnfv-promise').invoke 'create-reservation',
          start: start.toJSON()
          end: end.toJSON()
          capacity:
            cores: 1
            ram: 12800
            addresses: 1
            instances: 1
        .then (res) ->
          res.get('result').should.equal 'ok'
          reservation = id: res.get('reservation-id')
          done()
        .catch (err) -> done err

      it "should update promise.reservations with a new entry", ->
        app.get('opnfv-promise.promise.reservations').length.should.be.above(0)

      it "should contain a new ResourceReservation record in the store", ->
        assert reservation?.id?, "unable to check without ID"
        reservation = app.access('opnfv-promise').find('ResourceReservation', reservation.id)
        assert reservation?

    # TC-08
    describe "query-reservation", ->
      it "should contain newly created future reservation", (done) ->
        app.access('opnfv-promise').invoke 'query-reservation',
          window:
            start: start.toJSON()
            end: end.toJSON()
        .then (res) ->
          res.get('reservations').should.containEql reservation.id
          done()
        .catch (err) -> done err

    # TC-09
    describe "update-reservation", ->
      it "should modify existing reservation without error", (done) ->
        app.access('opnfv-promise').invoke 'update-reservation',
          'reservation-id': reservation.id
          capacity:
            cores: 3
            ram: 12800
            addresses: 2
            instances: 2
        .then (res) ->
          res.get('result').should.equal 'ok'
          done()
        .catch (err) -> done err

    # TC-10
    describe "cancel-reservation", ->
      it "should modify existing reservation without error", (done) ->
        app.access('opnfv-promise').invoke 'cancel-reservation',
          'reservation-id': reservation.id
        .then (res) ->
          res.get('result').should.equal 'ok'
          done()
        .catch (err) -> done err

      it "should no longer contain record of the deleted reservation", ->
        assert reservation?.id?, "unable to check without ID"
        reservation = app.access('opnfv-promise').find('ResourceReservation', reservation.id)
        assert not reservation?

  # Test Scenario 05
  describe "capacity planning", ->

    # TC-11
    describe "decrease-capacity", ->
      start = new Date
      end   = new Date
      # 30 days in the future
      start.setTime (start.getTime() + 30*60*60*1000)
      # 45 days in the future
      end.setTime (end.getTime() + 45*60*60*1000)

      it "should decrease available capacity from a provider in the future", (done) ->
        app.access('opnfv-promise').invoke 'decrease-capacity',
          source: provider
          capacity:
            cores: 5
            ram: 17920
            instances: 5
          start: start.toJSON()
          end: end.toJSON()
        .then (res) ->
          res.get('result').should.equal 'ok'
          done()
        .catch (err) -> done err

    # TC-12
    describe "increase-capacity", ->
      start = new Date
      end   = new Date
      # 14 days in the future
      start.setTime (start.getTime() + 14*60*60*1000)
      # 21 days in the future
      end.setTime (end.getTime() + 21*60*60*1000)

      it "should increase available capacity from a provider in the future", (done) ->
        app.access('opnfv-promise').invoke 'decrease-capacity',
          source: provider
          capacity:
            cores: 1
            ram: 3584
            instances: 1
          start: start.toJSON()
          end: end.toJSON()
        .then (res) ->
          res.get('result').should.equal 'ok'
          done()
        .catch (err) -> done err

    # TC-13 (Should improve this TC)
    describe "query-capacity", ->
      it "should report available collections and utilizations", (done) ->
        app.access('opnfv-promise').invoke 'query-capacity',
          capacity: 'available'
        .then (res) ->
          res.get('collections').should.be.Array
          res.get('collections').length.should.be.above(0)
          res.get('utilization').should.be.Array
          res.get('utilization').length.should.be.above(0)
          done()
        .catch (err) -> done err

  # Test Scenario 06
  describe "reservation with conflict", ->
    # TC-14
    describe "create-reservation", ->
      it "should fail to create immediate reservation record with proper error", (done) ->
        app.access('opnfv-promise').invoke 'create-reservation',
          capacity:
            cores: 5
            ram: 17920
            instances: 10
        .then (res) ->
          res.get('result').should.equal 'conflict'
          done()
        .catch (err) -> done err

      it "should fail to create future reservation record with proper error", (done) ->
        start = new Date
        # 30 days in the future
        start.setTime (start.getTime() + 30*60*60*1000)

        app.access('opnfv-promise').invoke 'create-reservation',
          capacity:
            cores: 5
            ram: 17920
            instances: 10
          start: start.toJSON()
        .then (res) ->
          res.get('result').should.equal 'conflict'
          done()
        .catch (err) -> done err

  # Test Scenario 07
  describe "cleanup test allocations", ->
    allocations = undefined
    before ->
      allocations = app.get('opnfv-promise.promise.allocations')
      debug provider.get()
      debug allocations
      allocations.length.should.be.above(0)

    describe "destroy-instance", ->
      it "should successfully destroy all allocations", (done) ->
        @timeout 5000
        promises = allocations.map (x) ->
          app.access('opnfv-promise').invoke 'destroy-instance',
            'instance-id': x.id
        promise.all promises
        .then (res) ->
          res.forEach (x) ->
            debug x.get()
            x.get('result').should.equal 'ok'
          done()
        .catch (err) -> done err