2017-03-07 19:25:18 +00:00
|
|
|
import { DockerfileServiceImpl, DockerfileInfoImpl } from './dockerfile.service.impl';
|
|
|
|
import Spy = jasmine.Spy;
|
|
|
|
|
|
|
|
|
2017-03-08 01:34:43 +00:00
|
|
|
describe("DockerfileServiceImpl", () => {
|
|
|
|
var dockerfileServiceImpl: DockerfileServiceImpl;
|
|
|
|
var dataFileServiceMock: any;
|
|
|
|
var configMock: any;
|
|
|
|
var fileReaderMock: FileReader;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dataFileServiceMock = jasmine.createSpyObj('dataFileServiceMock', [
|
|
|
|
'readDataArrayAsPossibleArchive',
|
|
|
|
'arrayToString',
|
|
|
|
'blobToString',
|
|
|
|
]);
|
|
|
|
configMock = jasmine.createSpyObj('configMock', ['getDomain']);
|
|
|
|
fileReaderMock = new FileReader();
|
|
|
|
dockerfileServiceImpl = new DockerfileServiceImpl(dataFileServiceMock, configMock, () => fileReaderMock);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getDockerfile", () => {
|
|
|
|
var file: any;
|
2017-03-08 05:46:12 +00:00
|
|
|
var invalidArchiveFile: any[];
|
|
|
|
var validArchiveFile: any[];
|
2017-03-08 01:34:43 +00:00
|
|
|
var readAsFileBufferSpy: Spy;
|
2017-03-08 05:46:12 +00:00
|
|
|
var forDataSpy: Spy;
|
2017-03-08 01:34:43 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
failure([]);
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
dataFileServiceMock.arrayToString.and.callFake((buf, callback) => {
|
|
|
|
var contents: string = "";
|
|
|
|
callback(contents);
|
|
|
|
});
|
|
|
|
|
|
|
|
dataFileServiceMock.blobToString.and.callFake((blob, callback) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
callback(blob.toString());
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
2017-03-08 05:46:12 +00:00
|
|
|
forDataSpy = spyOn(DockerfileInfoImpl, "forData").and.returnValue(new DockerfileInfoImpl(file, configMock));
|
2017-03-08 01:34:43 +00:00
|
|
|
readAsFileBufferSpy = spyOn(fileReaderMock, "readAsArrayBuffer").and.callFake(() => {
|
|
|
|
var event: any = {target: {result: file}};
|
|
|
|
fileReaderMock.onload(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
file = "FROM quay.io/coreos/nginx:latest";
|
2017-03-08 05:46:12 +00:00
|
|
|
validArchiveFile = [{name: 'Dockerfile', toBlob: jasmine.createSpy('toBlobSpy').and.returnValue(file)}];
|
|
|
|
invalidArchiveFile = [{name: 'main.exe', toBlob: jasmine.createSpy('toBlobSpy').and.returnValue("")}];
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("calls datafile service to read given file as possible archive file", (done) => {
|
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
expect(readAsFileBufferSpy.calls.argsFor(0)[0]).toEqual(file);
|
|
|
|
expect(dataFileServiceMock.readDataArrayAsPossibleArchive).toHaveBeenCalled();
|
2017-03-08 01:34:43 +00:00
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
fail("Should not invoke failure callback");
|
2017-03-08 01:34:43 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls datafile service to convert file to string if given file is not an archive", (done) => {
|
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
expect(dataFileServiceMock.arrayToString.calls.argsFor(0)[0]).toEqual(file);
|
2017-03-08 01:34:43 +00:00
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
fail("Should not invoke success callback");
|
2017-03-08 01:34:43 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls failure callback if given non-archive file that is not a valid Dockerfile", (done) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
forDataSpy.and.returnValue(null);
|
2017-03-08 01:34:43 +00:00
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
|
|
fail("Should not invoke success callback");
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
|
|
|
expect(error).toEqual('File chosen is not a valid Dockerfile');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls success callback with new DockerfileInfoImpl instance if given valid Dockerfile", (done) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
|
|
expect(dockerfile).toBeDefined();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
|
|
|
fail('Should not invoke failure callback');
|
|
|
|
done();
|
|
|
|
});
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("calls failure callback if given archive file with no Dockerfile present in root directory", (done) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
|
|
|
success(invalidArchiveFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
|
|
fail("Should not invoke success callback");
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
|
|
|
expect(error).toEqual('No Dockerfile found in root of archive');
|
|
|
|
done();
|
|
|
|
});
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("calls datafile service to convert blob to string if given file is an archive", (done) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
|
|
|
success(validArchiveFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
|
|
expect(validArchiveFile[0].toBlob).toHaveBeenCalled();
|
|
|
|
expect(dataFileServiceMock.blobToString.calls.argsFor(0)[0]).toEqual(validArchiveFile[0].toBlob());
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
|
|
|
fail("Should not invoke success callback");
|
|
|
|
done();
|
|
|
|
});
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("calls failure callback if given archive file with invalid Dockerfile", (done) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
forDataSpy.and.returnValue(null);
|
|
|
|
invalidArchiveFile[0].name = 'Dockerfile';
|
|
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
|
|
|
success(invalidArchiveFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
|
|
fail("Should not invoke success callback");
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
|
|
|
expect(error).toEqual('Dockerfile inside archive is not a valid Dockerfile');
|
|
|
|
done();
|
|
|
|
});
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("calls success callback with new DockerfileInfoImpl instance if given archive with valid Dockerfile", (done) => {
|
2017-03-08 05:46:12 +00:00
|
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
|
|
|
success(validArchiveFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
|
|
expect(dockerfile).toBeDefined();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(error: Event | string) => {
|
|
|
|
fail('Should not invoke failure callback');
|
|
|
|
done();
|
|
|
|
});
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extractDockerfile", () => {
|
2017-03-08 05:46:12 +00:00
|
|
|
// TODO: TDD promise-based method with same functionality as getDockerfile
|
2017-03-08 01:34:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-03-07 19:25:18 +00:00
|
|
|
describe("DockerfileInfoImpl", () => {
|
|
|
|
var dockerfileInfoImpl: DockerfileInfoImpl;
|
|
|
|
var contents: string;
|
|
|
|
var configMock: any;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
contents = "";
|
|
|
|
configMock = jasmine.createSpyObj('configMock', ['getDomain']);
|
|
|
|
dockerfileInfoImpl = new DockerfileInfoImpl(contents, configMock);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("forData", () => {
|
|
|
|
|
|
|
|
it("returns null if given contents do not contain a 'FROM' command", () => {
|
|
|
|
expect(DockerfileInfoImpl.forData(contents, configMock)).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns a new DockerfileInfoImpl instance if given contents are valid", () => {
|
|
|
|
contents = "FROM quay.io/coreos/nginx";
|
|
|
|
|
|
|
|
expect(DockerfileInfoImpl.forData(contents, configMock) instanceof DockerfileInfoImpl).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getRegistryBaseImage", () => {
|
2017-03-08 01:34:43 +00:00
|
|
|
var domain: string;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
domain = "quay.io";
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns null if instance's contents do not contain a 'FROM' command", () => {
|
|
|
|
var getBaseImageSpy: Spy = spyOn(dockerfileInfoImpl, "getBaseImage").and.returnValue(null);
|
|
|
|
|
|
|
|
expect(dockerfileInfoImpl.getRegistryBaseImage()).toBe(null);
|
|
|
|
expect(getBaseImageSpy).toHaveBeenCalled();
|
|
|
|
});
|
2017-03-07 19:25:18 +00:00
|
|
|
|
2017-03-08 01:34:43 +00:00
|
|
|
it("returns null if the domain of the instance's config does not match that of the base image", () => {
|
|
|
|
configMock.getDomain.and.returnValue(domain);
|
|
|
|
spyOn(dockerfileInfoImpl, "getBaseImage").and.returnValue('host.com');
|
2017-03-07 19:25:18 +00:00
|
|
|
|
2017-03-08 01:34:43 +00:00
|
|
|
expect(dockerfileInfoImpl.getRegistryBaseImage()).toBe(null);
|
|
|
|
expect(configMock.getDomain).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns the registry base image", () => {
|
|
|
|
spyOn(dockerfileInfoImpl, "getBaseImage").and.returnValue(null);
|
|
|
|
});
|
2017-03-07 19:25:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("getBaseImage", () => {
|
2017-03-08 01:34:43 +00:00
|
|
|
var host: string;
|
|
|
|
var port: number;
|
|
|
|
var tag: string;
|
|
|
|
var image: string;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
host = 'quay.io';
|
|
|
|
port = 80;
|
|
|
|
tag = 'latest';
|
|
|
|
image = 'coreos/nginx';
|
|
|
|
});
|
2017-03-07 19:25:18 +00:00
|
|
|
|
|
|
|
it("returns null if instance's contents do not contain a 'FROM' command", () => {
|
2017-03-08 01:34:43 +00:00
|
|
|
var getBaseImageAndTagSpy: Spy = spyOn(dockerfileInfoImpl, "getBaseImageAndTag").and.returnValue(null);
|
|
|
|
|
|
|
|
expect(dockerfileInfoImpl.getBaseImage()).toBe(null);
|
|
|
|
expect(getBaseImageAndTagSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns the image name if in the format 'someimage'", () => {
|
|
|
|
spyOn(dockerfileInfoImpl, "getBaseImageAndTag").and.returnValue(image);
|
|
|
|
|
|
|
|
expect(dockerfileInfoImpl.getBaseImage()).toEqual(image);
|
2017-03-07 19:25:18 +00:00
|
|
|
});
|
|
|
|
|
2017-03-08 01:34:43 +00:00
|
|
|
it("returns the image name if in the format 'someimage:tag'", () => {
|
|
|
|
spyOn(dockerfileInfoImpl, "getBaseImageAndTag").and.returnValue(`${image}:${tag}`);
|
|
|
|
|
|
|
|
expect(dockerfileInfoImpl.getBaseImage()).toEqual(image);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns the host, port, and image name if in the format 'host:port/someimage'", () => {
|
|
|
|
spyOn(dockerfileInfoImpl, "getBaseImageAndTag").and.returnValue(`${host}:${port}/${image}`);
|
|
|
|
|
|
|
|
expect(dockerfileInfoImpl.getBaseImage()).toEqual(`${host}:${port}/${image}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns the host, port, and image name if in the format 'host:port/someimage:tag'", () => {
|
|
|
|
spyOn(dockerfileInfoImpl, "getBaseImageAndTag").and.returnValue(`${host}:${port}/${image}:${tag}`);
|
|
|
|
|
|
|
|
expect(dockerfileInfoImpl.getBaseImage()).toEqual(`${host}:${port}/${image}`);
|
|
|
|
});
|
2017-03-07 19:25:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("getBaseImageAndTag", () => {
|
|
|
|
|
|
|
|
it("returns null if instance's contents do not contain a 'FROM' command", () => {
|
|
|
|
expect(dockerfileInfoImpl.getBaseImageAndTag()).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns a string containing the base image and tag from the instance's contents", () => {
|
|
|
|
contents = "FROM quay.io/coreos/nginx";
|
|
|
|
dockerfileInfoImpl = new DockerfileInfoImpl(contents, configMock);
|
|
|
|
var baseImageAndTag: string = dockerfileInfoImpl.getBaseImageAndTag();
|
|
|
|
|
|
|
|
expect(baseImageAndTag).toEqual(contents.substring('FROM '.length, contents.length).trim());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("handles the presence of newlines", () => {
|
|
|
|
contents = "FROM quay.io/coreos/nginx\nRUN echo $0";
|
|
|
|
dockerfileInfoImpl = new DockerfileInfoImpl(contents, configMock);
|
|
|
|
var baseImageAndTag: string = dockerfileInfoImpl.getBaseImageAndTag();
|
|
|
|
|
|
|
|
expect(baseImageAndTag).toEqual(contents.substring('FROM '.length, contents.indexOf('\n')).trim());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|