-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom-config.test.ts
219 lines (202 loc) · 7.48 KB
/
from-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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import specs from "@unleash/client-specification/specifications/index.json";
import got from "got";
import YAML from "yaml";
import fs from "fs";
import { GenericContainer, Network, StartedNetwork } from "testcontainers";
import {
MockServerConfig,
OSSServerConfig,
parseResult,
TestConfiguration,
TestYamlConfig,
} from "./lib/Config";
import {
ContainerInstance,
UnleashServerInterface,
} from "./lib/BaseContainers";
import { SDKOptions } from "./lib/SDKContainers";
const path = require("path");
const rawConfig = fs.readFileSync(
`./src/${process.env.CONFIG || "all-against-mock"}.yaml`,
"utf8"
);
const parsedConfig = YAML.parse(rawConfig) as TestYamlConfig;
const sdks = process.env.SDK
? [{ name: process.env.SDK, type: process.env.SDK }]
: parsedConfig.sdks.map((c) => {
c.name = c.name || c.type;
return c;
});
const servers = process.env.SERVER
? [{ type: process.env.SERVER }]
: parsedConfig.servers;
const testDetails = parsedConfig.tests.load;
const excludeTests = testDetails.excluding || [];
function parseConfig(
server: OSSServerConfig | MockServerConfig
): TestConfiguration {
let config: TestConfiguration;
if (server.type === "OSSServer") {
const ossServer = server as OSSServerConfig;
config = {
serverImpl: ossServer.type,
postgres: {
image: ossServer.postgresImage,
dbName: "unleash",
user: "unleash_user",
password: "unleash.the.password",
},
unleash: {
image: ossServer.image,
clientToken: parsedConfig.clientToken,
adminToken: parsedConfig.adminToken,
},
};
} else if (server.type === "MockServer" || server.type === "Edge") {
// This is pointless, we should remove TestConfiguration
config = {
serverImpl: server.type,
postgres: {
image: "",
dbName: "",
user: "",
password: "",
},
unleash: {
image: "",
clientToken: parsedConfig.clientToken,
adminToken: parsedConfig.adminToken,
},
};
} else {
throw new Error(`Invalid server config type ${server.type}`);
}
return config;
}
function partition<T>(arr: T[], check: (o: T) => boolean): T[][] {
let initialValue: T[][] = [[], []];
return arr.reduce((result, element) => {
result[check(element) ? 0 : 1].push(element);
return result;
}, initialValue);
}
const tests: string[] = specs.filter(
(spec) => !excludeTests.includes(spec.slice(0, 2))
);
describe.each(servers)(`$type`, (server) => {
let unleashServer: ContainerInstance & UnleashServerInterface;
let network: StartedNetwork;
let initialized = false;
let sdkContainers = new Map<string, ContainerInstance>();
let config = parseConfig(server);
describe.each(tests)(`%s suite`, (testName) => {
// eslint-disable-next-line
const definition: ISpecDefinition = require(`@unleash/client-specification/specifications/${testName}`);
beforeAll(async () => {
if (!initialized) {
console.log(
`===== Initializing Unleash ${server.type} ${definition.name} =====`
);
network = await new Network().start();
if (process.env.DEBUG) {
const ngrepContainer = await GenericContainer.fromDockerfile(
path.resolve("./src/tools/ngrep"),
"Dockerfile"
).build();
await ngrepContainer
.withNetworkMode("host") // bind to host network cause it will listen to docker bridged network
.withBindMount(path.resolve("."), "/output")
.withCmd([
"-q", // Be quiet; don't output any information other than packet headers and their payloads (if relevant).
"-t", // Print a timestamp in the form of YYYY/MM/DD HH:MM:SS.UUUUUU everytime a packet is matched.
"-l", // Make stdout line buffered
"-Wbyline", // Specify alternate manner for displaying packets
`-dbr-${network.getId().slice(0, 12)}`, // listen to the bridge interface created for this network id
".*HTTP.*", // match only HTTP
"/output/ngrep-http.out", // this last parameter will be used as output file
])
.start();
}
unleashServer = require("./servers/index").create(config, network);
await unleashServer.initialize();
initialized = true;
} else {
console.log(
`===== Reseting Unleash ${server.type} before ${definition.name} =====`
);
await unleashServer.reset();
}
// ========= set unleash state (~50ms)
let succeed = await unleashServer.setState(definition.state);
expect(succeed).toBeTruthy();
});
describe.each(sdks)(`$name SDK`, (sdkTestConfig) => {
const excludedForSDK = sdkTestConfig.excluding || [];
let sdkUrl: string;
console.log(`${JSON.stringify(sdkTestConfig)}`);
beforeAll(async () => {
let sdkContainer = sdkContainers.get(sdkTestConfig.type);
if (!sdkContainer) {
// console.log(`===== Initializing ${sdk} =====`)
let options: SDKOptions = {
unleashApiUrl: `http://${unleashServer.getInternalIpAddress()}:${unleashServer.getInternalPort()}/api`,
apiToken: config.unleash.adminToken,
network: network,
sdkImpl: sdkTestConfig.client,
};
let { create } = require(`./sdks/${sdkTestConfig.type}/container`);
sdkContainer = create(options) as ContainerInstance;
await sdkContainer.initialize();
sdkContainers.set(sdkTestConfig.name!, sdkContainer);
} else {
// cleanup cached SDK state
// console.log(`===== Reseting state of ${sdk} =====`)
await sdkContainer.reset();
}
sdkUrl = `http://localhost:${sdkContainer.getMappedPort()}`;
});
const splitFn = (t: ITestDef) =>
excludedForSDK.filter(
(s) => testName.startsWith(s) || t.description.startsWith(s)
).length > 0;
const [skip, evaluate] = partition(definition.tests || [], splitFn);
console.log(`=== ${definition.name} TO SKIP: ${JSON.stringify(skip)}`);
if (skip.length > 0) test.skip.each(skip)(`$description`, () => {});
if (evaluate.length > 0) {
test.each(evaluate)(`$description`, async (testCase) => {
const { body, statusCode } = await got.post(`${sdkUrl}/is-enabled`, {
json: {
toggle: testCase.toggleName,
context: testCase.context,
},
});
expect(statusCode).toBe(200);
const result: EnabledResult = JSON.parse(body);
expect(result.enabled).toBe(testCase.expectedResult);
});
}
const [skipVariants, evaluateVariants] = partition(
definition.variantTests || [],
splitFn
);
if (skipVariants.length > 0)
test.skip.each(skipVariants)(`$description`, () => {});
if (evaluateVariants.length > 0) {
test.each(evaluateVariants)(`$description`, async (testCase) => {
const { body, statusCode } = await got.post(`${sdkUrl}/variant`, {
json: {
toggle: testCase.toggleName,
context: testCase.context,
},
});
expect(statusCode).toBe(200);
const variantResult: VariantResult = JSON.parse(body);
const result = parseResult(sdkTestConfig.name!, variantResult);
expect(result).toEqual(
expect.objectContaining(testCase.expectedResult)
);
});
}
});
});
});