-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstep-function-service.spec.ts
More file actions
368 lines (334 loc) · 13.5 KB
/
step-function-service.spec.ts
File metadata and controls
368 lines (334 loc) · 13.5 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
import { PARENT_ID, StepFunctionContextService } from "./step-function-service";
describe("StepFunctionContextService", () => {
const legacyStepFunctionEvent = {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn: "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
} as const;
const lambdaRootStepFunctionEvent = {
_datadog: {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn: "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
"x-datadog-trace-id": "10593586103637578129",
"x-datadog-tags": "_dd.p.dm=-0,_dd.p.tid=6734e7c300000000",
"serverless-version": "v1",
},
} as const;
const nestedStepFunctionEvent = {
_datadog: {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn: "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
RootExecutionId:
"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:a1b2c3d4-e5f6-7890-1234-56789abcdef0:9f8e7d6c-5b4a-3c2d-1e0f-123456789abc",
"serverless-version": "v1",
},
} as const;
describe("instance", () => {
it("returns the same instance every time", () => {
const instance1 = StepFunctionContextService.instance();
const instance2 = StepFunctionContextService.instance();
expect(instance1).toBe(instance2);
});
});
describe("setContext", () => {
beforeEach(() => {
jest.resetModules();
StepFunctionContextService["_instance"] = undefined as any;
});
it.each([
["event is not an object", "event"],
["event is missing Execution property", {}],
[
"Execution is not defined",
{
...legacyStepFunctionEvent,
Execution: undefined,
},
],
[
"Execution Id is not a string",
{
...legacyStepFunctionEvent,
Execution: {
...legacyStepFunctionEvent.Execution,
Id: 1,
},
},
],
[
"State is not defined",
{
...legacyStepFunctionEvent,
State: undefined,
},
],
[
"State EnteredTime is not a string",
{
...legacyStepFunctionEvent,
State: {
...legacyStepFunctionEvent.State,
EnteredTime: 12345,
},
},
],
[
"State Name is not a string",
{
...legacyStepFunctionEvent,
State: {
...legacyStepFunctionEvent,
Name: 1,
},
},
],
])("skips setting context when `%s`", (_, event) => {
const instance = StepFunctionContextService.instance(event);
instance["setContext"](event);
expect(instance.context).toBeUndefined();
});
it("sets context from valid legacy event", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"](legacyStepFunctionEvent);
expect(instance.context).toEqual({
execution_id:
"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
redrive_count: "0",
retry_count: "2",
state_entered_time: "2022-12-08T21:08:19.224Z",
state_name: "step-one",
});
});
it("sets context from valid nested event", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"](nestedStepFunctionEvent);
expect(instance.context).toEqual({
execution_id:
"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
redrive_count: "0",
retry_count: "2",
state_entered_time: "2022-12-08T21:08:19.224Z",
state_name: "step-one",
root_execution_id:
"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:a1b2c3d4-e5f6-7890-1234-56789abcdef0:9f8e7d6c-5b4a-3c2d-1e0f-123456789abc",
serverless_version: "v1",
});
});
it("sets context from valid Lambda root event", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"](lambdaRootStepFunctionEvent);
expect(instance.context).toEqual({
execution_id:
"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
redrive_count: "0",
retry_count: "2",
state_entered_time: "2022-12-08T21:08:19.224Z",
state_name: "step-one",
trace_id: "10593586103637578129",
dd_p_tid: "6734e7c300000000",
serverless_version: "v1",
});
});
});
describe("spanContext", () => {
beforeEach(() => {
jest.resetModules();
StepFunctionContextService["_instance"] = undefined as any;
});
it("returns a SpanContextWrapper when legacy event is valid", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"](legacyStepFunctionEvent);
const spanContext = instance.spanContext;
expect(spanContext).not.toBeNull();
expect(spanContext?.toTraceId()).toBe("1139193989631387307");
expect(spanContext?.toSpanId()).toBe("7747304477664363642");
expect(spanContext?.sampleMode()).toBe("1");
expect(spanContext?.source).toBe("event");
});
it("returns a SpanContextWrapper when nested event is valid", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"](nestedStepFunctionEvent);
const spanContext = instance.spanContext;
expect(spanContext).not.toBeNull();
expect(spanContext?.toTraceId()).toBe("8676990472248253142");
expect(spanContext?.toSpanId()).toBe("7747304477664363642");
expect(spanContext?.sampleMode()).toBe("1");
expect(spanContext?.source).toBe("event");
});
it("returns a SpanContextWrapper when Lambda root event is valid", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"](lambdaRootStepFunctionEvent);
const spanContext = instance.spanContext;
expect(spanContext).not.toBeNull();
expect(spanContext?.toTraceId()).toBe("10593586103637578129");
expect(spanContext?.toSpanId()).toBe("7747304477664363642");
expect(spanContext?.sampleMode()).toBe("1");
expect(spanContext?.source).toBe("event");
});
it("returns null when context is not set", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"]({});
const spanContext = instance.spanContext;
expect(spanContext).toBeNull();
});
it("returns a SpanContextWrapper when event is from legacy lambda", () => {
const instance = StepFunctionContextService.instance();
// Force setting event
instance["setContext"]({ Payload: legacyStepFunctionEvent });
const spanContext = instance.spanContext;
expect(spanContext).not.toBeNull();
expect(spanContext?.toTraceId()).toBe("1139193989631387307");
expect(spanContext?.toSpanId()).toBe("7747304477664363642");
expect(spanContext?.sampleMode()).toBe("1");
expect(spanContext?.source).toBe("event");
});
});
describe("deterministicSha256HashToBigIntString", () => {
it("returns the same hash number generated in `logs backend` for a random string", () => {
const instance = StepFunctionContextService.instance();
const hash = instance["deterministicSha256HashToBigIntString"]("some_testing_random_string", PARENT_ID);
expect(hash).toEqual("4364271812988819936");
});
it("returns the same hash number generated in `logs backend` for execution id # state name # entered time", () => {
const instance = StepFunctionContextService.instance();
const hash = instance["deterministicSha256HashToBigIntString"](
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z",
PARENT_ID,
);
expect(hash).toEqual("4340734536022949921");
});
});
describe("deterministicSha256Hash", () => {
it.each([
[
"a random string",
"some_testing_random_string",
"0011110010010001000000100001011101001100011100101101100111100000",
],
[
"an execution id",
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d041f4",
"0100010100110010010010100001011001110100111011010100110010000100",
],
[
"another execution id",
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111",
"0010111110001100100010000101001100110000000000010111011100101011",
],
[
"execution id # state name # entered time",
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z",
"0011110000111101011000110000111111110011111010110000000000100001",
],
])("returns the same hash number generated in `logs backend` for %s", (_, str, expected) => {
const instance = StepFunctionContextService.instance();
const hash = instance["deterministicSha256Hash"](str, PARENT_ID);
expect(hash).toEqual(expected);
});
it("returns a hash always leading with 0", () => {
const instance = StepFunctionContextService.instance();
for (let i = 0; i < 20; i++) {
const hash = instance["deterministicSha256Hash"](i.toString(), PARENT_ID);
expect(hash.substring(0, 1)).toMatch("0");
}
});
it("returns different hashes with different strings", () => {
const instance = StepFunctionContextService.instance();
const times = 20;
for (let i = 0; i < times; i++) {
for (let j = i + 1; j < times; j++) {
const hash1 = instance["deterministicSha256Hash"](i.toString(), PARENT_ID);
const hash2 = instance["deterministicSha256Hash"](j.toString(), PARENT_ID);
expect(hash1).not.toMatch(hash2);
}
}
});
});
describe("numberToBinaryString", () => {
const instance = StepFunctionContextService.instance();
it.each([
[0, "00000000"],
[1, "00000001"],
[2, "00000010"],
[3, "00000011"],
[4, "00000100"],
])("returns the right binary number for %s => %s", (hex, expected) => {
const binary = instance["numberToBinaryString"](hex);
expect(binary).toBe(expected);
});
});
describe("test 64 bits deterministicSha256HashToBigIntString for span id", () => {
const instance = StepFunctionContextService.instance();
it("first test of #1", () => {
const actual = instance["deterministicSha256HashToBigIntString"](
"arn:aws:states:sa-east-1:425362996713:stateMachine:MyStateMachine-b276uka1j#lambda#1",
PARENT_ID,
);
expect(actual).toEqual("3711631873188331089");
});
it("test same hashing number is generated as logs-backend for execution id # state name # entered time", () => {
const actual = instance["deterministicSha256HashToBigIntString"](
"arn:aws:states:sa-east-1:425362996713:stateMachine:MyStateMachine-b276uka1j#lambda#2",
PARENT_ID,
);
expect(actual).toEqual("5759173372325510050");
});
});
});