296 lines
10 KiB
TypeScript
296 lines
10 KiB
TypeScript
import { DockerfileServiceImpl, DockerfileInfoImpl } from './dockerfile.service.impl';
|
|
import Spy = jasmine.Spy;
|
|
|
|
|
|
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;
|
|
var invalidArchiveFile: any[];
|
|
var validArchiveFile: any[];
|
|
var readAsFileBufferSpy: Spy;
|
|
var forDataSpy: Spy;
|
|
|
|
beforeEach(() => {
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
|
failure([]);
|
|
});
|
|
|
|
dataFileServiceMock.arrayToString.and.callFake((buf, callback) => {
|
|
var contents: string = "";
|
|
callback(contents);
|
|
});
|
|
|
|
dataFileServiceMock.blobToString.and.callFake((blob, callback) => {
|
|
callback(blob.toString());
|
|
});
|
|
|
|
forDataSpy = spyOn(DockerfileInfoImpl, "forData").and.returnValue(new DockerfileInfoImpl(file, configMock));
|
|
readAsFileBufferSpy = spyOn(fileReaderMock, "readAsArrayBuffer").and.callFake(() => {
|
|
var event: any = {target: {result: file}};
|
|
fileReaderMock.onload(event);
|
|
});
|
|
|
|
file = "FROM quay.io/coreos/nginx:latest";
|
|
validArchiveFile = [{name: 'Dockerfile', toBlob: jasmine.createSpy('toBlobSpy').and.returnValue(file)}];
|
|
invalidArchiveFile = [{name: 'main.exe', toBlob: jasmine.createSpy('toBlobSpy').and.returnValue("")}];
|
|
});
|
|
|
|
it("calls datafile service to read given file as possible archive file", (done) => {
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
expect(readAsFileBufferSpy.calls.argsFor(0)[0]).toEqual(file);
|
|
expect(dataFileServiceMock.readDataArrayAsPossibleArchive).toHaveBeenCalled();
|
|
done();
|
|
},
|
|
(error: Event | string) => {
|
|
fail("Should not invoke failure callback");
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("calls datafile service to convert file to string if given file is not an archive", (done) => {
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
expect(dataFileServiceMock.arrayToString.calls.argsFor(0)[0]).toEqual(file);
|
|
done();
|
|
},
|
|
(error: Event | string) => {
|
|
fail("Should not invoke success callback");
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("calls failure callback if given non-archive file that is not a valid Dockerfile", (done) => {
|
|
forDataSpy.and.returnValue(null);
|
|
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) => {
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
expect(dockerfile).toBeDefined();
|
|
done();
|
|
},
|
|
(error: Event | string) => {
|
|
fail('Should not invoke failure callback');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("calls failure callback if given archive file with no Dockerfile present in root directory", (done) => {
|
|
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();
|
|
});
|
|
});
|
|
|
|
it("calls datafile service to convert blob to string if given file is an archive", (done) => {
|
|
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();
|
|
});
|
|
});
|
|
|
|
it("calls failure callback if given archive file with invalid Dockerfile", (done) => {
|
|
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();
|
|
});
|
|
});
|
|
|
|
it("calls success callback with new DockerfileInfoImpl instance if given archive with valid Dockerfile", (done) => {
|
|
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();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("extractDockerfile", () => {
|
|
// TODO: TDD promise-based method with same functionality as getDockerfile
|
|
});
|
|
});
|
|
|
|
|
|
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", () => {
|
|
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();
|
|
});
|
|
|
|
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');
|
|
|
|
expect(dockerfileInfoImpl.getRegistryBaseImage()).toBe(null);
|
|
expect(configMock.getDomain).toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns the registry base image", () => {
|
|
spyOn(dockerfileInfoImpl, "getBaseImage").and.returnValue(null);
|
|
});
|
|
});
|
|
|
|
describe("getBaseImage", () => {
|
|
var host: string;
|
|
var port: number;
|
|
var tag: string;
|
|
var image: string;
|
|
|
|
beforeEach(() => {
|
|
host = 'quay.io';
|
|
port = 80;
|
|
tag = 'latest';
|
|
image = 'coreos/nginx';
|
|
});
|
|
|
|
it("returns null if instance's contents do not contain a 'FROM' command", () => {
|
|
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);
|
|
});
|
|
|
|
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}`);
|
|
});
|
|
});
|
|
|
|
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());
|
|
});
|
|
});
|
|
});
|