@datadog/datadog-ci 0.17.8 → 0.17.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/lambda/__tests__/fixtures.d.ts +42 -0
- package/dist/commands/lambda/__tests__/fixtures.js +73 -0
- package/dist/commands/lambda/__tests__/{function.test.d.ts → functions/commons.test.d.ts} +0 -0
- package/dist/commands/lambda/__tests__/functions/commons.test.js +171 -0
- package/dist/commands/lambda/__tests__/functions/instrument.test.d.ts +1 -0
- package/dist/commands/lambda/__tests__/{function.test.js → functions/instrument.test.js} +355 -417
- package/dist/commands/lambda/__tests__/functions/uninstrument.test.d.ts +1 -0
- package/dist/commands/lambda/__tests__/functions/uninstrument.test.js +298 -0
- package/dist/commands/lambda/__tests__/instrument.test.js +270 -163
- package/dist/commands/lambda/__tests__/loggroup.test.js +98 -34
- package/dist/commands/lambda/__tests__/tags.test.js +107 -31
- package/dist/commands/lambda/__tests__/uninstrument.test.d.ts +1 -0
- package/dist/commands/lambda/__tests__/uninstrument.test.js +223 -0
- package/dist/commands/lambda/constants.d.ts +1 -0
- package/dist/commands/lambda/constants.js +2 -1
- package/dist/commands/lambda/functions/commons.d.ts +71 -0
- package/dist/commands/lambda/functions/commons.js +170 -0
- package/dist/commands/lambda/functions/instrument.d.ts +16 -0
- package/dist/commands/lambda/{function.js → functions/instrument.js} +42 -102
- package/dist/commands/lambda/functions/uninstrument.d.ts +6 -0
- package/dist/commands/lambda/functions/uninstrument.js +110 -0
- package/dist/commands/lambda/index.js +2 -1
- package/dist/commands/lambda/instrument.d.ts +4 -3
- package/dist/commands/lambda/instrument.js +85 -40
- package/dist/commands/lambda/interfaces.d.ts +54 -0
- package/dist/commands/lambda/loggroup.d.ts +4 -7
- package/dist/commands/lambda/loggroup.js +27 -4
- package/dist/commands/lambda/tags.d.ts +3 -4
- package/dist/commands/lambda/tags.js +19 -3
- package/dist/commands/lambda/uninstrument.d.ts +11 -0
- package/dist/commands/lambda/uninstrument.js +127 -0
- package/dist/helpers/__tests__/ci.test.js +23 -0
- package/dist/helpers/ci.js +13 -6
- package/package.json +1 -1
- package/dist/commands/lambda/function.d.ts +0 -43
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import { CloudWatchLogs, Lambda } from 'aws-sdk';
|
|
3
|
+
import { Cli, Command } from 'clipanion/lib/advanced';
|
|
4
|
+
export declare const createMockContext: () => {
|
|
5
|
+
stdout: {
|
|
6
|
+
toString: () => string;
|
|
7
|
+
write: (input: string) => void;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export declare const makeCli: () => Cli<import("clipanion/lib/advanced").BaseContext>;
|
|
11
|
+
/**
|
|
12
|
+
* Allow for constructors with any amount of parameters.
|
|
13
|
+
* Mainly used for testing when we are creating commands.
|
|
14
|
+
*/
|
|
15
|
+
export declare type ConstructorOf<T> = new (...args: any[]) => T;
|
|
16
|
+
/**
|
|
17
|
+
* Allows to create an instance of any command that
|
|
18
|
+
* extends the Command clss.
|
|
19
|
+
*
|
|
20
|
+
* @param commandClass any class that extends the Command class.
|
|
21
|
+
* @param parameters parameters to use while creating the commandClass
|
|
22
|
+
* @returns the instance of the given command with a mock context attatched.
|
|
23
|
+
*/
|
|
24
|
+
export declare const createCommand: <T extends Command<import("clipanion/lib/advanced").BaseContext>>(commandClass: ConstructorOf<T>, ...parameters: any[]) => T;
|
|
25
|
+
export declare const makeMockLambda: (functionConfigs: Record<string, Lambda.FunctionConfiguration>) => {
|
|
26
|
+
getFunction: jest.Mock<any, any>;
|
|
27
|
+
listFunctions: jest.Mock<any, any>;
|
|
28
|
+
listTags: jest.Mock<any, any>;
|
|
29
|
+
tagResource: jest.Mock<any, any>;
|
|
30
|
+
updateFunctionConfiguration: jest.Mock<any, any>;
|
|
31
|
+
};
|
|
32
|
+
export declare const makeMockCloudWatchLogs: (logGroups: Record<string, {
|
|
33
|
+
config: CloudWatchLogs.DescribeLogGroupsResponse;
|
|
34
|
+
filters?: CloudWatchLogs.DescribeSubscriptionFiltersResponse;
|
|
35
|
+
}>) => {
|
|
36
|
+
createLogGroup: jest.Mock<any, any>;
|
|
37
|
+
deleteSubscriptionFilter: jest.Mock<any, any>;
|
|
38
|
+
describeLogGroups: jest.Mock<any, any>;
|
|
39
|
+
describeSubscriptionFilters: jest.Mock<any, any>;
|
|
40
|
+
putSubscriptionFilter: jest.Mock<any, any>;
|
|
41
|
+
};
|
|
42
|
+
export declare const mockAwsAccount = "123456789012";
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mockAwsAccount = exports.makeMockCloudWatchLogs = exports.makeMockLambda = exports.createCommand = exports.makeCli = exports.createMockContext = void 0;
|
|
4
|
+
const advanced_1 = require("clipanion/lib/advanced");
|
|
5
|
+
const instrument_1 = require("../instrument");
|
|
6
|
+
const uninstrument_1 = require("../uninstrument");
|
|
7
|
+
const createMockContext = () => {
|
|
8
|
+
let data = '';
|
|
9
|
+
return {
|
|
10
|
+
stdout: {
|
|
11
|
+
toString: () => data,
|
|
12
|
+
write: (input) => {
|
|
13
|
+
data += input;
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.createMockContext = createMockContext;
|
|
19
|
+
const makeCli = () => {
|
|
20
|
+
const cli = new advanced_1.Cli();
|
|
21
|
+
cli.register(instrument_1.InstrumentCommand);
|
|
22
|
+
cli.register(uninstrument_1.UninstrumentCommand);
|
|
23
|
+
return cli;
|
|
24
|
+
};
|
|
25
|
+
exports.makeCli = makeCli;
|
|
26
|
+
/**
|
|
27
|
+
* Allows to create an instance of any command that
|
|
28
|
+
* extends the Command clss.
|
|
29
|
+
*
|
|
30
|
+
* @param commandClass any class that extends the Command class.
|
|
31
|
+
* @param parameters parameters to use while creating the commandClass
|
|
32
|
+
* @returns the instance of the given command with a mock context attatched.
|
|
33
|
+
*/
|
|
34
|
+
const createCommand = (commandClass, ...parameters) => {
|
|
35
|
+
// Create a new instance of commandClass and pass in the parameters
|
|
36
|
+
const command = new commandClass(...parameters);
|
|
37
|
+
command.context = exports.createMockContext();
|
|
38
|
+
return command;
|
|
39
|
+
};
|
|
40
|
+
exports.createCommand = createCommand;
|
|
41
|
+
const makeMockLambda = (functionConfigs) => ({
|
|
42
|
+
getFunction: jest.fn().mockImplementation(({ FunctionName }) => ({
|
|
43
|
+
promise: () => Promise.resolve({ Configuration: functionConfigs[FunctionName] }),
|
|
44
|
+
})),
|
|
45
|
+
listFunctions: jest.fn().mockImplementation(() => ({
|
|
46
|
+
promise: () => Promise.resolve({ Functions: Object.values(functionConfigs) }),
|
|
47
|
+
})),
|
|
48
|
+
listTags: jest.fn().mockImplementation(() => ({ promise: () => Promise.resolve({ Tags: {} }) })),
|
|
49
|
+
tagResource: jest.fn().mockImplementation(() => ({ promise: () => Promise.resolve() })),
|
|
50
|
+
updateFunctionConfiguration: jest.fn().mockImplementation(() => ({ promise: () => Promise.resolve() })),
|
|
51
|
+
});
|
|
52
|
+
exports.makeMockLambda = makeMockLambda;
|
|
53
|
+
const makeMockCloudWatchLogs = (logGroups) => ({
|
|
54
|
+
createLogGroup: jest.fn().mockImplementation(() => ({ promise: () => Promise.resolve() })),
|
|
55
|
+
deleteSubscriptionFilter: jest.fn().mockImplementation(() => ({ promise: () => Promise.resolve() })),
|
|
56
|
+
describeLogGroups: jest.fn().mockImplementation(({ logGroupNamePrefix }) => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
const groups = (_b = (_a = logGroups[logGroupNamePrefix]) === null || _a === void 0 ? void 0 : _a.config) !== null && _b !== void 0 ? _b : { logGroups: [] };
|
|
59
|
+
return {
|
|
60
|
+
promise: () => Promise.resolve(groups),
|
|
61
|
+
};
|
|
62
|
+
}),
|
|
63
|
+
describeSubscriptionFilters: jest.fn().mockImplementation(({ logGroupName }) => {
|
|
64
|
+
var _a, _b;
|
|
65
|
+
const groups = (_b = (_a = logGroups[logGroupName]) === null || _a === void 0 ? void 0 : _a.filters) !== null && _b !== void 0 ? _b : { subscriptionFilters: [] };
|
|
66
|
+
return {
|
|
67
|
+
promise: () => Promise.resolve(groups),
|
|
68
|
+
};
|
|
69
|
+
}),
|
|
70
|
+
putSubscriptionFilter: jest.fn().mockImplementation(() => ({ promise: () => Promise.resolve() })),
|
|
71
|
+
});
|
|
72
|
+
exports.makeMockCloudWatchLogs = makeMockCloudWatchLogs;
|
|
73
|
+
exports.mockAwsAccount = '123456789012';
|
|
File without changes
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
/* tslint:disable:no-string-literal */
|
|
13
|
+
const constants_1 = require("../../constants");
|
|
14
|
+
const commons_1 = require("../../functions/commons");
|
|
15
|
+
const instrument_1 = require("../../instrument");
|
|
16
|
+
const fixtures_1 = require("../fixtures");
|
|
17
|
+
describe('commons', () => {
|
|
18
|
+
describe('collectFunctionsByRegion', () => {
|
|
19
|
+
test('groups functions with region read from arn', () => {
|
|
20
|
+
process.env = {};
|
|
21
|
+
const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
|
|
22
|
+
const region = 'us-east-1';
|
|
23
|
+
command['functions'] = [
|
|
24
|
+
'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
|
|
25
|
+
'arn:aws:lambda:us-east-1:123456789012:function:another',
|
|
26
|
+
'arn:aws:lambda:us-east-2:123456789012:function:third-func',
|
|
27
|
+
];
|
|
28
|
+
expect(commons_1.collectFunctionsByRegion(command['functions'], region)).toEqual({
|
|
29
|
+
'us-east-1': [
|
|
30
|
+
'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
|
|
31
|
+
'arn:aws:lambda:us-east-1:123456789012:function:another',
|
|
32
|
+
],
|
|
33
|
+
'us-east-2': ['arn:aws:lambda:us-east-2:123456789012:function:third-func'],
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
test('groups functions in the config object', () => {
|
|
37
|
+
process.env = {};
|
|
38
|
+
const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
|
|
39
|
+
const region = 'us-east-1';
|
|
40
|
+
command['config']['functions'] = [
|
|
41
|
+
'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
|
|
42
|
+
'arn:aws:lambda:us-east-1:123456789012:function:another',
|
|
43
|
+
'arn:aws:lambda:us-east-2:123456789012:function:third-func',
|
|
44
|
+
];
|
|
45
|
+
expect(commons_1.collectFunctionsByRegion(command['config']['functions'], region)).toEqual({
|
|
46
|
+
'us-east-1': [
|
|
47
|
+
'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
|
|
48
|
+
'arn:aws:lambda:us-east-1:123456789012:function:another',
|
|
49
|
+
],
|
|
50
|
+
'us-east-2': ['arn:aws:lambda:us-east-2:123456789012:function:third-func'],
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
test('uses default region for functions not in arn format', () => {
|
|
54
|
+
process.env = {};
|
|
55
|
+
const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
|
|
56
|
+
command['functions'] = [
|
|
57
|
+
'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
|
|
58
|
+
'arn:aws:lambda:*:123456789012:function:func-with-wildcard',
|
|
59
|
+
'func-without-region',
|
|
60
|
+
'arn:aws:lambda:us-east-2:123456789012:function:third-func',
|
|
61
|
+
];
|
|
62
|
+
command['region'] = 'ap-south-1';
|
|
63
|
+
expect(commons_1.collectFunctionsByRegion(command['functions'], command['region'])).toEqual({
|
|
64
|
+
'ap-south-1': ['arn:aws:lambda:*:123456789012:function:func-with-wildcard', 'func-without-region'],
|
|
65
|
+
'us-east-1': ['arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world'],
|
|
66
|
+
'us-east-2': ['arn:aws:lambda:us-east-2:123456789012:function:third-func'],
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
test('fails to collect when there are regionless functions and no default region is set', () => {
|
|
70
|
+
process.env = {};
|
|
71
|
+
const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
|
|
72
|
+
command['functions'] = [
|
|
73
|
+
'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
|
|
74
|
+
'arn:aws:lambda:*:123456789012:function:func-with-wildcard',
|
|
75
|
+
'func-without-region',
|
|
76
|
+
'arn:aws:lambda:us-east-2:123456789012:function:third-func',
|
|
77
|
+
];
|
|
78
|
+
command['region'] = undefined;
|
|
79
|
+
command['config']['region'] = undefined;
|
|
80
|
+
let functionsGroup;
|
|
81
|
+
try {
|
|
82
|
+
functionsGroup = commons_1.collectFunctionsByRegion(command['functions'], command['region']);
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
// Do nothing
|
|
86
|
+
}
|
|
87
|
+
expect(functionsGroup).toBeUndefined();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe('getRegion', () => {
|
|
91
|
+
test('should return the expected region', () => {
|
|
92
|
+
const functionARN = 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world';
|
|
93
|
+
const expectedRegion = 'us-east-1';
|
|
94
|
+
const region = commons_1.getRegion(functionARN);
|
|
95
|
+
expect(region).toBe(expectedRegion);
|
|
96
|
+
});
|
|
97
|
+
test('should return undefined if Function ARN does not contain the region', () => {
|
|
98
|
+
const functionName = 'lambda-hello-world';
|
|
99
|
+
const region = commons_1.getRegion(functionName);
|
|
100
|
+
expect(region).toBe(undefined);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('sentenceMatchesRegEx', () => {
|
|
104
|
+
const tags = [
|
|
105
|
+
['not-complying:regex-should-fail', false],
|
|
106
|
+
['1first-char-is-number:should-fail', false],
|
|
107
|
+
['_also-not-complying:should-fail', false],
|
|
108
|
+
['complying_tag:accepted/with/slashes.and.dots,but-empty-tag', false],
|
|
109
|
+
['also_complying:success,1but_is_illegal:should-fail', false],
|
|
110
|
+
['this:complies,also_this_one:yes,numb3r_in_name:should-succeed,dots:al.lo.wed', true],
|
|
111
|
+
['complying_ip_address_4:192.342.3134.231', true],
|
|
112
|
+
['complying:alone', true],
|
|
113
|
+
['one_divided_by_two:1/2,one_divided_by_four:0.25,three_minus_one_half:3-1/2', true],
|
|
114
|
+
['this_is_a_valid_t4g:yes/it.is-42', true],
|
|
115
|
+
];
|
|
116
|
+
test.each(tags)('check if the tags match the expected result from the regex', (tag, expectedResult) => {
|
|
117
|
+
const result = !!commons_1.sentenceMatchesRegEx(tag, constants_1.EXTRA_TAGS_REG_EXP);
|
|
118
|
+
expect(result).toEqual(expectedResult);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
describe('updateLambdaConfigs', () => {
|
|
122
|
+
const OLD_ENV = process.env;
|
|
123
|
+
beforeEach(() => {
|
|
124
|
+
jest.resetModules();
|
|
125
|
+
process.env = {};
|
|
126
|
+
});
|
|
127
|
+
afterAll(() => {
|
|
128
|
+
process.env = OLD_ENV;
|
|
129
|
+
});
|
|
130
|
+
test('updates every lambda', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const lambda = fixtures_1.makeMockLambda({});
|
|
132
|
+
const configs = [
|
|
133
|
+
{
|
|
134
|
+
functionARN: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
|
|
135
|
+
lambdaConfig: {
|
|
136
|
+
FunctionArn: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
|
|
137
|
+
Handler: 'index.handler',
|
|
138
|
+
Runtime: 'nodejs12.x',
|
|
139
|
+
},
|
|
140
|
+
lambdaLibraryLayerArn: 'arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node12-x',
|
|
141
|
+
updateRequest: {
|
|
142
|
+
Environment: {
|
|
143
|
+
Variables: {
|
|
144
|
+
[constants_1.LAMBDA_HANDLER_ENV_VAR]: 'index.handler',
|
|
145
|
+
[constants_1.MERGE_XRAY_TRACES_ENV_VAR]: 'false',
|
|
146
|
+
[constants_1.TRACE_ENABLED_ENV_VAR]: 'false',
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
FunctionName: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
|
|
150
|
+
Handler: '/opt/nodejs/node_modules/datadog-lambda-js/handler.handler',
|
|
151
|
+
Layers: ['arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node12-x:22'],
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
];
|
|
155
|
+
const cloudWatch = fixtures_1.makeMockCloudWatchLogs({});
|
|
156
|
+
yield commons_1.updateLambdaFunctionConfigs(lambda, cloudWatch, configs);
|
|
157
|
+
expect(lambda.updateFunctionConfiguration).toHaveBeenCalledWith({
|
|
158
|
+
Environment: {
|
|
159
|
+
Variables: {
|
|
160
|
+
[constants_1.LAMBDA_HANDLER_ENV_VAR]: 'index.handler',
|
|
161
|
+
[constants_1.MERGE_XRAY_TRACES_ENV_VAR]: 'false',
|
|
162
|
+
[constants_1.TRACE_ENABLED_ENV_VAR]: 'false',
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
FunctionName: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
|
|
166
|
+
Handler: '/opt/nodejs/node_modules/datadog-lambda-js/handler.handler',
|
|
167
|
+
Layers: ['arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node12-x:22'],
|
|
168
|
+
});
|
|
169
|
+
}));
|
|
170
|
+
});
|
|
171
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|