-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventbridge.js
98 lines (84 loc) · 2.5 KB
/
eventbridge.js
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
import {check} from 'k6';
import {EventBridgeClient} from "k6/x/aws";
import {config} from './localstack.js';
// Set your k6 run configuration:
// https://k6.io/docs/using-k6/k6-options
export const options = {
iterations: 1,
// Demonstrative k6 thresholds.
thresholds: {
checks: [{threshold: 'rate == 1.00', abortOnFail: true}],
},
};
export default function () {
const eb = new EventBridgeClient(config);
// Create an event bus.
const eventBusName = 'test-event-bus';
const {event_bus_arn} = eb.createEventBus({name: eventBusName});
check(event_bus_arn, {
'event bus creation must succeed': (event_bus_arn) => event_bus_arn.includes(`event-bus/${eventBusName}`),
});
// List event buses to confirm creation. It must return two event buses:
// - the default event bus
// - the one we created
const {event_buses} = eb.listEventBuses();
check(event_buses, {
'it must return two event buses': (buses) => buses.length === 2,
'test-event-bus must be in the list': (buses) => buses.some(b => b.name.normalize() === eventBusName),
});
// Put an event onto the event bus.
const eventDetail = JSON.stringify({key1: 'value1'});
const putResponse = eb.putEvents({
entries: [
{
event_bus_name: eventBusName,
source: 'my.source',
detail_type: 'myDetailType',
detail: eventDetail,
},
],
});
// Check that the event was successfully put.
check(putResponse, {
'put events must succeed': (res) => res.failed_entry_count === 0,
});
// Create a rule.
const ruleName = 'test-rule';
const {rule_arn} = eb.putRule({
name: ruleName,
event_bus_name: eventBusName,
event_pattern: JSON.stringify({
source: ['my.source'],
}),
});
check(rule_arn, {
'rule creation must succeed': (arn) => arn.includes(`rule/${eventBusName}/${ruleName}`),
});
// Put a target for the rule.
const targetId = 'test-target';
const targetArn = 'arn:aws:lambda:us-east-1:123456789012:function:test-function';
const putTargetsResponse = eb.putTargets({
rule: ruleName,
event_bus_name: eventBusName,
targets: [
{
id: targetId,
arn: targetArn,
},
],
});
check(putTargetsResponse, {
'put targets must succeed': (res) => res.failed_entry_count === 0,
});
// Remove the target.
const removeTargetsResponse = eb.removeTargets({
rule: ruleName,
event_bus_name: eventBusName,
ids: [targetId],
});
check(removeTargetsResponse, {
'remove targets must succeed': (res) => res.failed_entry_count === 0,
});
// Delete the event bus (idempotent).
eb.deleteEventBus({name: eventBusName});
}