|
| 1 | +//go:build windows |
| 2 | +// +build windows |
| 3 | + |
| 4 | +/* |
| 5 | + Copyright The containerd Authors. |
| 6 | +
|
| 7 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + you may not use this file except in compliance with the License. |
| 9 | + You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | + Unless required by applicable law or agreed to in writing, software |
| 14 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + See the License for the specific language governing permissions and |
| 17 | + limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package integration |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "os" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + defaultCommand = WithCommand("Powershell", "/c", "$env:CONTAINER_SANDBOX_MOUNT_POINT/pause.exe") |
| 33 | + localServiceUsername = WithWindowsUsername("NT AUTHORITY\\Local service") |
| 34 | + localSystemUsername = WithWindowsUsername("NT AUTHORITY\\System") |
| 35 | +) |
| 36 | + |
| 37 | +// Tests to verify the Windows HostProcess |
| 38 | +func TestWindowsHostProcess(t *testing.T) { |
| 39 | + EnsureImageExists(t, pauseImage) |
| 40 | + |
| 41 | + t.Run("run as Local Service", func(t *testing.T) { |
| 42 | + runHostProcess(t, false, pauseImage, localServiceUsername, defaultCommand) |
| 43 | + }) |
| 44 | + t.Run("run as Local System", func(t *testing.T) { |
| 45 | + runHostProcess(t, false, pauseImage, localSystemUsername, defaultCommand) |
| 46 | + }) |
| 47 | + t.Run("run as unacceptable user", func(t *testing.T) { |
| 48 | + runHostProcess(t, true, pauseImage, WithWindowsUsername("Guest"), defaultCommand) |
| 49 | + }) |
| 50 | + t.Run("run command on host", func(t *testing.T) { |
| 51 | + cmd := WithCommand("Powershell", "/c", "Get-Command containerd.exe") |
| 52 | + runHostProcess(t, false, pauseImage, localServiceUsername, cmd) |
| 53 | + }) |
| 54 | + t.Run("run withHostNetwork", func(t *testing.T) { |
| 55 | + hostname, err := os.Hostname() |
| 56 | + require.NoError(t, err) |
| 57 | + cmd := WithCommand("Powershell", "/c", fmt.Sprintf("if ($env:COMPUTERNAME -ne %s) { exit -1 }", hostname)) |
| 58 | + runHostProcess(t, false, pauseImage, localServiceUsername, cmd) |
| 59 | + }) |
| 60 | + t.Run("run with a different os.version image", func(t *testing.T) { |
| 61 | + image := "docker.io/e2eteam/busybox:1.29-windows-amd64-1909" |
| 62 | + EnsureImageExists(t, image) |
| 63 | + runHostProcess(t, false, image, localServiceUsername, defaultCommand) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func runHostProcess(t *testing.T, expectErr bool, image string, opts ...ContainerOpts) { |
| 68 | + t.Logf("Create a pod config and run sandbox container") |
| 69 | + sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox1", "hostprocess", WithWindowsHostProcess) |
| 70 | + |
| 71 | + t.Logf("Create a container config and run container in a pod") |
| 72 | + containerConfig := ContainerConfig( |
| 73 | + "container1", |
| 74 | + image, |
| 75 | + opts..., |
| 76 | + ) |
| 77 | + cn, err := runtimeService.CreateContainer(sb, containerConfig, sbConfig) |
| 78 | + require.NoError(t, err) |
| 79 | + defer func() { |
| 80 | + assert.NoError(t, runtimeService.RemoveContainer(cn)) |
| 81 | + }() |
| 82 | + _, err = t, runtimeService.StartContainer(cn) |
| 83 | + if err != nil { |
| 84 | + if !expectErr { |
| 85 | + t.Fatalf("Unexpected error while starting Container: %v", err) |
| 86 | + } |
| 87 | + return |
| 88 | + } |
| 89 | + defer func() { |
| 90 | + assert.NoError(t, runtimeService.StopContainer(cn, 10)) |
| 91 | + }() |
| 92 | +} |
0 commit comments