/* Copyright 2022 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package pod_test import ( "context" "fmt" "regexp" "testing" "time" "github.com/onsi/ginkgo/v2" "github.com/onsi/ginkgo/v2/reporters" "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" "k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework/internal/output" e2epod "k8s.io/kubernetes/test/e2e/framework/pod" _ "k8s.io/kubernetes/test/utils/format" // activate YAML object dumps ) // The line number of the following code is checked in TestFailureOutput below. // Be careful when moving it around or changing the import statements above. // Here are some intentionally blank lines that can be removed to compensate // for future additional import statements. // // // // // // // This must be line #50. var _ = ginkgo.Describe("pod", func() { ginkgo.It("not found, must exist", func(ctx context.Context) { gomega.Eventually(ctx, framework.HandleRetry(getNoSuchPod)).WithTimeout(timeout).Should(e2epod.BeInPhase(v1.PodRunning)) }) ginkgo.It("not found, retry", func(ctx context.Context) { framework.ExpectNoError(e2epod.WaitTimeoutForPodRunningInNamespace(ctx, clientSet, "no-such-pod", "default", timeout)) }) ginkgo.It("not found, retry with wrappers", func(ctx context.Context) { gomega.Eventually(ctx, framework.RetryNotFound(framework.HandleRetry(getNoSuchPod))).WithTimeout(timeout).Should(e2epod.BeInPhase(v1.PodRunning)) }) ginkgo.It("not found, retry with inverted wrappers", func(ctx context.Context) { gomega.Eventually(ctx, framework.HandleRetry(framework.RetryNotFound(getNoSuchPod))).WithTimeout(timeout).Should(e2epod.BeInPhase(v1.PodRunning)) }) ginkgo.It("not running", func(ctx context.Context) { ginkgo.By(fmt.Sprintf("waiting for pod %s to run", podName)) framework.ExpectNoError(e2epod.WaitTimeoutForPodRunningInNamespace(ctx, clientSet, podName, podNamespace, timeout)) }) ginkgo.It("failed", func(ctx context.Context) { framework.ExpectNoError(e2epod.WaitTimeoutForPodRunningInNamespace(ctx, clientSet, failedPodName, podNamespace, timeout)) }) ginkgo.It("gets reported with API error", func(ctx context.Context) { called := false getPod := func(ctx context.Context) (*v1.Pod, error) { if called { ginkgo.By("returning fake API error") return nil, apierrors.NewTooManyRequests("fake API error", 10) } called = true pod, err := clientSet.CoreV1().Pods(podNamespace).Get(ctx, podName, metav1.GetOptions{}) if err != nil { return nil, err } ginkgo.By("returning pod") return pod, err } gomega.Eventually(ctx, framework.HandleRetry(getPod)).WithTimeout(5 * timeout).Should(e2epod.BeInPhase(v1.PodRunning)) }) }) func getNoSuchPod(ctx context.Context) (*v1.Pod, error) { return clientSet.CoreV1().Pods("default").Get(ctx, "no-such-pod", metav1.GetOptions{}) } const ( podName = "pending-pod" podNamespace = "default" failedPodName = "failed-pod" timeout = time.Second ) var ( clientSet = fake.NewSimpleClientset( &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: podName, Namespace: podNamespace}, Status: v1.PodStatus{Phase: v1.PodPending}}, &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: failedPodName, Namespace: podNamespace}, Status: v1.PodStatus{Phase: v1.PodFailed}}, ) ) func TestFailureOutput(t *testing.T) { expected := output.TestResult{ NormalizeOutput: func(in string) string { return regexp.MustCompile(`wait.go:[[:digit:]]*`).ReplaceAllString(in, `wait.go`) }, Suite: reporters.JUnitTestSuite{ Tests: 7, Failures: 7, Errors: 0, Disabled: 0, Skipped: 0, TestCases: []reporters.JUnitTestCase{ { Name: "[It] pod not found, must exist", Status: "failed", Failure: &reporters.JUnitFailure{ Type: "failed", Description: `[FAILED] Told to stop trying after . Unexpected final error while getting *v1.Pod: pods "no-such-pod" not found In [It] at: wait_test.go:54