docker-mock
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -128,3 +128,3 @@ var _ = require('lodash'); | ||
app.get('/events', mw.notYetImplemented); | ||
app.get('/events', mw.getEvents); | ||
@@ -131,0 +131,0 @@ app.get('/images/:repository/get', mw.notYetImplemented); |
@@ -314,2 +314,34 @@ var _ = require('lodash'); | ||
}, | ||
getEvents: function (req, res) { | ||
var statuses = ['create', 'destroy', 'die', 'export', 'kill', 'pause', 'restart', 'start', 'stop', 'unpause', 'untag', 'delete']; | ||
var froms = ['sequenceiq/socat:latest', 'base:latest', 'nginx:latest']; | ||
// do streaming when params are not specified | ||
res.set('Content-Type', 'application/json'); | ||
var generateEvent = function () { | ||
var id = utils.randomId(); | ||
var from = froms[Math.floor(Math.random() * froms.length)]; | ||
var time = new Date().getTime(); | ||
var status = statuses[Math.floor(Math.random() * statuses.length)]; | ||
return JSON.stringify({ | ||
status: status, | ||
id: id, | ||
from: from, | ||
time: time | ||
}); | ||
}; | ||
// send reponse immediately something when params are there | ||
if (req.query.since || req.query.until) { | ||
var data = ''; | ||
for (var i = 0; i < 100; i++) { | ||
data += generateEvent(); | ||
} | ||
res.send(data); | ||
} else { | ||
res.write(generateEvent()); | ||
setInterval(function () { | ||
res.write(generateEvent()); | ||
}, 100); | ||
} | ||
}, | ||
getVersion: function (req, res, next) { | ||
@@ -316,0 +348,0 @@ res.json({ |
{ | ||
"name": "docker-mock", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Mock for Docker", | ||
@@ -44,2 +44,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"JSONStream": "^0.9.0", | ||
"async": "~0.9.0", | ||
@@ -46,0 +47,0 @@ "blanket": "^1.1.6", |
@@ -8,2 +8,3 @@ var dockerMock = require('../lib/index'); | ||
var createCount = require('callback-count'); | ||
var JSONStream = require('JSONStream'); | ||
var noop = function () {}; | ||
@@ -362,2 +363,52 @@ dockerMock.listen(5354); | ||
describe('events', function () { | ||
it('should return one time result when since is provided', function (done) { | ||
docker.getEvents({since: new Date().getTime()}, function (err, eventStream) { | ||
if (err) return done(err); | ||
var count = createCount(100, done); | ||
var i = 0; | ||
eventStream.pipe(JSONStream.parse()).on('data', function (json) { | ||
json.status.should.be.a.String; | ||
json.id.should.be.a.String; | ||
json.from.should.be.a.String; | ||
json.time.should.be.a.Number; | ||
count.next(); | ||
}) | ||
}); | ||
}); | ||
it('should return one time result when until is provided', function (done) { | ||
docker.getEvents({until: new Date().getTime()}, function (err, eventStream) { | ||
if (err) return done(err); | ||
var count = createCount(100, done); | ||
var i = 0; | ||
eventStream.pipe(JSONStream.parse()).on('data', function (json) { | ||
json.status.should.be.a.String; | ||
json.id.should.be.a.String; | ||
json.from.should.be.a.String; | ||
json.time.should.be.a.Number; | ||
count.next(); | ||
}) | ||
}); | ||
}); | ||
it('should stream events', function (done) { | ||
docker.getEvents(function (err, eventStream) { | ||
if (err) return done(err); | ||
var count = createCount(10, done); | ||
eventStream.on('data', function (data) { | ||
var json = JSON.parse(data.toString()); | ||
json.status.should.be.a.String; | ||
json.id.should.be.a.String; | ||
json.from.should.be.a.String; | ||
json.time.should.be.a.Number; | ||
count.next(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('misc', function () { | ||
@@ -364,0 +415,0 @@ describe('/info', function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
154651
991
11