Skip to content

Commit

Permalink
initial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham Tiwari committed Mar 23, 2022
1 parent 2d8d0d1 commit 1a306b5
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 113 deletions.
56 changes: 54 additions & 2 deletions __tests__/restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ beforeEach(() => {
process.env[RefKey] = "refs/heads/feature-branch";

jest.spyOn(actionUtils, "isGhes").mockImplementation(() => false);
jest.spyOn(cache, "isAvailable").mockImplementation(() => true);
});

afterEach(() => {
Expand All @@ -55,8 +56,27 @@ test("restore with invalid event outputs warning", async () => {
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("restore on GHES should no-op", async () => {
test("restore without AC available should no-op", async () => {
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => false);
jest.spyOn(cache, "isAvailable").mockImplementation(() => false);

const logWarningMock = jest.spyOn(actionUtils, "logWarning");
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");

await run();

expect(restoreCacheMock).toHaveBeenCalledTimes(0);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
expect(logWarningMock).toHaveBeenCalledWith(
"Something is going wrong with ArtifactCache service which supports cache actions. Please check https://www.githubstatus.com/ for any ongoing issue in actions."
);
});

test("restore on GHES without AC available should no-op", async () => {
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => true);
jest.spyOn(cache, "isAvailable").mockImplementation(() => false);

const logWarningMock = jest.spyOn(actionUtils, "logWarning");
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
Expand All @@ -68,10 +88,42 @@ test("restore on GHES should no-op", async () => {
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
expect(logWarningMock).toHaveBeenCalledWith(
"Cache action is not supported on GHES. See https://github.com/actions/cache/issues/505 for more details"
"Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if ArtifactCache service is enabled or not."
);
});

test("restore on GHES with AC available ", async () => {
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => true);
const path = "node_modules";
const key = "node-test";
testUtils.setInputs({
path: path,
key
});

const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
const stateMock = jest.spyOn(core, "saveState");
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
const restoreCacheMock = jest
.spyOn(cache, "restoreCache")
.mockImplementationOnce(() => {
return Promise.resolve(key);
});

await run();

expect(restoreCacheMock).toHaveBeenCalledTimes(1);
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);

expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);

expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("restore with no path should fail", async () => {
const failedMock = jest.spyOn(core, "setFailed");
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
Expand Down
58 changes: 56 additions & 2 deletions __tests__/save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ beforeEach(() => {
process.env[RefKey] = "refs/heads/feature-branch";

jest.spyOn(actionUtils, "isGhes").mockImplementation(() => false);
jest.spyOn(cache, "isAvailable").mockImplementation(() => true);
});

afterEach(() => {
Expand Down Expand Up @@ -101,8 +102,23 @@ test("save with no primary key in state outputs warning", async () => {
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("save on GHES should no-op", async () => {
test("save without AC available should no=op", async () => {
jest.spyOn(cache, "isAvailable").mockImplementation(() => false);

const logWarningMock = jest.spyOn(actionUtils, "logWarning");
const saveCacheMock = jest.spyOn(cache, "saveCache");

await run();

expect(saveCacheMock).toHaveBeenCalledTimes(0);
expect(logWarningMock).toHaveBeenCalledWith(
"Something is going wrong with ArtifactCache service which supports cache actions. Please check https://www.githubstatus.com/ for any ongoing issue in actions."
);
});

test("save on ghes without AC available should no=op", async () => {
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => true);
jest.spyOn(cache, "isAvailable").mockImplementation(() => false);

const logWarningMock = jest.spyOn(actionUtils, "logWarning");
const saveCacheMock = jest.spyOn(cache, "saveCache");
Expand All @@ -111,10 +127,48 @@ test("save on GHES should no-op", async () => {

expect(saveCacheMock).toHaveBeenCalledTimes(0);
expect(logWarningMock).toHaveBeenCalledWith(
"Cache action is not supported on GHES. See https://github.com/actions/cache/issues/505 for more details"
"Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if ArtifactCache service is enabled or not."
);
});

test("save on GHES with AC available", async () => {
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => true);
const failedMock = jest.spyOn(core, "setFailed");

const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
const savedCacheKey = "Linux-node-";

jest.spyOn(core, "getState")
// Cache Entry State
.mockImplementationOnce(() => {
return savedCacheKey;
})
// Cache Key State
.mockImplementationOnce(() => {
return primaryKey;
});

const inputPath = "node_modules";
testUtils.setInput(Inputs.Path, inputPath);
testUtils.setInput(Inputs.UploadChunkSize, "4000000");

const cacheId = 4;
const saveCacheMock = jest
.spyOn(cache, "saveCache")
.mockImplementationOnce(() => {
return Promise.resolve(cacheId);
});

await run();

expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey, {
uploadChunkSize: 4000000
});

expect(failedMock).toHaveBeenCalledTimes(0);
});

test("save with exact match returns early", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");
Expand Down
Loading

0 comments on commit 1a306b5

Please sign in to comment.