@@ -3,100 +3,71 @@ package parser
33import (
44 "testing"
55
6+ "github.com/samber/lo"
67 "github.com/stretchr/testify/assert"
78 "github.com/stretchr/testify/require"
89)
910
10- func Test_resolve_find_in_map_value (t * testing.T ) {
11-
11+ func Test_FindInMap (t * testing.T ) {
1212 source := `---
1313Parameters:
1414 Environment:
1515 Type: String
16- Default: production
16+ Default: dev
1717Mappings:
1818 CacheNodeTypes:
1919 production:
2020 NodeType: cache.t2.large
21+ CacheSecurityGroupNames: [ "sg-1", "sg-2" ]
2122 test:
2223 NodeType: cache.t2.small
24+ CacheSecurityGroupNames: [ "sg-3" ]
2325 dev:
2426 NodeType: cache.t2.micro
27+ CacheSecurityGroupNames: [ "sg-4" ]
2528Resources:
26- ElasticacheSecurityGroup:
27- Type: 'AWS::EC2::SecurityGroup'
28- Properties:
29- GroupDescription: Elasticache Security Group
30- SecurityGroupIngress:
31- - IpProtocol: tcp
32- FromPort: 11211
33- ToPort: 11211
34- SourceSecurityGroupName: !Ref InstanceSecurityGroup
35- ElasticacheCluster:
36- Type: 'AWS::ElastiCache::CacheCluster'
37- Properties:
38- Engine: memcached
39- CacheNodeType: !FindInMap [ CacheNodeTypes, production, NodeType ]
40- NumCacheNodes: '1'
41- VpcSecurityGroupIds:
42- - !GetAtt
43- - ElasticacheSecurityGroup
44- - GroupId
45- `
46- ctx := createTestFileContext (t , source )
47- require .NotNil (t , ctx )
48-
49- testRes := ctx .GetResourceByLogicalID ("ElasticacheCluster" )
50- assert .NotNil (t , testRes )
51-
52- nodeTypeProp := testRes .GetStringProperty ("CacheNodeType" , "" )
53- assert .Equal (t , "cache.t2.large" , nodeTypeProp .Value ())
54- }
29+ ElasticacheCluster:
30+ Type: 'AWS::ElastiCache::CacheCluster'
31+ Properties:
32+ Engine: memcached
33+ CacheNodeType: !FindInMap [ CacheNodeTypes, !Ref Environment, NodeType ]
34+ NumCacheNodes: '1'
5535
56- func Test_resolve_find_in_map_with_nested_intrinsic_value (t * testing.T ) {
36+ ElasticacheClusterWithDefault:
37+ Type: 'AWS::ElastiCache::CacheCluster'
38+ Properties:
39+ Engine: memcached
40+ CacheNodeType: !FindInMap [ CacheNodeTypes, staging, NodeType, DefaultValue: cache.t2.medium ]
41+ NumCacheNodes: '1'
5742
58- source := `---
59- Parameters:
60- Environment:
61- Type: String
62- Default: dev
63- Mappings:
64- CacheNodeTypes:
65- production:
66- NodeType: cache.t2.large
67- test:
68- NodeType: cache.t2.small
69- dev:
70- NodeType: cache.t2.micro
71- Resources:
72- ElasticacheSecurityGroup:
73- Type: 'AWS::EC2::SecurityGroup'
74- Properties:
75- GroupDescription: Elasticache Security Group
76- SecurityGroupIngress:
77- - IpProtocol: tcp
78- FromPort: 11211
79- ToPort: 11211
80- SourceSecurityGroupName: !Ref InstanceSecurityGroup
81- ElasticacheCluster:
82- Type: 'AWS::ElastiCache::CacheCluster'
83- Properties:
84- Engine: memcached
85- CacheNodeType: !FindInMap [ CacheNodeTypes, !Ref Environment, NodeType ]
86- NumCacheNodes: '1'
87- VpcSecurityGroupIds:
88- - !GetAtt
89- - ElasticacheSecurityGroup
90- - GroupId
43+ ElasticacheClusterList:
44+ Type: 'AWS::ElastiCache::CacheCluster'
45+ Properties:
46+ Engine: memcached
47+ CacheSecurityGroupNames: !FindInMap [ CacheNodeTypes, production, CacheSecurityGroupNames ]
48+ NumCacheNodes: '1'
9149`
50+
9251 ctx := createTestFileContext (t , source )
9352 require .NotNil (t , ctx )
9453
95- testRes := ctx .GetResourceByLogicalID ("ElasticacheCluster" )
96- assert .NotNil (t , testRes )
97-
98- nodeTypeProp := testRes .GetStringProperty ("CacheNodeType" , "" )
54+ cluster := ctx .GetResourceByLogicalID ("ElasticacheCluster" )
55+ require .NotNil (t , cluster )
56+ nodeTypeProp := cluster .GetStringProperty ("CacheNodeType" , "" )
9957 assert .Equal (t , "cache.t2.micro" , nodeTypeProp .Value ())
58+
59+ clusterDefault := ctx .GetResourceByLogicalID ("ElasticacheClusterWithDefault" )
60+ require .NotNil (t , clusterDefault )
61+ nodeTypePropDefault := clusterDefault .GetStringProperty ("CacheNodeType" , "" )
62+ assert .Equal (t , "cache.t2.medium" , nodeTypePropDefault .Value ())
63+
64+ clusterList := ctx .GetResourceByLogicalID ("ElasticacheClusterList" )
65+ require .NotNil (t , clusterList )
66+ sgNamesProp := clusterList .GetProperty ("CacheSecurityGroupNames" ).AsList ()
67+ groupNames := lo .Map (sgNamesProp , func (prop * Property , _ int ) any {
68+ return prop .AsString ()
69+ })
70+ assert .ElementsMatch (t , []any {"sg-1" , "sg-2" }, groupNames )
10071}
10172
10273func Test_InferType (t * testing.T ) {
0 commit comments