Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 69fdf8a

Browse files
authored
[TEST] add test to deployment dashboard command (#530)
1 parent 9dc82b7 commit 69fdf8a

File tree

2 files changed

+127
-18
lines changed

2 files changed

+127
-18
lines changed

src/commands/deployment/dashboard.test.ts

+125-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
import child_process from "child_process";
12
jest.mock("open");
23
import open from "open";
34
jest.mock("../../config");
45
import { Config } from "../../config";
56
import { exec } from "../../lib/shell";
7+
import * as shell from "../../lib/shell";
68
import { validatePrereqs } from "../../lib/validator";
9+
import * as validator from "../../lib/validator";
710
import {
811
disableVerboseLogging,
912
enableVerboseLogging,
1013
logger,
1114
} from "../../logger";
1215
import {
16+
cleanDashboardContainers,
1317
DashboardConfig,
1418
execute,
1519
extractManifestRepositoryInformation,
@@ -21,6 +25,7 @@ import * as dashboard from "./dashboard";
2125

2226
import uuid from "uuid/v4";
2327
import { deepClone } from "../../lib/util";
28+
import { getErrorMessage } from "../../lib/errorBuilder";
2429

2530
const dashboardConf: DashboardConfig = {
2631
port: 2020,
@@ -112,9 +117,7 @@ describe("Test execute function", () => {
112117
it("positive test", async () => {
113118
mockConfig();
114119
const exitFn = jest.fn();
115-
jest
116-
.spyOn(dashboard, "launchDashboard")
117-
.mockReturnValueOnce(Promise.resolve(uuid()));
120+
jest.spyOn(dashboard, "launchDashboard").mockResolvedValueOnce(uuid());
118121
jest.spyOn(dashboard, "validateValues").mockReturnValueOnce(dashboardConf);
119122
(open as jest.Mock).mockReturnValueOnce(Promise.resolve());
120123
await execute(
@@ -196,19 +199,61 @@ describe("Validate dashboard clean up", () => {
196199
});
197200

198201
describe("Fallback to azure devops access token", () => {
199-
test("Has repo_access_token specified", async () => {
200-
const envVars = (await getEnvVars(dashboardConf)).toString();
201-
logger.info(
202-
`spin: ${envVars}, act: ${mockedConf.introspection.azure.source_repo_access_token}`
203-
);
204-
const expectedSubstring = "REACT_APP_SOURCE_REPO_ACCESS_TOKEN=test_token";
205-
expect(envVars.includes(expectedSubstring)).toBeTruthy();
202+
test("with repo_access_token and without sourceRepoAccessToken", async () => {
203+
const conf = deepClone(dashboardConf);
204+
delete conf.sourceRepoAccessToken;
205+
const envVars = getEnvVars(conf).toString();
206+
207+
expect(
208+
envVars.includes(`REACT_APP_PIPELINE_ACCESS_TOKEN=${conf.accessToken}`)
209+
).toBeTruthy();
210+
expect(
211+
envVars.includes(`REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${conf.accessToken}`)
212+
).toBeTruthy();
213+
expect(
214+
envVars.includes(`REACT_APP_MANIFEST_ACCESS_TOKEN=${conf.accessToken}`)
215+
).toBeTruthy();
216+
});
217+
test("without repo_access_token and with sourceRepoAccessToken", async () => {
218+
const conf = deepClone(dashboardConf);
219+
delete conf.accessToken;
220+
const envVars = getEnvVars(conf).toString();
221+
222+
expect(envVars.includes("REACT_APP_PIPELINE_ACCESS_TOKEN")).toBeFalsy();
223+
expect(
224+
envVars.includes(
225+
`REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${dashboardConf.sourceRepoAccessToken}`
226+
)
227+
).toBeTruthy();
228+
expect(
229+
envVars.includes(
230+
`REACT_APP_MANIFEST_ACCESS_TOKEN=${dashboardConf.sourceRepoAccessToken}`
231+
)
232+
).toBeTruthy();
206233
});
234+
test("with manifest repository information", async () => {
235+
jest
236+
.spyOn(dashboard, "extractManifestRepositoryInformation")
237+
.mockReturnValueOnce({
238+
manifestRepoName: "mName",
239+
githubUsername: "gitUser",
240+
});
241+
const envVars = getEnvVars(dashboardConf).toString();
207242

208-
it("No repo_access_token was specified", async () => {
209-
const envVars = (await getEnvVars(dashboardConf)).toString();
210-
const expectedSubstring = `REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${dashboardConf.sourceRepoAccessToken}`;
211-
expect(envVars.includes(expectedSubstring)).toBeTruthy();
243+
expect(envVars.includes("REACT_APP_MANIFEST=mName")).toBeTruthy();
244+
expect(
245+
envVars.includes("REACT_APP_GITHUB_MANIFEST_USERNAME=gitUser")
246+
).toBeTruthy();
247+
});
248+
test("negative test", async () => {
249+
jest
250+
.spyOn(dashboard, "extractManifestRepositoryInformation")
251+
.mockImplementationOnce(() => {
252+
throw Error("fake");
253+
});
254+
expect(() => {
255+
getEnvVars(dashboardConf);
256+
}).toThrow(getErrorMessage("introspect-dashboard-cmd-get-env"));
212257
});
213258
});
214259

@@ -238,3 +283,69 @@ describe("Extract manifest repository information", () => {
238283
logger.info("Verified that manifest repository extraction works");
239284
});
240285
});
286+
287+
describe("test cleanDashboardContainers function", () => {
288+
it("positive test", async () => {
289+
const containerIds = ["f5ad0bff2448", "f5ad0bff2449"];
290+
jest.spyOn(shell, "exec").mockResolvedValueOnce(containerIds.join("\n"));
291+
292+
jest.spyOn(shell, "exec").mockImplementationOnce(
293+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
294+
async (
295+
cmd: string,
296+
args?: string[],
297+
opts?: child_process.SpawnOptions
298+
): Promise<string> => {
299+
expect(args).toStrictEqual(["kill", ...containerIds]);
300+
return "";
301+
}
302+
);
303+
await cleanDashboardContainers({
304+
image: "fake",
305+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
306+
} as any);
307+
});
308+
it("negative test: cannot get docker image ids", async () => {
309+
jest.spyOn(shell, "exec").mockRejectedValueOnce(Error("fake"));
310+
311+
await expect(
312+
cleanDashboardContainers({
313+
image: "fake",
314+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
315+
} as any)
316+
).rejects.toThrow(
317+
getErrorMessage("introspect-dashboard-cmd-kill-docker-container")
318+
);
319+
});
320+
it("negative test: cannot kill images", async () => {
321+
const containerIds = ["f5ad0bff2448", "f5ad0bff2449"];
322+
jest.spyOn(shell, "exec").mockResolvedValueOnce(containerIds.join("\n"));
323+
324+
jest.spyOn(shell, "exec").mockRejectedValueOnce(Error("fake"));
325+
await expect(
326+
cleanDashboardContainers({
327+
image: "fake",
328+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
329+
} as any)
330+
).rejects.toThrow(
331+
getErrorMessage("introspect-dashboard-cmd-kill-docker-container")
332+
);
333+
});
334+
});
335+
336+
describe("test launchDashboard function", () => {
337+
it("postive test", async () => {
338+
jest.spyOn(validator, "validatePrereqs").mockReturnValueOnce(true);
339+
jest.spyOn(dashboard, "cleanDashboardContainers").mockResolvedValueOnce();
340+
jest.spyOn(shell, "exec").mockResolvedValueOnce("ok");
341+
jest.spyOn(shell, "exec").mockResolvedValueOnce("container-identifier");
342+
const res = await launchDashboard(dashboardConf, true);
343+
expect(res).toBe("container-identifier");
344+
});
345+
it("negative test", async () => {
346+
jest.spyOn(validator, "validatePrereqs").mockReturnValueOnce(false);
347+
await expect(launchDashboard(dashboardConf, true)).rejects.toThrow(
348+
getErrorMessage("introspect-dashboard-cmd-launch-err")
349+
);
350+
});
351+
});

src/commands/deployment/dashboard.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ export const extractManifestRepositoryInformation = (
154154
* Creates and returns an array of env vars that need to be passed into the
155155
* docker run command
156156
*/
157-
export const getEnvVars = async (
158-
config: DashboardConfig
159-
): Promise<string[]> => {
157+
export const getEnvVars = (config: DashboardConfig): string[] => {
160158
try {
161159
const envVars = [
162160
"-e",
@@ -256,7 +254,7 @@ export const launchDashboard = async (
256254
"run",
257255
"-d",
258256
"--rm",
259-
...(await getEnvVars(config)),
257+
...getEnvVars(config),
260258
"-p",
261259
`${config.port}:5000`,
262260
dockerRepository,

0 commit comments

Comments
 (0)