11package builder
22
33import (
4+ "encoding/json"
45 "fmt"
56 "testing"
67
78 "github.com/stretchr/testify/require"
89 api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
10+ cft "github.com/weaveworks/eksctl/pkg/cfn/template"
911 "github.com/weaveworks/eksctl/pkg/nodebootstrap"
1012 "github.com/weaveworks/eksctl/pkg/nodebootstrap/fakes"
1113 "github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
@@ -18,7 +20,9 @@ import (
1820func TestManagedPolicyResources (t * testing.T ) {
1921 iamRoleTests := []struct {
2022 addons api.NodeGroupIAMAddonPolicies
23+ attachPolicy api.InlineDocument
2124 attachPolicyARNs []string
25+ expectedNewPolicies []string
2226 expectedManagedPolicies []* gfnt.Value
2327 description string
2428 }{
@@ -42,16 +46,36 @@ func TestManagedPolicyResources(t *testing.T) {
4246 "AmazonEC2ContainerRegistryReadOnly" , "AmazonSSMManagedInstanceCore" , "CloudWatchAgentServerPolicy" ),
4347 description : "CloudWatch enabled" ,
4448 },
49+ {
50+ addons : api.NodeGroupIAMAddonPolicies {
51+ AutoScaler : api .Enabled (),
52+ },
53+ expectedNewPolicies : []string {"PolicyAutoScaling" },
54+ expectedManagedPolicies : makePartitionedPolicies ("AmazonEKSWorkerNodePolicy" , "AmazonEKS_CNI_Policy" , "AmazonEC2ContainerRegistryReadOnly" , "AmazonSSMManagedInstanceCore" ),
55+ description : "AutoScaler enabled" ,
56+ },
57+ {
58+ attachPolicy : cft .MakePolicyDocument (cft.MapOfInterfaces {
59+ "Effect" : "Allow" ,
60+ "Action" : []string {
61+ "s3:Get*" ,
62+ },
63+ "Resource" : "*" ,
64+ }),
65+ expectedNewPolicies : []string {"Policy1" },
66+ expectedManagedPolicies : makePartitionedPolicies ("AmazonEKSWorkerNodePolicy" , "AmazonEKS_CNI_Policy" , "AmazonEC2ContainerRegistryReadOnly" , "AmazonSSMManagedInstanceCore" ),
67+ description : "Custom inline policies" ,
68+ },
4569 {
4670 attachPolicyARNs : []string {"AmazonEKSWorkerNodePolicy" , "AmazonEKS_CNI_Policy" },
4771 expectedManagedPolicies : subs (prefixPolicies ("AmazonEKSWorkerNodePolicy" , "AmazonEKS_CNI_Policy" )),
48- description : "Custom policies" ,
72+ description : "Custom managed policies" ,
4973 },
5074 // should not attach any additional policies
5175 {
5276 attachPolicyARNs : []string {"CloudWatchAgentServerPolicy" },
5377 expectedManagedPolicies : subs (prefixPolicies ("CloudWatchAgentServerPolicy" )),
54- description : "Custom policies" ,
78+ description : "Custom managed policies" ,
5579 },
5680 // no duplicate values
5781 {
@@ -81,6 +105,7 @@ func TestManagedPolicyResources(t *testing.T) {
81105 ng := api .NewManagedNodeGroup ()
82106 api .SetManagedNodeGroupDefaults (ng , clusterConfig .Metadata )
83107 ng .IAM .WithAddonPolicies = tt .addons
108+ ng .IAM .AttachPolicy = tt .attachPolicy
84109 ng .IAM .AttachPolicyARNs = prefixPolicies (tt .attachPolicyARNs ... )
85110
86111 p := mockprovider .NewMockProvider ()
@@ -99,11 +124,29 @@ func TestManagedPolicyResources(t *testing.T) {
99124 template , err := goformation .ParseJSON (bytes )
100125 require .NoError (err )
101126
102- role , ok := template .GetAllIAMRoleResources ()[ "NodeInstanceRole" ]
103- require .True ( ok )
127+ role , err := template .GetIAMRoleWithName ( cfnIAMInstanceRoleName )
128+ require .NoError ( err )
104129
105130 require .ElementsMatch (tt .expectedManagedPolicies , role .ManagedPolicyArns .Raw ().(gfnt.Slice ))
106131
132+ policyNames := make ([]string , 0 )
133+ for name := range template .GetAllIAMPolicyResources () {
134+ policyNames = append (policyNames , name )
135+ }
136+ require .ElementsMatch (tt .expectedNewPolicies , policyNames )
137+
138+ // assert custom inline policy matches
139+ if tt .attachPolicy != nil {
140+ policy , err := template .GetIAMPolicyWithName ("Policy1" )
141+ require .NoError (err )
142+
143+ // convert to json for comparison since interfaces are not identical
144+ expectedPolicy , err := json .Marshal (tt .attachPolicy )
145+ require .NoError (err )
146+ actualPolicy , err := json .Marshal (policy .PolicyDocument )
147+ require .NoError (err )
148+ require .Equal (string (expectedPolicy ), string (actualPolicy ))
149+ }
107150 })
108151 }
109152
@@ -167,7 +210,7 @@ func TestManagedNodeRole(t *testing.T) {
167210
168211 template , err := goformation .ParseJSON (bytes )
169212 require .NoError (err )
170- ngResource , ok := template .Resources ["ManagedNodeGroup" ]
213+ ngResource , ok := template .Resources [ManagedNodeGroupResourceName ]
171214 require .True (ok )
172215 ng , ok := ngResource .(* gfneks.Nodegroup )
173216 require .True (ok )
0 commit comments