233 lines
7.8 KiB
TypeScript
233 lines
7.8 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 readAsFileBufferSpy: Spy;
|
|
|
|
beforeEach(() => {
|
|
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
|
var files: any[] = [];
|
|
failure(files);
|
|
});
|
|
|
|
dataFileServiceMock.arrayToString.and.callFake((buf, callback) => {
|
|
var contents: string = "";
|
|
callback(contents);
|
|
});
|
|
|
|
dataFileServiceMock.blobToString.and.callFake((blob, callback) => {
|
|
var contents: string = "";
|
|
callback(contents);
|
|
});
|
|
|
|
spyOn(DockerfileInfoImpl, "forData").and.returnValue(null);
|
|
readAsFileBufferSpy = spyOn(fileReaderMock, "readAsArrayBuffer").and.callFake(() => {
|
|
var event: any = {target: {result: file}};
|
|
fileReaderMock.onload(event);
|
|
});
|
|
|
|
file = "FROM quay.io/coreos/nginx:latest";
|
|
});
|
|
|
|
it("calls datafile service to read given file as possible archive file", (done) => {
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
fail("Should not invoke success callback");
|
|
done();
|
|
},
|
|
(error: Event | string) => {
|
|
expect(readAsFileBufferSpy.calls.argsFor(0)[0]).toEqual(file);
|
|
expect(dataFileServiceMock.readDataArrayAsPossibleArchive).toHaveBeenCalled();
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("calls datafile service to convert file to string if given file is not an archive", (done) => {
|
|
dockerfileServiceImpl.getDockerfile(file,
|
|
(dockerfile: DockerfileInfoImpl) => {
|
|
fail("Should not invoke success callback");
|
|
done();
|
|
},
|
|
(error: Event | string) => {
|
|
expect(dataFileServiceMock.arrayToString.calls.argsFor(0)[0]).toEqual(file);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("calls failure callback if given non-archive file that is not a valid Dockerfile", (done) => {
|
|
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) => {
|
|
done();
|
|
});
|
|
|
|
it("calls failure callback if given archive file with no Dockerfile present in root directory", (done) => {
|
|
done();
|
|
});
|
|
|
|
it("calls datafile service to convert blob to string if given file is an archive", (done) => {
|
|
done();
|
|
});
|
|
|
|
it("calls failure callback if given archive file with invalid Dockerfile", (done) => {
|
|
done();
|
|
});
|
|
|
|
it("calls success callback with new DockerfileInfoImpl instance if given archive with valid Dockerfile", (done) => {
|
|
done();
|
|
});
|
|
});
|
|
|
|
describe("extractDockerfile", () => {
|
|
// TODO
|
|
});
|
|
});
|
|
|
|
|
|
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());
|
|
});
|
|
});
|
|
});
|