Skip to content

Commit

Permalink
pipelines - using capture
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed Sep 9, 2021
1 parent 091b20c commit ffa9eab
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { anything, arrayWith, Capture, objectLike } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { Capture, Match, Template } from '@aws-cdk/assertions';
import * as ccommit from '@aws-cdk/aws-codecommit';
import { CodeCommitTrigger, GitHubTrigger } from '@aws-cdk/aws-codepipeline-actions';
import { AnyPrincipal, Role } from '@aws-cdk/aws-iam';
Expand Down Expand Up @@ -28,18 +27,18 @@ test('CodeCommit source handles tokenized names correctly', () => {
input: cdkp.CodePipelineSource.codeCommit(repo, 'main'),
});

expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'Source',
Actions: [
objectLike({
Configuration: objectLike({
RepositoryName: { 'Fn::GetAtt': [anything(), 'Name'] },
Match.objectLike({
Configuration: Match.objectLike({
RepositoryName: { 'Fn::GetAtt': [Match.anyValue(), 'Name'] },
}),
Name: { 'Fn::GetAtt': [anything(), 'Name'] },
Name: { 'Fn::GetAtt': [Match.anyValue(), 'Name'] },
}),
],
}),
}]),
});
});

Expand All @@ -58,20 +57,20 @@ test('CodeCommit source honors all valid properties', () => {
}),
});

expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'Source',
Actions: [
objectLike({
Configuration: objectLike({
Match.objectLike({
Configuration: Match.objectLike({
BranchName: 'main',
PollForSourceChanges: true,
OutputArtifactFormat: 'CODEBUILD_CLONE_REF',
}),
RoleArn: { 'Fn::GetAtt': [anything(), 'Arn'] },
RoleArn: { 'Fn::GetAtt': [Match.anyValue(), 'Arn'] },
}),
],
}),
}]),
});
});

Expand All @@ -81,19 +80,19 @@ test('S3 source handles tokenized names correctly', () => {
input: cdkp.CodePipelineSource.s3(buckit, 'thefile.zip'),
});

expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'Source',
Actions: [
objectLike({
Configuration: objectLike({
S3Bucket: { Ref: anything() },
Match.objectLike({
Configuration: Match.objectLike({
S3Bucket: { Ref: Match.anyValue() },
S3ObjectKey: 'thefile.zip',
}),
Name: { Ref: anything() },
Name: { Ref: Match.anyValue() },
}),
],
}),
}]),
});
});

Expand All @@ -105,12 +104,12 @@ test('GitHub source honors all valid properties', () => {
}),
});

expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'Source',
Actions: [
objectLike({
Configuration: objectLike({
Match.objectLike({
Configuration: Match.objectLike({
Owner: 'owner',
Repo: 'repo',
Branch: 'main',
Expand All @@ -120,7 +119,7 @@ test('GitHub source honors all valid properties', () => {
Name: 'owner_repo',
}),
],
}),
}]),
});
});

Expand All @@ -145,17 +144,17 @@ test('Dashes in repo names are removed from artifact names', () => {
input: cdkp.CodePipelineSource.gitHub('owner/my-repo', 'main'),
});

expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'Source',
Actions: [
objectLike({
Match.objectLike({
OutputArtifacts: [
{ Name: 'owner_my_repo_Source' },
],
}),
],
}),
}]),
});
});

Expand All @@ -164,19 +163,19 @@ test('artifact names are never longer than 128 characters', () => {
input: cdkp.CodePipelineSource.gitHub('owner/' + 'my-repo'.repeat(100), 'main'),
});

const artifactId = Capture.aString();
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
const artifactId = new Capture();
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'Source',
Actions: [
objectLike({
Match.objectLike({
OutputArtifacts: [
{ Name: artifactId.capture() },
{ Name: artifactId },
],
}),
],
}),
}]),
});

