-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtransmissionCycle_test.js
More file actions
130 lines (112 loc) · 3.6 KB
/
Copy pathtransmissionCycle_test.js
File metadata and controls
130 lines (112 loc) · 3.6 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
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2020
*/
'use strict';
const path = require('path');
const { fork } = require('child_process');
const { expect } = require('chai');
const proxyquire = require('proxyquire');
const portfinder = require('@instana/collector/test/test_util/portfinder');
const { retry, createFakeLogger } = require('../../../core/test/test_util');
const testConfig = require('@instana/core/test/config');
const core = require('@instana/core');
let transmissionCycle;
// NOTE: This test does not run against AWS Fargate. Instead, it is mocked using a metadata mock API.
// It mimics the Amazon ECS Task Metadata Endpoint, which provides env details for tasks running on AWS Fargate.
// API Documentation: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3-fargate.html
// Local mock path: packages/aws-fargate/test/metadata_mock/index.js
describe('transmission cycle', function () {
this.timeout(testConfig.getTestTimeout());
this.slow(testConfig.getTestTimeout() / 2);
let messagesFromMetadataMock = [];
let metadataMock;
let metadataMockUrl;
let metricsSent;
let onReadyPayload;
let onReadyError;
before(() => {
const config = core.config.normalize();
core.secrets.init(config);
const metadataMockPort = portfinder();
metadataMockUrl = `http://localhost:${metadataMockPort}`;
messagesFromMetadataMock = [];
metadataMock = fork(path.join(__dirname, '../metadata_mock'), {
stdio: testConfig.getAppStdio(),
env: Object.assign({ METADATA_MOCK_PORT: metadataMockPort })
});
metadataMock.on('message', message => {
messagesFromMetadataMock.push(message);
});
return retry(() => {
if (messagesFromMetadataMock.indexOf('metadata mock: started') < 0) {
return Promise.reject(new Error('The metadata mock is still not up.'));
}
});
});
beforeEach(() => {
metricsSent = [];
onReadyPayload = null;
onReadyError = null;
transmissionCycle = proxyquire('../../src/metrics/transmissionCycle', {
'@instana/serverless': {
backendConnector: { sendMetrics }
}
});
});
afterEach(() => {
transmissionCycle.deactivate();
});
after(() => {
messagesFromMetadataMock = [];
return new Promise(resolve => {
if (metadataMock) {
metadataMock.once('exit', resolve);
metadataMock.kill();
} else {
resolve();
}
});
});
it('should call onReady with initial payload', () => {
init();
return retry(() => {
expect(onReadyError).to.not.exist;
expect(onReadyPayload).to.exist;
expect(onReadyPayload.name).to.equal('com.instana.plugin.aws.ecs.container');
expect(onReadyPayload.entityId).to.equal(
'arn:aws:ecs:us-east-2:555123456789:task/55566677-c1e5-5780-9806-aabbccddeeff::nodejs-fargate-test-container'
);
});
});
it('should send out metrics', () => {
init();
return retry(() => {
expect(metricsSent).to.have.lengthOf.at.least(1);
const plugins = metricsSent[0].plugins;
expect(plugins).to.have.lengthOf.at.least(5);
});
});
function init() {
transmissionCycle.init(
{
logger: createFakeLogger(),
metrics: { transmissionDelay: 1000 }
},
metadataMockUrl,
onReady
);
}
function onReady(err, payload) {
if (err) {
onReadyError = err;
} else {
onReadyPayload = payload;
transmissionCycle.activate();
}
}
function sendMetrics(metrics, callback) {
metricsSent.push(metrics);
process.nextTick(callback);
}
});