Skip to content

Commit ff7960a

Browse files
Eytan AvisrorEytan Avisror
authored andcommitted
add unit test
Signed-off-by: Eytan Avisror <[email protected]>
1 parent b9b5c23 commit ff7960a

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

controllers/provisioners/eks/eks_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,29 @@ func MockTagDescription(key, value string) *autoscaling.TagDescription {
309309
}
310310
}
311311

312+
func MockTemplateOverrides(weight string, types ...string) []*autoscaling.LaunchTemplateOverrides {
313+
overrides := make([]*autoscaling.LaunchTemplateOverrides, 0)
314+
for _, t := range types {
315+
overrides = append(overrides, &autoscaling.LaunchTemplateOverrides{
316+
InstanceType: aws.String(t),
317+
WeightedCapacity: aws.String(weight),
318+
})
319+
}
320+
return overrides
321+
}
322+
312323
func MockScalingGroup(name string, withTemplate bool, t ...*autoscaling.TagDescription) *autoscaling.Group {
313324
asg := &autoscaling.Group{
314325
AutoScalingGroupName: aws.String(name),
315326
Tags: t,
316327
MinSize: aws.Int64(3),
317328
MaxSize: aws.Int64(6),
318329
VPCZoneIdentifier: aws.String("subnet-1,subnet-2,subnet-3"),
330+
Instances: []*autoscaling.Instance{
331+
{
332+
InstanceType: aws.String("m5.xlarge"),
333+
},
334+
},
319335
}
320336

321337
if withTemplate {

controllers/provisioners/eks/helpers_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,85 @@ func TestGetMountOpts(t *testing.T) {
806806
}
807807
}
808808

809+
func TestGetOverrides(t *testing.T) {
810+
var (
811+
g = gomega.NewGomegaWithT(t)
812+
k = MockKubernetesClientSet()
813+
ig = MockInstanceGroup()
814+
configuration = ig.GetEKSConfiguration()
815+
asgMock = NewAutoScalingMocker()
816+
iamMock = NewIamMocker()
817+
eksMock = NewEksMocker()
818+
ec2Mock = NewEc2Mocker()
819+
ssmMock = NewSsmMocker()
820+
)
821+
822+
w := MockAwsWorker(asgMock, iamMock, eksMock, ec2Mock, ssmMock)
823+
ctx := MockContext(ig, k, w)
824+
state := ctx.GetDiscoveredState()
825+
826+
instancePool := "SubFamilyFlexible"
827+
828+
tests := []struct {
829+
primaryType string
830+
scalingGroup *autoscaling.Group
831+
mixedInstancesSpec *v1alpha1.MixedInstancesPolicySpec
832+
expectedOverrides []*autoscaling.LaunchTemplateOverrides
833+
}{
834+
{
835+
primaryType: "m5.xlarge",
836+
scalingGroup: MockScalingGroup("asg-1", true),
837+
mixedInstancesSpec: &v1alpha1.MixedInstancesPolicySpec{
838+
InstanceTypes: []*v1alpha1.InstanceTypeSpec{
839+
{
840+
Type: "m5a.xlarge",
841+
Weight: 1,
842+
},
843+
{
844+
Type: "m5g.xlarge",
845+
Weight: 1,
846+
},
847+
},
848+
},
849+
expectedOverrides: MockTemplateOverrides("1", "m5a.xlarge", "m5g.xlarge", "m5.xlarge"),
850+
},
851+
{
852+
primaryType: "m5.xlarge",
853+
scalingGroup: MockScalingGroup("asg-1", true),
854+
mixedInstancesSpec: &v1alpha1.MixedInstancesPolicySpec{
855+
InstancePool: &instancePool,
856+
},
857+
expectedOverrides: MockTemplateOverrides("1", "m5.xlarge"),
858+
},
859+
{
860+
primaryType: "t2.xlarge",
861+
scalingGroup: MockScalingGroup("asg-1", true),
862+
mixedInstancesSpec: &v1alpha1.MixedInstancesPolicySpec{
863+
InstanceTypes: []*v1alpha1.InstanceTypeSpec{
864+
{
865+
Type: "t2a.xlarge",
866+
Weight: 1,
867+
},
868+
{
869+
Type: "t2g.xlarge",
870+
Weight: 1,
871+
},
872+
},
873+
},
874+
expectedOverrides: MockTemplateOverrides("1", "t2a.xlarge", "t2g.xlarge", "t2.xlarge", "m5.xlarge"),
875+
},
876+
}
877+
878+
for i, tc := range tests {
879+
t.Logf("Test #%v - %+v", i, tc.expectedOverrides)
880+
configuration.MixedInstancesPolicy = tc.mixedInstancesSpec
881+
state.ScalingGroup = tc.scalingGroup
882+
ig.Spec.EKSSpec.EKSConfiguration.InstanceType = tc.primaryType
883+
overrides := ctx.GetOverrides()
884+
g.Expect(overrides).To(gomega.ConsistOf(tc.expectedOverrides))
885+
}
886+
}
887+
809888
func TestGetUserDataStages(t *testing.T) {
810889
var (
811890
g = gomega.NewGomegaWithT(t)

0 commit comments

Comments
 (0)