Skip to content

Commit

Permalink
Fix flaky test: wait until the CIDR created
Browse files Browse the repository at this point in the history
  • Loading branch information
mneverov committed Sep 12, 2024
1 parent ffaa46c commit 7f00315
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ KUBEBUILDER_ASSETS=$(shell go run sigs.k8s.io/controller-runtime/tools/setup-env

.PHONY: test
test: manifests generate fmt ## Run tests.
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... -coverprofile cover.out -v
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -coverprofile cover.out -v ./...

##@ Build

Expand Down
10 changes: 9 additions & 1 deletion pkg/controller/ipam/ipam_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ var (

func TestAPIs(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Node IPAM Controller Suite")
// ginkgo only prints test names and output when either tests failed or when ginkgo run with -ginkgo.v (verbose).
// When running test with `go test -v ./... -ginkgo.v` each package is built and run as a separate binary, hence
// for all other packages but node-ipam-controller/pkg/controller/ipam the flag `-ginkgo.v` is unknown, which lead
// to error "flag provided but not defined: -ginkgo.v".
// To work it around the following two lines set verbosity manually, so the output contains test names (and log
// messages from the controller).
suiteCfg, repCfg := ginkgo.GinkgoConfiguration()
repCfg.Verbose = true
ginkgo.RunSpecs(t, "Node IPAM Controller Suite", suiteCfg, repCfg)
}

var _ = ginkgo.BeforeSuite(func() {
Expand Down
17 changes: 12 additions & 5 deletions pkg/controller/ipam/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,19 @@ var _ = ginkgo.Describe("Pod CIDRs", ginkgo.Ordered, func() {
clusterCIDR, err := cidrClient.NetworkingV1().ClusterCIDRs().Create(ctx, originalClusterCIDR, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())

clusterCIDR.Spec.NodeSelector = nodeSelector(map[string][]string{"ipv4": {"false"}, "ipv6": {"false"}})
clusterCIDR.Spec.PerNodeHostBits = int32(9)
clusterCIDR.Spec.IPv4 = "10.10.0.0/23"
clusterCIDR.Spec.IPv6 = "fd00:30:101::/119"
// wait until creation is done and the finalizer is set.
gomega.Eventually(komega.Object(clusterCIDR)).Should(gomega.WithTransform(func(c *v1.ClusterCIDR) []string {
return c.GetFinalizers()
}, gomega.ContainElement(clusterCIDRFinalizer)))

_, err = cidrClient.NetworkingV1().ClusterCIDRs().Update(ctx, clusterCIDR, metav1.UpdateOptions{})
updated := clusterCIDR.DeepCopy()

updated.Spec.NodeSelector = nodeSelector(map[string][]string{"ipv4": {"false"}, "ipv6": {"false"}})
updated.Spec.PerNodeHostBits = int32(9)
updated.Spec.IPv4 = "10.10.0.0/23"
updated.Spec.IPv6 = "fd00:30:101::/119"

_, err = cidrClient.NetworkingV1().ClusterCIDRs().Update(ctx, updated, metav1.UpdateOptions{})

gomega.Expect(err).Should(gomega.MatchError(gomega.ContainSubstring("NodeSelector cannot be changed.")))
gomega.Expect(err).Should(gomega.MatchError(gomega.ContainSubstring("PerNodeHostBits cannot be changed.")))
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/ipam/multi_cidr_range_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (r *multiCIDRRangeAllocator) processNextCIDRWorkItem(ctx context.Context) b
// Finally, if no error occurs we Forget this item so it does not
// get cidrQueued again until another change happens.
r.cidrQueue.Forget(obj)
logger.Info("Successfully synced", "key", key)
logger.Info("Successfully synced cidr", "key", key)
return nil
}(ctx, obj)
if err != nil {
Expand Down Expand Up @@ -454,7 +454,7 @@ func (r *multiCIDRRangeAllocator) processNextNodeWorkItem(ctx context.Context) b
// Finally, if no error occurs we Forget this item so it does not
// get nodeQueue again until another change happens.
r.nodeQueue.Forget(obj)
logger.Info("Successfully synced", "key", key)
logger.Info("Successfully synced node", "key", key)
return nil
}(klog.FromContext(ctx), obj)
if err != nil {
Expand Down

0 comments on commit 7f00315

Please sign in to comment.