-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
063b7a9
commit 7f66df8
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}`) | ||
}) | ||
}) |