1919package integration
2020
2121import (
22+ "fmt"
2223 "os"
2324 "path/filepath"
2425 "testing"
@@ -32,47 +33,98 @@ import (
3233)
3334
3435func TestAdditionalGids (t * testing.T ) {
35- testPodLogDir := t .TempDir ()
36-
37- t .Log ("Create a sandbox with log directory" )
38- sb , sbConfig := PodSandboxConfigWithCleanup (t , "sandbox" , "additional-gids" ,
39- WithPodLogDirectory (testPodLogDir ))
40-
41- var (
42- testImage = images .Get (images .BusyBox )
43- containerName = "test-container"
44- )
45-
36+ testImage := images .Get (images .BusyBox )
4637 EnsureImageExists (t , testImage )
47-
48- t .Log ("Create a container to print id" )
49- cnConfig := ContainerConfig (
50- containerName ,
51- testImage ,
52- WithCommand ("id" ),
53- WithLogPath (containerName ),
54- WithSupplementalGroups ([]int64 {1 /*daemon*/ , 1234 /*new group*/ }),
55- )
56- cn , err := runtimeService .CreateContainer (sb , cnConfig , sbConfig )
57- require .NoError (t , err )
58-
59- t .Log ("Start the container" )
60- require .NoError (t , runtimeService .StartContainer (cn ))
61-
62- t .Log ("Wait for container to finish running" )
63- require .NoError (t , Eventually (func () (bool , error ) {
64- s , err := runtimeService .ContainerStatus (cn )
65- if err != nil {
66- return false , err
67- }
68- if s .GetState () == runtime .ContainerState_CONTAINER_EXITED {
69- return true , nil
70- }
71- return false , nil
72- }, time .Second , 30 * time .Second ))
73-
74- t .Log ("Search additional groups in container log" )
75- content , err := os .ReadFile (filepath .Join (testPodLogDir , containerName ))
76- assert .NoError (t , err )
77- assert .Contains (t , string (content ), "groups=1(daemon),10(wheel),1234" )
38+ type testCase struct {
39+ description string
40+ opts []ContainerOpts
41+ expected string
42+ }
43+
44+ testCases := []testCase {
45+ {
46+ description : "Equivalent of `docker run` (no option)" ,
47+ opts : nil ,
48+ expected : "groups=0(root),10(wheel)" ,
49+ },
50+ {
51+ description : "Equivalent of `docker run --group-add 1 --group-add 1234`" ,
52+ opts : []ContainerOpts {WithSupplementalGroups ([]int64 {1 /*daemon*/ , 1234 /*new group*/ })},
53+ expected : "groups=0(root),1(daemon),10(wheel),1234" ,
54+ },
55+ {
56+ description : "Equivalent of `docker run --user 1234`" ,
57+ opts : []ContainerOpts {WithRunAsUser (1234 )},
58+ expected : "groups=0(root)" ,
59+ },
60+ {
61+ description : "Equivalent of `docker run --user 1234:1234`" ,
62+ opts : []ContainerOpts {WithRunAsUser (1234 ), WithRunAsGroup (1234 )},
63+ expected : "groups=1234" ,
64+ },
65+ {
66+ description : "Equivalent of `docker run --user 1234 --group-add 1234`" ,
67+ opts : []ContainerOpts {WithRunAsUser (1234 ), WithSupplementalGroups ([]int64 {1234 })},
68+ expected : "groups=0(root),1234" ,
69+ },
70+ {
71+ description : "Equivalent of `docker run --user daemon` (Supported by CRI, although unsupported by kube-apiserver)" ,
72+ opts : []ContainerOpts {WithRunAsUsername ("daemon" )},
73+ expected : "groups=1(daemon)" ,
74+ },
75+ {
76+ description : "Equivalent of `docker run --user daemon --group-add 1234` (Supported by CRI, although unsupported by kube-apiserver)" ,
77+ opts : []ContainerOpts {WithRunAsUsername ("daemon" ), WithSupplementalGroups ([]int64 {1234 })},
78+ expected : "groups=1(daemon),1234" ,
79+ },
80+ }
81+
82+ for i , tc := range testCases {
83+ i , tc := i , tc
84+ tBasename := fmt .Sprintf ("case-%d" , i )
85+ t .Run (tBasename , func (t * testing.T ) {
86+ t .Log (tc .description )
87+ t .Logf ("Expected=%q" , tc .expected )
88+
89+ testPodLogDir := t .TempDir ()
90+
91+ t .Log ("Create a sandbox with log directory" )
92+ sb , sbConfig := PodSandboxConfigWithCleanup (t , "sandbox" , tBasename ,
93+ WithPodLogDirectory (testPodLogDir ))
94+
95+ t .Log ("Create a container to print id" )
96+ containerName := tBasename
97+ cnConfig := ContainerConfig (
98+ containerName ,
99+ testImage ,
100+ append (
101+ []ContainerOpts {
102+ WithCommand ("id" ),
103+ WithLogPath (containerName ),
104+ }, tc .opts ... )... ,
105+ )
106+ cn , err := runtimeService .CreateContainer (sb , cnConfig , sbConfig )
107+ require .NoError (t , err )
108+
109+ t .Log ("Start the container" )
110+ require .NoError (t , runtimeService .StartContainer (cn ))
111+
112+ t .Log ("Wait for container to finish running" )
113+ require .NoError (t , Eventually (func () (bool , error ) {
114+ s , err := runtimeService .ContainerStatus (cn )
115+ if err != nil {
116+ return false , err
117+ }
118+ if s .GetState () == runtime .ContainerState_CONTAINER_EXITED {
119+ return true , nil
120+ }
121+ return false , nil
122+ }, time .Second , 30 * time .Second ))
123+
124+ t .Log ("Search additional groups in container log" )
125+ content , err := os .ReadFile (filepath .Join (testPodLogDir , containerName ))
126+ assert .NoError (t , err )
127+ assert .Contains (t , string (content ), tc .expected + "\n " )
128+ })
129+ }
78130}
0 commit comments