Skip to content

Commit b45e302

Browse files
junnplusAkihiroSuda
authored andcommitted
add WithAdditionalGIDs test
Signed-off-by: Ye Sijun <[email protected]> (cherry picked from commit 72b87ad) Signed-off-by: Akihiro Suda <[email protected]>
1 parent 0a06c28 commit b45e302

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

oci/spec_opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ func WithUsername(username string) SpecOpts {
733733
}
734734

735735
// WithAdditionalGIDs sets the OCI spec's additionalGids array to any additional groups listed
736-
// for a particular user in the /etc/groups file of the image's root filesystem
736+
// for a particular user in the /etc/group file of the image's root filesystem
737737
// The passed in user can be either a uid or a username.
738738
func WithAdditionalGIDs(userstr string) SpecOpts {
739739
return func(ctx context.Context, client Client, c *containers.Container, s *Spec) (err error) {

oci/spec_opts_linux_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,71 @@ import (
3030
"golang.org/x/sys/unix"
3131
)
3232

33+
// nolint:gosec
34+
func TestWithAdditionalGIDs(t *testing.T) {
35+
t.Parallel()
36+
expectedPasswd := `root:x:0:0:root:/root:/bin/ash
37+
bin:x:1:1:bin:/bin:/sbin/nologin
38+
daemon:x:2:2:daemon:/sbin:/sbin/nologin
39+
`
40+
expectedGroup := `root:x:0:root
41+
bin:x:1:root,bin,daemon
42+
daemon:x:2:root,bin,daemon
43+
sys:x:3:root,bin,adm
44+
`
45+
td := t.TempDir()
46+
apply := fstest.Apply(
47+
fstest.CreateDir("/etc", 0777),
48+
fstest.CreateFile("/etc/passwd", []byte(expectedPasswd), 0777),
49+
fstest.CreateFile("/etc/group", []byte(expectedGroup), 0777),
50+
)
51+
if err := apply.Apply(td); err != nil {
52+
t.Fatalf("failed to apply: %v", err)
53+
}
54+
c := containers.Container{ID: t.Name()}
55+
56+
testCases := []struct {
57+
name string
58+
user string
59+
expected []uint32
60+
}{
61+
{
62+
user: "root",
63+
expected: []uint32{},
64+
},
65+
{
66+
user: "1000",
67+
expected: []uint32{},
68+
},
69+
{
70+
user: "bin",
71+
expected: []uint32{2, 3},
72+
},
73+
{
74+
user: "bin:root",
75+
expected: []uint32{},
76+
},
77+
{
78+
user: "daemon",
79+
expected: []uint32{1},
80+
},
81+
}
82+
for _, testCase := range testCases {
83+
t.Run(testCase.user, func(t *testing.T) {
84+
t.Parallel()
85+
s := Spec{
86+
Version: specs.Version,
87+
Root: &specs.Root{
88+
Path: td,
89+
},
90+
}
91+
err := WithAdditionalGIDs(testCase.user)(context.Background(), nil, &c, &s)
92+
assert.NoError(t, err)
93+
assert.Equal(t, testCase.expected, s.Process.User.AdditionalGids)
94+
})
95+
}
96+
}
97+
3398
func TestAddCaps(t *testing.T) {
3499
t.Parallel()
35100

0 commit comments

Comments
 (0)