|
| 1 | +/* |
| 2 | +Copyright 2018 The containerd 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 integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" |
| 29 | +) |
| 30 | + |
| 31 | +func TestPodHostnameEnv(t *testing.T) { |
| 32 | + hostname, err := os.Hostname() |
| 33 | + require.NoError(t, err) |
| 34 | + for name, test := range map[string]struct { |
| 35 | + opts []PodSandboxOpts |
| 36 | + expectedHostname string |
| 37 | + }{ |
| 38 | + "regular pod with custom hostname": { |
| 39 | + opts: []PodSandboxOpts{ |
| 40 | + WithPodHostname("test-hostname"), |
| 41 | + }, |
| 42 | + expectedHostname: "test-hostname", |
| 43 | + }, |
| 44 | + "host network pod without custom hostname": { |
| 45 | + opts: []PodSandboxOpts{ |
| 46 | + WithHostNetwork, |
| 47 | + }, |
| 48 | + expectedHostname: hostname, |
| 49 | + }, |
| 50 | + "host network pod with custom hostname": { |
| 51 | + opts: []PodSandboxOpts{ |
| 52 | + WithHostNetwork, |
| 53 | + WithPodHostname("test-hostname"), |
| 54 | + }, |
| 55 | + expectedHostname: "test-hostname", |
| 56 | + }, |
| 57 | + } { |
| 58 | + t.Run(name, func(t *testing.T) { |
| 59 | + testPodLogDir, err := ioutil.TempDir("/tmp", "hostname-env") |
| 60 | + require.NoError(t, err) |
| 61 | + defer os.RemoveAll(testPodLogDir) |
| 62 | + |
| 63 | + opts := append(test.opts, WithPodLogDirectory(testPodLogDir)) |
| 64 | + t.Log("Create a sandbox with hostname") |
| 65 | + sbConfig := PodSandboxConfig("sandbox", "hostname-env", opts...) |
| 66 | + sb, err := runtimeService.RunPodSandbox(sbConfig) |
| 67 | + require.NoError(t, err) |
| 68 | + defer func() { |
| 69 | + assert.NoError(t, runtimeService.StopPodSandbox(sb)) |
| 70 | + assert.NoError(t, runtimeService.RemovePodSandbox(sb)) |
| 71 | + }() |
| 72 | + |
| 73 | + const ( |
| 74 | + testImage = "busybox" |
| 75 | + containerName = "test-container" |
| 76 | + ) |
| 77 | + t.Logf("Pull test image %q", testImage) |
| 78 | + img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil) |
| 79 | + require.NoError(t, err) |
| 80 | + defer func() { |
| 81 | + assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img})) |
| 82 | + }() |
| 83 | + |
| 84 | + t.Log("Create a container to print env") |
| 85 | + cnConfig := ContainerConfig( |
| 86 | + containerName, |
| 87 | + "busybox", |
| 88 | + WithCommand("env"), |
| 89 | + WithLogPath(containerName), |
| 90 | + ) |
| 91 | + cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + t.Log("Start the container") |
| 95 | + require.NoError(t, runtimeService.StartContainer(cn)) |
| 96 | + |
| 97 | + t.Log("Wait for container to finish running") |
| 98 | + require.NoError(t, Eventually(func() (bool, error) { |
| 99 | + s, err := runtimeService.ContainerStatus(cn) |
| 100 | + if err != nil { |
| 101 | + return false, err |
| 102 | + } |
| 103 | + if s.GetState() == runtime.ContainerState_CONTAINER_EXITED { |
| 104 | + return true, nil |
| 105 | + } |
| 106 | + return false, nil |
| 107 | + }, time.Second, 30*time.Second)) |
| 108 | + |
| 109 | + t.Log("Search hostname env in container log") |
| 110 | + content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName)) |
| 111 | + assert.NoError(t, err) |
| 112 | + assert.Contains(t, string(content), "HOSTNAME="+test.expectedHostname) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments