@@ -23,6 +23,7 @@ import (
2323 "github.com/golang/mock/gomock"
2424 "github.com/stretchr/testify/assert"
2525 v1 "k8s.io/api/core/v1"
26+ "k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
2627 "k8s.io/autoscaler/cluster-autoscaler/expander/grpcplugin/protos"
2728 "k8s.io/autoscaler/cluster-autoscaler/expander/mocks"
2829 . "k8s.io/autoscaler/cluster-autoscaler/utils/test"
5859 Debug : "m4.4xlarge" ,
5960 NodeGroup : test .NewTestNodeGroup ("my-asg.m4.4xlarge" , 10 , 1 , 1 , true , false , "m4.4xlarge" , nil , nil ),
6061 }
62+ eoT2MicroWithSimilar = expander.Option {
63+ Debug : "t2.micro" ,
64+ NodeGroup : test .NewTestNodeGroup ("my-asg.t2.micro" , 10 , 1 , 1 , true , false , "t2.micro" , nil , nil ),
65+ SimilarNodeGroups : []cloudprovider.NodeGroup {test .NewTestNodeGroup ("my-similar-asg.t2.micro" , 10 , 1 , 1 , true , false , "t2.micro" , nil , nil )},
66+ }
6167 options = []expander.Option {eoT2Micro , eoT2Large , eoT3Large , eoM44XLarge }
6268
6369 grpcEoT2Micro = protos.Option {
8490 Debug : eoM44XLarge .Debug ,
8591 Pod : eoM44XLarge .Pods ,
8692 }
93+ grpcEoT2MicroWithSimilar = protos.Option {
94+ NodeGroupId : eoT2Micro .NodeGroup .Id (),
95+ NodeCount : int32 (eoT2Micro .NodeCount ),
96+ Debug : eoT2Micro .Debug ,
97+ Pod : eoT2Micro .Pods ,
98+ SimilarNodeGroupIds : []string {eoT2MicroWithSimilar .SimilarNodeGroups [0 ].Id ()},
99+ }
100+ grpcEoT2MicroWithSimilarWithExtraOptions = protos.Option {
101+ NodeGroupId : eoT2Micro .NodeGroup .Id (),
102+ NodeCount : int32 (eoT2Micro .NodeCount ),
103+ Debug : eoT2Micro .Debug ,
104+ Pod : eoT2Micro .Pods ,
105+ SimilarNodeGroupIds : []string {eoT2MicroWithSimilar .SimilarNodeGroups [0 ].Id (), "extra-ng-id" },
106+ }
87107)
88108
89109func TestPopulateOptionsForGrpc (t * testing.T ) {
@@ -116,6 +136,12 @@ func TestPopulateOptionsForGrpc(t *testing.T) {
116136 eoM44XLarge .NodeGroup .Id (): eoM44XLarge ,
117137 },
118138 },
139+ {
140+ desc : "similar nodegroups are included" ,
141+ opts : []expander.Option {eoT2MicroWithSimilar },
142+ expectedOpts : []* protos.Option {& grpcEoT2MicroWithSimilar },
143+ expectedMap : map [string ]expander.Option {eoT2MicroWithSimilar .NodeGroup .Id (): eoT2MicroWithSimilar },
144+ },
119145 }
120146 for _ , tc := range testCases {
121147 grpcOptionsSlice , nodeGroupIDOptionMap := populateOptionsForGRPC (tc .opts )
@@ -146,18 +172,44 @@ func TestPopulateNodeInfoForGRPC(t *testing.T) {
146172}
147173
148174func TestValidTransformAndSanitizeOptionsFromGRPC (t * testing.T ) {
149- responseOptionsSlice := []* protos.Option {& grpcEoT2Micro , & grpcEoT3Large , & grpcEoM44XLarge }
150- nodeGroupIDOptionMap := map [string ]expander.Option {
151- eoT2Micro .NodeGroup .Id (): eoT2Micro ,
152- eoT2Large .NodeGroup .Id (): eoT2Large ,
153- eoT3Large .NodeGroup .Id (): eoT3Large ,
154- eoM44XLarge .NodeGroup .Id (): eoM44XLarge ,
175+ testCases := []struct {
176+ desc string
177+ responseOptions []* protos.Option
178+ expectedOptions []expander.Option
179+ nodegroupIDOptionaMap map [string ]expander.Option
180+ }{
181+ {
182+ desc : "valid transform and sanitize options" ,
183+ responseOptions : []* protos.Option {& grpcEoT2Micro , & grpcEoT3Large , & grpcEoM44XLarge },
184+ nodegroupIDOptionaMap : map [string ]expander.Option {
185+ eoT2Micro .NodeGroup .Id (): eoT2Micro ,
186+ eoT2Large .NodeGroup .Id (): eoT2Large ,
187+ eoT3Large .NodeGroup .Id (): eoT3Large ,
188+ eoM44XLarge .NodeGroup .Id (): eoM44XLarge ,
189+ },
190+ expectedOptions : []expander.Option {eoT2Micro , eoT3Large , eoM44XLarge },
191+ },
192+ {
193+ desc : "similar ngs are retained in proto options are retained" ,
194+ responseOptions : []* protos.Option {& grpcEoT2MicroWithSimilar },
195+ nodegroupIDOptionaMap : map [string ]expander.Option {
196+ eoT2MicroWithSimilar .NodeGroup .Id (): eoT2MicroWithSimilar ,
197+ },
198+ expectedOptions : []expander.Option {eoT2MicroWithSimilar },
199+ },
200+ {
201+ desc : "extra similar ngs added to expander response are ignored" ,
202+ responseOptions : []* protos.Option {& grpcEoT2MicroWithSimilarWithExtraOptions },
203+ nodegroupIDOptionaMap : map [string ]expander.Option {
204+ eoT2MicroWithSimilar .NodeGroup .Id (): eoT2MicroWithSimilar ,
205+ },
206+ expectedOptions : []expander.Option {eoT2MicroWithSimilar },
207+ },
208+ }
209+ for _ , tc := range testCases {
210+ ret := transformAndSanitizeOptionsFromGRPC (tc .responseOptions , tc .nodegroupIDOptionaMap )
211+ assert .Equal (t , tc .expectedOptions , ret )
155212 }
156-
157- expectedOptions := []expander.Option {eoT2Micro , eoT3Large , eoM44XLarge }
158-
159- ret := transformAndSanitizeOptionsFromGRPC (responseOptionsSlice , nodeGroupIDOptionMap )
160- assert .Equal (t , expectedOptions , ret )
161213}
162214
163215func TestAnInvalidTransformAndSanitizeOptionsFromGRPC (t * testing.T ) {
0 commit comments