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

Commit 7ffaca9

Browse files
authored
[REFACTOR] HLD pipeline command (#239)
1 parent b5f7124 commit 7ffaca9

File tree

4 files changed

+290
-290
lines changed

4 files changed

+290
-290
lines changed

src/commands/hld/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Command } from "../command";
22
import { commandDecorator as initCommandDecorator } from "./init";
3-
import { installHldToManifestPipelineDecorator } from "./pipeline";
3+
import { commandDecorator as installHldToManifestPipelineDecorator } from "./pipeline";
44
import { reconcileHldDecorator } from "./reconcile";
55
export const hldCommand = Command(
66
"hld",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"command": "install-manifest-pipeline",
3+
"alias": "p",
4+
"description": "Install the manifest generation pipeline to your Azure DevOps instance. Default values are set in spk-config.yaml and can be loaded via spk init or overriden via option flags.",
5+
"options": [
6+
{
7+
"arg": "-n, --pipeline-name <pipeline-name>",
8+
"description": "Name of the pipeline to be created",
9+
"defaultValue": ""
10+
},
11+
{
12+
"arg": "-p, --personal-access-token <personal-access-token>",
13+
"description": "Personal Access Token",
14+
"defaultValue": ""
15+
},
16+
{
17+
"arg": "-o, --org-name <org-name>",
18+
"description": "Organization Name for Azure DevOps",
19+
"defaultValue": ""
20+
},
21+
{
22+
"arg": "-r, --hld-name <hld-name>",
23+
"description": "HLD Repository Name in Azure DevOps",
24+
"defaultValue": ""
25+
},
26+
{
27+
"arg": "-u, --hld-url <hld-url>",
28+
"description": "HLD Repository URL",
29+
"defaultValue": ""
30+
},
31+
{
32+
"arg": "-m, --manifest-url <manifest-url>",
33+
"description": "Manifest Repository URL",
34+
"defaultValue": ""
35+
},
36+
{
37+
"arg": "-d, --devops-project <devops-project>",
38+
"description": "Azure DevOps Project",
39+
"defaultValue": ""
40+
},
41+
{
42+
"arg": "-b, --build-script-url <build-script-url>",
43+
"description": "Build Script URL. By default it is 'https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/build.sh'.",
44+
"defaultValue": "https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/build.sh"
45+
}
46+
]
47+
}

src/commands/hld/pipeline.test.ts

Lines changed: 142 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { Config, loadConfiguration } from "../../config";
2+
import * as config from "../../config";
3+
import { BUILD_SCRIPT_URL } from "../../lib/constants";
4+
import { getRepositoryName } from "../../lib/gitutils";
15
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
26

37
jest.mock("../../lib/pipelines/pipelines");
@@ -7,12 +11,43 @@ import {
711
getBuildApiClient,
812
queueBuild
913
} from "../../lib/pipelines/pipelines";
14+
import { IConfigYaml } from "../../types";
1015

1116
import {
17+
emptyStringIfUndefined,
18+
execute,
19+
ICommandOptions,
1220
installHldToManifestPipeline,
13-
isValidConfig,
21+
populateValues,
1422
requiredPipelineVariables
1523
} from "./pipeline";
24+
import * as pipeline from "./pipeline";
25+
26+
const MOCKED_VALUES: ICommandOptions = {
27+
buildScriptUrl: "buildScriptUrl",
28+
devopsProject: "project",
29+
hldName: "hldName",
30+
hldUrl: "https://dev.azure.com/test/fabrikam/_git/hld",
31+
manifestUrl: "https://dev.azure.com/test/fabrikam/_git/materialized",
32+
orgName: "orgName",
33+
personalAccessToken: "personalAccessToken",
34+
pipelineName: "pipelineName"
35+
};
36+
37+
const MOCKED_CONFIG = {
38+
azure_devops: {
39+
access_token: "mocked_access_token",
40+
hld_repository: "https://dev.azure.com/mocked/fabrikam/_git/hld",
41+
manifest_repository:
42+
"https://dev.azure.com/mocked/fabrikam/_git/materialized",
43+
org: "mocked_org",
44+
project: "mocked_project"
45+
}
46+
};
47+
48+
const getMockObject = (): ICommandOptions => {
49+
return JSON.parse(JSON.stringify(MOCKED_VALUES));
50+
};
1651

