From de83723fbb6a9324481861c078ba9d3b4d013e96 Mon Sep 17 00:00:00 2001 From: Tibi <110664232+TiberiuGC@users.noreply.github.com> Date: Thu, 24 Aug 2023 11:27:27 +0300 Subject: [PATCH] Improve unit tests' runtime (#6992) * improve cluster creation tests run time * remove go:generate duplicates, exploit counterfeiter directives instead * don't generate files twice on the workflow --- .github/workflows/test-and-build.yaml | 2 +- Makefile | 5 ++++- pkg/cfn/manager/api.go | 8 +------- pkg/cfn/waiter/stack.go | 8 ++++++++ pkg/ctl/create/cluster_test.go | 5 +++++ pkg/eks/fargate.go | 1 - pkg/eks/nodegroup_service.go | 2 -- pkg/karpenter/providers/helm.go | 3 ++- pkg/outposts/outposts.go | 5 ++--- 9 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test-and-build.yaml b/.github/workflows/test-and-build.yaml index a1d1b9d793..f6877be69f 100644 --- a/.github/workflows/test-and-build.yaml +++ b/.github/workflows/test-and-build.yaml @@ -32,7 +32,7 @@ jobs: - name: Unit test run: | PATH=$PATH:$(go env GOPATH)/bin make build - PATH=$PATH:$(go env GOPATH)/bin make unit-test + PATH=$PATH:$(go env GOPATH)/bin make unit-test-no-generate lint: name: Lint runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index a21148d6d2..acafc8ca58 100644 --- a/Makefile +++ b/Makefile @@ -92,7 +92,10 @@ test: ## Lint, generate and run unit tests. Also ensure that integration tests c $(MAKE) build-integration-test .PHONY: unit-test -unit-test: check-all-generated-files-up-to-date ## Run unit test only +unit-test: check-all-generated-files-up-to-date unit-test-no-generate + +.PHONY: unit-test-no-generate ## Run unit test only +unit-test-no-generate: CGO_ENABLED=0 go test -tags=release ./pkg/... ./cmd/... $(UNIT_TEST_ARGS) .PHONY: unit-test-race diff --git a/pkg/cfn/manager/api.go b/pkg/cfn/manager/api.go index aeaecdb9c3..3533e9d2d9 100644 --- a/pkg/cfn/manager/api.go +++ b/pkg/cfn/manager/api.go @@ -204,13 +204,7 @@ func (c *StackCollection) createClusterStack(ctx context.Context, stackName stri ctx, cancelFunc := context.WithTimeout(context.Background(), c.waitTimeout) defer cancelFunc() - stack, err := waiter.WaitForStack(ctx, c.cloudformationAPI, *stack.StackId, *stack.StackName, func(attempts int) time.Duration { - // Wait 30s for the first two requests, and 1m for subsequent requests. - if attempts <= 2 { - return 30 * time.Second - } - return 1 * time.Minute - }) + stack, err := waiter.WaitForStack(ctx, c.cloudformationAPI, *stack.StackId, *stack.StackName, waiter.ClusterCreationNextDelay) if err != nil { troubleshoot() diff --git a/pkg/cfn/waiter/stack.go b/pkg/cfn/waiter/stack.go index 99eb0fc6ee..0b7637edb1 100644 --- a/pkg/cfn/waiter/stack.go +++ b/pkg/cfn/waiter/stack.go @@ -13,6 +13,14 @@ import ( "github.com/weaveworks/eksctl/pkg/awsapi" ) +var ClusterCreationNextDelay = func(attempts int) time.Duration { + // Wait 30s for the first two requests, and 1m for subsequent requests. + if attempts <= 2 { + return 30 * time.Second + } + return 1 * time.Minute +} + // NextDelay returns the amount of time to wait before the next retry given the number of attempts. type NextDelay func(attempts int) time.Duration diff --git a/pkg/ctl/create/cluster_test.go b/pkg/ctl/create/cluster_test.go index 5872102d81..9a787acfe6 100644 --- a/pkg/ctl/create/cluster_test.go +++ b/pkg/ctl/create/cluster_test.go @@ -33,6 +33,7 @@ import ( karpenteractions "github.com/weaveworks/eksctl/pkg/actions/karpenter" karpenterfakes "github.com/weaveworks/eksctl/pkg/actions/karpenter/fakes" "github.com/weaveworks/eksctl/pkg/cfn/manager" + "github.com/weaveworks/eksctl/pkg/cfn/waiter" api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" "github.com/weaveworks/eksctl/pkg/cfn/outputs" @@ -936,6 +937,10 @@ func defaultProviderMocks(p *mockprovider.MockProvider, output []cftypes.Output, }, }, }, nil) + + waiter.ClusterCreationNextDelay = func(_ int) time.Duration { + return 0 + } p.MockCloudFormation().On("CreateStack", mock.Anything, mock.Anything).Return(&cloudformation.CreateStackOutput{ StackId: aws.String(clusterStackName), }, nil).Once() diff --git a/pkg/eks/fargate.go b/pkg/eks/fargate.go index 1a9da6430c..d9f809ec94 100644 --- a/pkg/eks/fargate.go +++ b/pkg/eks/fargate.go @@ -18,7 +18,6 @@ import ( api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" ) -//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate //counterfeiter:generate -o fakes/fargate_client.go . FargateClient type FargateClient interface { CreateProfile(ctx context.Context, profile *api.FargateProfile, waitForCreation bool) error diff --git a/pkg/eks/nodegroup_service.go b/pkg/eks/nodegroup_service.go index a744427297..f4cb170f5c 100644 --- a/pkg/eks/nodegroup_service.go +++ b/pkg/eks/nodegroup_service.go @@ -26,8 +26,6 @@ import ( // a CloudFormation template. const maxInstanceTypes = 40 -//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate - // InstanceSelector selects a set of instance types matching the specified instance selector criteria. // //counterfeiter:generate -o fakes/fake_instance_selector.go . InstanceSelector diff --git a/pkg/karpenter/providers/helm.go b/pkg/karpenter/providers/helm.go index 185559e77f..25c6cee35e 100644 --- a/pkg/karpenter/providers/helm.go +++ b/pkg/karpenter/providers/helm.go @@ -9,6 +9,7 @@ import ( ) // URLGetter is an interface to support GET to the specified URL. +// //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate //counterfeiter:generate -o fakes/fake_helm_getter.go . URLGetter type URLGetter interface { @@ -28,7 +29,7 @@ type InstallChartOpts struct { } // HelmInstaller deals with setting up Helm related resources. -//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate +// //counterfeiter:generate -o fakes/fake_helm_installer.go . HelmInstaller type HelmInstaller interface { // InstallChart takes a releaseName's name and a chart name and installs it. If namespace is not empty diff --git a/pkg/outposts/outposts.go b/pkg/outposts/outposts.go index 85a57d6257..aab423a766 100644 --- a/pkg/outposts/outposts.go +++ b/pkg/outposts/outposts.go @@ -98,10 +98,9 @@ func (o *Service) describeOutpostInstanceTypes(ctx context.Context) ([]ec2types. return o.instanceTypeInfoList, nil } -//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate -//counterfeiter:generate -o fakes . OutpostInstance - // OutpostInstance represents an instance running on Outposts. +// +//counterfeiter:generate -o fakes . OutpostInstance type OutpostInstance interface { // SetInstanceType sets the instance type. SetInstanceType(instanceType string)