|
1 | 1 | import { DEFAULT_ACCOUNT_CONTEXT_KEY, DEFAULT_REGION_CONTEXT_KEY } from '@aws-cdk/cx-api';
|
| 2 | +import cxapi = require('@aws-cdk/cx-api'); |
2 | 3 | import { Test } from 'nodeunit';
|
3 |
| -import { App, Stack, Token } from '../lib'; |
| 4 | +import { App, Aws, Stack, Token } from '../lib'; |
4 | 5 |
|
5 | 6 | export = {
|
6 | 7 | 'By default, environment region and account are not defined and resolve to intrinsics'(test: Test) {
|
@@ -40,4 +41,140 @@ export = {
|
40 | 41 |
|
41 | 42 | test.done();
|
42 | 43 | },
|
| 44 | + |
| 45 | + 'environment defaults': { |
| 46 | + 'default-account-unknown-region'(test: Test) { |
| 47 | + // GIVEN |
| 48 | + const app = new App(); |
| 49 | + |
| 50 | + // WHEN |
| 51 | + app.node.setContext(cxapi.DEFAULT_ACCOUNT_CONTEXT_KEY, 'my-default-account'); |
| 52 | + const stack = new Stack(app, 'stack'); |
| 53 | + |
| 54 | + // THEN |
| 55 | + test.deepEqual(stack.resolve(stack.account), { Ref: 'AWS::AccountId' }); // TODO: after we implement #2866 this should be 'my-default-account' |
| 56 | + test.deepEqual(stack.resolve(stack.region), { Ref: 'AWS::Region' }); |
| 57 | + test.deepEqual(app.synth().getStack(stack.stackName).environment, { |
| 58 | + account: 'my-default-account', |
| 59 | + region: 'unknown-region', |
| 60 | + name: 'aws://my-default-account/unknown-region' |
| 61 | + }); |
| 62 | + |
| 63 | + test.done(); |
| 64 | + }, |
| 65 | + |
| 66 | + 'default-account-explicit-region'(test: Test) { |
| 67 | + // GIVEN |
| 68 | + const app = new App(); |
| 69 | + |
| 70 | + // WHEN |
| 71 | + app.node.setContext(cxapi.DEFAULT_ACCOUNT_CONTEXT_KEY, 'my-default-account'); |
| 72 | + const stack = new Stack(app, 'stack', { env: { region: 'explicit-region' }}); |
| 73 | + |
| 74 | + // THEN |
| 75 | + test.deepEqual(stack.resolve(stack.account), { Ref: 'AWS::AccountId' }); // TODO: after we implement #2866 this should be 'my-default-account' |
| 76 | + test.deepEqual(stack.resolve(stack.region), 'explicit-region'); |
| 77 | + test.deepEqual(app.synth().getStack(stack.stackName).environment, { |
| 78 | + account: 'my-default-account', |
| 79 | + region: 'explicit-region', |
| 80 | + name: 'aws://my-default-account/explicit-region' |
| 81 | + }); |
| 82 | + |
| 83 | + test.done(); |
| 84 | + }, |
| 85 | + |
| 86 | + 'explicit-account-explicit-region'(test: Test) { |
| 87 | + // GIVEN |
| 88 | + const app = new App(); |
| 89 | + |
| 90 | + // WHEN |
| 91 | + app.node.setContext(cxapi.DEFAULT_ACCOUNT_CONTEXT_KEY, 'my-default-account'); |
| 92 | + const stack = new Stack(app, 'stack', { env: { |
| 93 | + account: 'explicit-account', |
| 94 | + region: 'explicit-region' |
| 95 | + }}); |
| 96 | + |
| 97 | + // THEN |
| 98 | + test.deepEqual(stack.resolve(stack.account), 'explicit-account'); |
| 99 | + test.deepEqual(stack.resolve(stack.region), 'explicit-region'); |
| 100 | + test.deepEqual(app.synth().getStack(stack.stackName).environment, { |
| 101 | + account: 'explicit-account', |
| 102 | + region: 'explicit-region', |
| 103 | + name: 'aws://explicit-account/explicit-region' |
| 104 | + }); |
| 105 | + |
| 106 | + test.done(); |
| 107 | + }, |
| 108 | + |
| 109 | + 'default-account-default-region'(test: Test) { |
| 110 | + // GIVEN |
| 111 | + const app = new App(); |
| 112 | + |
| 113 | + // WHEN |
| 114 | + app.node.setContext(cxapi.DEFAULT_ACCOUNT_CONTEXT_KEY, 'my-default-account'); |
| 115 | + app.node.setContext(cxapi.DEFAULT_REGION_CONTEXT_KEY, 'my-default-region'); |
| 116 | + const stack = new Stack(app, 'stack'); |
| 117 | + |
| 118 | + // THEN |
| 119 | + test.deepEqual(stack.resolve(stack.account), { Ref: 'AWS::AccountId' }); // TODO: after we implement #2866 this should be 'my-default-account' |
| 120 | + test.deepEqual(stack.resolve(stack.region), { Ref: 'AWS::Region' }); // TODO: after we implement #2866 this should be 'my-default-region' |
| 121 | + test.deepEqual(app.synth().getStack(stack.stackName).environment, { |
| 122 | + account: 'my-default-account', |
| 123 | + region: 'my-default-region', |
| 124 | + name: 'aws://my-default-account/my-default-region' |
| 125 | + }); |
| 126 | + |
| 127 | + test.done(); |
| 128 | + }, |
| 129 | + |
| 130 | + 'token-account-token-region-no-defaults'(test: Test) { |
| 131 | + // GIVEN |
| 132 | + const app = new App(); |
| 133 | + |
| 134 | + // WHEN |
| 135 | + app.node.setContext(cxapi.DEFAULT_ACCOUNT_CONTEXT_KEY, 'my-default-account'); |
| 136 | + app.node.setContext(cxapi.DEFAULT_REGION_CONTEXT_KEY, 'my-default-region'); |
| 137 | + const stack = new Stack(app, 'stack', { |
| 138 | + env: { |
| 139 | + account: Aws.accountId, |
| 140 | + region: Aws.region |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + // THEN |
| 145 | + test.deepEqual(stack.resolve(stack.account), { Ref: 'AWS::AccountId' }); |
| 146 | + test.deepEqual(stack.resolve(stack.region), { Ref: 'AWS::Region' }); |
| 147 | + test.deepEqual(app.synth().getStack(stack.stackName).environment, { |
| 148 | + account: 'my-default-account', |
| 149 | + region: 'my-default-region', |
| 150 | + name: 'aws://my-default-account/my-default-region' |
| 151 | + }); |
| 152 | + |
| 153 | + test.done(); |
| 154 | + }, |
| 155 | + |
| 156 | + 'token-account-token-region-with-defaults'(test: Test) { |
| 157 | + // GIVEN |
| 158 | + const app = new App(); |
| 159 | + |
| 160 | + // WHEN |
| 161 | + const stack = new Stack(app, 'stack', { |
| 162 | + env: { |
| 163 | + account: Aws.accountId, |
| 164 | + region: Aws.region |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + // THEN |
| 169 | + test.deepEqual(stack.resolve(stack.account), { Ref: 'AWS::AccountId' }); |
| 170 | + test.deepEqual(stack.resolve(stack.region), { Ref: 'AWS::Region' }); |
| 171 | + test.deepEqual(app.synth().getStack(stack.stackName).environment, { |
| 172 | + account: 'unknown-account', |
| 173 | + region: 'unknown-region', |
| 174 | + name: 'aws://unknown-account/unknown-region' |
| 175 | + }); |
| 176 | + |
| 177 | + test.done(); |
| 178 | + } |
| 179 | + }, |
43 | 180 | };
|
0 commit comments