This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
/
test-detect-resource.ts
327 lines (298 loc) · 12.5 KB
/
test-detect-resource.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
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
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CoreResource } from '@opencensus/core';
import * as assert from 'assert';
import {
BASE_PATH,
HEADER_NAME,
HEADER_VALUE,
HOST_ADDRESS,
SECONDARY_HOST_ADDRESS,
} from 'gcp-metadata';
import * as nock from 'nock';
import * as resource from '../src';
import {
CLOUD_RESOURCE,
CONTAINER_RESOURCE,
HOST_RESOURCE,
K8S_RESOURCE,
} from '../src/constants';
import * as resourceUtil from '../src/resource-utils';
// NOTE: nodejs switches all incoming header names to lower case.
const HEADERS = {
[HEADER_NAME.toLowerCase()]: HEADER_VALUE,
};
const INSTANCE_PATH = BASE_PATH + '/instance';
const INSTANCE_ID_PATH = BASE_PATH + '/instance/id';
const PROJECT_ID_PATH = BASE_PATH + '/project/project-id';
const ZONE_PATH = BASE_PATH + '/instance/zone';
const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
const mockedAwsResponse = {
instanceId: 'my-instance-id',
accountId: 'my-account-id',
region: 'my-region',
};
describe('detectResource', () => {
before(() => {
nock.disableNetConnect();
});
after(() => {
nock.enableNetConnect();
});
beforeEach(() => {
nock.cleanAll();
resourceUtil.clear();
delete process.env.KUBERNETES_SERVICE_HOST;
delete process.env.NAMESPACE;
delete process.env.CONTAINER_NAME;
delete process.env.OC_RESOURCE_TYPE;
delete process.env.OC_RESOURCE_LABELS;
delete process.env.HOSTNAME;
CoreResource.setup();
});
it('should return GCP_GKE_CONTAINER resource when KUBERNETES_SERVICE_HOST is set', async () => {
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
process.env.HOSTNAME = 'my-hostname';
const scope = nock(HOST_ADDRESS)
.get(CLUSTER_NAME_PATH)
.reply(200, () => 'my-cluster', HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS);
const { type, labels } = await resource.detectResource();
scope.done();
assert.deepStrictEqual(type, resource.K8S_CONTAINER_TYPE);
assert.strictEqual(Object.keys(labels).length, 6);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], 'my-zone');
assert.strictEqual(labels[K8S_RESOURCE.CLUSTER_NAME_KEY], 'my-cluster');
assert.strictEqual(labels[K8S_RESOURCE.POD_NAME_KEY], 'my-hostname');
assert.strictEqual(labels[K8S_RESOURCE.NAMESPACE_NAME_KEY], '');
assert.strictEqual(labels[CONTAINER_RESOURCE.NAME_KEY], '');
});
it('should return GCP_GKE_CONTAINER resource when KUBERNETES_SERVICE_HOST, NAMESPACE and CONTAINER_NAME is set', async () => {
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
process.env.NAMESPACE = 'my-namespace';
process.env.HOSTNAME = 'my-hostname';
process.env.CONTAINER_NAME = 'my-container-name';
const scope = nock(HOST_ADDRESS)
.get(CLUSTER_NAME_PATH)
.reply(200, () => 'my-cluster', HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS);
const { type, labels } = await resource.detectResource();
scope.done();
assert.deepStrictEqual(type, resource.K8S_CONTAINER_TYPE);
assert.strictEqual(Object.keys(labels).length, 6);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], 'my-zone');
assert.strictEqual(labels[K8S_RESOURCE.CLUSTER_NAME_KEY], 'my-cluster');
assert.strictEqual(labels[K8S_RESOURCE.POD_NAME_KEY], 'my-hostname');
assert.strictEqual(labels[K8S_RESOURCE.NAMESPACE_NAME_KEY], 'my-namespace');
assert.strictEqual(
labels[CONTAINER_RESOURCE.NAME_KEY],
'my-container-name'
);
// fetch again, this shouldn't make http call again
const { type: type1, labels: labels1 } = await resource.detectResource();
assert.deepStrictEqual(type, type1);
assert.deepStrictEqual(labels, labels1);
});
it('Should merge resources from CoreResource and detected k8s resource', async () => {
process.env.OC_RESOURCE_TYPE = 'k8s.io/container';
process.env.OC_RESOURCE_LABELS =
'k8s.pod.name=pod-xyz-123,container.name=c1,k8s.namespace.name=default';
CoreResource.setup();
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
const scope = nock(HOST_ADDRESS)
.get(CLUSTER_NAME_PATH)
.reply(200, () => 'my-cluster', HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS);
const { type, labels } = await resource.detectResource();
scope.done();
assert.deepStrictEqual(type, resource.K8S_CONTAINER_TYPE);
assert.strictEqual(Object.keys(labels).length, 6);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], 'my-zone');
assert.strictEqual(labels[K8S_RESOURCE.CLUSTER_NAME_KEY], 'my-cluster');
assert.strictEqual(labels[K8S_RESOURCE.POD_NAME_KEY], 'pod-xyz-123');
assert.strictEqual(labels[K8S_RESOURCE.NAMESPACE_NAME_KEY], 'default');
assert.strictEqual(labels[CONTAINER_RESOURCE.NAME_KEY], 'c1');
});
it('should return GCP_GCE_INSTANCE resource', async () => {
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(INSTANCE_ID_PATH)
.reply(200, () => 4520031799277581759, HEADERS);
const scope1 = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const { type, labels } = await resource.detectResource();
scope1.done();
scope.done();
assert.deepStrictEqual(type, resource.GCP_GCE_INSTANCE_TYPE);
assert.strictEqual(Object.keys(labels).length, 3);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], 'my-zone');
assert.strictEqual(labels[HOST_RESOURCE.ID_KEY], '4520031799277582000');
});
it('should retry if the initial request fails', async () => {
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(500)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(INSTANCE_ID_PATH)
.reply(200, () => 4520031799277581759, HEADERS);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const { type, labels } = await resource.detectResource();
secondaryScope.done();
scope.done();
assert.deepStrictEqual(type, resource.GCP_GCE_INSTANCE_TYPE);
assert.strictEqual(Object.keys(labels).length, 3);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], 'my-zone');
assert.strictEqual(labels[HOST_RESOURCE.ID_KEY], '4520031799277582000');
});
it('should return GCP_GCE_INSTANCE resource and empty data for non avaiable metadata attribute', async () => {
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(413)
.get(INSTANCE_ID_PATH)
.reply(400, undefined, HEADERS);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const { type, labels } = await resource.detectResource();
secondaryScope.done();
scope.done();
assert.deepStrictEqual(type, resource.GCP_GCE_INSTANCE_TYPE);
assert.strictEqual(Object.keys(labels).length, 3);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], '');
assert.strictEqual(labels[HOST_RESOURCE.ID_KEY], '');
// fetch again, this shouldn't make http call again
const { type: type1, labels: labels1 } = await resource.detectResource();
assert.deepStrictEqual(type, type1);
assert.deepStrictEqual(labels, labels1);
});
it('Should merge resources from CoreResource and detected gce resource', async () => {
process.env.OC_RESOURCE_TYPE = 'global';
process.env.OC_RESOURCE_LABELS = 'cloud.zone=zone1,user=user1,version=1.0';
CoreResource.setup();
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(INSTANCE_ID_PATH)
.reply(200, () => 4520031799277581759, HEADERS);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const { type, labels } = await resource.detectResource();
secondaryScope.done();
scope.done();
assert.deepStrictEqual(type, 'global');
assert.strictEqual(Object.keys(labels).length, 5);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-project-id');
assert.strictEqual(labels[CLOUD_RESOURCE.ZONE_KEY], 'zone1');
assert.strictEqual(labels[HOST_RESOURCE.ID_KEY], '4520031799277582000');
assert.strictEqual(labels['user'], 'user1');
assert.strictEqual(labels['version'], '1.0');
});
it('should return aws_ec2_instance resource', async () => {
const gcpScope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.replyWithError({
code: 'ENOTFOUND',
});
const awsScope = nock(resourceUtil.AWS_INSTANCE_IDENTITY_DOCUMENT_URI)
.get('')
.reply(200, () => mockedAwsResponse, HEADERS);
const { type, labels } = await resource.detectResource();
awsScope.done();
gcpScope.done();
assert.deepStrictEqual(type, resource.AWS_EC2_INSTANCE_TYPE);
assert.strictEqual(Object.keys(labels).length, 3);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-account-id');
assert.strictEqual(labels[HOST_RESOURCE.ID_KEY], 'my-instance-id');
assert.strictEqual(labels[CLOUD_RESOURCE.REGION_KEY], 'my-region');
// fetch again, this shouldn't make http call again
const { type: type1, labels: labels1 } = await resource.detectResource();
assert.deepStrictEqual(type, type1);
assert.deepStrictEqual(labels, labels1);
});
it('Should merge resources from CoreResource and detected aws_ec2_instance resource', async () => {
process.env.OC_RESOURCE_TYPE = 'aws.com/ec2/instance';
process.env.OC_RESOURCE_LABELS = 'cloud.region=default';
CoreResource.setup();
const gcpScope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.replyWithError({
code: 'ENOTFOUND',
});
const awsScope = nock(resourceUtil.AWS_INSTANCE_IDENTITY_DOCUMENT_URI)
.get('')
.reply(200, () => mockedAwsResponse, HEADERS);
const { type, labels } = await resource.detectResource();
awsScope.done();
gcpScope.done();
assert.deepStrictEqual(type, resource.AWS_EC2_INSTANCE_TYPE);
assert.strictEqual(Object.keys(labels).length, 3);
assert.strictEqual(labels[CLOUD_RESOURCE.ACCOUNT_ID_KEY], 'my-account-id');
assert.strictEqual(labels[HOST_RESOURCE.ID_KEY], 'my-instance-id');
assert.strictEqual(labels[CLOUD_RESOURCE.REGION_KEY], 'default');
});
it('return empty labels when failed to find any resources', async () => {
const gcpScope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.replyWithError({
code: 'ENOTFOUND',
});
const awsScope = nock(resourceUtil.AWS_INSTANCE_IDENTITY_DOCUMENT_URI)
.get('')
.replyWithError({ code: 'ENOTFOUND' });
const { type, labels } = await resource.detectResource();
awsScope.done();
gcpScope.done();
assert.deepStrictEqual(type, null);
assert.strictEqual(Object.keys(labels).length, 0);
});
});