|
| 1 | +import * as path from 'path'; |
| 2 | +import { Template } from '@aws-cdk/assertions'; |
| 3 | +import * as iam from '@aws-cdk/aws-iam'; |
| 4 | +import * as s3 from '@aws-cdk/aws-s3'; |
| 5 | +import * as cdk from '@aws-cdk/core'; |
| 6 | +import * as cxapi from '@aws-cdk/cx-api'; |
| 7 | +import * as gamelift from '../lib'; |
| 8 | +import { OperatingSystem } from '../lib'; |
| 9 | + |
| 10 | +describe('build', () => { |
| 11 | + const buildId = 'test-identifier'; |
| 12 | + const buildName = 'test-build'; |
| 13 | + let stack: cdk.Stack; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + const app = new cdk.App({ context: { [cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT]: false } }); |
| 17 | + stack = new cdk.Stack(app); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('.fromBuildId()', () => { |
| 21 | + test('with required fields', () => { |
| 22 | + const build = gamelift.Build.fromBuildId(stack, 'ImportedBuild', buildId); |
| 23 | + |
| 24 | + expect(build.buildId).toEqual(buildId); |
| 25 | + expect(build.grantPrincipal).toEqual(new iam.UnknownPrincipal({ resource: build })); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('.fromBuildAttributes()', () => { |
| 30 | + test('with required attrs only', () => { |
| 31 | + const build = gamelift.Build.fromBuildAttributes(stack, 'ImportedBuild', { buildId }); |
| 32 | + |
| 33 | + expect(build.buildId).toEqual(buildId); |
| 34 | + expect(build.grantPrincipal).toEqual(new iam.UnknownPrincipal({ resource: build })); |
| 35 | + }); |
| 36 | + |
| 37 | + test('with all attrs', () => { |
| 38 | + const role = iam.Role.fromRoleArn(stack, 'Role', 'arn:aws:iam::123456789012:role/TestRole'); |
| 39 | + const build = gamelift.Build.fromBuildAttributes(stack, 'ImportedBuild', { buildId, role }); |
| 40 | + |
| 41 | + expect(buildId).toEqual(buildId); |
| 42 | + expect(build.grantPrincipal).toEqual(role); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('new', () => { |
| 47 | + const localAsset = path.join(__dirname, 'my-game-build'); |
| 48 | + const contentBucketName = 'bucketname'; |
| 49 | + const contentBucketAccessStatement = { |
| 50 | + Action: [ |
| 51 | + 's3:GetObject*', |
| 52 | + 's3:GetBucket*', |
| 53 | + 's3:List*', |
| 54 | + ], |
| 55 | + Effect: 'Allow', |
| 56 | + Resource: [ |
| 57 | + { |
| 58 | + 'Fn::Join': [ |
| 59 | + '', |
| 60 | + [ |
| 61 | + 'arn:', |
| 62 | + { |
| 63 | + Ref: 'AWS::Partition', |
| 64 | + }, |
| 65 | + `:s3:::${contentBucketName}`, |
| 66 | + ], |
| 67 | + ], |
| 68 | + }, |
| 69 | + { |
| 70 | + 'Fn::Join': [ |
| 71 | + '', |
| 72 | + [ |
| 73 | + 'arn:', |
| 74 | + { |
| 75 | + Ref: 'AWS::Partition', |
| 76 | + }, |
| 77 | + `:s3:::${contentBucketName}/content`, |
| 78 | + ], |
| 79 | + ], |
| 80 | + }, |
| 81 | + ], |
| 82 | + }; |
| 83 | + let contentBucket: s3.IBucket; |
| 84 | + let content: gamelift.Content; |
| 85 | + let build: gamelift.Build; |
| 86 | + let defaultProps: gamelift.BuildProps; |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + contentBucket = s3.Bucket.fromBucketName(stack, 'ContentBucket', contentBucketName); |
| 90 | + content = gamelift.Content.fromBucket(contentBucket, 'content'); |
| 91 | + defaultProps = { |
| 92 | + content, |
| 93 | + }; |
| 94 | + }); |
| 95 | + |
| 96 | + describe('.fromAsset()', () => { |
| 97 | + test('should create a new build from asset', () => { |
| 98 | + build = gamelift.Build.fromAsset(stack, 'ImportedBuild', localAsset); |
| 99 | + |
| 100 | + expect(stack.node.metadata.find(m => m.type === 'aws:cdk:asset')).toBeDefined(); |
| 101 | + Template.fromStack(stack).hasResourceProperties('AWS::GameLift::Build', { |
| 102 | + StorageLocation: { |
| 103 | + Bucket: { |
| 104 | + Ref: 'AssetParameters6019bfc8ab05a24b0ae9b5d8f4585cbfc7d1c30a23286d0b25ce7066a368a5d7S3Bucket72AA8348', |
| 105 | + }, |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('.fromBucket()', () => { |
| 113 | + test('should create a new build from bucket', () => { |
| 114 | + build = gamelift.Build.fromBucket(stack, 'ImportedBuild', contentBucket, 'content'); |
| 115 | + |
| 116 | + Template.fromStack(stack).hasResourceProperties('AWS::GameLift::Build', { |
| 117 | + StorageLocation: { |
| 118 | + Bucket: 'bucketname', |
| 119 | + Key: 'content', |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('with necessary props only', () => { |
| 127 | + beforeEach(() => { |
| 128 | + build = new gamelift.Build(stack, 'Build', defaultProps); |
| 129 | + }); |
| 130 | + |
| 131 | + test('should create a role and use it with the build', () => { |
| 132 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', { |
| 133 | + AssumeRolePolicyDocument: { |
| 134 | + Statement: [ |
| 135 | + { |
| 136 | + Action: 'sts:AssumeRole', |
| 137 | + Effect: 'Allow', |
| 138 | + Principal: { |
| 139 | + Service: 'gamelift.amazonaws.com', |
| 140 | + }, |
| 141 | + }, |
| 142 | + ], |
| 143 | + Version: '2012-10-17', |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + // Role policy should grant reading from the assets bucket |
| 148 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 149 | + PolicyDocument: { |
| 150 | + Statement: [ |
| 151 | + contentBucketAccessStatement, |
| 152 | + ], |
| 153 | + }, |
| 154 | + Roles: [ |
| 155 | + { |
| 156 | + Ref: 'BuildServiceRole1F57E904', |
| 157 | + }, |
| 158 | + ], |
| 159 | + }); |
| 160 | + |
| 161 | + // check the build using the role |
| 162 | + Template.fromStack(stack).hasResourceProperties('AWS::GameLift::Build', { |
| 163 | + StorageLocation: { |
| 164 | + Bucket: 'bucketname', |
| 165 | + Key: 'content', |
| 166 | + RoleArn: { |
| 167 | + 'Fn::GetAtt': [ |
| 168 | + 'BuildServiceRole1F57E904', |
| 169 | + 'Arn', |
| 170 | + ], |
| 171 | + }, |
| 172 | + }, |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + test('should return correct buildId from CloudFormation', () => { |
| 177 | + expect(stack.resolve(build.buildId)).toEqual({ Ref: 'Build45A36621' }); |
| 178 | + }); |
| 179 | + |
| 180 | + test('with a custom role should use it and set it in CloudFormation', () => { |
| 181 | + const role = iam.Role.fromRoleArn(stack, 'Role', 'arn:aws:iam::123456789012:role/TestRole'); |
| 182 | + build = new gamelift.Build(stack, 'BuildWithRole', { |
| 183 | + ...defaultProps, |
| 184 | + role, |
| 185 | + }); |
| 186 | + |
| 187 | + expect(build.grantPrincipal).toEqual(role); |
| 188 | + Template.fromStack(stack).hasResourceProperties('AWS::GameLift::Build', { |
| 189 | + StorageLocation: { |
| 190 | + RoleArn: role.roleArn, |
| 191 | + }, |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + test('with a custom buildName should set it in CloudFormation', () => { |
| 196 | + build = new gamelift.Build(stack, 'BuildWithName', { |
| 197 | + ...defaultProps, |
| 198 | + buildName: buildName, |
| 199 | + }); |
| 200 | + |
| 201 | + Template.fromStack(stack).hasResourceProperties('AWS::GameLift::Build', { |
| 202 | + Name: buildName, |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + test('with all optional attributes should set it in CloudFormation', () => { |
| 207 | + build = new gamelift.Build(stack, 'BuildWithName', { |
| 208 | + ...defaultProps, |
| 209 | + buildName: buildName, |
| 210 | + operatingSystem: OperatingSystem.AMAZON_LINUX_2, |
| 211 | + buildVersion: '1.0', |
| 212 | + }); |
| 213 | + |
| 214 | + Template.fromStack(stack).hasResourceProperties('AWS::GameLift::Build', { |
| 215 | + Name: buildName, |
| 216 | + OperatingSystem: OperatingSystem.AMAZON_LINUX_2, |
| 217 | + Version: '1.0', |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + test('with an incorrect buildName (>1024)', () => { |
| 222 | + let incorrectBuildName = ''; |
| 223 | + for (let i = 0; i < 1025; i++) { |
| 224 | + incorrectBuildName += 'A'; |
| 225 | + } |
| 226 | + |
| 227 | + expect(() => new gamelift.Build(stack, 'BuildWithWrongName', { |
| 228 | + content, |
| 229 | + buildName: incorrectBuildName, |
| 230 | + })).toThrow(/Build name can not be longer than 1024 characters but has 1025 characters./); |
| 231 | + }); |
| 232 | + }); |
| 233 | + }); |
| 234 | +}); |
| 235 | + |
| 236 | + |
0 commit comments