Skip to content

Commit

Permalink
Merge pull request #1172 from DataDog/teodor.todorov/SYNTH-12059/trai…
Browse files Browse the repository at this point in the history
…ling-slash-in-test-suite-id-leads-to-found-test-undefined

[SYNTH-12059] Trailing slash in test suite id leads to `[] Found test "undefined"`
  • Loading branch information
teodor2312 authored Feb 7, 2024
2 parents 14abc77 + 23117c0 commit 80aaccf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
32 changes: 16 additions & 16 deletions src/commands/synthetics/__tests__/run-tests-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ describe('run-test', () => {
runTests.executeTests(mockReporter, {
...ciConfig,
global: userConfigOverride,
publicIds: ['public-id-1', 'public-id-2'],
publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'],
})
).rejects.toThrow()
expect(getTestsToTriggersMock).toHaveBeenCalledWith(
apiHelper,
expect.arrayContaining([
expect.objectContaining({id: 'public-id-1', config: userConfigOverride}),
expect.objectContaining({id: 'public-id-2', config: userConfigOverride}),
expect.objectContaining({id: 'aaa-aaa-aaa', config: userConfigOverride}),
expect.objectContaining({id: 'bbb-bbb-bbb', config: userConfigOverride}),
]),
expect.anything(),
false,
Expand Down Expand Up @@ -103,14 +103,14 @@ describe('run-test', () => {
runTests.executeTests(mockReporter, {
...ciConfig,
...partialCIConfig,
publicIds: ['public-id-1', 'public-id-2'],
publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'],
})
).rejects.toThrow(new CiError('NO_TESTS_TO_RUN'))
expect(getTestsToTriggersMock).toHaveBeenCalledWith(
apiHelper,
expect.arrayContaining([
expect.objectContaining({id: 'public-id-1', config: expectedOverriddenConfig}),
expect.objectContaining({id: 'public-id-2', config: expectedOverriddenConfig}),
expect.objectContaining({id: 'aaa-aaa-aaa', config: expectedOverriddenConfig}),
expect.objectContaining({id: 'bbb-bbb-bbb', config: expectedOverriddenConfig}),
]),
expect.anything(),
false,
Expand All @@ -137,14 +137,14 @@ describe('run-test', () => {
runTests.executeTests(mockReporter, {
...ciConfig,
global: configOverride,
publicIds: ['public-id-1', 'public-id-2'],
publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'],
})
).rejects.toThrow(new CiError('NO_TESTS_TO_RUN'))
expect(getTestsToTriggersMock).toHaveBeenCalledWith(
apiHelper,
expect.arrayContaining([
expect.objectContaining({id: 'public-id-1', config: configOverride}),
expect.objectContaining({id: 'public-id-2', config: configOverride}),
expect.objectContaining({id: 'aaa-aaa-aaa', config: configOverride}),
expect.objectContaining({id: 'bbb-bbb-bbb', config: configOverride}),
]),
expect.anything(),
false,
Expand Down Expand Up @@ -172,15 +172,15 @@ describe('run-test', () => {
runTests.executeTests(mockReporter, {
...ciConfig,
global: configOverride,
publicIds: ['public-id-1', 'public-id-2'],
publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'],
tunnel: true,
})
).rejects.toThrow(new CiError('NO_TESTS_TO_RUN'))
expect(getTestsToTriggersMock).toHaveBeenCalledWith(
apiHelper,
expect.arrayContaining([
expect.objectContaining({id: 'public-id-1', config: configOverride}),
expect.objectContaining({id: 'public-id-2', config: configOverride}),
expect.objectContaining({id: 'aaa-aaa-aaa', config: configOverride}),
expect.objectContaining({id: 'bbb-bbb-bbb', config: configOverride}),
]),
expect.anything(),
false,
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('run-test', () => {
}
jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any)
await expect(
runTests.executeTests(mockReporter, {...ciConfig, publicIds: ['public-id-1'], tunnel: true})
runTests.executeTests(mockReporter, {...ciConfig, publicIds: ['aaa-aaa-aaa'], tunnel: true})
).rejects.toThrow(
new CriticalError(
error,
Expand All @@ -278,7 +278,7 @@ describe('run-test', () => {

jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any)
await expect(
runTests.executeTests(mockReporter, {...ciConfig, publicIds: ['public-id-1', 'public-id-2'], tunnel: true})
runTests.executeTests(mockReporter, {...ciConfig, publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'], tunnel: true})
).rejects.toThrow(new CriticalError('UNAVAILABLE_TUNNEL_CONFIG', 'Server Error'))
})

Expand Down Expand Up @@ -366,7 +366,7 @@ describe('run-test', () => {

jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any)
await expect(
runTests.executeTests(mockReporter, {...ciConfig, publicIds: ['public-id-1', 'public-id-2'], tunnel: true})
runTests.executeTests(mockReporter, {...ciConfig, publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'], tunnel: true})
).rejects.toThrow(
new CriticalError(
'TRIGGER_TESTS_FAILED',
Expand Down Expand Up @@ -416,7 +416,7 @@ describe('run-test', () => {
runTests.executeTests(mockReporter, {
...ciConfig,
failOnCriticalErrors: true,
publicIds: ['public-id-1', 'public-id-2'],
publicIds: ['aaa-aaa-aaa', 'bbb-bbb-bbb'],
tunnel: true,
})
).rejects.toThrow(
Expand Down
21 changes: 21 additions & 0 deletions src/commands/synthetics/__tests__/utils/public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,27 @@ describe('utils', () => {
).rejects.toThrow('Failed to get test: could not query https://app.datadoghq.com/example\nForbidden\n')
})

test('Passes when public ID is valid', async () => {
const axiosMock = jest.spyOn(axios, 'create')
axiosMock.mockImplementation((() => (e: any) => {
return {data: {subtype: 'http', public_id: '123-456-789'}}
}) as any)

const triggerConfig = {suite: 'Suite 1', config: {}, id: '123-456-789'}
expect(await utils.getTestAndOverrideConfig(api, triggerConfig, mockReporter, getSummary())).toEqual(
expect.objectContaining({test: expect.objectContaining({public_id: '123-456-789', subtype: 'http'})})
)
})

test('Fails when public ID is NOT valid', async () => {
const expectedError = new CiError('INVALID_CONFIG', `No valid public ID found in: \`a123-456-789\``)

const triggerConfig = {suite: 'Suite 1', config: {}, id: 'a123-456-789'}
await expect(utils.getTestAndOverrideConfig(api, triggerConfig, mockReporter, getSummary())).rejects.toThrow(
expectedError
)
})

test('Passes when the tunnel is enabled for HTTP test', async () => {
const axiosMock = jest.spyOn(axios, 'create')
axiosMock.mockImplementation((() => (e: any) => {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/synthetics/utils/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
} from './internal'

const POLLING_INTERVAL = 5000 // In ms
const PUBLIC_ID_REGEX = /^[\d\w]{3}-[\d\w]{3}-[\d\w]{3}$/
const PUBLIC_ID_REGEX = /\b[a-z0-9]{3}-[a-z0-9]{3}-[a-z0-9]{3}\b/
const TEMPLATE_REGEX = /{{\s*([^{}]*?)\s*}}/g

export const readableOperation: {[key in Operator]: string} = {
Expand Down Expand Up @@ -644,7 +644,11 @@ export const getTestAndOverrideConfig = async (
summary: InitialSummary,
isTunnelEnabled?: boolean
): Promise<NotFound | Skipped | TestWithOverride> => {
const normalizedId = PUBLIC_ID_REGEX.test(id) ? id : id.substring(id.lastIndexOf('/') + 1)
const normalizedId = id.match(PUBLIC_ID_REGEX)?.[0]

if (!normalizedId) {
throw new CriticalError('INVALID_CONFIG', `No valid public ID found in: \`${id}\``)
}

const testResult = await getTest(api, {config, id: normalizedId, suite})
if ('errorMessage' in testResult) {
Expand Down

0 comments on commit 80aaccf

Please sign in to comment.