From 16eee5e744616ca0b06beb640f8a80fb68e00f1a Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Mon, 1 Jul 2024 17:46:41 +0000 Subject: [PATCH] Improve error handling of CheckDeployments test helper Expose actual error, so that we can tell if the deployment is not found or not ready/available Signed-off-by: Brad Davidson --- tests/integration/integration.go | 29 +++++++------------ tests/integration/startup/startup_int_test.go | 2 +- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/tests/integration/integration.go b/tests/integration/integration.go index 42b775a49216..6ff0f7cbb192 100644 --- a/tests/integration/integration.go +++ b/tests/integration/integration.go @@ -130,33 +130,24 @@ func K3sServerArgs() []string { // K3sDefaultDeployments checks if the default deployments for K3s are ready, otherwise returns an error func K3sDefaultDeployments() error { - return CheckDeployments([]string{"coredns", "local-path-provisioner", "metrics-server", "traefik"}) + return CheckDeployments(metav1.NamespaceSystem, []string{"coredns", "local-path-provisioner", "metrics-server", "traefik"}) } // CheckDeployments checks if the provided list of deployments are ready, otherwise returns an error -func CheckDeployments(deployments []string) error { - - deploymentSet := make(map[string]bool) - for _, d := range deployments { - deploymentSet[d] = false - } - +func CheckDeployments(namespace string, deployments []string) error { client, err := k8sClient() if err != nil { return err } - deploymentList, err := client.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{}) - if err != nil { - return err - } - for _, deployment := range deploymentList.Items { - if _, ok := deploymentSet[deployment.Name]; ok && deployment.Status.ReadyReplicas == deployment.Status.Replicas { - deploymentSet[deployment.Name] = true + + for _, deploymentName := range deployments { + deployment, err := client.AppsV1().Deployments(namespace).Get(context.Background(), deploymentName, metav1.GetOptions{}) + if err != nil { + return err } - } - for d, found := range deploymentSet { - if !found { - return fmt.Errorf("failed to deploy %s", d) + if deployment.Status.ReadyReplicas != deployment.Status.Replicas || deployment.Status.AvailableReplicas != deployment.Status.Replicas { + return fmt.Errorf("deployment %s not ready: replicas=%d readyReplicas=%d availableReplicas=%d", + deploymentName, deployment.Status.Replicas, deployment.Status.ReadyReplicas, deployment.Status.AvailableReplicas) } } diff --git a/tests/integration/startup/startup_int_test.go b/tests/integration/startup/startup_int_test.go index e0f23f916db3..bc339f8d4a57 100644 --- a/tests/integration/startup/startup_int_test.go +++ b/tests/integration/startup/startup_int_test.go @@ -124,7 +124,7 @@ var _ = Describe("startup tests", Ordered, func() { }) It("has the default pods without traefik deployed", func() { Eventually(func() error { - return testutil.CheckDeployments([]string{"coredns", "local-path-provisioner", "metrics-server"}) + return testutil.CheckDeployments("kube-system", []string{"coredns", "local-path-provisioner", "metrics-server"}) }, "90s", "10s").Should(Succeed()) nodes, err := testutil.ParseNodes() Expect(err).NotTo(HaveOccurred())