2222 runtimeCommand = "runc"
2323)
2424
25+ // build test environment before running container
26+ type preFunc func (string ) error
27+
2528func init () {
2629 runtimeInEnv := os .Getenv ("RUNTIME" )
2730 if runtimeInEnv != "" {
@@ -50,15 +53,22 @@ func prepareBundle() (string, error) {
5053func getDefaultGenerator () * generate.Generator {
5154 g := generate .New ()
5255 g .SetRootPath ("." )
53- g .SetProcessArgs ([]string {"/runtimetest" })
56+ g .SetProcessArgs ([]string {"/runtimetest" , "--path=/" })
5457 return & g
5558}
5659
57- func runtimeInsideValidate (g * generate.Generator ) error {
60+ func runtimeInsideValidate (g * generate.Generator , f preFunc ) error {
5861 bundleDir , err := prepareBundle ()
5962 if err != nil {
6063 return err
6164 }
65+
66+ if f != nil {
67+ if err := f (bundleDir ); err != nil {
68+ return err
69+ }
70+ }
71+
6272 r , err := NewRuntime (runtimeCommand , bundleDir )
6373 if err != nil {
6474 os .RemoveAll (bundleDir )
@@ -85,15 +95,15 @@ func runtimeInsideValidate(g *generate.Generator) error {
8595func TestValidateBasic (t * testing.T ) {
8696 g := getDefaultGenerator ()
8797
88- assert .Nil (t , runtimeInsideValidate (g ))
98+ assert .Nil (t , runtimeInsideValidate (g , nil ))
8999}
90100
91101// Test whether rootfs Readonly can be applied as false
92102func TestValidateRootFSReadWrite (t * testing.T ) {
93103 g := getDefaultGenerator ()
94104 g .SetRootReadonly (false )
95105
96- assert .Nil (t , runtimeInsideValidate (g ))
106+ assert .Nil (t , runtimeInsideValidate (g , nil ))
97107}
98108
99109// Test whether rootfs Readonly can be applied as true
@@ -105,15 +115,40 @@ func TestValidateRootFSReadonly(t *testing.T) {
105115 g := getDefaultGenerator ()
106116 g .SetRootReadonly (true )
107117
108- assert .Nil (t , runtimeInsideValidate (g ))
118+ assert .Nil (t , runtimeInsideValidate (g , nil ))
119+ }
120+
121+ // Test Process
122+ func TestValidateProcess (t * testing.T ) {
123+ g := getDefaultGenerator ()
124+ g .SetProcessCwd ("/test" )
125+ g .AddProcessEnv ("testa" , "valuea" )
126+ g .AddProcessEnv ("testb" , "123" )
127+
128+ assert .Nil (t , runtimeInsideValidate (g , func (path string ) error {
129+ pathName := filepath .Join (path , "test" )
130+ return os .MkdirAll (pathName , 0700 )
131+ }))
132+ }
133+
134+ // Test whether Capabilites can be applied or not
135+ func TestValidateCapabilities (t * testing.T ) {
136+ if "linux" != runtime .GOOS {
137+ t .Skip ("skip linux-specific capabilities test" )
138+ }
139+
140+ g := getDefaultGenerator ()
141+ g .SetupPrivileged (true )
142+
143+ assert .Nil (t , runtimeInsideValidate (g , nil ))
109144}
110145
111146// Test whether hostname can be applied or not
112147func TestValidateHostname (t * testing.T ) {
113148 g := getDefaultGenerator ()
114149 g .SetHostname ("hostname-specific" )
115150
116- assert .Nil (t , runtimeInsideValidate (g ))
151+ assert .Nil (t , runtimeInsideValidate (g , nil ))
117152}
118153
119154func TestValidateRootfsPropagationPrivate (t * testing.T ) {
@@ -129,15 +164,98 @@ func TestValidateRootfsPropagationShared(t *testing.T) {
129164 g .SetupPrivileged (true )
130165 g .SetLinuxRootPropagation ("shared" )
131166
132- assert .Nil (t , runtimeInsideValidate (g ))
167+ assert .Nil (t , runtimeInsideValidate (g , nil ))
133168}
134169
135170func TestValidateRootfsPropagationUnbindable (t * testing.T ) {
136171 g := getDefaultGenerator ()
137172 g .SetupPrivileged (true )
138173 g .SetLinuxRootPropagation ("unbindable" )
139174
140- assert .Nil (t , runtimeInsideValidate (g ))
175+ assert .Nil (t , runtimeInsideValidate (g , nil ))
176+ }
177+
178+ func TestValidateLinuxDevices (t * testing.T ) {
179+ g := getDefaultGenerator ()
180+
181+ // add char device
182+ cdev := rspecs.LinuxDevice {}
183+ cdev .Path = "/dev/test1"
184+ cdev .Type = "c"
185+ cdev .Major = 10
186+ cdev .Minor = 666
187+ cmode := os .FileMode (int32 (432 ))
188+ cdev .FileMode = & cmode
189+ cuid := uint32 (0 )
190+ cdev .UID = & cuid
191+ cgid := uint32 (0 )
192+ cdev .GID = & cgid
193+ g .AddDevice (cdev )
194+ // add block device
195+ bdev := rspecs.LinuxDevice {}
196+ bdev .Path = "/dev/test2"
197+ bdev .Type = "b"
198+ bdev .Major = 8
199+ bdev .Minor = 666
200+ bmode := os .FileMode (int32 (432 ))
201+ bdev .FileMode = & bmode
202+ uid := uint32 (0 )
203+ bdev .UID = & uid
204+ gid := uint32 (0 )
205+ bdev .GID = & gid
206+ g .AddDevice (bdev )
207+ // add fifo device
208+ pdev := rspecs.LinuxDevice {}
209+ pdev .Path = "/dev/test3"
210+ pdev .Type = "p"
211+ pdev .Major = 8
212+ pdev .Minor = 666
213+ pmode := os .FileMode (int32 (432 ))
214+ pdev .FileMode = & pmode
215+ g .AddDevice (pdev )
216+
217+ assert .Nil (t , runtimeInsideValidate (g , nil ))
218+ }
219+
220+ func TestValidateMaskedPaths (t * testing.T ) {
221+ g := getDefaultGenerator ()
222+ g .AddLinuxMaskedPaths ("/masktest" )
223+
224+ assert .Nil (t , runtimeInsideValidate (g , func (path string ) error {
225+ pathName := filepath .Join (path , "masktest" )
226+ return os .MkdirAll (pathName , 0700 )
227+ }))
228+ }
229+
230+ func TestValidateROPaths (t * testing.T ) {
231+ g := getDefaultGenerator ()
232+ g .AddLinuxReadonlyPaths ("readonlytest" )
233+
234+ assert .Nil (t , runtimeInsideValidate (g , func (path string ) error {
235+ pathName := filepath .Join (path , "readonlytest" )
236+ return os .MkdirAll (pathName , 0700 )
237+ }))
238+ }
239+
240+ func TestValidateOOMScoreAdj (t * testing.T ) {
241+ g := getDefaultGenerator ()
242+ g .SetProcessOOMScoreAdj (500 )
243+
244+ assert .Nil (t , runtimeInsideValidate (g , nil ))
245+ }
246+
247+ func TestValidateUIDMappings (t * testing.T ) {
248+ g := getDefaultGenerator ()
249+ g .AddLinuxUIDMapping (uint32 (1000 ), uint32 (0 ), uint32 (3200 ))
250+
251+ assert .Nil (t , runtimeInsideValidate (g , nil ))
252+ }
253+
254+ func TestValidateGIDMappings (t * testing.T ) {
255+ g := getDefaultGenerator ()
256+ g .AddLinuxGIDMapping (uint32 (1000 ), uint32 (0 ), uint32 (3200 ))
257+
258+ assert .Nil (t , runtimeInsideValidate (g , nil ))
141259}
142260
143261// Test whether mounts are correctly mounted
@@ -151,15 +269,15 @@ func TestValidateRlimits(t *testing.T) {
151269 g := getDefaultGenerator ()
152270 g .AddProcessRlimits ("RLIMIT_NOFILE" , 1024 , 1024 )
153271
154- assert .Nil (t , runtimeInsideValidate (g ))
272+ assert .Nil (t , runtimeInsideValidate (g , nil ))
155273}
156274
157275// Test whether sysctls can be applied or not
158276func TestValidateSysctls (t * testing.T ) {
159277 g := getDefaultGenerator ()
160278 g .AddLinuxSysctl ("net.ipv4.ip_forward" , "1" )
161279
162- assert .Nil (t , runtimeInsideValidate (g ))
280+ assert .Nil (t , runtimeInsideValidate (g , nil ))
163281}
164282
165283// Test Create operation
0 commit comments