|  | 
|  | 1 | +'use strict' | 
|  | 2 | + | 
|  | 3 | +require('loadenv')({ debugName: 'khronos:test' }) | 
|  | 4 | + | 
|  | 5 | +var chai = require('chai') | 
|  | 6 | +var assert = chai.assert | 
|  | 7 | +chai.use(require('chai-as-promised')) | 
|  | 8 | + | 
|  | 9 | +// external | 
|  | 10 | +var rabbitmq = require('models/rabbitmq') | 
|  | 11 | +var sinon = require('sinon') | 
|  | 12 | +require('sinon-as-promised')(require('bluebird')) | 
|  | 13 | + | 
|  | 14 | +// Internal | 
|  | 15 | +const MongoDB = require('models/mongodb') | 
|  | 16 | + | 
|  | 17 | +// internal (being tested) | 
|  | 18 | +var CleanupInstances = require('tasks/instances/cleanup') | 
|  | 19 | + | 
|  | 20 | +describe('khronos:instances:cleanup', function () { | 
|  | 21 | +  var mockInstances | 
|  | 22 | + | 
|  | 23 | +  beforeEach(function () { | 
|  | 24 | +    mockInstances = [ | 
|  | 25 | +      { | 
|  | 26 | +        _id: '1234' | 
|  | 27 | +      }, | 
|  | 28 | +      { | 
|  | 29 | +        _id: '5678' | 
|  | 30 | +      } | 
|  | 31 | +    ] | 
|  | 32 | +    sinon.stub(MongoDB.prototype, 'close').yieldsAsync() | 
|  | 33 | +    sinon.stub(MongoDB.prototype, 'connect').yieldsAsync() | 
|  | 34 | +    sinon.stub(MongoDB.prototype, 'fetchInstances').yieldsAsync(null, mockInstances) | 
|  | 35 | +    sinon.stub(rabbitmq, 'publishEvent').resolves() | 
|  | 36 | +  }) | 
|  | 37 | + | 
|  | 38 | +  afterEach(function () { | 
|  | 39 | +    MongoDB.prototype.close.restore() | 
|  | 40 | +    MongoDB.prototype.connect.restore() | 
|  | 41 | +    MongoDB.prototype.fetchInstances.restore() | 
|  | 42 | +    rabbitmq.publishEvent.restore() | 
|  | 43 | +  }) | 
|  | 44 | + | 
|  | 45 | +  describe('when there are instances to cleanup', function () { | 
|  | 46 | +    it('should fetch instances with the propery query parameters', function (done) { | 
|  | 47 | +      return assert.isFulfilled(CleanupInstances({})) | 
|  | 48 | +        .then(function () { | 
|  | 49 | +          sinon.assert.calledOnce(MongoDB.prototype.fetchInstances) | 
|  | 50 | +          sinon.assert.calledWith( | 
|  | 51 | +            MongoDB.prototype.fetchInstances, | 
|  | 52 | +            { | 
|  | 53 | +              masterPod: false, | 
|  | 54 | +              'contextVersion.created': { $lt: sinon.match.date }, | 
|  | 55 | +              $or: [ | 
|  | 56 | +                { isolated: { $exists: false } }, | 
|  | 57 | +                { isIsolationGroupMaster: true } | 
|  | 58 | +              ] | 
|  | 59 | +            } | 
|  | 60 | +          ) | 
|  | 61 | +        }) | 
|  | 62 | +        .asCallback(done) | 
|  | 63 | +    }) | 
|  | 64 | + | 
|  | 65 | +    it('should cleanup the old instances', function (done) { | 
|  | 66 | +      return assert.isFulfilled(CleanupInstances({})) | 
|  | 67 | +        .then(function () { | 
|  | 68 | +          sinon.assert.calledTwice(rabbitmq.publishEvent) | 
|  | 69 | +          sinon.assert.calledWith( | 
|  | 70 | +            rabbitmq.publishEvent, | 
|  | 71 | +            'instance.expired', | 
|  | 72 | +            { | 
|  | 73 | +              instanceId: '1234' | 
|  | 74 | +            } | 
|  | 75 | +          ) | 
|  | 76 | +          sinon.assert.calledWith( | 
|  | 77 | +            rabbitmq.publishEvent, | 
|  | 78 | +            'instance.expired', | 
|  | 79 | +            { | 
|  | 80 | +              instanceId: '5678' | 
|  | 81 | +            } | 
|  | 82 | +          ) | 
|  | 83 | +        }) | 
|  | 84 | +        .asCallback(done) | 
|  | 85 | +    }) | 
|  | 86 | +  }) | 
|  | 87 | +}) | 
0 commit comments