@datadog/datadog-ci 0.17.6 → 0.17.10

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.
Files changed (68) hide show
  1. package/dist/commands/{commit → git-metadata}/__tests__/git.test.d.ts +0 -0
  2. package/dist/commands/{commit → git-metadata}/__tests__/git.test.js +0 -0
  3. package/dist/commands/{commit → git-metadata}/__tests__/upload.test.d.ts +0 -0
  4. package/dist/commands/{commit → git-metadata}/__tests__/upload.test.js +1 -1
  5. package/dist/commands/{commit → git-metadata}/api.d.ts +0 -0
  6. package/dist/commands/{commit → git-metadata}/api.js +0 -0
  7. package/dist/commands/{commit → git-metadata}/git.d.ts +0 -0
  8. package/dist/commands/{commit → git-metadata}/git.js +0 -0
  9. package/dist/commands/{commit → git-metadata}/index.d.ts +0 -0
  10. package/dist/commands/{commit → git-metadata}/index.js +0 -0
  11. package/dist/commands/{commit → git-metadata}/interfaces.d.ts +0 -0
  12. package/dist/commands/{commit → git-metadata}/interfaces.js +0 -0
  13. package/dist/commands/{commit → git-metadata}/renderer.d.ts +3 -2
  14. package/dist/commands/{commit → git-metadata}/renderer.js +5 -8
  15. package/dist/commands/{commit → git-metadata}/upload.d.ts +0 -0
  16. package/dist/commands/{commit → git-metadata}/upload.js +8 -3
  17. package/dist/commands/lambda/__tests__/fixtures.d.ts +42 -0
  18. package/dist/commands/lambda/__tests__/fixtures.js +73 -0
  19. package/dist/commands/lambda/__tests__/{function.test.d.ts → functions/commons.test.d.ts} +0 -0
  20. package/dist/commands/lambda/__tests__/functions/commons.test.js +336 -0
  21. package/dist/commands/lambda/__tests__/functions/instrument.test.d.ts +1 -0
  22. package/dist/commands/lambda/__tests__/{function.test.js → functions/instrument.test.js} +468 -473
  23. package/dist/commands/lambda/__tests__/functions/uninstrument.test.d.ts +1 -0
  24. package/dist/commands/lambda/__tests__/functions/uninstrument.test.js +299 -0
  25. package/dist/commands/lambda/__tests__/instrument.test.js +270 -163
  26. package/dist/commands/lambda/__tests__/loggroup.test.js +98 -34
  27. package/dist/commands/lambda/__tests__/tags.test.js +107 -31
  28. package/dist/commands/lambda/__tests__/uninstrument.test.d.ts +1 -0
  29. package/dist/commands/lambda/__tests__/uninstrument.test.js +224 -0
  30. package/dist/commands/lambda/constants.d.ts +17 -6
  31. package/dist/commands/lambda/constants.js +23 -6
  32. package/dist/commands/lambda/functions/commons.d.ts +83 -0
  33. package/dist/commands/lambda/functions/commons.js +195 -0
  34. package/dist/commands/lambda/functions/instrument.d.ts +13 -0
  35. package/dist/commands/lambda/{function.js → functions/instrument.js} +63 -133
  36. package/dist/commands/lambda/functions/uninstrument.d.ts +6 -0
  37. package/dist/commands/lambda/functions/uninstrument.js +111 -0
  38. package/dist/commands/lambda/index.js +2 -1
  39. package/dist/commands/lambda/instrument.d.ts +4 -3
  40. package/dist/commands/lambda/instrument.js +85 -40
  41. package/dist/commands/lambda/interfaces.d.ts +54 -0
  42. package/dist/commands/lambda/loggroup.d.ts +4 -7
  43. package/dist/commands/lambda/loggroup.js +27 -4
  44. package/dist/commands/lambda/tags.d.ts +3 -4
  45. package/dist/commands/lambda/tags.js +19 -3
  46. package/dist/commands/lambda/uninstrument.d.ts +11 -0
  47. package/dist/commands/lambda/uninstrument.js +127 -0
  48. package/dist/commands/synthetics/__tests__/fixtures.js +0 -1
  49. package/dist/commands/synthetics/__tests__/reporters/default.test.js +3 -2
  50. package/dist/commands/synthetics/__tests__/run-test.test.js +6 -6
  51. package/dist/commands/synthetics/__tests__/utils.test.js +9 -5
  52. package/dist/commands/synthetics/cli.js +2 -2
  53. package/dist/commands/synthetics/interfaces.d.ts +5 -2
  54. package/dist/commands/synthetics/reporters/default.d.ts +3 -3
  55. package/dist/commands/synthetics/reporters/default.js +4 -3
  56. package/dist/commands/synthetics/run-test.d.ts +8 -2
  57. package/dist/commands/synthetics/utils.d.ts +1 -3
  58. package/dist/commands/synthetics/utils.js +14 -15
  59. package/dist/helpers/__tests__/ci.test.js +51 -114
  60. package/dist/helpers/__tests__/user-provided-git.test.js +14 -2
  61. package/dist/helpers/__tests__/utils.test.js +4 -0
  62. package/dist/helpers/ci.js +62 -96
  63. package/dist/helpers/interfaces.d.ts +28 -2
  64. package/dist/helpers/user-provided-git.js +11 -2
  65. package/dist/helpers/utils.d.ts +4 -0
  66. package/dist/helpers/utils.js +18 -1
  67. package/package.json +2 -2
  68. package/dist/commands/lambda/function.d.ts +0 -43
