|
| 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 TestAdditionalGids(t *testing.T) { |
| 32 | + testPodLogDir, err := ioutil.TempDir("/tmp", "additional-gids") |
| 33 | + require.NoError(t, err) |
| 34 | + defer os.RemoveAll(testPodLogDir) |
| 35 | + |
| 36 | + t.Log("Create a sandbox with log directory") |
| 37 | + sbConfig := PodSandboxConfig("sandbox", "additional-gids", |
| 38 | + WithPodLogDirectory(testPodLogDir)) |
| 39 | + sb, err := runtimeService.RunPodSandbox(sbConfig) |
| 40 | + require.NoError(t, err) |
| 41 | + defer func() { |
| 42 | + assert.NoError(t, runtimeService.StopPodSandbox(sb)) |
| 43 | + assert.NoError(t, runtimeService.RemovePodSandbox(sb)) |
| 44 | + }() |
| 45 | + |
| 46 | + const ( |
| 47 | + testImage = "busybox" |
| 48 | + containerName = "test-container" |
| 49 | + ) |
| 50 | + t.Logf("Pull test image %q", testImage) |
| 51 | + img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil) |
| 52 | + require.NoError(t, err) |
| 53 | + defer func() { |
| 54 | + assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img})) |
| 55 | + }() |
| 56 | + |
| 57 | + t.Log("Create a container to print id") |
| 58 | + cnConfig := ContainerConfig( |
| 59 | + containerName, |
| 60 | + "busybox", |
| 61 | + WithCommand("id"), |
| 62 | + WithLogPath(containerName), |
| 63 | + WithSupplementalGroups([]int64{1 /*daemon*/, 1234 /*new group*/}), |
| 64 | + ) |
| 65 | + cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + t.Log("Start the container") |
| 69 | + require.NoError(t, runtimeService.StartContainer(cn)) |
| 70 | + |
| 71 | + t.Log("Wait for container to finish running") |
| 72 | + require.NoError(t, Eventually(func() (bool, error) { |
| 73 | + s, err := runtimeService.ContainerStatus(cn) |
| 74 | + if err != nil { |
| 75 | + return false, err |
| 76 | + } |
| 77 | + if s.GetState() == runtime.ContainerState_CONTAINER_EXITED { |
| 78 | + return true, nil |
| 79 | + } |
| 80 | + return false, nil |
| 81 | + }, time.Second, 30*time.Second)) |
| 82 | + |
| 83 | + t.Log("Search additional groups in container log") |
| 84 | + content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName)) |
| 85 | + assert.NoError(t, err) |
| 86 | + assert.Contains(t, string(content), "groups=1(daemon),10(wheel),1234") |
| 87 | +} |
0 commit comments