@@ -22,11 +22,79 @@ import (
2222 "path/filepath"
2323 "testing"
2424
25+ "github.com/containerd/containerd/containers"
2526 "github.com/containerd/containerd/pkg/testutil"
27+ "github.com/containerd/continuity/fs/fstest"
2628 specs "github.com/opencontainers/runtime-spec/specs-go"
29+ "github.com/stretchr/testify/assert"
2730 "golang.org/x/sys/unix"
2831)
2932
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+
3098func TestAddCaps (t * testing.T ) {
3199 t .Parallel ()
32100
0 commit comments