Skip to content

Commit

Permalink
feat: add more unite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martin.mueller committed Jan 4, 2021
1 parent b4b7c1c commit 69da64a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/custom-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import * as core from '@aws-cdk/core';
new core.CfnOutput(customStage.customStack, 'BuildUrl', { value: process.env.CODEBUILD_BUILD_URL || 'not set!' });
*/
export class CustomStack extends core.Stack {
// Necessary to wrap the CfnOutputs and make them available for the testCommands

/**
* Necessary to wrap the CfnOutputs and make them available for the testCommands
*/
cfnOutputs: Record<string, core.CfnOutput> = {};

constructor(scope: core.Construct, id: string, props?: core.StackProps) {
Expand Down
81 changes: 55 additions & 26 deletions test/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,64 @@ describe('Create', () => {
describe('StagingPipeline', () => {
const app = new TestApp();
describe('succeed', () => {
test('which exist', () => {
let customStack;
const stack = new PipelineStack(app, 'PipelineStack', {
env: PIPELINE_ENV,
stageAccounts: [{
account: {
id: '1233334',
region: 'eu-central-1',
},
stage: 'dev',
}, {
account: {
id: '1233334',
region: 'eu-central-1',
},
stage: 'qa',
}],
branch: 'master',
repositoryName: 'aws-cdk-staging-pipeline',
customStack: (scope, _) => {
customStack = new CustomStack(scope, 'TestCustomStack');
customStack.cfnOutputs.Blub = new core.CfnOutput(customStack, 'OutputBlub', { value: 'BlubValue ' });
return customStack;

const stack = new PipelineStack(app, 'PipelineStack', {
env: PIPELINE_ENV,
stageAccounts: [{
account: {
id: '1233334',
region: 'eu-central-1',
},
stage: 'dev',
}, {
account: {
id: '1233334',
region: 'eu-central-1',
},
gitHub: { owner: 'mmuller88', oauthToken: new core.SecretValue('repo-token') },
stage: 'prod',
}],
branch: 'master',
repositoryName: 'aws-cdk-staging-pipeline',
customStack: (scope, _) => {
const customStack = new CustomStack(scope, 'TestCustomStack');
customStack.cfnOutputs.Blub = new core.CfnOutput(customStack, 'OutputBlub', { value: 'BlubValue ' });
return customStack;
},
testCommands: (_) => [
'echo $Blub',
],
manualApprovals: (stageAccount) => stageAccount.stage === 'prod',
gitHub: { owner: 'mmuller88', oauthToken: new core.SecretValue('repo-token') },
});
test('with Blub as CfnOutput and reused in testCommands', () => {
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
// Source: { BuildSpec: "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"set -eu\",\n \"export Blub=\\\"$(node -pe 'require(process.env.CODEBUILD_SRC_DIR_Artifact_PipelineStackPipelineStackqaTestCustomStack958E5CA1_Outputs + \\\"/outputs.json\\\")[\\\"OutputBlub\\\"]')\\\"\",\n \"echo $Blub\"\n ]\n }\n }\n}" },
});

expect(stack).toHaveResourceLike('AWS::CodeBuild::Project');
// expect(customStack).toHaveOutput()
// expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', { Actions: [] });
});

test('with manually approval only for prod', () => {
expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
// Stages: [{
// Actions: [{
// ActionTypeId: {
// Category: 'Approval',
// Owner: 'AWS',
// Provider: 'Manual',
// Version: '1',
// },
// Name: 'ManualApproval',
// RoleArn: {
// 'Fn::GetAtt': [
// 'PipelinePipelineStackprodManualApprovalCodePipelineActionRoleF8189188',
// 'Arn',
// ],
// },
// RunOrder: 2,
// }],
// }],
});
});
});

Expand Down
15 changes: 10 additions & 5 deletions test/integ.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ export class IntegTesting {
env: PIPELINE_ENV,
stageAccounts: [{
account: {
id: '981237193288',
id: '1233334',
region: 'eu-central-1',
},
stage: 'dev',
}, {
account: {
id: '1233334',
region: 'eu-central-1',
},
stage: 'prod',
}],
branch: 'master',
repositoryName: 'aws-cdk-staging-pipeline',
Expand All @@ -25,10 +31,9 @@ export class IntegTesting {
customStack.cfnOutputs.Blub = new core.CfnOutput(customStack, 'OutputBlub', { value: 'BlubValue ' });
return customStack;
},
manualApprovals: (_) => true,
testCommands: (stageAccount) => [
`echo "${stageAccount.stage} stage"`,
`echo ${stageAccount.account.id} id + ${stageAccount.account.region} region`,
manualApprovals: (stageAccount) => stageAccount.stage === 'prod',
testCommands: (_) => [
'echo $Blub',
],
gitHub: { owner: 'mmuller88', oauthToken: new core.SecretValue('repo-token') },
});
Expand Down

0 comments on commit 69da64a

Please sign in to comment.