Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions src/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ describe("Docker images", (): void => {
key: string,
readOnly = false,
prevSave = false,
gzip = false,
): void => {
expect(core.getInput).nthCalledWith<[string, InputOptions]>(1, "key", {
required: true,
});
expect(core.getState).nthCalledWith<[string]>(1, docker.CACHE_HIT);
if (!cacheHit) {
expect(core.getInput).lastCalledWith("read-only");
expect(core.getInput).nthCalledWith(2, "read-only");
if (!readOnly) {
expect(cache.restoreCache).lastCalledWith([""], key, [], {
lookupOnly: true,
Expand All @@ -110,6 +111,9 @@ describe("Docker images", (): void => {
"Listing Docker images.",
);
expect(util.execBashCommand).nthCalledWith<[string]>(1, LIST_COMMAND);
if (gzip) {
expect(core.getInput).lastCalledWith("gzip");
}
}
}
}
Expand All @@ -120,6 +124,7 @@ describe("Docker images", (): void => {
cacheHit: boolean,
readOnly: boolean,
prevSave: boolean,
gzip: boolean,
preexistingImages: string[],
newImages: string[],
): Promise<void> => {
Expand All @@ -132,6 +137,9 @@ describe("Docker images", (): void => {
cache.restoreCache.mockResolvedValueOnce(key);
} else {
cache.restoreCache.mockResolvedValueOnce(undefined);
if (gzip) {
core.getInput.mockReturnValueOnce(gzip.toString());
}
core.getState.mockReturnValueOnce(preexistingImages.join("\n"));
const images = preexistingImages.concat(newImages);
util.execBashCommand.mockResolvedValueOnce(images.join("\n"));
Expand All @@ -140,7 +148,7 @@ describe("Docker images", (): void => {
}
await docker.saveDockerImages();

assertSaveDockerImages(cacheHit, key, readOnly, prevSave);
assertSaveDockerImages(cacheHit, key, readOnly, prevSave, gzip);
};

const assertCacheNotSaved = (): void => {
Expand Down Expand Up @@ -178,6 +186,32 @@ describe("Docker images", (): void => {
expect(cache.saveCache).not.toHaveBeenCalled();
};

const assertSaveCacheMissWithGzip = (key: string, newImages: string[]): void => {
expect(core.info).lastCalledWith(
"Images present before restore step will be skipped; only new images " +
"will be saved.",
);
expect(util.execBashCommand).lastCalledWith(
`docker save ${newImages.join(" ")} | gzip > ${docker.DOCKER_IMAGES_PATH}`
);
expect(cache.saveCache).lastCalledWith([docker.DOCKER_IMAGES_PATH], key);

/* The Docker images must be saved before the cache can be. This at least
* checks that the calls are made in the right order, but doesn't ensure
* that the Docker images finished saving before the cache started saving.
*/
assertCalledInOrder<FunctionLike>(
core.getInput,
core.getState,
core.getInput,
core.getState,
util.execBashCommand,
core.getInput,
util.execBashCommand,
cache.saveCache,
);
};

const assertSaveCacheMiss = (key: string, newImages: string[]): void => {
expect(core.info).lastCalledWith(
"Images present before restore step will be skipped; only new images " +
Expand All @@ -200,6 +234,7 @@ describe("Docker images", (): void => {
core.getInput,
core.getState,
util.execBashCommand,
core.getInput,
util.execBashCommand,
cache.saveCache,
);
Expand Down Expand Up @@ -262,13 +297,15 @@ describe("Docker images", (): void => {
boolean(),
boolean(),
boolean(),
boolean(),
uniquePair(dockerImages(), dockerImages()),
],
async (
key: string,
cacheHit: boolean,
readOnly: boolean,
prevSave: boolean,
gzip: boolean,
[preexistingImages, newImages]: [string[], string[]],
): Promise<void> => {
jest.clearAllMocks();
Expand All @@ -277,6 +314,7 @@ describe("Docker images", (): void => {
cacheHit,
readOnly,
prevSave,
gzip,
preexistingImages,
newImages,
);
Expand All @@ -289,17 +327,20 @@ describe("Docker images", (): void => {
assertSavePrevSave(key);
} else if (newImages.length === 0) {
assertNoNewImagesToSave();
} else if (gzip) {
assertSaveCacheMissWithGzip(key, newImages);
} else {
assertSaveCacheMiss(key, newImages);
}
},
{
examples: [
["my-key", false, false, false, [["preexisting-image"], ["new-image"]]],
["my-key", false, false, false, [["preexisting-image"], []]],
["my-key", false, true, false, [["preexisting-image"], ["new-image"]]],
["my-key", true, false, false, [["preexisting-image"], ["new-image"]]],
["my-key", false, false, true, [["preexisting-image"], ["new-image"]]],
["my-key", false, false, false, false, [["preexisting-image"], ["new-image"]]],
["my-key", false, false, false, false, [["preexisting-image"], []]],
["my-key", false, true, false, false, [["preexisting-image"], ["new-image"]]],
["my-key", true, false, false, false, [["preexisting-image"], ["new-image"]]],
["my-key", false, false, true, false, [["preexisting-image"], ["new-image"]]],
["my-key", false, false, false, true, [["preexisting-image"], ["new-image"]]],
],
},
);
Expand Down
3 changes: 2 additions & 1 deletion src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const saveDockerImages = async (): Promise<void> => {
"will be saved.",
);
const newImagesArgs = newImages.join(" ");
const cmd = `docker save --output ${DOCKER_IMAGES_PATH} ${newImagesArgs}`;
const isUseGzip = getInput("gzip") === "true";
const cmd = isUseGzip ? `docker save ${newImagesArgs} | gzip > ${DOCKER_IMAGES_PATH}` : `docker save --output ${DOCKER_IMAGES_PATH} ${newImagesArgs}`;
await execBashCommand(cmd);
await saveCache([DOCKER_IMAGES_PATH], key);
}
Expand Down
2 changes: 1 addition & 1 deletion src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe("Integration Test", (): void => {
listStderr: string,
saveOutput: ConsoleOutput,
): void => {
expect(core.getInput).lastCalledWith("read-only");
expect(core.getInput).nthCalledWith(2, "read-only");
expect(core.info).nthCalledWith<[string]>(1, "Listing Docker images.");
const listOutput = joinOutput(dockerImages, listStderr);
assertExecBashCommand(2, 1, LIST_COMMAND, listOutput);
Expand Down