|
| 1 | +import { Template } from 'aws-cdk-lib/assertions' |
| 2 | +import * as cdk from 'aws-cdk-lib' |
| 3 | +import { CloudfrontInvalidation } from '../src' |
| 4 | +import { Distribution } from 'aws-cdk-lib/aws-cloudfront' |
| 5 | +import { HttpOrigin } from 'aws-cdk-lib/aws-cloudfront-origins' |
| 6 | + |
| 7 | +const synth = (items?: string[]) => { |
| 8 | + const app = new cdk.App() |
| 9 | + const stack = new cdk.Stack(app, 'stack', { |
| 10 | + env: { |
| 11 | + region: 'eu-west-2', |
| 12 | + }, |
| 13 | + }) |
| 14 | + const distribution = new Distribution(stack, 'distribution', { |
| 15 | + defaultBehavior: { |
| 16 | + origin: new HttpOrigin('example.org'), |
| 17 | + }, |
| 18 | + }) |
| 19 | + const invalidation = new CloudfrontInvalidation(stack, 'invalidation', { |
| 20 | + distribution, |
| 21 | + items, |
| 22 | + }) |
| 23 | + const template = () => Template.fromStack(stack) |
| 24 | + return { |
| 25 | + invalidation, |
| 26 | + template, |
| 27 | + stack, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +describe('cloudfront-invalidation', () => { |
| 32 | + test('synthesizes', () => { |
| 33 | + const { invalidation, template } = synth() |
| 34 | + expect(invalidation).toBeDefined() |
| 35 | + expect(template).toBeDefined() |
| 36 | + }) |
| 37 | + test('invalidates /index.html by default', () => { |
| 38 | + const { template } = synth() |
| 39 | + template().hasResourceProperties('AWS::StepFunctions::StateMachine', { |
| 40 | + DefinitionString: { |
| 41 | + 'Fn::Join': [ |
| 42 | + '', |
| 43 | + [ |
| 44 | + '{"StartAt":"CreateInvalidation","States":{"CreateInvalidation":{"End":true,"Retry":[{"ErrorEquals":["CloudFront.CloudFrontException"],"IntervalSeconds":5,"MaxAttempts":10,"BackoffRate":2}],"Type":"Task","Resource":"arn:', |
| 45 | + { |
| 46 | + Ref: 'AWS::Partition', |
| 47 | + }, |
| 48 | + ':states:::aws-sdk:cloudfront:createInvalidation","Parameters":{"DistributionId":"', |
| 49 | + { |
| 50 | + Ref: 'distribution114A0A2A', |
| 51 | + }, |
| 52 | + '","InvalidationBatch":{"CallerReference.$":"$","Paths":{"Items":["/index.html"],"Quantity":1}}}}}}', |
| 53 | + ], |
| 54 | + ], |
| 55 | + }, |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + test('invalidates the items passed in lazily', () => { |
| 60 | + const items = ['/index.html'] |
| 61 | + const { template } = synth(items) |
| 62 | + items.push('/config.js') |
| 63 | + template().hasResourceProperties('AWS::StepFunctions::StateMachine', { |
| 64 | + DefinitionString: { |
| 65 | + 'Fn::Join': [ |
| 66 | + '', |
| 67 | + [ |
| 68 | + '{"StartAt":"CreateInvalidation","States":{"CreateInvalidation":{"End":true,"Retry":[{"ErrorEquals":["CloudFront.CloudFrontException"],"IntervalSeconds":5,"MaxAttempts":10,"BackoffRate":2}],"Type":"Task","Resource":"arn:', |
| 69 | + { |
| 70 | + Ref: 'AWS::Partition', |
| 71 | + }, |
| 72 | + ':states:::aws-sdk:cloudfront:createInvalidation","Parameters":{"DistributionId":"', |
| 73 | + { |
| 74 | + Ref: 'distribution114A0A2A', |
| 75 | + }, |
| 76 | + '","InvalidationBatch":{"CallerReference.$":"$","Paths":{"Items":["/index.html","/config.js"],"Quantity":2}}}}}}', |
| 77 | + ], |
| 78 | + ], |
| 79 | + }, |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments