diff --git a/test/e2e/karmadactl_test.go b/test/e2e/karmadactl_test.go index bb91868d55ad..08192663d388 100644 --- a/test/e2e/karmadactl_test.go +++ b/test/e2e/karmadactl_test.go @@ -892,3 +892,103 @@ var _ = ginkgo.Describe("Karmadactl get testing", func() { }) }) }) + +var _ = ginkgo.Describe("Karmadactl describe testing", func() { + var member1 string + var member1Client kubernetes.Interface + + ginkgo.BeforeEach(func() { + member1 = "member1" + member1Client = framework.GetClusterClient(member1) + }) + + ginkgo.Context("Test karmadactl describe for existing resource", func() { + var namespace, podName string + var ( + ns *corev1.Namespace + pod *corev1.Pod + ) + + ginkgo.BeforeEach(func() { + namespace = fmt.Sprintf("karmadatest-%s", rand.String(RandomStrLength)) + podName = podNamePrefix + rand.String(RandomStrLength) + pod = helper.NewPod(namespace, podName) + ns = helper.NewNamespace(namespace) + // Create the namespace and pod in the member cluster. + _, err := member1Client.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + _, err = member1Client.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.AfterEach(func() { + err := member1Client.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.It("should describe the existing pod successfully", func() { + cmd := framework.NewKarmadactlCommand(kubeconfig, karmadaContext, karmadactlPath, namespace, karmadactlTimeout, "describe", "pods", podName, "-C", member1) + output, err := cmd.ExecOrDie() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + gomega.Expect(strings.Contains(output, podName)).Should(gomega.BeTrue()) + }) + }) + + ginkgo.Context("Test karmadactl describe for non-existing resource", func() { + var namespace, podName string + var ns *corev1.Namespace + + ginkgo.BeforeEach(func() { + namespace = fmt.Sprintf("karmadatest-%s", rand.String(RandomStrLength)) + podName = podNamePrefix + rand.String(RandomStrLength) + ns = helper.NewNamespace(namespace) + // Create the namespace in the member cluster. + _, err := member1Client.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.AfterEach(func() { + err := member1Client.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.It("should return not found error for non-existing pod", func() { + cmd := framework.NewKarmadactlCommand(kubeconfig, karmadaContext, karmadactlPath, namespace, karmadactlTimeout, "describe", "pods", podName, "-C", member1) + _, err := cmd.ExecOrDie() + gomega.Expect(err).Should(gomega.HaveOccurred()) + gomega.Expect(strings.Contains(err.Error(), fmt.Sprintf("pods \"%s\" not found", podName))).Should(gomega.BeTrue()) + }) + }) + + ginkgo.Context("Test karmadactl describe for non-existing resource in non-existing namespace", func() { + var namespace, podName string + + ginkgo.BeforeEach(func() { + namespace = fmt.Sprintf("karmadatest-%s", rand.String(RandomStrLength)) + podName = podNamePrefix + rand.String(RandomStrLength) + }) + + ginkgo.It("should return not found error for non-existing namespace", func() { + cmd := framework.NewKarmadactlCommand(kubeconfig, karmadaContext, karmadactlPath, namespace, karmadactlTimeout, "describe", "pods", podName, "-C", member1) + _, err := cmd.ExecOrDie() + gomega.Expect(err).Should(gomega.HaveOccurred()) + gomega.Expect(strings.Contains(err.Error(), fmt.Sprintf("namespaces \"%s\" not found", namespace))).Should(gomega.BeTrue()) + }) + }) + + ginkgo.Context("Test karmadactl describe with invalid input", func() { + var namespace string + + ginkgo.BeforeEach(func() { + namespace = fmt.Sprintf("karmadatest-%s", rand.String(RandomStrLength)) + }) + + ginkgo.It("should return error for invalid resource type", func() { + cmd := framework.NewKarmadactlCommand(kubeconfig, karmadaContext, karmadactlPath, namespace, karmadactlTimeout, "describe", "invalidresource", "invalidname", "-C", member1) + _, err := cmd.ExecOrDie() + gomega.Expect(err).Should(gomega.HaveOccurred()) + gomega.Expect(strings.Contains(err.Error(), "the server doesn't have a resource type \"invalidresource\"")).Should(gomega.BeTrue()) + }) + }) +})