Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 51ee6ea

Browse files
committed
Add integration test
Signed-off-by: Lantao Liu <[email protected]>
1 parent ca3b806 commit 51ee6ea

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

integration/addition_gids_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

integration/test_utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ func WithLogPath(path string) ContainerOpts {
202202
}
203203
}
204204

205+
// WithSupplementalGroups adds supplemental groups.
206+
func WithSupplementalGroups(gids []int64) ContainerOpts {
207+
return func(c *runtime.ContainerConfig) {
208+
if c.Linux == nil {
209+
c.Linux = &runtime.LinuxContainerConfig{}
210+
}
211+
if c.Linux.SecurityContext == nil {
212+
c.Linux.SecurityContext = &runtime.LinuxContainerSecurityContext{}
213+
}
214+
c.Linux.SecurityContext.SupplementalGroups = gids
215+
}
216+
}
217+
205218
// ContainerConfig creates a container config given a name and image name
206219
// and additional container config options
207220
func ContainerConfig(name, image string, opts ...ContainerOpts) *runtime.ContainerConfig {

0 commit comments

Comments
 (0)