From bd43f19b6d806266c970ebaa99690b5b2a79651a Mon Sep 17 00:00:00 2001 From: sbadiger Date: Wed, 30 Aug 2023 14:54:35 -0700 Subject: [PATCH] add unit tests Signed-off-by: sbadiger --- Makefile | 2 +- api/v1alpha1/instancegroup_types_test.go | 78 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 28e6b234..5e14cedb 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ all: check-go test clean manager # Run tests .PHONY: test test: generate fmt vet manifests - go test -v ./controllers/... ./api/... -coverprofile coverage.txt + go test ./controllers/... ./api/... -coverprofile coverage.txt .PHONY: bdd bdd: diff --git a/api/v1alpha1/instancegroup_types_test.go b/api/v1alpha1/instancegroup_types_test.go index 5a85134f..5a79390f 100644 --- a/api/v1alpha1/instancegroup_types_test.go +++ b/api/v1alpha1/instancegroup_types_test.go @@ -22,6 +22,7 @@ import ( type EksUnitTest struct { InstanceGroup *InstanceGroup + Overrides *ValidationOverrides } func (u *EksUnitTest) Run(t *testing.T) string { @@ -34,12 +35,15 @@ func (u *EksUnitTest) Run(t *testing.T) string { } func TestInstanceGroupSpecValidate(t *testing.T) { + launchconfiguration := LaunchConfiguration type args struct { instancegroup *InstanceGroup + overrides *ValidationOverrides } testFunction := func(t *testing.T, args args) string { testCase := EksUnitTest{ InstanceGroup: args.instancegroup, + Overrides: args.overrides, } return testCase.Run(t) } @@ -432,6 +436,16 @@ func TestInstanceGroupSpecValidate(t *testing.T) { }, want: "validation failed, HostResourceGroupArn must be a valid dedicated HostResourceGroup ARN", }, + { + name: "default to launch config instead of launch template", + args: args{ + instancegroup: MockInstanceGroup("eks-fargate", "managed", nil, nil, basicFargateSpec()), + overrides: &ValidationOverrides{ + scalingConfigurationOverride: &launchconfiguration, + }, + }, + want: "", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -444,6 +458,70 @@ func TestInstanceGroupSpecValidate(t *testing.T) { } } +func TestScalingConfigOverride(t *testing.T) { + launchconfiguration := LaunchConfiguration + launchtemplate := LaunchTemplate + type args struct { + instancegroup *InstanceGroup + overrides *ValidationOverrides + } + testFunction := func(t *testing.T, args args) string { + testCase := EksUnitTest{ + InstanceGroup: args.instancegroup, + Overrides: args.overrides, + } + return testCase.Run(t) + } + tests := []struct { + name string + args args + want ScalingConfigurationType + }{ + { + name: "override default to launchconfig instead of launchtemplate", + args: args{ + instancegroup: MockInstanceGroup("eks-fargate", "managed", nil, nil, basicFargateSpec()), + overrides: &ValidationOverrides{ + scalingConfigurationOverride: &launchconfiguration, + }, + }, + want: LaunchConfiguration, + }, + { + name: "no default overrides", + args: args{ + instancegroup: MockInstanceGroup("eks-fargate", "managed", nil, nil, basicFargateSpec()), + overrides: &ValidationOverrides{}, + }, + want: LaunchTemplate, + }, + { + name: "override default to launchtemplate", + args: args{ + instancegroup: MockInstanceGroup("eks-fargate", "managed", nil, nil, basicFargateSpec()), + overrides: &ValidationOverrides{ + scalingConfigurationOverride: &launchtemplate, + }, + }, + want: LaunchTemplate, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := testFunction(t, tt.args) + if err != "" { + t.Errorf("error:%v", err) + } + got := tt.args.instancegroup.Spec.EKSSpec.Type + if got != tt.want { + t.Errorf("%v: got %v, want %v", tt.name, got, tt.want) + } + + }) + + } +} + func TestLockedAnnotation(t *testing.T) { tests := []struct { name string