-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathkeploy.ts
More file actions
430 lines (393 loc) · 12 KB
/
keploy.ts
File metadata and controls
430 lines (393 loc) · 12 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import HttpClient, { Request } from "./client";
import { toHttpHeaders, transformToSnakeCase } from "./util";
import { getRequestHeader } from "../integrations/express/middleware";
import { name as packageName } from "../package.json";
import * as grpc from "@grpc/grpc-js";
import * as protoLoader from "@grpc/proto-loader";
import { ProtoGrpcType } from "../proto/services";
import path from "path";
import { RegressionServiceClient } from "../proto/services/RegressionService";
import { TestCaseReq } from "../proto/services/TestCaseReq";
import { TestCase } from "../proto/services/TestCase";
import { StrArr } from "../proto/services/StrArr";
import assert = require("assert");
import { createExecutionContext, getExecutionContext } from "./context";
const PROTO_PATH = "../proto/services.proto";
const packageDef = protoLoader.loadSync(path.resolve(__dirname, PROTO_PATH));
const grpcObj = grpc.loadPackageDefinition(
packageDef
) as unknown as ProtoGrpcType;
// export const Http = "Http";
export const V1_BETA2 = "api.keploy.io/v1beta2",
V1_BETA1 = "api.keploy.io/v1beta1",
HTTP = "Http",
GENERIC = "Generic";
type AppConfigFilter = {
urlRegex?: string;
};
type AppConfig = {
name: string;
host: string;
port: number;
delay: number;
timeout: number;
filter: AppConfigFilter;
testCasePath: string;
mockPath: string;
};
type ServerConfig = {
url: string;
licenseKey: string;
};
type ID = string;
type HttpResponse = {
status_code: number;
header: { [key: string]: StrArr };
body: string;
};
export default class Keploy {
appConfig: AppConfig;
serverConfig: ServerConfig;
responses: Record<ID, HttpResponse>;
dependencies: Record<ID, unknown>;
mocks: Record<ID, unknown>;
grpcClient: RegressionServiceClient;
constructor(
app: Partial<AppConfig> = {},
server: Partial<ServerConfig> = {}
) {
this.appConfig = this.validateAppConfig(app);
this.serverConfig = this.validateServerConfig(server);
this.grpcClient = new grpcObj.services.RegressionService(
this.serverConfig.url,
grpc.credentials.createInsecure()
);
this.responses = {};
this.dependencies = {};
this.mocks = {};
console.log("Keploy running in: ",process.env.KEPLOY_MODE," mode");
}
validateServerConfig({
url = process.env.KEPLOY_SERVER_URL || "localhost:6789",
licenseKey = process.env.KEPLOY_LICENSE_KEY || "",
}) {
return { url, licenseKey };
}
validateAppConfig({
name = process.env.KEPLOY_APP_NAME || packageName,
host = process.env.KEPLOY_APP_HOST || "localhost",
port = process.env.KEPLOY_APP_PORT || 8080,
delay = process.env.KEPLOY_APP_DELAY || 5,
timeout = process.env.KEPLOY_APP_TIMEOUT || 60,
filter = process.env.KEPLOY_APP_FILTER || {},
// testCasePath and mockPath can be defined in the .env file. If not defined then a folder named
// keploy-tests will be created which will contain mock folder.
testCasePath = path.resolve(
process.env.KEPLOY_TEST_CASE_PATH || "./keploy-tests"
),
mockPath = path.resolve(
process.env.KEPLOY_MOCK_PATH || "./keploy-tests/mock"
),
}) {
const errorFactory = (key: string) =>
new Error(`Invalid App config key: ${key}`);
port = Number(port);
if (Number.isNaN(port)) {
throw errorFactory("port");
}
delay = Number(delay);
if (Number.isNaN(delay)) {
throw errorFactory("delay");
}
timeout = Number(timeout);
if (Number.isNaN(timeout)) {
throw errorFactory("timeout");
}
if (typeof filter === "string") {
try {
filter = JSON.parse(filter);
} catch {
throw errorFactory("filter");
}
}
return { name, host, port, delay, timeout, filter, testCasePath, mockPath };
}
async runTests() {
if (process.env.KEPLOY_MODE == "test") {
console.log("test starting in " + this.appConfig.delay + "s");
setTimeout(async () => {
await this.test();
}, this.appConfig.delay * 1000);
// await this.test();
}
return this;
}
getDependencies(id: ID) {
return this.dependencies[id] as object[] | undefined;
}
getMocks(id: ID) {
return this.mocks[id] as object[] | undefined;
}
getResp(id: ID) {
return this.responses[id];
}
// stores http response for unique test-ids to capture them at middleware layer
putResp(id: ID, resp: HttpResponse) {
// put http response in map only once for unique test-ids. Since, finish event
// can trigger multiple time for redirect.
if (this.responses[id] === null || this.responses[id] === undefined) {
this.responses[id] = resp;
}
}
capture(req: TestCaseReq) {
return this.put(req);
}
async fetch(testcases: TestCase[], offset: number) {
const limit = 25;
const app = this.appConfig.name;
let end = false;
await this.grpcClient.getTcs(
{
app: app,
offset: offset.toString(),
limit: limit.toString(),
TestCasePath: this.appConfig.testCasePath,
MockPath: this.appConfig.mockPath,
},
(err, response) => {
if (err !== null) {
console.error("failed to fetch test cases from keploy. error: ", err);
}
if (
response == null ||
response.tcs == undefined ||
response.tcs.length == 0
) {
// Base case of the recursive function.
// If the response is null then all the testcases cases will go to the afterfetch function.
end = true;
this.afterFetch(testcases);
return;
}
testcases.push(...response.tcs);
if (response.eof == true) {
// Base case of the recursive function.
// If the eof is true then all the testcases cases will go to the afterfetch function.
end = true;
this.afterFetch(testcases);
return testcases;
}
// Recursive call to the function fetch.
this.fetch(testcases, offset + 25);
}
);
return testcases;
}
async test() {
await this.fetch([], 0);
}
// returns promise to capture the code coverage of recorded testc cases
async assertTests() {
return new Promise(async (resolve) => {
createExecutionContext({ resolve: resolve });
await this.test();
});
}
// afterFetch fuction contains Start, Test and End grpc calls to the function.
// The nesting is done in the grpc calls because they return their responses in the callback function.
async afterFetch(testCases: TestCase[]) {
const totalTests = testCases.length;
this.grpcClient.Start(
{
app: this.appConfig.name,
total: totalTests.toString(),
MockPath: this.appConfig.mockPath,
TestCasePath: this.appConfig.testCasePath,
},
async (err, resp) => {
if (err !== null) {
console.error("failed to call start method of keploy. error: ", err);
}
const testId = resp?.id;
console.log(
"starting test execution. { id: ",
testId,
" }, { total tests: ",
totalTests,
" }"
);
let pass = true;
for (const [i, testCase] of testCases.entries()) {
console.log(
"testing ",
i + 1,
" of ",
totalTests,
" { testcase id: ",
testCase.id,
" }"
);
const resp = await this.simulate(testCase).catch((err) =>
console.log(err)
);
this.grpcClient.Test(
{
AppID: this.appConfig.name,
ID: testCase.id,
RunID: testId,
Resp: {
Body: resp?.body,
StatusCode: resp?.status_code,
Header: resp?.header,
},
TestCasePath: this.appConfig.testCasePath,
MockPath: this.appConfig.mockPath,
Type: HTTP,
},
(err, response) => {
if (err !== null) {
console.error(
"failed to call test method of keploy. error: ",
err
);
}
if (response?.pass?.pass === false) {
pass = false;
}
console.log(
"result { testcase id: ",
testCase.id,
" }, { passed: ",
response?.pass?.pass,
" }"
);
if (i === testCases.length - 1) {
this.end(testId, pass);
console.log(
"test run completed { run id: ",
testId,
" }, passed overall: ",
pass
);
// fetches resolve function of the Promise which was returned to unit test for code-coverage
const resolve = getExecutionContext()?.context?.resolve;
if (resolve !== undefined) {
// asserts for testrun result
assert.equal(pass, true);
resolve(1);
}
}
}
);
}
}
);
}
async get(id: ID) {
this.grpcClient.GetTC({ id: id }, (err, resp) => {
if (err !== null) {
console.error("failed to get testcase with id: ", id, ", error: ", err);
}
});
}
private end(id: string | undefined, status: boolean) {
this.grpcClient.End(
{ id: id, status: status.toString() },
(err, response) => {
if (err !== null) {
console.error("failed to call end method of keploy. error: ", err);
}
}
);
// return resp.id;
}
private async simulate(tc: TestCase) {
if (tc.id == undefined) {
return;
}
this.dependencies[tc.id] = tc.Deps;
this.mocks[tc.id] = tc.Mocks;
const headers: { [key: string]: string | string[] } = {
KEPLOY_TEST_ID: tc.id,
};
if (tc.HttpReq?.Header !== undefined) {
Object.assign(headers, toHttpHeaders(tc.HttpReq?.Header));
}
const client = new HttpClient(
`http://${this.appConfig.host}:${this.appConfig.port}`
);
//@ts-ignore
const requestUrl = `${tc.HttpReq?.URL.substr(1)}`;
await client.makeHttpRequestRaw(
new Request()
.setHttpHeader("KEPLOY_TEST_ID", tc.id)
//@ts-ignore
.setHttpHeaders(headers)
//@ts-ignore
.create(tc.HttpReq?.Method, requestUrl, tc.HttpReq?.Body)
);
const resp = this.getResp(tc.id);
delete this.dependencies[tc.id];
delete this.mocks[tc.id];
delete this.responses[tc.id];
return resp;
}
private async put(tcs: TestCaseReq) {
if (
this.appConfig.filter.urlRegex &&
tcs?.URI?.match(this.appConfig.filter.urlRegex)
) {
return;
}
this.grpcClient.PostTC(tcs, (err, response) => {
if (err != null) {
console.error("failed to post testcase to keploy server. error: ", err);
}
if (
response === undefined ||
response.tcsId === undefined ||
response.tcsId.id === ""
) {
return;
}
this.denoise(response.tcsId.id, tcs);
});
}
private async denoise(id: string, tcs: TestCaseReq) {
if (tcs.URI === undefined) {
return;
}
const resp = await this.simulate({
id: id,
URI: tcs.URI,
HttpReq: tcs.HttpReq,
Deps: tcs.Dependency,
Mocks: tcs.Mocks,
}).catch((err) => console.log(err));
this.grpcClient.DeNoise(
{
AppID: this.appConfig.name,
ID: id,
Resp: {
Body: resp?.body,
Header: resp?.header,
StatusCode: resp?.status_code,
},
TestCasePath: this.appConfig.testCasePath,
MockPath: this.appConfig.mockPath,
Type: HTTP,
},
(err, response) => {
if (err != undefined) {
console.error(
"failed to call denoise method of keploy. error: ",
err
);
}
return response;
}
);
}
}