Skip to content

Commit

Permalink
Merge branch 'master' into ruizb/release-2-2-0
Browse files Browse the repository at this point in the history
  • Loading branch information
ruizb committed Dec 7, 2022
2 parents 699e9fa + d7a7e28 commit ef5f67c
Show file tree
Hide file tree
Showing 14 changed files with 794 additions and 252 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"form-data": "3.0.0",
"fuzzy": "^0.1.3",
"glob": "7.1.4",
"inquirer": "8.2.0",
"inquirer": "^8.2.5",
"inquirer-checkbox-plus-prompt": "^1.0.1",
"js-yaml": "3.13.1",
"ora": "5.4.1",
Expand Down
16 changes: 16 additions & 0 deletions src/commands/lambda/__mocks__/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const renderer = jest.requireActual('../renderer')

const makeMockSpinner = () => {
const f = jest.fn()
return jest.fn(() => ({
fail: f,
start: f,
succeed: f,
}))
}

renderer['fetchingFunctionsConfigSpinner'] = makeMockSpinner()
renderer['fetchingFunctionsSpinner'] = makeMockSpinner()
renderer['updatingFunctionsSpinner'] = makeMockSpinner()

module.exports = renderer
26 changes: 0 additions & 26 deletions src/commands/lambda/__tests__/functions/commons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
isMissingAnyDatadogApiKeyEnvVar,
isMissingAWSCredentials,
isMissingDatadogEnvVars,
isMissingDatadogSiteEnvVar,
sentenceMatchesRegEx,
updateLambdaFunctionConfigs,
} from '../../functions/commons'
Expand Down Expand Up @@ -364,31 +363,6 @@ describe('commons', () => {
})
})

describe('isMissingDatadogSiteEnvVar', () => {
const OLD_ENV = process.env
beforeEach(() => {
jest.resetModules()
process.env = {}
})
afterAll(() => {
process.env = OLD_ENV
})

test('returns true when Datadog Site Env Var is missing', () => {
expect(isMissingDatadogSiteEnvVar()).toBe(true)
})

test('returns false when Datadog Site Env Var is set', () => {
process.env[CI_SITE_ENV_VAR] = 'datadoghq.com'
expect(isMissingDatadogSiteEnvVar()).toBe(false)
})

test('returns true when Datadog Site Env Var is set and is not a valid Datadog site', () => {
process.env[CI_SITE_ENV_VAR] = 'datacathq.com'
expect(isMissingDatadogSiteEnvVar()).toBe(true)
})
})

