navy
Version:
Quick and powerful development environments using Docker and Docker Compose
136 lines (134 loc) • 4.51 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _chai = require("chai");
var _sinon = _interopRequireDefault(require("sinon"));
var _proxyquire = _interopRequireDefault(require("proxyquire"));
/* eslint-env mocha */
// NOTE: hasUpdate has a defect at line 17 in `../has-update`: when the
// supplied navyFile is null/undefined or has no
// `ignoreUnauthorizedRequestsForRegistries` array, `R.includes(registry,
// undefined)` throws (in current ramda). The tests below provide a valid
// navyFile shape to exercise the supported call patterns.
function withMocks({
inspect,
getFatManifest = _sinon.default.stub()
}) {
const docker = {
getImage: _sinon.default.stub().returns({
inspect
})
};
const stubs = {
'./docker-client': docker,
'../client/registry/get-fat-manifest': getFatManifest
};
return {
docker,
getFatManifest,
hasUpdate: _proxyquire.default.noCallThru()('../has-update', stubs)
};
}
describe('has-update', function () {
it('should return false when the remote digest matches one of the local RepoDigests', async function () {
const inspect = _sinon.default.stub().resolves({
RepoDigests: ['library/node@sha256:abc', 'library/node@sha256:other']
});
const {
hasUpdate
} = withMocks({
inspect,
getFatManifest: _sinon.default.stub().resolves({
tag: 'sha256:abc'
})
});
const result = await hasUpdate('library/node:lts', 'image-id', {
ignoreUnauthorizedRequestsForRegistries: []
});
(0, _chai.expect)(result).to.equal(false);
});
it('should return true when the remote digest is not in the local RepoDigests', async function () {
const inspect = _sinon.default.stub().resolves({
RepoDigests: ['library/node@sha256:old']
});
const {
hasUpdate
} = withMocks({
inspect,
getFatManifest: _sinon.default.stub().resolves({
tag: 'sha256:new'
})
});
const result = await hasUpdate('library/node:lts', 'image-id', {
ignoreUnauthorizedRequestsForRegistries: []
});
(0, _chai.expect)(result).to.equal(true);
});
it('should return "INVALID_REMOTE" when getFatManifest fails', async function () {
const inspect = _sinon.default.stub().resolves({
RepoDigests: []
});
const {
hasUpdate
} = withMocks({
inspect,
getFatManifest: _sinon.default.stub().rejects(new Error('boom'))
});
const result = await hasUpdate('library/node:lts', 'image-id', {
ignoreUnauthorizedRequestsForRegistries: []
});
(0, _chai.expect)(result).to.equal('INVALID_REMOTE');
});
it('should return "INVALID_REMOTE" when docker.getImage().inspect() fails', async function () {
const inspect = _sinon.default.stub().rejects(new Error('no such image'));
const {
hasUpdate
} = withMocks({
inspect
});
const result = await hasUpdate('library/node:lts', 'image-id', {
ignoreUnauthorizedRequestsForRegistries: []
});
(0, _chai.expect)(result).to.equal('INVALID_REMOTE');
});
it('should pass allowUnauthorizedRequest=true when navyFile lists the registry as ignored', async function () {
const inspect = _sinon.default.stub().resolves({
RepoDigests: ['someregistry.com/some/image@sha256:abc']
});
const getFatManifest = _sinon.default.stub().resolves({
tag: 'sha256:abc'
});
const {
hasUpdate
} = withMocks({
inspect,
getFatManifest
});
await hasUpdate('someregistry.com/some/image:1', 'image-id', {
ignoreUnauthorizedRequestsForRegistries: ['someregistry.com']
});
(0, _chai.expect)(getFatManifest.firstCall.args[0]).to.eql({
allowUnauthorizedRequest: true,
registry: 'someregistry.com',
repository: 'some/image',
tag: '1'
});
});
it('should pass allowUnauthorizedRequest=false when registry is not in the ignore list', async function () {
const inspect = _sinon.default.stub().resolves({
RepoDigests: []
});
const getFatManifest = _sinon.default.stub().resolves({
tag: 'sha256:abc'
});
const {
hasUpdate
} = withMocks({
inspect,
getFatManifest
});
await hasUpdate('someregistry.com/some/image:1', 'image-id', {
ignoreUnauthorizedRequestsForRegistries: ['otherregistry.com']
});
(0, _chai.expect)(getFatManifest.firstCall.args[0].allowUnauthorizedRequest).to.equal(false);
});
});