expect(artifactId.capturedValue.length).toBeLessThanOrEqual(128);
expect(artifactId.asString().length).toBeLessThanOrEqual(128);
});
75 changes: 38 additions & 37 deletions packages/@aws-cdk/pipelines/test/compliance/basic-behavior.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as fs from 'fs';
import * as path from 'path';
import { arrayWith, Capture, objectLike, stringLike } from '@aws-cdk/assert-internal';
import { Capture, Match, Template } from '@aws-cdk/assertions';
import '@aws-cdk/assert-internal/jest';
import { Stack, Stage, StageProps, Tags } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { behavior, LegacyTestGitHubNpmPipeline, OneStackApp, BucketStack, PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline } from '../testhelpers';
import { behavior, LegacyTestGitHubNpmPipeline, OneStackApp, BucketStack, PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, stringLike } from '../testhelpers';

let app: TestApp;
let pipelineStack: Stack;
Expand Down Expand Up @@ -37,20 +37,20 @@ behavior('stack templates in nested assemblies are correctly addressed', (suite)
});

function THEN_codePipelineExpectation() {
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'App',
Actions: arrayWith(
objectLike({
Actions: Match.arrayWith([
Match.objectLike({
Name: stringLike('*Prepare'),
InputArtifacts: [objectLike({})],
Configuration: objectLike({
InputArtifacts: [Match.objectLike({})],
Configuration: Match.objectLike({
StackName: 'App-Stack',
TemplatePath: stringLike('*::assembly-App/*.template.json'),
}),
}),
),
}),
]),
}]),
});
}
});
Expand Down Expand Up @@ -94,27 +94,27 @@ behavior('overridden stack names are respected', (suite) => {
});

function THEN_codePipelineExpectation() {
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith(
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([
{
Name: 'App1',
Actions: arrayWith(objectLike({
Actions: Match.arrayWith([Match.objectLike({
Name: stringLike('*Prepare'),
Configuration: objectLike({
Configuration: Match.objectLike({
StackName: 'MyFancyStack',
}),
})),
})]),
},
{
Name: 'App2',
Actions: arrayWith(objectLike({
Actions: Match.arrayWith([Match.objectLike({
Name: stringLike('*Prepare'),
Configuration: objectLike({
Configuration: Match.objectLike({
StackName: 'MyFancyStack',
}),
})),
})]),
},
),
]),
});
}
});
Expand Down Expand Up @@ -154,17 +154,17 @@ behavior('changing CLI version leads to a different pipeline structure (restarti

function THEN_codePipelineExpectation(stack2: Stack, stack3: Stack) {
// THEN
const structure2 = Capture.anyType();
const structure3 = Capture.anyType();
const structure2 = new Capture();
const structure3 = new Capture();

expect(stack2).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: structure2.capture(),
Template.fromStack(stack2).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: structure2,
});
expect(stack3).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: structure3.capture(),
Template.fromStack(stack3).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: structure3,
});

expect(JSON.stringify(structure2.capturedValue)).not.toEqual(JSON.stringify(structure3.capturedValue));
expect(JSON.stringify(structure2.asArray())).not.toEqual(JSON.stringify(structure3.asArray()));
}
});

Expand All @@ -190,24 +190,25 @@ behavior('tags get reflected in pipeline', (suite) => {

function THEN_codePipelineExpectation() {
// THEN
const templateConfig = Capture.aString();
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
const templateConfig = new Capture();
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'App',
Actions: arrayWith(
objectLike({
Actions: Match.arrayWith([
Match.objectLike({
Name: stringLike('*Prepare'),
InputArtifacts: [objectLike({})],
Configuration: objectLike({
InputArtifacts: [Match.objectLike({})],
Configuration: Match.objectLike({
StackName: 'App-Stack',
TemplateConfiguration: templateConfig.capture(stringLike('*::assembly-App/*.template.*json')),
TemplateConfiguration: templateConfig,
}),
}),
),
}),
]),
}]),
});

const [, relConfigFile] = templateConfig.capturedValue.split('::');
expect(templateConfig.asString()).toMatch(/::assembly-App\/.*\.template\..*json/);
const [, relConfigFile] = templateConfig.asString().split('::');
const absConfigFile = path.join(app.outdir, relConfigFile);
const configFile = JSON.parse(fs.readFileSync(absConfigFile, { encoding: 'utf-8' }));
expect(configFile).toEqual(expect.objectContaining({
Expand Down
Loading

0 comments on commit ffa9eab

Please sign in to comment.