Skip to content

Commit

Permalink
Improve unit tests' runtime (#6992)
Browse files Browse the repository at this point in the history
* improve cluster creation tests run time

* remove go:generate duplicates, exploit counterfeiter directives instead

* don't generate files twice on the workflow
  • Loading branch information
TiberiuGC committed Aug 24, 2023
1 parent 3b4727d commit de83723
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-and-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions pkg/cfn/manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions pkg/cfn/waiter/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions pkg/ctl/create/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion pkg/eks/fargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pkg/eks/nodegroup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/karpenter/providers/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/outposts/outposts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit de83723

Please sign in to comment.