This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/services/dockerfile/dockerfile.service.impl.spec.ts

234 lines
7.8 KiB
TypeScript
Raw Normal View History

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;
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
});
});
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());
});
});
});