@@ -22,8 +22,11 @@ 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
@@ -247,3 +250,76 @@ func TestGetDevices(t *testing.T) {
247250 })
248251 })
249252}
253+
254+ func TestWithAppendAdditionalGroups (t * testing.T ) {
255+ t .Parallel ()
256+ expectedContent := `root:x:0:root
257+ bin:x:1:root,bin,daemon
258+ daemon:x:2:root,bin,daemon
259+ `
260+ td := t .TempDir ()
261+ apply := fstest .Apply (
262+ fstest .CreateDir ("/etc" , 0777 ),
263+ fstest .CreateFile ("/etc/group" , []byte (expectedContent ), 0777 ),
264+ )
265+ if err := apply .Apply (td ); err != nil {
266+ t .Fatalf ("failed to apply: %v" , err )
267+ }
268+ c := containers.Container {ID : t .Name ()}
269+
270+ testCases := []struct {
271+ name string
272+ additionalGIDs []uint32
273+ groups []string
274+ expected []uint32
275+ err string
276+ }{
277+ {
278+ name : "no additional gids" ,
279+ groups : []string {},
280+ },
281+ {
282+ name : "no additional gids, append root gid" ,
283+ groups : []string {"root" },
284+ expected : []uint32 {0 },
285+ },
286+ {
287+ name : "no additional gids, append bin and daemon gids" ,
288+ groups : []string {"bin" , "daemon" },
289+ expected : []uint32 {1 , 2 },
290+ },
291+ {
292+ name : "has root additional gids, append bin and daemon gids" ,
293+ additionalGIDs : []uint32 {0 },
294+ groups : []string {"bin" , "daemon" },
295+ expected : []uint32 {0 , 1 , 2 },
296+ },
297+ {
298+ name : "unknown group" ,
299+ groups : []string {"unknown" },
300+ err : "unable to find group unknown" ,
301+ },
302+ }
303+
304+ for _ , testCase := range testCases {
305+ t .Run (testCase .name , func (t * testing.T ) {
306+ t .Parallel ()
307+ s := Spec {
308+ Version : specs .Version ,
309+ Root : & specs.Root {
310+ Path : td ,
311+ },
312+ Process : & specs.Process {
313+ User : specs.User {
314+ AdditionalGids : testCase .additionalGIDs ,
315+ },
316+ },
317+ }
318+ err := WithAppendAdditionalGroups (testCase .groups ... )(context .Background (), nil , & c , & s )
319+ if err != nil {
320+ assert .EqualError (t , err , testCase .err )
321+ }
322+ assert .Equal (t , testCase .expected , s .Process .User .AdditionalGids )
323+ })
324+ }
325+ }
0 commit comments