|
| 1 | +import * as cdk from 'aws-cdk-lib' |
| 2 | +import { Construct } from 'constructs' |
| 3 | +import { Template } from 'aws-cdk-lib/assertions' |
| 4 | +import { CommonStackProps } from '../../lib/types' |
| 5 | +import { CommonConstruct } from '../../lib/common/commonConstruct' |
| 6 | +import { CommonStack } from '../../lib/common/commonStack' |
| 7 | + |
| 8 | +interface TestStackProps extends CommonStackProps {} |
| 9 | + |
| 10 | +const testStackProps = { |
| 11 | + env: { |
| 12 | + account: '123456789', |
| 13 | + region: 'eu-west-1', |
| 14 | + }, |
| 15 | + name: 'test-common-stack', |
| 16 | + domainName: 'gradientedge.io', |
| 17 | + region: 'eu-west-1', |
| 18 | + stackName: 'test', |
| 19 | + stage: 'test', |
| 20 | + extraContexts: [], |
| 21 | + stageContextPath: 'src/test/common/cdkEnv', |
| 22 | +} |
| 23 | + |
| 24 | +class TestCommonStack extends CommonStack { |
| 25 | + declare props: TestStackProps |
| 26 | + |
| 27 | + constructor(parent: cdk.App, name: string, props: cdk.StackProps) { |
| 28 | + super(parent, name, props) |
| 29 | + |
| 30 | + this.construct = new TestCommonConstruct(this, testStackProps.name, this.props) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +class TestCommonConstruct extends CommonConstruct { |
| 35 | + declare props: TestStackProps |
| 36 | + paramValue: string |
| 37 | + paramValueFromRegion: string |
| 38 | + |
| 39 | + constructor(parent: Construct, name: string, props: TestStackProps) { |
| 40 | + super(parent, name, props) |
| 41 | + this.ssMManager.writeStringToParameters('test-param-write', this, { |
| 42 | + parameterName: 'test-param', |
| 43 | + description: `test param description`, |
| 44 | + stringValue: 'Hello World!', |
| 45 | + }) |
| 46 | + this.paramValue = this.ssMManager.readStringParameter('test-param-read', this, 'test-param-test') |
| 47 | + this.paramValueFromRegion = this.ssMManager.readStringParameterFromRegion( |
| 48 | + 'test-param-read-from-region', |
| 49 | + this, |
| 50 | + 'test-param-test', |
| 51 | + 'eu-west-1' |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const app = new cdk.App({ context: testStackProps }) |
| 57 | +const commonStack = new TestCommonStack(app, 'test-common-stack', testStackProps) |
| 58 | +const template = Template.fromStack(commonStack) |
| 59 | + |
| 60 | +describe('TestSsmConstruct', () => { |
| 61 | + test('synthesises as expected', () => { |
| 62 | + /* test if number of resources are correctly synthesised */ |
| 63 | + template.resourceCountIs('AWS::SSM::Parameter', 1) |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +describe('TestSsmConstruct', () => { |
| 68 | + test('outputs as expected', () => { |
| 69 | + template.hasOutput('testParamWriteParameterArn', {}) |
| 70 | + template.hasOutput('testParamWriteParameterName', {}) |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +describe('TestSsmConstruct', () => { |
| 75 | + test('provisions new ip set as expected', () => { |
| 76 | + template.hasResourceProperties('AWS::SSM::Parameter', { |
| 77 | + Type: 'String', |
| 78 | + Value: 'Hello World!', |
| 79 | + Description: 'test param description - test stage', |
| 80 | + Name: 'test-param-test', |
| 81 | + }) |
| 82 | + }) |
| 83 | +}) |
0 commit comments