1/*
2Copyright 2021 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package windows
18
19import (
20	"context"
21	"time"
22
23	"github.com/onsi/ginkgo"
24	v1 "k8s.io/api/core/v1"
25	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26	"k8s.io/kubernetes/pkg/features"
27	"k8s.io/kubernetes/test/e2e/framework"
28	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
29	imageutils "k8s.io/kubernetes/test/utils/image"
30)
31
32var _ = SIGDescribe("[Feature:WindowsHostProcessContainers] [Excluded:WindowsDocker] [MinimumKubeletVersion:1.22] HostProcess containers", func() {
33	ginkgo.BeforeEach(func() {
34		e2eskipper.SkipUnlessNodeOSDistroIs("windows")
35		SkipUnlessWindowsHostProcessContainersEnabled()
36	})
37
38	f := framework.NewDefaultFramework("host-process-test-windows")
39
40	ginkgo.It("should run as a process on the host/node", func() {
41
42		ginkgo.By("selecting a Windows node")
43		targetNode, err := findWindowsNode(f)
44		framework.ExpectNoError(err, "Error finding Windows node")
45		framework.Logf("Using node: %v", targetNode.Name)
46
47		ginkgo.By("scheduling a pod with a container that verifies %COMPUTERNAME% matches selected node name")
48		image := imageutils.GetConfig(imageutils.BusyBox)
49
50		trueVar := true
51		podName := "host-process-test-pod"
52		user := "NT AUTHORITY\\Local service"
53		pod := &v1.Pod{
54			ObjectMeta: metav1.ObjectMeta{
55				Name: podName,
56			},
57			Spec: v1.PodSpec{
58				SecurityContext: &v1.PodSecurityContext{
59					WindowsOptions: &v1.WindowsSecurityContextOptions{
60						HostProcess:   &trueVar,
61						RunAsUserName: &user,
62					},
63				},
64				HostNetwork: true,
65				Containers: []v1.Container{
66					{
67						Image:   image.GetE2EImage(),
68						Name:    "computer-name-test",
69						Command: []string{"cmd.exe", "/K", "IF", "NOT", "%COMPUTERNAME%", "==", targetNode.Name, "(", "exit", "-1", ")"},
70					},
71				},
72				RestartPolicy: v1.RestartPolicyNever,
73				NodeName:      targetNode.Name,
74			},
75		}
76
77		f.PodClient().Create(pod)
78
79		ginkgo.By("Waiting for pod to run")
80		f.PodClient().WaitForFinish(podName, 3*time.Minute)
81
82		ginkgo.By("Then ensuring pod finished running successfully")
83		p, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(
84			context.TODO(),
85			podName,
86			metav1.GetOptions{})
87
88		framework.ExpectNoError(err, "Error retrieving pod")
89		framework.ExpectEqual(p.Status.Phase, v1.PodSucceeded)
90	})
91})
92
93func SkipUnlessWindowsHostProcessContainersEnabled() {
94	if !framework.TestContext.FeatureGates[string(features.WindowsHostProcessContainers)] {
95		e2eskipper.Skipf("Skipping test because feature 'WindowsHostProcessContainers' is not enabled")
96	}
97}
98