Skip to content

Commit

Permalink
feat: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martin.mueller committed Jan 10, 2021
1 parent 00a5b91 commit 76eb2c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ app.synth();
# Thanks To

- The CDK Community cdk-dev.slack.com
- [Projen](https://github.com/projen/projen) project and the community around i
- [Projen](https://github.com/projen/projen) project and the community around it
112 changes: 58 additions & 54 deletions test/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,77 @@
import { SynthUtils } from '@aws-cdk/assert';
import * as core from '@aws-cdk/core';
import '@aws-cdk/assert/jest';
import { StageAccount } from '../src/accountConfig';
import { CustomStack } from '../src/custom-stack';
import { PipelineStack } from '../src/pipeline-stack';
import { PIPELINE_ENV, TestApp } from './testutil';

let defaultStackProperties: any = {
env: PIPELINE_ENV,
stageAccounts: [{
account: {
id: '1233334',
region: 'eu-central-1',
},
stage: 'dev',
}, {
account: {
id: '1233334',
region: 'eu-central-1',
},
stage: 'prod',
}],
branch: 'master',
repositoryName: 'aws-cdk-staging-pipeline',
customStack: (scope: core.Construct) => {
const customStack = new CustomStack(scope, 'TestCustomStack');
customStack.cfnOutputs.Blub = new core.CfnOutput(customStack, 'OutputBlub', { value: 'BlubValue ' });
return customStack;
},
testCommands: () => [
'echo $Blub',
],
manualApprovals: (stageAccount: StageAccount) => stageAccount.stage === 'prod',
gitHub: { owner: 'mmuller88', oauthToken: new core.SecretValue('repo-token') },
};

describe('Create', () => {
describe('StagingPipeline', () => {
const app = new TestApp();
describe('succeed', () => {

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: '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') },
});
let stack = new PipelineStack(app, 'PipelineStack', defaultStackProperties);
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(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', { Actions: [] });
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).toContain('export Blub=');
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).toContain('OutputBlub');
});

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,
// }],
// }],
});
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStackprod');
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).not.toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStackdev');
});

test('with manually approval for dev and prod', () => {
defaultStackProperties.manualApprovals = () => true;
stack = new PipelineStack(app, 'PipelineStack2', defaultStackProperties);
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStack2dev');
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStack2prod');
});

test('with no manually approval for dev and prod when return false', () => {
defaultStackProperties.manualApprovals = () => false;
stack = new PipelineStack(app, 'PipelineStack3', defaultStackProperties);
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).not.toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStack3dev');
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).not.toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStack3prod');
});

test('with no manually approval for dev and prod as default', () => {
defaultStackProperties.manualApprovals = undefined;
stack = new PipelineStack(app, 'PipelineStack4', defaultStackProperties);
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).not.toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStack4dev');
expect(JSON.stringify(SynthUtils.toCloudFormation(stack))).not.toContain('ManualApproval\",\"RoleArn\":{\"Fn::GetAtt\":[\"PipelinePipelineStack4prod');
});
});

Expand Down Expand Up @@ -113,8 +119,6 @@ describe('Create', () => {
});
}).toThrowError();
});


});
});
});

0 comments on commit 76eb2c3

Please sign in to comment.