Skip to content

Commit

Permalink
Merge pull request #213 from pcarranzav/enable-docker-import
Browse files Browse the repository at this point in the history
Add importImage and its corresponding test.
  • Loading branch information
apocas committed Jan 20, 2016
2 parents 7b41d96 + 1d812bc commit 09b125f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ Docker.prototype.loadImage = function(file, opts, callback) {
});
};

/**
* Import image from a tar archive
* @param {String} file File
* @param {Object} opts Options (optional)
* @param {Function} callback Callback
*/
Docker.prototype.importImage = function(file, opts, callback) {
if (!callback && typeof opts === 'function') {
callback = opts;
opts = {};
}

opts.fromSrc = '-'

var self = this;
var optsf = {
path: '/images/create?',
method: 'POST',
options: opts,
file: file,
isStream: true,
statusCodes: {
200: true,
500: 'server error'
}
};

this.modem.dial(optsf, function(err, data) {
callback(err, data);
});
};

/**
* Verifies auth
* @param {Object} opts Options
Expand Down
24 changes: 23 additions & 1 deletion test/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ describe("#docker", function() {
function onFinished(err, output) {
if (err) return done(err);
expect(output).to.be.a('array');
done();
}

function onProgress(event) {
stream.destroy();
expect(event).to.be.ok;
done();
}
});
});
Expand Down Expand Up @@ -401,6 +401,27 @@ describe("#docker", function() {
});
});

describe("#importImage", function() {
it("should import an image from a tar archive", function(done) {
this.timeout(120000);

function handler(err, stream) {
expect(err).to.be.null;
expect(stream).to.be.ok;

stream.pipe(process.stdout, {
end: true
});

stream.on('end', function() {
done();
});
}
var data = require('fs').createReadStream('./test/empty.tar');
docker.importImage(data, handler);
});
});

describe("#listContainers", function() {
it("should list containers", function(done) {
this.timeout(5000);
Expand Down Expand Up @@ -510,6 +531,7 @@ describe("#docker", function() {

// after fn to cleanup created containers after testsuite execution
after(function(done) {
this.timeout(10000)
if (!created_containers.length) return done();
created_containers.forEach(function(container, index) {
container.remove(function(err, data) {
Expand Down
Binary file added test/empty.tar
Binary file not shown.

0 comments on commit 09b125f

Please sign in to comment.