This repository was archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.test.ts
205 lines (190 loc) · 5.67 KB
/
config.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os from "os";
import path from "path";
import fs from "fs";
import shell from "shelljs";
import uuid from "uuid/v4";
import {
Bedrock,
Config,
defaultConfigFile,
loadConfiguration,
saveConfiguration,
updateVariableWithLocalEnv,
} from "./config";
import { createTempDir } from "./lib/ioUtil";
import { disableVerboseLogging, enableVerboseLogging } from "./logger";
import { BedrockFile } from "./types";
import { getErrorMessage } from "./lib/errorBuilder";
import { getVersionMessage } from "./lib/fileutils";
import * as bedrockYaml from "./lib/bedrockYaml";
beforeAll(() => {
enableVerboseLogging();
});
afterAll(() => {
disableVerboseLogging();
});
describe("Test updateVariableWithLocalEnv function", () => {
beforeAll(() => {
process.env.hello = "world";
process.env.hello1 = "world1";
});
afterAll(() => {
delete process.env.hello;
delete process.env.hello1;
});
it("positive test", () => {
expect(
updateVariableWithLocalEnv("${env:hello} - ${env:hello} : ${env:hello1}")
).toBe("world - world : world1");
});
it("negative test", () => {
expect(() => {
updateVariableWithLocalEnv(
"${env:hello2} - ${env:hello} : ${env:hello1}"
);
}).toThrow(
getErrorMessage({
errorKey: "spk-config-yaml-var-undefined",
values: ["hello2"],
})
);
});
});
describe("Bedrock", () => {
const writeSpy = jest.spyOn(fs, "writeFileSync");
test("valid helm configuration passes", () => {
const randomTmpDir = path.join(os.tmpdir(), uuid());
shell.mkdir("-p", randomTmpDir);
const validBedrockYaml: BedrockFile = {
rings: {},
services: [
{
path: "foo/a",
helm: {
chart: {
chart: "elastic",
repository: "some-repo",
},
},
k8sBackend: "backendservice",
k8sBackendPort: 1337,
pathPrefix: "servicepath",
pathPrefixMajorVersion: "v1",
},
{
path: "foo/b",
helm: {
chart: {
git: "foo",
path: "some/path",
sha: "cef8361c62e7a91887625336eb13a8f90dbcf8df",
},
},
k8sBackend: "backendservice",
k8sBackendPort: 1337,
pathPrefix: "servicepath",
pathPrefixMajorVersion: "v1",
},
],
version: "1.0",
};
bedrockYaml.create(randomTmpDir, validBedrockYaml);
const expectedFilePath = path.join(randomTmpDir, "bedrock.yaml");
expect(writeSpy).toBeCalledWith(
expectedFilePath,
`${getVersionMessage()}\n`,
"utf8"
);
const bedrockConfig = Bedrock(randomTmpDir);
expect(bedrockConfig).toBeTruthy();
});
test("invalid helm configuration fails", () => {
const randomTmpDir = path.join(os.tmpdir(), uuid());
shell.mkdir("-p", randomTmpDir);
const validBedrockYaml: BedrockFile = {
rings: {},
services: {
"foo/a": {
helm: {
// Missing 'chart'
chart: {
repository: "some-repo",
},
},
},
"foo/b": {
helm: {
// missing 'path'
chart: {
git: "foo",
sha: "cef8361c62e7a91887625336eb13a8f90dbcf8df",
},
},
},
},
version: "1.0",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
bedrockYaml.create(randomTmpDir, validBedrockYaml);
const expectedFilePath = path.join(randomTmpDir, "bedrock.yaml");
expect(writeSpy).toBeCalledWith(
expectedFilePath,
`${getVersionMessage()}\n`,
"utf8"
);
expect(() => {
Bedrock(randomTmpDir);
}).toThrow();
});
});
const mockFileName = "src/commands/mocks/spk-config.yaml";
describe("Initializing a project to use spk with a config file", () => {
test("init command basic file test", async () => {
// Create random directory to initialize
const randomTmpDir = createTempDir();
process.env["test_name"] = "my_storage_account";
process.env["test_key"] = "my_storage_key";
const filename = path.resolve(mockFileName);
saveConfiguration(filename, randomTmpDir);
loadConfiguration(path.join(randomTmpDir, "config.yaml"));
const config = Config();
expect(config.introspection).toBeDefined();
expect(config.introspection?.azure?.account_name).toBe(
process.env.test_name
);
const key = await config.introspection?.azure?.key;
expect(key).toBe(process.env.test_key);
expect(config.introspection?.azure?.table_name).toBe(
process.env.test_name + "+" + process.env.test_key
);
});
});
describe("Initializing a project a config file but no env vars", () => {
test("init command basic file without env vars", () => {
const filename = path.resolve(mockFileName);
process.env["test_name"] = "";
process.env["test_key"] = "";
expect(() => {
loadConfiguration(filename);
}).toThrow();
});
});
describe("Initializing a project with a non-existent file", () => {
test("Non-existent file test", () => {
const filename = path.resolve("./spk-config-test.yaml");
expect(() => {
loadConfiguration(filename);
}).toThrow(getErrorMessage("spk-config-yaml-load-err"));
});
});
describe("Writing to default config location", () => {
test("Default config location exists", () => {
const filename = path.resolve(mockFileName);
process.env["test_name"] = "testStorageName";
process.env["test_key"] = "testStorageKey";
loadConfiguration(filename);
saveConfiguration(filename);
loadConfiguration(defaultConfigFile());
expect(Config().azure_devops).toBeDefined();
});
});