|
4 | 4 | haveResourceLike,
|
5 | 5 | } from '@aws-cdk/assert-internal';
|
6 | 6 | import '@aws-cdk/assert-internal/jest';
|
| 7 | +import { testFutureBehavior, testLegacyBehavior } from '@aws-cdk/cdk-build-tools'; |
7 | 8 | import * as cdk from '@aws-cdk/core';
|
| 9 | +import * as cxapi from '@aws-cdk/cx-api'; |
8 | 10 | import {
|
9 | 11 | CfnLaunchTemplate,
|
10 | 12 | Instance,
|
@@ -135,6 +137,57 @@ describe('RequireImdsv2Aspect', () => {
|
135 | 137 | trace: undefined,
|
136 | 138 | });
|
137 | 139 | });
|
| 140 | + |
| 141 | + testFutureBehavior('launch template name is unique with feature flag', { [cxapi.EC2_UNIQUE_IMDSV2_LAUNCH_TEMPLATE_NAME]: true }, cdk.App, (app2) => { |
| 142 | + // GIVEN |
| 143 | + const otherStack = new cdk.Stack(app2, 'OtherStack'); |
| 144 | + const otherVpc = new Vpc(otherStack, 'OtherVpc'); |
| 145 | + const otherInstance = new Instance(otherStack, 'OtherInstance', { |
| 146 | + vpc: otherVpc, |
| 147 | + instanceType: new InstanceType('t2.micro'), |
| 148 | + machineImage: MachineImage.latestAmazonLinux(), |
| 149 | + }); |
| 150 | + const imdsv2Stack = new cdk.Stack(app2, 'RequireImdsv2Stack'); |
| 151 | + const imdsv2Vpc = new Vpc(imdsv2Stack, 'Vpc'); |
| 152 | + const instance = new Instance(imdsv2Stack, 'Instance', { |
| 153 | + vpc: imdsv2Vpc, |
| 154 | + instanceType: new InstanceType('t2.micro'), |
| 155 | + machineImage: MachineImage.latestAmazonLinux(), |
| 156 | + }); |
| 157 | + const aspect = new InstanceRequireImdsv2Aspect(); |
| 158 | + |
| 159 | + // WHEN |
| 160 | + cdk.Aspects.of(imdsv2Stack).add(aspect); |
| 161 | + cdk.Aspects.of(otherStack).add(aspect); |
| 162 | + app2.synth(); |
| 163 | + |
| 164 | + // THEN |
| 165 | + const launchTemplate = instance.node.tryFindChild('LaunchTemplate') as LaunchTemplate; |
| 166 | + const otherLaunchTemplate = otherInstance.node.tryFindChild('LaunchTemplate') as LaunchTemplate; |
| 167 | + expect(launchTemplate).toBeDefined(); |
| 168 | + expect(otherLaunchTemplate).toBeDefined(); |
| 169 | + expect(launchTemplate.launchTemplateName !== otherLaunchTemplate.launchTemplateName); |
| 170 | + }); |
| 171 | + |
| 172 | + testLegacyBehavior('launch template name uses legacy id without feature flag', cdk.App, (app2) => { |
| 173 | + // GIVEN |
| 174 | + const imdsv2Stack = new cdk.Stack(app2, 'RequireImdsv2Stack'); |
| 175 | + const imdsv2Vpc = new Vpc(imdsv2Stack, 'Vpc'); |
| 176 | + const instance = new Instance(imdsv2Stack, 'Instance', { |
| 177 | + vpc: imdsv2Vpc, |
| 178 | + instanceType: new InstanceType('t2.micro'), |
| 179 | + machineImage: MachineImage.latestAmazonLinux(), |
| 180 | + }); |
| 181 | + const aspect = new InstanceRequireImdsv2Aspect(); |
| 182 | + |
| 183 | + // WHEN |
| 184 | + cdk.Aspects.of(imdsv2Stack).add(aspect); |
| 185 | + app2.synth(); |
| 186 | + |
| 187 | + // THEN |
| 188 | + const launchTemplate = instance.node.tryFindChild('LaunchTemplate') as LaunchTemplate; |
| 189 | + expect(launchTemplate.launchTemplateName).toEqual(`${instance.node.id}LaunchTemplate`); |
| 190 | + }); |
138 | 191 | });
|
139 | 192 |
|
140 | 193 | describe('LaunchTemplateRequireImdsv2Aspect', () => {
|
|
0 commit comments