From 69da64ab48a393a1452c3973f5d452f2aab8a61a Mon Sep 17 00:00:00 2001 From: "martin.mueller" Date: Mon, 4 Jan 2021 08:38:19 +0100 Subject: [PATCH] feat: add more unite tests --- src/custom-stack.ts | 5 ++- test/default.test.ts | 81 +++++++++++++++++++++++++++++-------------- test/integ.default.ts | 15 +++++--- 3 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/custom-stack.ts b/src/custom-stack.ts index 45a5286..082f5bb 100644 --- a/src/custom-stack.ts +++ b/src/custom-stack.ts @@ -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 = {}; constructor(scope: core.Construct, id: string, props?: core.StackProps) { diff --git a/test/default.test.ts b/test/default.test.ts index 6cc4438..3f669ad 100644 --- a/test/default.test.ts +++ b/test/default.test.ts @@ -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, + // }], + // }], + }); }); }); diff --git a/test/integ.default.ts b/test/integ.default.ts index 43d0391..9ebcb91 100644 --- a/test/integ.default.ts +++ b/test/integ.default.ts @@ -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', @@ -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') }, });