Skip to content

Commit

Permalink
add test file
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-roca committed Mar 19, 2024
1 parent 063b7a9 commit 7f66df8
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/commands/deployment/__tests__/correlate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {Cli} from 'clipanion/lib/advanced'

import {createMockContext} from '../../../helpers/__tests__/fixtures'

import {DeploymentCorrelateCommand} from '../correlate'

describe('execute', () => {
const runCLI = async (extraArgs: string[], extraEnv?: Record<string, string>) => {
const cli = new Cli()
cli.register(DeploymentCorrelateCommand)
const context = createMockContext() as any
process.env = {DD_API_KEY: 'PLACEHOLDER', ...extraEnv}
context.env = process.env
const code = await cli.run(['deployment', 'correlate', ...extraArgs], context)

return {context, code}
}
test('no arguments', async () => {
const {context, code} = await runCLI([])
expect(code).toBe(1)
expect(context.stdout.toString()).toContain('Missing CD provider')
})
test('invalid CD provider', async () => {
const {context, code} = await runCLI(['--provider', 'helloworld'])
expect(code).toBe(1)
expect(context.stdout.toString()).toContain('Unsupported CD provider')
})
test('no repository URL on environment variables', async () => {
const envVars = {
GITLAB_CI: 'placeholder',
CI_COMMIT_SHA: 'abcdef',
}
const {context, code} = await runCLI(['--provider', 'argocd', '--dry-run'], envVars)
expect(code).toBe(1)
expect(context.stdout.toString()).toContain('Could not extract the source code repository URL')
})
test('no git commit sha on environment variables', async () => {
const envVars = {
GITLAB_CI: 'placeholder',
CI_REPOSITORY_URL: 'https://github.com/DataDog/example',
}
const {context, code} = await runCLI(['--provider', 'argocd', '--dry-run'], envVars)
expect(code).toBe(1)
expect(context.stdout.toString()).toContain('Could not extract the source git commit sha')
})
test('valid with minimal data', async () => {
const envVars = {
GITLAB_CI: 'placeholder',
CI_REPOSITORY_URL: 'https://github.com/DataDog/example',
CI_COMMIT_SHA: 'abcdef',
}
const {context: _, code} = await runCLI(['--provider', 'argocd', '--dry-run'], envVars)
expect(code).toBe(0)
})
test('valid', async () => {
const envVars = {
GITLAB_CI: 'placeholder',
CI_PROJECT_URL: 'https://gitlab.com/DataDog/example',
CI_COMMIT_SHA: 'abcdef',
CI_REPOSITORY_URL: 'https://github.com/DataDog/example',
CI_PIPELINE_ID: '1',
CI_JOB_ID: '1',
}
const {context, code} = await runCLI(['--provider', 'argocd', '--dry-run'], envVars)
expect(code).toBe(0)
const output = context.stdout.toString()
expect(output).toContain(`"type": "ci_app_deployment_correlate"`)
expect(output).toContain(`"ci_provider": "gitlab"`)
expect(output).toContain(`"cd_provider": "argocd"`)
expect(output).toContain(`"config_repo_url": "[email protected]:DataDog/datadog-ci.git"`)
expect(output).toContain(`"config_commit_shas"`)
expect(output).toContain(`"ci_env": {
"ci.pipeline.id": "1",
"ci.provider.name": "gitlab",
"git.commit.sha": "abcdef",
"git.repository_url": "https://github.com/DataDog/example",
"CI_PROJECT_URL": "https://gitlab.com/DataDog/example",
"CI_PIPELINE_ID": "1",
"CI_JOB_ID": "1"
}`)
})
})

0 comments on commit 7f66df8

Please sign in to comment.