@@ -31,7 +31,7 @@ describe('execute', () => {
31
31
  const cli = makeCli();
32
32
  const context = createMockContext();
33
33
  process.env = { DATADOG_API_KEY: 'PLACEHOLDER' };
34
- const code = yield cli.run(['commit', 'upload', '--dry-run'], context);
34
+ const code = yield cli.run(['git-metadata', 'upload', '--dry-run'], context);
35
35
  return { context, code };
36
36
  });
37
37
  test('runCLI', () => __awaiter(void 0, void 0, void 0, function* () {
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,7 +1,8 @@
1
+ import { CommitInfo } from './interfaces';
1
2
  export declare const renderGitError: (errorMessage: string) => string;
2
3
  export declare const renderConfigurationError: (error: Error) => string;
3
4
  export declare const renderFailedUpload: (errorMessage: string) => string;
4
5
  export declare const renderRetriedUpload: (errorMessage: string, attempt: number) => string;
5
- export declare const renderUpload = "Uploading\n";
6
6
  export declare const renderSuccessfulCommand: (duration: number, dryRun: boolean) => string;
7
- export declare const renderCommandInfo: (dryRun: boolean) => string;
7
+ export declare const renderDryRunWarning: () => string;
8
+ export declare const renderCommandInfo: (commit: CommitInfo) => string;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.renderCommandInfo = exports.renderSuccessfulCommand = exports.renderUpload = exports.renderRetriedUpload = exports.renderFailedUpload = exports.renderConfigurationError = exports.renderGitError = void 0;
6
+ exports.renderCommandInfo = exports.renderDryRunWarning = exports.renderSuccessfulCommand = exports.renderRetriedUpload = exports.renderFailedUpload = exports.renderConfigurationError = exports.renderGitError = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  const formatting_1 = require("../../helpers/formatting");
9
9
  const renderGitError = (errorMessage) => chalk_1.default.red(`${formatting_1.ICONS.FAILED} An error occured while invoking git: ${errorMessage}
@@ -15,7 +15,6 @@ const renderFailedUpload = (errorMessage) => chalk_1.default.red(`${formatting_1
15
15
  exports.renderFailedUpload = renderFailedUpload;
16
16
  const renderRetriedUpload = (errorMessage, attempt) => chalk_1.default.yellow(`[attempt ${attempt}] Retrying upload: ${errorMessage}\n`);
17
17
  exports.renderRetriedUpload = renderRetriedUpload;
18
- exports.renderUpload = 'Uploading\n';
19
18
  const renderSuccessfulCommand = (duration, dryRun) => {
20
19
  if (dryRun) {
21
20
  return chalk_1.default.green(`${formatting_1.ICONS.SUCCESS} [DRYRUN] Handled in ${duration} seconds.\n`);
@@ -25,10 +24,8 @@ const renderSuccessfulCommand = (duration, dryRun) => {
25
24
  }
26
25
  };
27
26
  exports.renderSuccessfulCommand = renderSuccessfulCommand;
28
- const renderCommandInfo = (dryRun) => {
29
- if (dryRun) {
30
- return chalk_1.default.yellow(`${formatting_1.ICONS.WARNING} DRY-RUN MODE ENABLED. WILL NOT UPLOAD\n`);
31
- }
32
- return chalk_1.default.green('Starting upload. \n');
33
- };
27
+ const renderDryRunWarning = () => chalk_1.default.yellow(`${formatting_1.ICONS.WARNING} DRY-RUN MODE ENABLED. WILL NOT UPLOAD\n`);
28
+ exports.renderDryRunWarning = renderDryRunWarning;
29
+ const renderCommandInfo = (commit) => `Reporting commit ${commit.hash} from repository ${commit.remote}.
30
+ ${commit.trackedFiles.length} tracked file paths will be reported.\n`;
34
31
  exports.renderCommandInfo = renderCommandInfo;
@@ -36,7 +36,9 @@ class UploadCommand extends clipanion_1.Command {
36
36
  execute() {
37
37
  return __awaiter(this, void 0, void 0, function* () {
38
38
  const initialTime = new Date().getTime();
39
- this.context.stdout.write(renderer_1.renderCommandInfo(this.dryRun));
39
+ if (this.dryRun) {
40
+ this.context.stdout.write(renderer_1.renderDryRunWarning());
41
+ }
40
42
  const metricsLogger = metrics_1.getMetricsLogger({
41
43
  datadogSite: process.env.DATADOG_SITE,
42
44
  defaultTags: [`cli_version:${this.cliVersion}`],
@@ -51,6 +53,7 @@ class UploadCommand extends clipanion_1.Command {
51
53
  if (payload === undefined) {
52
54
  return 0;
53
55
  }
56
+ this.context.stdout.write(renderer_1.renderCommandInfo(payload));
54
57
  try {
55
58
  const requestBuilder = this.getRequestBuilder();
56
59
  const status = yield this.uploadRepository(requestBuilder)(payload, {
@@ -63,7 +66,9 @@ class UploadCommand extends clipanion_1.Command {
63
66
  this.context.stdout.write(renderer_1.renderRetriedUpload(e.message, attempt));
64
67
  metricsLogger.logger.increment('retries', 1);
65
68
  },
66
- onUpload: () => this.context.stdout.write(renderer_1.renderUpload),
69
+ onUpload: () => {
70
+ return;
71
+ },
67
72
  retries: 5,
68
73
  });
69
74
  metricsLogger.logger.increment('success', 1);
@@ -122,6 +127,6 @@ UploadCommand.usage = clipanion_1.Command.Usage({
122
127
  `,
123
128
  examples: [['Upload the current commit details', 'datadog-ci report-commits upload']],
124
129
  });
125
- UploadCommand.addPath('commit', 'upload');
130
+ UploadCommand.addPath('git-metadata', 'upload');
126
131
  UploadCommand.addOption('dryRun', clipanion_1.Command.Boolean('--dry-run'));
127
132
  UploadCommand.addOption('repositoryURL', clipanion_1.Command.String('--repository-url'));
@@ -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';
@@ -0,0 +1,336 @@
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('addLayerArn', () => {
19
+ test('adds layers and removes previous versions', () => {
20
+ const runtime = 'python3.9';
21
+ const config = {
22
+ Runtime: runtime,
23
+ };
24
+ let layerARNs = [
25
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Python39:48',
26
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Extension:10',
27
+ ];
28
+ const region = 'sa-east-1';
29
+ const lambdaLibraryLayerName = constants_1.RUNTIME_LAYER_LOOKUP[runtime];
30
+ const fullLambdaLibraryLayerArn = commons_1.getLayerArn(config, config.Runtime, region) + ':49';
31
+ const fullExtensionLayerArn = commons_1.getLayerArn(config, constants_1.EXTENSION_LAYER_KEY, region) + ':11';
32
+ layerARNs = commons_1.addLayerArn(fullLambdaLibraryLayerArn, lambdaLibraryLayerName, layerARNs);
33
+ layerARNs = commons_1.addLayerArn(fullExtensionLayerArn, constants_1.DD_LAMBDA_EXTENSION_LAYER_NAME, layerARNs);
34
+ expect(layerARNs).toEqual([
35
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Python39:49',
36
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Extension:11',
37
+ ]);
38
+ });
39
+ test('swaps layers if architecture is arm64', () => {
40
+ const runtime = 'python3.9';
41
+ const config = {
42
+ Architectures: ['arm64'],
43
+ Runtime: runtime,
44
+ };
45
+ let layerARNs = [
46
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Python39:49',
47
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Extension:11',
48
+ ];
49
+ const region = 'sa-east-1';
50
+ const lambdaLibraryLayerName = constants_1.RUNTIME_LAYER_LOOKUP[runtime];
51
+ const fullLambdaLibraryLayerArn = commons_1.getLayerArn(config, config.Runtime, region) + ':49';
52
+ const fullExtensionLayerArn = commons_1.getLayerArn(config, constants_1.EXTENSION_LAYER_KEY, region) + ':11';
53
+ layerARNs = commons_1.addLayerArn(fullLambdaLibraryLayerArn, lambdaLibraryLayerName, layerARNs);
54
+ layerARNs = commons_1.addLayerArn(fullExtensionLayerArn, constants_1.DD_LAMBDA_EXTENSION_LAYER_NAME, layerARNs);
55
+ expect(layerARNs).toEqual([
56
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Python39-ARM:49',
57
+ 'arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Extension-ARM:11',
58
+ ]);
59
+ });
60
+ });
61
+ describe('collectFunctionsByRegion', () => {
62
+ test('groups functions with region read from arn', () => {
63
+ process.env = {};
64
+ const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
65
+ const region = 'us-east-1';
66
+ command['functions'] = [
67
+ 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
68
+ 'arn:aws:lambda:us-east-1:123456789012:function:another',
69
+ 'arn:aws:lambda:us-east-2:123456789012:function:third-func',
70
+ ];
71
+ expect(commons_1.collectFunctionsByRegion(command['functions'], region)).toEqual({
72
+ 'us-east-1': [
73
+ 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
74
+ 'arn:aws:lambda:us-east-1:123456789012:function:another',
75
+ ],
76
+ 'us-east-2': ['arn:aws:lambda:us-east-2:123456789012:function:third-func'],
77
+ });
78
+ });
79
+ test('groups functions in the config object', () => {
80
+ process.env = {};
81
+ const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
82
+ const region = 'us-east-1';
83
+ command['config']['functions'] = [
84
+ 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
85
+ 'arn:aws:lambda:us-east-1:123456789012:function:another',
86
+ 'arn:aws:lambda:us-east-2:123456789012:function:third-func',
87
+ ];
88
+ expect(commons_1.collectFunctionsByRegion(command['config']['functions'], region)).toEqual({
89
+ 'us-east-1': [
90
+ 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
91
+ 'arn:aws:lambda:us-east-1:123456789012:function:another',
92
+ ],
93
+ 'us-east-2': ['arn:aws:lambda:us-east-2:123456789012:function:third-func'],
94
+ });
95
+ });
96
+ test('uses default region for functions not in arn format', () => {
97
+ process.env = {};
98
+ const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
99
+ command['functions'] = [
100
+ 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
101
+ 'arn:aws:lambda:*:123456789012:function:func-with-wildcard',
102
+ 'func-without-region',
103
+ 'arn:aws:lambda:us-east-2:123456789012:function:third-func',
104
+ ];
105
+ command['region'] = 'ap-south-1';
106
+ expect(commons_1.collectFunctionsByRegion(command['functions'], command['region'])).toEqual({
107
+ 'ap-south-1': ['arn:aws:lambda:*:123456789012:function:func-with-wildcard', 'func-without-region'],
108
+ 'us-east-1': ['arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world'],
109
+ 'us-east-2': ['arn:aws:lambda:us-east-2:123456789012:function:third-func'],
110
+ });
111
+ });
112
+ test('fails to collect when there are regionless functions and no default region is set', () => {
113
+ process.env = {};
114
+ const command = fixtures_1.createCommand(instrument_1.InstrumentCommand);
115
+ command['functions'] = [
116
+ 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world',
117
+ 'arn:aws:lambda:*:123456789012:function:func-with-wildcard',
118
+ 'func-without-region',
119
+ 'arn:aws:lambda:us-east-2:123456789012:function:third-func',
120
+ ];
121
+ command['region'] = undefined;
122
+ command['config']['region'] = undefined;
123
+ let functionsGroup;
124
+ try {
125
+ functionsGroup = commons_1.collectFunctionsByRegion(command['functions'], command['region']);
126
+ }
127
+ catch (err) {
128
+ // Do nothing
129
+ }
130
+ expect(functionsGroup).toBeUndefined();
131
+ });
132
+ });
133
+ describe('getLayerArn', () => {
134
+ const OLD_ENV = process.env;
135
+ beforeEach(() => {
136
+ jest.resetModules();
137
+ process.env = {};
138
+ });
139
+ afterAll(() => {
140
+ process.env = OLD_ENV;
141
+ });
142
+ test('gets sa-east-1 Lambda Extension layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
143
+ const settings = {
144
+ flushMetricsToLogs: false,
145
+ layerAWSAccount: fixtures_1.mockAwsAccount,
146
+ mergeXrayTraces: false,
147
+ tracingEnabled: false,
148
+ };
149
+ const region = 'sa-east-1';
150
+ const layerArn = commons_1.getLayerArn({}, constants_1.EXTENSION_LAYER_KEY, region, settings);
151
+ expect(layerArn).toEqual(`arn:aws:lambda:${region}:${fixtures_1.mockAwsAccount}:layer:Datadog-Extension`);
152
+ }));
153
+ test('gets sa-east-1 arm64 Lambda Extension layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
154
+ const config = {
155
+ Architectures: ['arm64'],
156
+ };
157
+ const settings = {
158
+ flushMetricsToLogs: false,
159
+ layerAWSAccount: fixtures_1.mockAwsAccount,
160
+ mergeXrayTraces: false,
161
+ tracingEnabled: false,
162
+ };
163
+ const region = 'sa-east-1';
164
+ const layerArn = commons_1.getLayerArn(config, constants_1.EXTENSION_LAYER_KEY, region, settings);
165
+ expect(layerArn).toEqual(`arn:aws:lambda:${region}:${fixtures_1.mockAwsAccount}:layer:Datadog-Extension-ARM`);
166
+ }));
167
+ test('gets us-gov-1 gov cloud Lambda Extension layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
168
+ const settings = {
169
+ flushMetricsToLogs: false,
170
+ layerAWSAccount: fixtures_1.mockAwsAccount,
171
+ mergeXrayTraces: false,
172
+ tracingEnabled: false,
173
+ };
174
+ const region = 'us-gov-1';
175
+ const layerArn = commons_1.getLayerArn({}, constants_1.EXTENSION_LAYER_KEY, region, settings);
176
+ expect(layerArn).toEqual(`arn:aws-us-gov:lambda:${region}:${constants_1.GOVCLOUD_LAYER_AWS_ACCOUNT}:layer:Datadog-Extension`);
177
+ }));
178
+ test('gets us-gov-1 gov cloud arm64 Lambda Extension layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
179
+ const config = {
180
+ Architectures: ['arm64'],
181
+ };
182
+ const settings = {
183
+ flushMetricsToLogs: false,
184
+ layerAWSAccount: fixtures_1.mockAwsAccount,
185
+ mergeXrayTraces: false,
186
+ tracingEnabled: false,
187
+ };
188
+ const region = 'us-gov-1';
189
+ const layerArn = commons_1.getLayerArn(config, constants_1.EXTENSION_LAYER_KEY, region, settings);
190
+ expect(layerArn).toEqual(`arn:aws-us-gov:lambda:${region}:${constants_1.GOVCLOUD_LAYER_AWS_ACCOUNT}:layer:Datadog-Extension-ARM`);
191
+ }));
192
+ test('gets sa-east-1 Node12 Lambda Library layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
193
+ const runtime = 'nodejs12.x';
194
+ const config = {
195
+ Runtime: runtime,
196
+ };
197
+ const settings = {
198
+ flushMetricsToLogs: false,
199
+ layerAWSAccount: fixtures_1.mockAwsAccount,
200
+ mergeXrayTraces: false,
201
+ tracingEnabled: false,
202
+ };
203
+ const region = 'sa-east-1';
204
+ const layerArn = commons_1.getLayerArn(config, config.Runtime, region, settings);
205
+ expect(layerArn).toEqual(`arn:aws:lambda:${region}:${fixtures_1.mockAwsAccount}:layer:Datadog-Node12-x`);
206
+ }));
207
+ test('gets sa-east-1 Python3.9 arm64 Lambda Library layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
208
+ const runtime = 'python3.9';
209
+ const config = {
210
+ Architectures: ['arm64'],
211
+ Runtime: runtime,
212
+ };
213
+ const settings = {
214
+ flushMetricsToLogs: false,
215
+ layerAWSAccount: fixtures_1.mockAwsAccount,
216
+ mergeXrayTraces: false,
217
+ tracingEnabled: false,
218
+ };
219
+ const region = 'sa-east-1';
220
+ const layerArn = commons_1.getLayerArn(config, config.Runtime, region, settings);
221
+ expect(layerArn).toEqual(`arn:aws:lambda:${region}:${fixtures_1.mockAwsAccount}:layer:Datadog-Python39-ARM`);
222
+ }));
223
+ test('gets us-gov-1 Python37 gov cloud Lambda Library layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
224
+ const runtime = 'python3.7';
225
+ const config = {
226
+ Runtime: runtime,
227
+ };
228
+ const settings = {
229
+ flushMetricsToLogs: false,
230
+ layerAWSAccount: fixtures_1.mockAwsAccount,
231
+ mergeXrayTraces: false,
232
+ tracingEnabled: false,
233
+ };
234
+ const region = 'us-gov-1';
235
+ const layerArn = commons_1.getLayerArn(config, config.Runtime, region, settings);
236
+ expect(layerArn).toEqual(`arn:aws-us-gov:lambda:${region}:${constants_1.GOVCLOUD_LAYER_AWS_ACCOUNT}:layer:Datadog-Python37`);
237
+ }));
238
+ test('gets us-gov-1 Python39 gov cloud arm64 Lambda Library layer ARN', () => __awaiter(void 0, void 0, void 0, function* () {
239
+ const runtime = 'python3.9';
240
+ const config = {
241
+ Architectures: ['arm64'],
242
+ Runtime: runtime,
243
+ };
244
+ const settings = {
245
+ flushMetricsToLogs: false,
246
+ layerAWSAccount: fixtures_1.mockAwsAccount,
247
+ mergeXrayTraces: false,
248
+ tracingEnabled: false,
249
+ };
250
+ const region = 'us-gov-1';
251
+ const layerArn = commons_1.getLayerArn(config, config.Runtime, region, settings);
252
+ expect(layerArn).toEqual(`arn:aws-us-gov:lambda:${region}:${constants_1.GOVCLOUD_LAYER_AWS_ACCOUNT}:layer:Datadog-Python39-ARM`);
253
+ }));
254
+ });
255
+ describe('getRegion', () => {
256
+ test('should return the expected region', () => {
257
+ const functionARN = 'arn:aws:lambda:us-east-1:123456789012:function:lambda-hello-world';
258
+ const expectedRegion = 'us-east-1';
259
+ const region = commons_1.getRegion(functionARN);
260
+ expect(region).toBe(expectedRegion);
261
+ });
262
+ test('should return undefined if Function ARN does not contain the region', () => {
263
+ const functionName = 'lambda-hello-world';
264
+ const region = commons_1.getRegion(functionName);
265
+ expect(region).toBe(undefined);
266
+ });
267
+ });
268
+ describe('sentenceMatchesRegEx', () => {
269
+ const tags = [
270
+ ['not-complying:regex-should-fail', false],
271
+ ['1first-char-is-number:should-fail', false],
272
+ ['_also-not-complying:should-fail', false],
273
+ ['complying_tag:accepted/with/slashes.and.dots,but-empty-tag', false],
274
+ ['also_complying:success,1but_is_illegal:should-fail', false],
275
+ ['this:complies,also_this_one:yes,numb3r_in_name:should-succeed,dots:al.lo.wed', true],
276
+ ['complying_ip_address_4:192.342.3134.231', true],
277
+ ['complying:alone', true],
278
+ ['one_divided_by_two:1/2,one_divided_by_four:0.25,three_minus_one_half:3-1/2', true],
279
+ ['this_is_a_valid_t4g:yes/it.is-42', true],
280
+ ];
281
+ test.each(tags)('check if the tags match the expected result from the regex', (tag, expectedResult) => {
282
+ const result = !!commons_1.sentenceMatchesRegEx(tag, constants_1.EXTRA_TAGS_REG_EXP);
283
+ expect(result).toEqual(expectedResult);
284
+ });
285
+ });
286
+ describe('updateLambdaConfigs', () => {
287
+ const OLD_ENV = process.env;
288
+ beforeEach(() => {
289
+ jest.resetModules();
290
+ process.env = {};
291
+ });
292
+ afterAll(() => {
293
+ process.env = OLD_ENV;
294
+ });
295
+ test('updates every lambda', () => __awaiter(void 0, void 0, void 0, function* () {
296
+ const lambda = fixtures_1.makeMockLambda({});
297
+ const configs = [
298
+ {
299
+ functionARN: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
300
+ lambdaConfig: {
301
+ FunctionArn: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
302
+ Handler: 'index.handler',
303
+ Runtime: 'nodejs12.x',
304
+ },
305
+ lambdaLibraryLayerArn: 'arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node12-x',
306
+ updateRequest: {
307
+ Environment: {
308
+ Variables: {
309
+ [constants_1.LAMBDA_HANDLER_ENV_VAR]: 'index.handler',
310
+ [constants_1.MERGE_XRAY_TRACES_ENV_VAR]: 'false',
311
+ [constants_1.TRACE_ENABLED_ENV_VAR]: 'false',
312
+ },
313
+ },
314
+ FunctionName: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
315
+ Handler: '/opt/nodejs/node_modules/datadog-lambda-js/handler.handler',
316
+ Layers: ['arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node12-x:22'],
317
+ },
318
+ },
319
+ ];
320
+ const cloudWatch = fixtures_1.makeMockCloudWatchLogs({});
321
+ yield commons_1.updateLambdaFunctionConfigs(lambda, cloudWatch, configs);
322
+ expect(lambda.updateFunctionConfiguration).toHaveBeenCalledWith({
323
+ Environment: {
324
+ Variables: {
325
+ [constants_1.LAMBDA_HANDLER_ENV_VAR]: 'index.handler',
326
+ [constants_1.MERGE_XRAY_TRACES_ENV_VAR]: 'false',
327
+ [constants_1.TRACE_ENABLED_ENV_VAR]: 'false',
328
+ },
329
+ },
330
+ FunctionName: 'arn:aws:lambda:us-east-1:000000000000:function:autoinstrument',
331
+ Handler: '/opt/nodejs/node_modules/datadog-lambda-js/handler.handler',
332
+ Layers: ['arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node12-x:22'],
333
+ });
334
+ }));
335
+ });
336
+ });