<commit_before>/*
Copyright 2016 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 common

import (
	"fmt"
	"path"
	"time"

	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/util/uuid"
	"k8s.io/kubernetes/pkg/kubelet/images"
	"k8s.io/kubernetes/test/e2e/framework"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	gomegatypes "github.com/onsi/gomega/types"
)

var _ = framework.KubeDescribe("Container Runtime", func() {
	f := framework.NewDefaultFramework("container-runtime")

	Describe("blackbox test", func() {
		Context("when starting a container that exits", func() {

			/*
				Release : v1.13
				Testname: Container Runtime, Restart Policy, Pod Phases
				Description: If the restart policy is set to ‘Always’, Pod MUST be restarted when terminated, If restart policy is ‘OnFailure’, Pod MUST be started only if it is terminated with non-zero exit code. If the restart policy is ‘Never’, Pod MUST never be restarted. All these three test cases MUST verify the restart counts accordingly.
			*/
			framework.ConformanceIt("should run with the expected status [NodeConformance]", func() {
				restartCountVolumeName := "restart-count"
				restartCountVolumePath := "/restart-count"
				testContainer := v1.Container{
					Image: framework.BusyBoxImage,
					VolumeMounts: []v1.VolumeMount{
						{
							MountPath: restartCountVolumePath,
							Name:      restartCountVolumeName,
						},
					},
				}
				testVolumes := []v1.Volume{
					{
						Name: restartCountVolumeName,
						VolumeSource: v1.VolumeSource{
							EmptyDir: &v1.EmptyDirVolumeSource{Medium: v1.StorageMediumMemory},
						},
					},
				}
				testCases := []struct {
					Name          string
					RestartPolicy v1.RestartPolicy
					Phase         v1.PodPhase
					State         ContainerState
					RestartCount  int32
					Ready         bool
				}{
					{"terminate-cmd-rpa", v1.RestartPolicyAlways, v1.PodRunning, ContainerStateRunning, 2, true},
					{"terminate-cmd-rpof", v1.RestartPolicyOnFailure, v1.PodSucceeded, ContainerStateTerminated, 1, false},
					{"terminate-cmd-rpn", v1.RestartPolicyNever, v1.PodFailed, ContainerStateTerminated, 0, false},
				}
				for _, testCase := range testCases {

					// It failed at the 1st run, then succeeded at 2nd run, then run forever
					cmdScripts := `
f=%s
count=$(echo 'hello' >> $f ; wc -l $f | awk {'print $1'})
if [ $count -eq 1 ]; then
	exit 1
fi
if [ $count -eq 2 ]; then
	exit 0
fi
while true; do sleep 1; done
`
					tmpCmd := fmt.Sprintf(cmdScripts, path.Join(restartCountVolumePath, "restartCount"))
					testContainer.Name = testCase.Name
					testContainer.Command = []string{"sh", "-c", tmpCmd}
					terminateContainer := ConformanceContainer{
						PodClient:     f.PodClient(),
						Container:     testContainer,
						RestartPolicy: testCase.RestartPolicy,
						Volumes:       testVolumes,
					}
					terminateContainer.Create()
					defer terminateContainer.Delete()

					By(fmt.Sprintf("Container '%s': should get the expected 'RestartCount'", testContainer.Name))
					Eventually(func() (int32, error) {
						status, err := terminateContainer.GetStatus()
						return status.RestartCount, err
					}, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(Equal(testCase.RestartCount))

					By(fmt.Sprintf("Container '%s': should get the expected 'Phase'", testContainer.Name))
					Eventually(terminateContainer.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(Equal(testCase.Phase))

					By(fmt.Sprintf("Container '%s': should get the expected 'Ready' condition", testContainer.Name))
					Expect(terminateContainer.IsReady()).Should(Equal(testCase.Ready))

					status, err := terminateContainer.GetStatus()
					Expect(err).ShouldNot(HaveOccurred())

					By(fmt.Sprintf("Container '%s': should get the expected 'State'", testContainer.Name))
					Expect(GetContainerState(status.State)).To(Equal(testCase.State))

					By(fmt.Sprintf("Container '%s': should be possible to delete [NodeConformance]", testContainer.Name))
					Expect(terminateContainer.Delete()).To(Succeed())
					Eventually(terminateContainer.Present, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(BeFalse())
				}
			})
		})

		Context("on terminated container", func() {
			rootUser := int64(0)
			nonRootUser := int64(10000)

			// Create and then terminate the container under defined PodPhase to verify if termination message matches the expected output. Lastly delete the created container.
			matchTerminationMessage := func(container v1.Container, expectedPhase v1.PodPhase, expectedMsg gomegatypes.GomegaMatcher) {
				container.Name = "termination-message-container"
				c := ConformanceContainer{
					PodClient:     f.PodClient(),
					Container:     container,
					RestartPolicy: v1.RestartPolicyNever,
				}

				By("create the container")
				c.Create()
				defer c.Delete()

				By(fmt.Sprintf("wait for the container to reach %s", expectedPhase))
				Eventually(c.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(Equal(expectedPhase))

				By("get the container status")
				status, err := c.GetStatus()
				Expect(err).NotTo(HaveOccurred())

				By("the container should be terminated")
				Expect(GetContainerState(status.State)).To(Equal(ContainerStateTerminated))

				By("the termination message should be set")
				framework.Logf("Expected: %v to match Container's Termination Message: %v --", expectedMsg, status.State.Terminated.Message)
				Expect(status.State.Terminated.Message).Should(expectedMsg)

				By("delete the container")
				Expect(c.Delete()).To(Succeed())
			}

			It("should report termination message [LinuxOnly] if TerminationMessagePath is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                  framework.BusyBoxImage,
					Command:                []string{"/bin/sh", "-c"},
					Args:                   []string{"/bin/echo -n DONE > /dev/termination-log"},
					TerminationMessagePath: "/dev/termination-log",
					SecurityContext: &v1.SecurityContext{
						RunAsUser: &rootUser,
					},
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal("DONE"))
			})

			It("should report termination message [LinuxOnly] if TerminationMessagePath is set as non-root user and at a non-default path [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                  framework.BusyBoxImage,
					Command:                []string{"/bin/sh", "-c"},
					Args:                   []string{"/bin/echo -n DONE > /dev/termination-custom-log"},
					TerminationMessagePath: "/dev/termination-custom-log",
					SecurityContext: &v1.SecurityContext{
						RunAsUser: &nonRootUser,
					},
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal("DONE"))
			})

			It("should report termination message [LinuxOnly] from log output if TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                    framework.BusyBoxImage,
					Command:                  []string{"/bin/sh", "-c"},
					Args:                     []string{"/bin/echo -n DONE; /bin/false"},
					TerminationMessagePath:   "/dev/termination-log",
					TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
				}
				matchTerminationMessage(container, v1.PodFailed, Equal("DONE"))
			})

			It("should report termination message [LinuxOnly] as empty when pod succeeds and TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                    framework.BusyBoxImage,
					Command:                  []string{"/bin/sh", "-c"},
					Args:                     []string{"/bin/echo DONE; /bin/true"},
					TerminationMessagePath:   "/dev/termination-log",
					TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal(""))
			})

			It("should report termination message [LinuxOnly] from file when pod succeeds and TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                    framework.BusyBoxImage,
					Command:                  []string{"/bin/sh", "-c"},
					Args:                     []string{"/bin/echo -n OK > /dev/termination-log; /bin/echo DONE; /bin/true"},
					TerminationMessagePath:   "/dev/termination-log",
					TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal("OK"))
			})
		})

		Context("when running a container with a new image", func() {

			// Images used for ConformanceContainer are not added into NodeImageWhiteList, because this test is
			// testing image pulling, these images don't need to be prepulled. The ImagePullPolicy
			// is v1.PullAlways, so it won't be blocked by framework image white list check.
			imagePullTest := func(image string, hasSecret bool, expectedPhase v1.PodPhase, expectedPullStatus bool, windowsImage bool) {
				command := []string{"/bin/sh", "-c", "while true; do sleep 1; done"}
				if windowsImage {
					// -t: Ping the specified host until stopped.
					command = []string{"ping", "-t", "localhost"}
				}
				container := ConformanceContainer{
					PodClient: f.PodClient(),
					Container: v1.Container{
						Name:            "image-pull-test",
						Image:           image,
						Command:         command,
						ImagePullPolicy: v1.PullAlways,
					},
					RestartPolicy: v1.RestartPolicyNever,
				}
				if hasSecret {
					// The service account only has pull permission
					auth := `
{
	"auths": {
		"https://gcr.io": {
			"auth": "<KEY>
			"email": "<EMAIL>"
		}
	}
}`

					secret := &v1.Secret{
						Data: map[string][]byte{v1.DockerConfigJsonKey: []byte(auth)},
						Type: v1.SecretTypeDockerConfigJson,
					}
					secret.Name = "image-pull-secret-" + string(uuid.NewUUID())
					By("create image pull secret")
					_, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(secret)
					Expect(err).NotTo(HaveOccurred())
					defer f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete(secret.Name, nil)
					container.ImagePullSecrets = []string{secret.Name}
				}
				// checkContainerStatus checks whether the container status matches expectation.
				checkContainerStatus := func() error {
					status, err := container.GetStatus()
					if err != nil {
						return fmt.Errorf("failed to get container status: %v", err)
					}
					// We need to check container state first. The default pod status is pending, If we check pod phase first,
					// and the expected pod phase is Pending, the container status may not even show up when we check it.
					// Check container state
					if !expectedPullStatus {
						if status.State.Running == nil {
							return fmt.Errorf("expected container state: Running, got: %q",
								GetContainerState(status.State))
						}
					}
					if expectedPullStatus {
						if status.State.Waiting == nil {
							return fmt.Errorf("expected container state: Waiting, got: %q",
								GetContainerState(status.State))
						}
						reason := status.State.Waiting.Reason
						if reason != images.ErrImagePull.Error() &&
							reason != images.ErrImagePullBackOff.Error() {
							return fmt.Errorf("unexpected waiting reason: %q", reason)
						}
					}
					// Check pod phase
					phase, err := container.GetPhase()
					if err != nil {
						return fmt.Errorf("failed to get pod phase: %v", err)
					}
					if phase != expectedPhase {
						return fmt.Errorf("expected pod phase: %q, got: %q", expectedPhase, phase)
					}
					return nil
				}

				// The image registry is not stable, which sometimes causes the test to fail. Add retry mechanism to make this less flaky.
				const flakeRetry = 3
				for i := 1; i <= flakeRetry; i++ {
					var err error
					By("create the container")
					container.Create()
					By("check the container status")
					for start := time.Now(); time.Since(start) < ContainerStatusRetryTimeout; time.Sleep(ContainerStatusPollInterval) {
						if err = checkContainerStatus(); err == nil {
							break
						}
					}
					By("delete the container")
					container.Delete()
					if err == nil {
						break
					}
					if i < flakeRetry {
						framework.Logf("No.%d attempt failed: %v, retrying...", i, err)
					} else {
						framework.Failf("All %d attempts failed: %v", flakeRetry, err)
					}
				}
			}

			It("should not be able to pull image from invalid registry [NodeConformance]", func() {
				image := "invalid.com/invalid/alpine:3.1"
				imagePullTest(image, false, v1.PodPending, true, false)
			})

			It("should not be able to pull non-existing image from gcr.io [NodeConformance]", func() {
				image := "k8s.gcr.io/invalid-image:invalid-tag"
				imagePullTest(image, false, v1.PodPending, true, false)
			})

			It("should be able to pull image from gcr.io [LinuxOnly] [NodeConformance]", func() {
				image := "gcr.io/google-containers/debian-base:0.4.1"
				imagePullTest(image, false, v1.PodRunning, false, false)
			})

			It("should be able to pull image from gcr.io [NodeConformance]", func() {
				framework.SkipUnlessNodeOSDistroIs("windows")
				image := "gcr.io/kubernetes-e2e-test-images/windows-nanoserver:v1"
				imagePullTest(image, false, v1.PodRunning, false, true)
			})

			It("should be able to pull image from docker hub [LinuxOnly] [NodeConformance]", func() {
				image := "alpine:3.7"
				imagePullTest(image, false, v1.PodRunning, false, false)
			})

			It("should be able to pull image from docker hub [NodeConformance]", func() {
				framework.SkipUnlessNodeOSDistroIs("windows")
				image := "e2eteam/busybox:1.29"
				imagePullTest(image, false, v1.PodRunning, false, true)
			})

			It("should not be able to pull from private registry without secret [NodeConformance]", func() {
				image := "gcr.io/authenticated-image-pulling/alpine:3.7"
				imagePullTest(image, false, v1.PodPending, true, false)
			})

			It("should be able to pull from private registry with secret [LinuxOnly] [NodeConformance]", func() {
				image := "gcr.io/authenticated-image-pulling/alpine:3.7"
				imagePullTest(image, true, v1.PodRunning, false, false)
			})

			It("should be able to pull from private registry with secret [NodeConformance]", func() {
				framework.SkipUnlessNodeOSDistroIs("windows")
				image := "gcr.io/authenticated-image-pulling/windows-nanoserver:v1"
				imagePullTest(image, true, v1.PodRunning, false, true)
			})
		})
	})
})
<commit_msg>tests: Adds TODO for Windows only test<commit_after>/*
Copyright 2016 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 common

import (
	"fmt"
	"path"
	"time"

	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/util/uuid"
	"k8s.io/kubernetes/pkg/kubelet/images"
	"k8s.io/kubernetes/test/e2e/framework"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	gomegatypes "github.com/onsi/gomega/types"
)

var _ = framework.KubeDescribe("Container Runtime", func() {
	f := framework.NewDefaultFramework("container-runtime")

	Describe("blackbox test", func() {
		Context("when starting a container that exits", func() {

			/*
				Release : v1.13
				Testname: Container Runtime, Restart Policy, Pod Phases
				Description: If the restart policy is set to ‘Always’, Pod MUST be restarted when terminated, If restart policy is ‘OnFailure’, Pod MUST be started only if it is terminated with non-zero exit code. If the restart policy is ‘Never’, Pod MUST never be restarted. All these three test cases MUST verify the restart counts accordingly.
			*/
			framework.ConformanceIt("should run with the expected status [NodeConformance]", func() {
				restartCountVolumeName := "restart-count"
				restartCountVolumePath := "/restart-count"
				testContainer := v1.Container{
					Image: framework.BusyBoxImage,
					VolumeMounts: []v1.VolumeMount{
						{
							MountPath: restartCountVolumePath,
							Name:      restartCountVolumeName,
						},
					},
				}
				testVolumes := []v1.Volume{
					{
						Name: restartCountVolumeName,
						VolumeSource: v1.VolumeSource{
							EmptyDir: &v1.EmptyDirVolumeSource{Medium: v1.StorageMediumMemory},
						},
					},
				}
				testCases := []struct {
					Name          string
					RestartPolicy v1.RestartPolicy
					Phase         v1.PodPhase
					State         ContainerState
					RestartCount  int32
					Ready         bool
				}{
					{"terminate-cmd-rpa", v1.RestartPolicyAlways, v1.PodRunning, ContainerStateRunning, 2, true},
					{"terminate-cmd-rpof", v1.RestartPolicyOnFailure, v1.PodSucceeded, ContainerStateTerminated, 1, false},
					{"terminate-cmd-rpn", v1.RestartPolicyNever, v1.PodFailed, ContainerStateTerminated, 0, false},
				}
				for _, testCase := range testCases {

					// It failed at the 1st run, then succeeded at 2nd run, then run forever
					cmdScripts := `
f=%s
count=$(echo 'hello' >> $f ; wc -l $f | awk {'print $1'})
if [ $count -eq 1 ]; then
	exit 1
fi
if [ $count -eq 2 ]; then
	exit 0
fi
while true; do sleep 1; done
`
					tmpCmd := fmt.Sprintf(cmdScripts, path.Join(restartCountVolumePath, "restartCount"))
					testContainer.Name = testCase.Name
					testContainer.Command = []string{"sh", "-c", tmpCmd}
					terminateContainer := ConformanceContainer{
						PodClient:     f.PodClient(),
						Container:     testContainer,
						RestartPolicy: testCase.RestartPolicy,
						Volumes:       testVolumes,
					}
					terminateContainer.Create()
					defer terminateContainer.Delete()

					By(fmt.Sprintf("Container '%s': should get the expected 'RestartCount'", testContainer.Name))
					Eventually(func() (int32, error) {
						status, err := terminateContainer.GetStatus()
						return status.RestartCount, err
					}, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(Equal(testCase.RestartCount))

					By(fmt.Sprintf("Container '%s': should get the expected 'Phase'", testContainer.Name))
					Eventually(terminateContainer.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(Equal(testCase.Phase))

					By(fmt.Sprintf("Container '%s': should get the expected 'Ready' condition", testContainer.Name))
					Expect(terminateContainer.IsReady()).Should(Equal(testCase.Ready))

					status, err := terminateContainer.GetStatus()
					Expect(err).ShouldNot(HaveOccurred())

					By(fmt.Sprintf("Container '%s': should get the expected 'State'", testContainer.Name))
					Expect(GetContainerState(status.State)).To(Equal(testCase.State))

					By(fmt.Sprintf("Container '%s': should be possible to delete [NodeConformance]", testContainer.Name))
					Expect(terminateContainer.Delete()).To(Succeed())
					Eventually(terminateContainer.Present, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(BeFalse())
				}
			})
		})

		Context("on terminated container", func() {
			rootUser := int64(0)
			nonRootUser := int64(10000)

			// Create and then terminate the container under defined PodPhase to verify if termination message matches the expected output. Lastly delete the created container.
			matchTerminationMessage := func(container v1.Container, expectedPhase v1.PodPhase, expectedMsg gomegatypes.GomegaMatcher) {
				container.Name = "termination-message-container"
				c := ConformanceContainer{
					PodClient:     f.PodClient(),
					Container:     container,
					RestartPolicy: v1.RestartPolicyNever,
				}

				By("create the container")
				c.Create()
				defer c.Delete()

				By(fmt.Sprintf("wait for the container to reach %s", expectedPhase))
				Eventually(c.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(Equal(expectedPhase))

				By("get the container status")
				status, err := c.GetStatus()
				Expect(err).NotTo(HaveOccurred())

				By("the container should be terminated")
				Expect(GetContainerState(status.State)).To(Equal(ContainerStateTerminated))

				By("the termination message should be set")
				framework.Logf("Expected: %v to match Container's Termination Message: %v --", expectedMsg, status.State.Terminated.Message)
				Expect(status.State.Terminated.Message).Should(expectedMsg)

				By("delete the container")
				Expect(c.Delete()).To(Succeed())
			}

			It("should report termination message [LinuxOnly] if TerminationMessagePath is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                  framework.BusyBoxImage,
					Command:                []string{"/bin/sh", "-c"},
					Args:                   []string{"/bin/echo -n DONE > /dev/termination-log"},
					TerminationMessagePath: "/dev/termination-log",
					SecurityContext: &v1.SecurityContext{
						RunAsUser: &rootUser,
					},
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal("DONE"))
			})

			It("should report termination message [LinuxOnly] if TerminationMessagePath is set as non-root user and at a non-default path [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                  framework.BusyBoxImage,
					Command:                []string{"/bin/sh", "-c"},
					Args:                   []string{"/bin/echo -n DONE > /dev/termination-custom-log"},
					TerminationMessagePath: "/dev/termination-custom-log",
					SecurityContext: &v1.SecurityContext{
						RunAsUser: &nonRootUser,
					},
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal("DONE"))
			})

			It("should report termination message [LinuxOnly] from log output if TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                    framework.BusyBoxImage,
					Command:                  []string{"/bin/sh", "-c"},
					Args:                     []string{"/bin/echo -n DONE; /bin/false"},
					TerminationMessagePath:   "/dev/termination-log",
					TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
				}
				matchTerminationMessage(container, v1.PodFailed, Equal("DONE"))
			})

			It("should report termination message [LinuxOnly] as empty when pod succeeds and TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                    framework.BusyBoxImage,
					Command:                  []string{"/bin/sh", "-c"},
					Args:                     []string{"/bin/echo DONE; /bin/true"},
					TerminationMessagePath:   "/dev/termination-log",
					TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal(""))
			})

			It("should report termination message [LinuxOnly] from file when pod succeeds and TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
				// Cannot mount files in Windows Containers.
				container := v1.Container{
					Image:                    framework.BusyBoxImage,
					Command:                  []string{"/bin/sh", "-c"},
					Args:                     []string{"/bin/echo -n OK > /dev/termination-log; /bin/echo DONE; /bin/true"},
					TerminationMessagePath:   "/dev/termination-log",
					TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
				}
				matchTerminationMessage(container, v1.PodSucceeded, Equal("OK"))
			})
		})

		Context("when running a container with a new image", func() {

			// Images used for ConformanceContainer are not added into NodeImageWhiteList, because this test is
			// testing image pulling, these images don't need to be prepulled. The ImagePullPolicy
			// is v1.PullAlways, so it won't be blocked by framework image white list check.
			imagePullTest := func(image string, hasSecret bool, expectedPhase v1.PodPhase, expectedPullStatus bool, windowsImage bool) {
				command := []string{"/bin/sh", "-c", "while true; do sleep 1; done"}
				if windowsImage {
					// -t: Ping the specified host until stopped.
					command = []string{"ping", "-t", "localhost"}
				}
				container := ConformanceContainer{
					PodClient: f.PodClient(),
					Container: v1.Container{
						Name:            "image-pull-test",
						Image:           image,
						Command:         command,
						ImagePullPolicy: v1.PullAlways,
					},
					RestartPolicy: v1.RestartPolicyNever,
				}
				if hasSecret {
					// The service account only has pull permission
					auth := `
{
	"auths": {
		"https://gcr.io": {
			"auth": "<KEY>
			"email": "<EMAIL>"
		}
	}
}`

					secret := &v1.Secret{
						Data: map[string][]byte{v1.DockerConfigJsonKey: []byte(auth)},
						Type: v1.SecretTypeDockerConfigJson,
					}
					secret.Name = "image-pull-secret-" + string(uuid.NewUUID())
					By("create image pull secret")
					_, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(secret)
					Expect(err).NotTo(HaveOccurred())
					defer f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete(secret.Name, nil)
					container.ImagePullSecrets = []string{secret.Name}
				}
				// checkContainerStatus checks whether the container status matches expectation.
				checkContainerStatus := func() error {
					status, err := container.GetStatus()
					if err != nil {
						return fmt.Errorf("failed to get container status: %v", err)
					}
					// We need to check container state first. The default pod status is pending, If we check pod phase first,
					// and the expected pod phase is Pending, the container status may not even show up when we check it.
					// Check container state
					if !expectedPullStatus {
						if status.State.Running == nil {
							return fmt.Errorf("expected container state: Running, got: %q",
								GetContainerState(status.State))
						}
					}
					if expectedPullStatus {
						if status.State.Waiting == nil {
							return fmt.Errorf("expected container state: Waiting, got: %q",
								GetContainerState(status.State))
						}
						reason := status.State.Waiting.Reason
						if reason != images.ErrImagePull.Error() &&
							reason != images.ErrImagePullBackOff.Error() {
							return fmt.Errorf("unexpected waiting reason: %q", reason)
						}
					}
					// Check pod phase
					phase, err := container.GetPhase()
					if err != nil {
						return fmt.Errorf("failed to get pod phase: %v", err)
					}
					if phase != expectedPhase {
						return fmt.Errorf("expected pod phase: %q, got: %q", expectedPhase, phase)
					}
					return nil
				}

				// The image registry is not stable, which sometimes causes the test to fail. Add retry mechanism to make this less flaky.
				const flakeRetry = 3
				for i := 1; i <= flakeRetry; i++ {
					var err error
					By("create the container")
					container.Create()
					By("check the container status")
					for start := time.Now(); time.Since(start) < ContainerStatusRetryTimeout; time.Sleep(ContainerStatusPollInterval) {
						if err = checkContainerStatus(); err == nil {
							break
						}
					}
					By("delete the container")
					container.Delete()
					if err == nil {
						break
					}
					if i < flakeRetry {
						framework.Logf("No.%d attempt failed: %v, retrying...", i, err)
					} else {
						framework.Failf("All %d attempts failed: %v", flakeRetry, err)
					}
				}
			}

			It("should not be able to pull image from invalid registry [NodeConformance]", func() {
				image := "invalid.com/invalid/alpine:3.1"
				imagePullTest(image, false, v1.PodPending, true, false)
			})

			It("should not be able to pull non-existing image from gcr.io [NodeConformance]", func() {
				image := "k8s.gcr.io/invalid-image:invalid-tag"
				imagePullTest(image, false, v1.PodPending, true, false)
			})

			It("should be able to pull image from gcr.io [LinuxOnly] [NodeConformance]", func() {
				image := "gcr.io/google-containers/debian-base:0.4.1"
				imagePullTest(image, false, v1.PodRunning, false, false)
			})

			It("should be able to pull image from gcr.io [NodeConformance]", func() {
				framework.SkipUnlessNodeOSDistroIs("windows")
				image := "gcr.io/kubernetes-e2e-test-images/windows-nanoserver:v1"
				imagePullTest(image, false, v1.PodRunning, false, true)
			})

			It("should be able to pull image from docker hub [LinuxOnly] [NodeConformance]", func() {
				image := "alpine:3.7"
				imagePullTest(image, false, v1.PodRunning, false, false)
			})

			It("should be able to pull image from docker hub [NodeConformance]", func() {
				framework.SkipUnlessNodeOSDistroIs("windows")
				// TODO(claudiub): Switch to nanoserver image manifest list.
				image := "e2eteam/busybox:1.29"
				imagePullTest(image, false, v1.PodRunning, false, true)
			})

			It("should not be able to pull from private registry without secret [NodeConformance]", func() {
				image := "gcr.io/authenticated-image-pulling/alpine:3.7"
				imagePullTest(image, false, v1.PodPending, true, false)
			})

			It("should be able to pull from private registry with secret [LinuxOnly] [NodeConformance]", func() {
				image := "gcr.io/authenticated-image-pulling/alpine:3.7"
				imagePullTest(image, true, v1.PodRunning, false, false)
			})

			It("should be able to pull from private registry with secret [NodeConformance]", func() {
				framework.SkipUnlessNodeOSDistroIs("windows")
				image := "gcr.io/authenticated-image-pulling/windows-nanoserver:v1"
				imagePullTest(image, true, v1.PodRunning, false, true)
			})
		})
	})
})

{'fraction_non_alphanumeric': 0.11741931285788652, 'fraction_numerical': 0.007483081728266528, 'mean_word_length': 3.236080485115766, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4523580', 'n_tokens_mistral': 11294, 'n_tokens_neox': 8856, 'n_words': 2447}