describe('isMissingAnyDatadogApiKeyEnvVar', () => {
const OLD_ENV = process.env
beforeEach(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/lambda/__tests__/functions/uninstrument.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
jest.mock('../../loggroup')
jest.mock('../../renderer')

import {
API_KEY_ENV_VAR,
API_KEY_SECRET_ARN_ENV_VAR,
Expand Down
133 changes: 74 additions & 59 deletions src/commands/lambda/__tests__/instrument.test.ts

Large diffs are not rendered by default.

40 changes: 24 additions & 16 deletions src/commands/lambda/__tests__/uninstrument.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
jest.mock('fs')
jest.mock('aws-sdk')
jest.mock('../prompt')
jest.mock('../renderer', () => require('../__mocks__/renderer'))

import * as fs from 'fs'

import {Lambda, SharedIniFileCredentials} from 'aws-sdk'
Expand Down Expand Up @@ -90,7 +92,8 @@ describe('uninstrument', () => {
const output = context.stdout.toString()
expect(code).toBe(0)
expect(output).toMatchInlineSnapshot(`
"\n[!] Functions to be updated:
"\n[Dry Run] 🐶 Uninstrumenting Lambda function
\n[!] Functions to be updated:
\t- arn:aws:lambda:us-east-1:000000000000:function:uninstrument\n
[Dry Run] Will apply the following updates:
UpdateFunctionConfiguration -> arn:aws:lambda:us-east-1:000000000000:function:uninstrument
Expand Down Expand Up @@ -190,9 +193,10 @@ UpdateFunctionConfiguration -> arn:aws:lambda:us-east-1:000000000000:function:un
const output = context.stdout.toString()
expect(code).toBe(1)
expect(output).toMatchInlineSnapshot(`
"[Error] No functions specified for un-instrumentation.
"
`)
"\n🐶 Uninstrumenting Lambda function
[Error] No functions specified to remove instrumentation.
"
`)
})
test('aborts early when no functions are specified while using config file', async () => {
;(fs.readFile as any).mockImplementation((a: any, b: any, callback: any) => callback({}))
Expand All @@ -203,9 +207,10 @@ UpdateFunctionConfiguration -> arn:aws:lambda:us-east-1:000000000000:function:un
await command['execute']()
const output = command.context.stdout.toString()
expect(output).toMatchInlineSnapshot(`
"[Error] No functions specified for un-instrumentation.
"
`)
"\n🐶 Uninstrumenting Lambda function
[Error] No functions specified to remove instrumentation.
"
`)
})
test('aborts if functions and a pattern are set at the same time', async () => {
;(fs.readFile as any).mockImplementation((a: any, b: any, callback: any) => callback({}))
Expand Down Expand Up @@ -268,7 +273,7 @@ UpdateFunctionConfiguration -> arn:aws:lambda:us-east-1:000000000000:function:un
const output = command.context.stdout.toString()
expect(code).toBe(1)
expect(output).toMatch(
"Fetching Lambda functions, this might take a while.\n[Error] Couldn't fetch Lambda functions. Error: Max retry count exceeded. ListFunctionsError\n"
"\n[Error] Couldn't fetch Lambda functions. Error: Max retry count exceeded. ListFunctionsError\n"
)
})

Expand Down Expand Up @@ -364,9 +369,9 @@ UpdateFunctionConfiguration -> arn:aws:lambda:us-east-1:000000000000:function:un
const output = context.stdout.toString()
expect(code).toBe(0)
expect(output).toMatchInlineSnapshot(`
"[!] No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI.
Fetching Lambda functions, this might take a while.\n
[!] Functions to be updated:
"\n🐶 Uninstrumenting Lambda function
[!] No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI.
\n[!] Functions to be updated:
\t- arn:aws:lambda:sa-east-1:123456789012:function:lambda-hello-world
\t- arn:aws:lambda:sa-east-1:123456789012:function:lambda-hello-world-2\n
Will apply the following updates:
Expand Down Expand Up @@ -499,7 +504,8 @@ UpdateFunctionConfiguration -> arn:aws:lambda:sa-east-1:123456789012:function:la
const output = context.stdout.toString()
expect(code).toBe(0)
expect(output).toMatchInlineSnapshot(`
"[!] No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI.\n
"\n🐶 Uninstrumenting Lambda function
[!] No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI.\n
[!] Functions to be updated:
\t- arn:aws:lambda:sa-east-1:123456789012:function:lambda-hello-world
\t- arn:aws:lambda:sa-east-1:123456789012:function:lambda-hello-world-2\n
Expand Down Expand Up @@ -539,7 +545,8 @@ UpdateFunctionConfiguration -> arn:aws:lambda:sa-east-1:123456789012:function:la
const output = context.stdout.toString()
expect(code).toBe(1)
expect(output).toMatchInlineSnapshot(`
"[!] No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI.
"\n🐶 Uninstrumenting Lambda function
[!] No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI.
[Error] Unexpected error
"
`)
Expand All @@ -559,7 +566,7 @@ UpdateFunctionConfiguration -> arn:aws:lambda:sa-east-1:123456789012:function:la
const output = context.stdout.toString()
expect(code).toBe(1)
expect(output).toMatchInlineSnapshot(`
"Fetching Lambda functions, this might take a while.
"\n🐶 Uninstrumenting Lambda function
[Error] Couldn't find any Lambda functions in the specified region.
"
`)
Expand All @@ -582,7 +589,7 @@ UpdateFunctionConfiguration -> arn:aws:lambda:sa-east-1:123456789012:function:la
const output = context.stdout.toString()
expect(code).toBe(1)
expect(output).toMatchInlineSnapshot(`
"Fetching Lambda functions, this might take a while.
"\n🐶 Uninstrumenting Lambda function
[Error] Couldn't fetch Lambda functions. Error: Max retry count exceeded. ListFunctionsError
"
`)
Expand Down Expand Up @@ -617,7 +624,8 @@ UpdateFunctionConfiguration -> arn:aws:lambda:sa-east-1:123456789012:function:la
command['printPlannedActions']([])
const output = command.context.stdout.toString()
expect(output).toMatchInlineSnapshot(`
"No updates will be applied
"
No updates will be applied.
"
`)
})
Expand Down
12 changes: 2 additions & 10 deletions src/commands/lambda/functions/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,14 @@ export const isMissingAWSCredentials = () =>
(process.env[AWS_ACCESS_KEY_ID_ENV_VAR] === undefined || process.env[AWS_SECRET_ACCESS_KEY_ENV_VAR] === undefined) &&
!aws_sdk_config.credentials

export const isMissingDatadogSiteEnvVar = () => {
const site = process.env[CI_SITE_ENV_VAR]
if (site !== undefined) {
return !isValidDatadogSite(site)
}

return true
}

export const isMissingAnyDatadogApiKeyEnvVar = () =>
!(
process.env[CI_API_KEY_ENV_VAR] ||
process.env[CI_KMS_API_KEY_ENV_VAR] ||
process.env[CI_API_KEY_SECRET_ARN_ENV_VAR]
)
export const isMissingDatadogEnvVars = () => isMissingDatadogSiteEnvVar() || isMissingAnyDatadogApiKeyEnvVar()
export const isMissingDatadogEnvVars = () =>
!isValidDatadogSite(process.env[CI_SITE_ENV_VAR]) || isMissingAnyDatadogApiKeyEnvVar()

export const getAllLambdaFunctionConfigs = async (lambda: Lambda) => getLambdaFunctionConfigsFromRegex(lambda, '.')

Expand Down
Loading

0 comments on commit ef5f67c

Please sign in to comment.