k8s.io/kubernetes@v1.29.3/test/e2e/common/node/containers.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package node
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/onsi/gomega"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/util/uuid"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    28  	e2epodoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
    29  	admissionapi "k8s.io/pod-security-admission/api"
    30  )
    31  
    32  var _ = SIGDescribe("Containers", func() {
    33  	f := framework.NewDefaultFramework("containers")
    34  	f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
    35  
    36  	/*
    37  		Release: v1.9
    38  		Testname: Containers, without command and arguments
    39  		Description: Default command and arguments from the container image entrypoint MUST be used when Pod does not specify the container command
    40  	*/
    41  	framework.ConformanceIt("should use the image defaults if command and args are blank", f.WithNodeConformance(), func(ctx context.Context) {
    42  		pod := entrypointTestPod(f.Namespace.Name)
    43  		pod.Spec.Containers[0].Args = nil
    44  		pod = e2epod.NewPodClient(f).Create(ctx, pod)
    45  		err := e2epod.WaitForPodNameRunningInNamespace(ctx, f.ClientSet, pod.Name, f.Namespace.Name)
    46  		framework.ExpectNoError(err, "Expected pod %q to be running, got error: %v", pod.Name, err)
    47  		pollLogs := func() (string, error) {
    48  			return e2epod.GetPodLogs(ctx, f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name)
    49  		}
    50  
    51  		// The agnhost's image default entrypoint / args are: "/agnhost pause"
    52  		// which will print out "Paused".
    53  		gomega.Eventually(ctx, pollLogs, 3, framework.Poll).Should(gomega.ContainSubstring("Paused"))
    54  	})
    55  
    56  	/*
    57  		Release: v1.9
    58  		Testname: Containers, with arguments
    59  		Description: Default command and  from the container image entrypoint MUST be used when Pod does not specify the container command but the arguments from Pod spec MUST override when specified.
    60  	*/
    61  	framework.ConformanceIt("should be able to override the image's default arguments (container cmd)", f.WithNodeConformance(), func(ctx context.Context) {
    62  		pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments")
    63  		e2epodoutput.TestContainerOutput(ctx, f, "override arguments", pod, 0, []string{
    64  			"[/agnhost entrypoint-tester override arguments]",
    65  		})
    66  	})
    67  
    68  	// Note: when you override the entrypoint, the image's arguments (container cmd)
    69  	// are ignored.
    70  	/*
    71  		Release: v1.9
    72  		Testname: Containers, with command
    73  		Description: Default command from the container image entrypoint MUST NOT be used when Pod specifies the container command.  Command from Pod spec MUST override the command in the image.
    74  	*/
    75  	framework.ConformanceIt("should be able to override the image's default command (container entrypoint)", f.WithNodeConformance(), func(ctx context.Context) {
    76  		pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester")
    77  		pod.Spec.Containers[0].Command = []string{"/agnhost-2"}
    78  
    79  		e2epodoutput.TestContainerOutput(ctx, f, "override command", pod, 0, []string{
    80  			"[/agnhost-2 entrypoint-tester]",
    81  		})
    82  	})
    83  
    84  	/*
    85  		Release: v1.9
    86  		Testname: Containers, with command and arguments
    87  		Description: Default command and arguments from the container image entrypoint MUST NOT be used when Pod specifies the container command and arguments.  Command and arguments from Pod spec MUST override the command and arguments in the image.
    88  	*/
    89  	framework.ConformanceIt("should be able to override the image's default command and arguments", f.WithNodeConformance(), func(ctx context.Context) {
    90  		pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments")
    91  		pod.Spec.Containers[0].Command = []string{"/agnhost-2"}
    92  
    93  		e2epodoutput.TestContainerOutput(ctx, f, "override all", pod, 0, []string{
    94  			"[/agnhost-2 entrypoint-tester override arguments]",
    95  		})
    96  	})
    97  })
    98  
    99  // Return a prototypical entrypoint test pod
   100  func entrypointTestPod(namespace string, entrypointArgs ...string) *v1.Pod {
   101  	podName := "client-containers-" + string(uuid.NewUUID())
   102  	pod := e2epod.NewAgnhostPod(namespace, podName, nil, nil, nil, entrypointArgs...)
   103  
   104  	one := int64(1)
   105  	pod.Spec.TerminationGracePeriodSeconds = &one
   106  	pod.Spec.RestartPolicy = v1.RestartPolicyNever
   107  	return pod
   108  }