1752
beforeAll(() => {
1853
enableVerboseLogging();
@@ -22,6 +57,90 @@ afterAll(() => {
2257
disableVerboseLogging();
2358
});
2459

60+
describe("test emptyStringIfUndefined function", () => {
61+
it("pass in undefined", () => {
62+
expect(emptyStringIfUndefined(undefined)).toBe("");
63+
});
64+
it("send in empty string", () => {
65+
expect(emptyStringIfUndefined("")).toBe("");
66+
});
67+
it("send in string", () => {
68+
expect(emptyStringIfUndefined("test")).toBe("test");
69+
});
70+
});
71+
72+
describe("test populateValues function", () => {
73+
it("with all values in command opts", () => {
74+
jest.spyOn(config, "Config").mockImplementationOnce(
75+
(): IConfigYaml => {
76+
return MOCKED_CONFIG;
77+
}
78+
);
79+
const mockedObject = getMockObject();
80+
expect(populateValues(mockedObject)).toEqual(mockedObject);
81+
});
82+
it("without any values in command opts", () => {
83+
jest.spyOn(config, "Config").mockImplementationOnce(
84+
(): IConfigYaml => {
85+
return MOCKED_CONFIG;
86+
}
87+
);
88+
const values = populateValues({
89+
buildScriptUrl: "",
90+
devopsProject: "",
91+
hldName: "",
92+
hldUrl: "",
93+
manifestUrl: "",
94+
orgName: "",
95+
personalAccessToken: "",
96+
pipelineName: ""
97+
});
98+
99+
expect(values.buildScriptUrl).toBe(BUILD_SCRIPT_URL);
100+
expect(values.devopsProject).toBe(MOCKED_CONFIG.azure_devops.project);
101+
expect(values.hldName).toBe(
102+
getRepositoryName(MOCKED_CONFIG.azure_devops.hld_repository)
103+
);
104+
expect(values.hldUrl).toBe(MOCKED_CONFIG.azure_devops.hld_repository);
105+
expect(values.manifestUrl).toBe(
106+
MOCKED_CONFIG.azure_devops.manifest_repository
107+
);
108+
expect(values.orgName).toBe(MOCKED_CONFIG.azure_devops.org);
109+
expect(values.personalAccessToken).toBe(
110+
MOCKED_CONFIG.azure_devops.access_token
111+
);
112+
expect(values.pipelineName).toBe(
113+
getRepositoryName(MOCKED_CONFIG.azure_devops.hld_repository) +
114+
"-to-" +
115+
getRepositoryName(MOCKED_CONFIG.azure_devops.manifest_repository)
116+
);
117+
});
118+
});
119+
120+
describe("test execute function", () => {
121+
it("positive test", async () => {
122+
jest.spyOn(config, "Config").mockImplementationOnce(
123+
(): IConfigYaml => {
124+
return MOCKED_CONFIG;
125+
}
126+
);
127+
const exitFn = jest.fn();
128+
jest
129+
.spyOn(pipeline, "installHldToManifestPipeline")
130+
.mockReturnValueOnce(Promise.resolve());
131+
132+
await execute(MOCKED_VALUES, exitFn);
133+
expect(exitFn).toBeCalledTimes(1);
134+
expect(exitFn.mock.calls).toEqual([[0]]);
135+
});
136+
it("negative test", async () => {
137+
const exitFn = jest.fn();
138+
await execute(MOCKED_VALUES, exitFn);
139+
expect(exitFn).toBeCalledTimes(1);
140+
expect(exitFn.mock.calls).toEqual([[1]]);
141+
});
142+
});
143+
25144
describe("required pipeline variables", () => {
26145
it("should use have the proper pipeline vars vars", () => {
27146
const variables = requiredPipelineVariables(
@@ -46,113 +165,45 @@ describe("required pipeline variables", () => {
46165
});
47166
});
48167

49-
describe("validate pipeline config", () => {
50-
const configValues: any[] = [
51-
"testOrg",
52-
"testDevopsProject",
53-
"testPipeline",
54-
"https://manifestulr",
55-
"testHld",
56-
"https://hldurl",
57-
"https://buildscript",
58-
"af8e99c1234ef93e8c4365b1dc9bd8d9ba987d3"
59-
];
60-
61-
it("config is valid", () => {
62-
expect(isValidConfig.apply(undefined, configValues as any)).toBe(true);
63-
});
64-
65-
it("undefined values", () => {
66-
for (const i of configValues.keys()) {
67-
const configValuesWithInvalidValue = configValues.map((value, j) =>
68-
i === j ? undefined : value
69-
);
70-
expect(
71-
isValidConfig.apply(undefined, configValuesWithInvalidValue as any)
72-
).toBe(false);
73-
}
74-
});
75-
});
76-
77168
describe("create hld to manifest pipeline test", () => {
78169
it("should create a pipeline", async () => {
79170
(createPipelineForDefinition as jest.Mock).mockReturnValue({ id: 10 });
80-
81-
const exitFn = jest.fn();
82-
await installHldToManifestPipeline(
83-
"orgName",
84-
"personalAccessToken",
85-
"hldRepoName",
86-
"hldRepoUrl",
87-
"manifestRepoUrl",
88-
"project",
89-
"pipelineName",
90-
"buildScriptUrl",
91-
exitFn
92-
);
93-
94-
expect(exitFn).toBeCalledTimes(0);
171+
await installHldToManifestPipeline(getMockObject());
95172
});
96173

97174
it("should fail if the build client cant be instantiated", async () => {
98-
(getBuildApiClient as jest.Mock).mockReturnValue(Promise.reject());
99-
100-
const exitFn = jest.fn();
101-
await installHldToManifestPipeline(
102-
"orgName",
103-
"personalAccessToken",
104-
"hldRepoName",
105-
"hldRepoUrl",
106-
"manifestRepoUrl",
107-
"project",
108-
"pipelineName",
109-
"buildScriptUrl",
110-
exitFn
111-
);
112-
113-
expect(exitFn).toBeCalledTimes(1);
175+
(getBuildApiClient as jest.Mock).mockReturnValue(Promise.reject("Error"));
176+
try {
177+
await installHldToManifestPipeline(getMockObject());
178+
expect(true).toBe(false);
179+
} catch (err) {
180+
expect(err).toBeDefined();
181+
}
114182
});
115183

116184
it("should fail if the pipeline definition cannot be created", async () => {
117185
(getBuildApiClient as jest.Mock).mockReturnValue({});
118186
(createPipelineForDefinition as jest.Mock).mockReturnValue(
119-
Promise.reject()
187+
Promise.reject("Error")
120188
);
121-
122-
const exitFn = jest.fn();
123-
await installHldToManifestPipeline(
124-
"orgName",
125-
"personalAccessToken",
126-
"hldRepoName",
127-
"hldRepoUrl",
128-
"manifestRepoUrl",
129-
"project",
130-
"pipelineName",
131-
"buildScriptUrl",
132-
exitFn
133-
);
134-
135-
expect(exitFn).toBeCalledTimes(1);
189+
try {
190+
await installHldToManifestPipeline(getMockObject());
191+
expect(true).toBe(false);
192+
} catch (err) {
193+
expect(err).toBeDefined();
194+
}
136195
});
137196

138197
it("should fail if a build cannot be queued on the pipeline", async () => {
139198
(getBuildApiClient as jest.Mock).mockReturnValue({});
140199
(createPipelineForDefinition as jest.Mock).mockReturnValue({ id: 10 });
141-
(queueBuild as jest.Mock).mockReturnValue(Promise.reject());
200+
(queueBuild as jest.Mock).mockReturnValue(Promise.reject("Error"));
142201

143-
const exitFn = jest.fn();
144-
await installHldToManifestPipeline(
145-
"orgName",
146-
"personalAccessToken",
147-
"hldRepoName",
148-
"hldRepoUrl",
149-
"manifestRepoUrl",
150-
"project",
151-
"pipelineName",
152-
"buildScriptUrl",
153-
exitFn
154-
);
155-
156-
expect(exitFn).toBeCalledTimes(1);
202+
try {
203+
await installHldToManifestPipeline(getMockObject());
204+
expect(true).toBe(false);
205+
} catch (err) {
206+
expect(err).toBeDefined();
207+
}
157208
});
158209
});

0 commit comments

Comments
 (0)