Skip to content

Commit 6a7b761

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 832bcf3 commit 6a7b761

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
@@ -720,7 +720,7 @@ func WithUsername(username string) SpecOpts {
720720
}
721721

722722
// WithAdditionalGIDs sets the OCI spec's additionalGids array to any additional groups listed
723-
// for a particular user in the /etc/groups file of the image's root filesystem
723+
// for a particular user in the /etc/group file of the image's root filesystem
724724
// The passed in user can be either a uid or a username.
725725
func WithAdditionalGIDs(userstr string) SpecOpts {
726726
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
@@ -31,6 +31,71 @@ import (
3131
"golang.org/x/sys/unix"
3232
)
3333

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

0 commit comments

Comments
 (0)