1
1
package docker
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
6
+ "path/filepath"
5
7
"testing"
6
8
9
+ "github.com/docker/docker/client"
7
10
"github.com/openshift/source-to-image/pkg/api"
8
11
"github.com/openshift/source-to-image/pkg/api/constants"
9
12
"github.com/openshift/source-to-image/pkg/util/user"
@@ -233,14 +236,17 @@ func TestGetDefaultDockerConfig(t *testing.T) {
233
236
},
234
237
}
235
238
for _ , tc := range tests {
239
+ oldXdgRuntimeDir := os .Getenv ("XDG_RUNTIME_DIR" )
236
240
oldHost := os .Getenv ("DOCKER_HOST" )
237
241
oldCertPath := os .Getenv ("DOCKER_CERT_PATH" )
238
242
oldTLSVerify := os .Getenv ("DOCKER_TLS_VERIFY" )
239
243
oldTLS := os .Getenv ("DOCKER_TLS" )
244
+ os .Setenv ("XDG_RUNTIME_DIR" , "" )
240
245
os .Setenv ("DOCKER_HOST" , tc .envHost )
241
246
os .Setenv ("DOCKER_CERT_PATH" , tc .envCertPath )
242
247
os .Setenv ("DOCKER_TLS_VERIFY" , tc .envTLSVerify )
243
248
os .Setenv ("DOCKER_TLS" , tc .envTLS )
249
+ defer os .Setenv ("XDG_RUNTIME_DIR" , oldXdgRuntimeDir )
244
250
defer os .Setenv ("DOCKER_HOST" , oldHost )
245
251
defer os .Setenv ("DOCKER_CERT_PATH" , oldCertPath )
246
252
defer os .Setenv ("DOCKER_TLS_VERIFY" , oldTLSVerify )
@@ -262,6 +268,79 @@ func TestGetDefaultDockerConfig(t *testing.T) {
262
268
}
263
269
}
264
270
271
+ func TestGetDefaultContainerEngineHost (t * testing.T ) {
272
+
273
+ tmpDir , err := os .MkdirTemp ("" , "s2i-container-engine-host-*" )
274
+ if err != nil {
275
+ t .Fatalf ("failed to create xdg temp dir: %v" , err )
276
+ }
277
+
278
+ testCases := []struct {
279
+ name string
280
+ xdgRuntimeDir string
281
+ createPodmanSocket bool
282
+ expectedHost string
283
+ }{
284
+ {
285
+ name : "rootless podman - socket exists" ,
286
+ xdgRuntimeDir : tmpDir ,
287
+ createPodmanSocket : true ,
288
+ expectedHost : fmt .Sprintf ("unix://%s" , filepath .Join (tmpDir , "podman" , "podman.sock" )),
289
+ },
290
+ {
291
+ name : "rootless podman - socket does not exist" ,
292
+ xdgRuntimeDir : tmpDir ,
293
+ createPodmanSocket : false ,
294
+ expectedHost : client .DefaultDockerHost ,
295
+ },
296
+ {
297
+ name : "docker default" ,
298
+ expectedHost : client .DefaultDockerHost ,
299
+ },
300
+ }
301
+
302
+ for _ , tc := range testCases {
303
+ t .Run (tc .name , func (t * testing.T ) {
304
+ oldXdgDir := os .Getenv ("XDG_RUNTIME_DIR" )
305
+ os .Setenv ("XDG_RUNTIME_DIR" , tc .xdgRuntimeDir )
306
+ defer os .Setenv ("XDG_RUNTIME_DIR" , oldXdgDir )
307
+
308
+ socketCreated := false
309
+ socketPath := filepath .Join (tc .xdgRuntimeDir , "podman" , "podman.sock" )
310
+ if tc .createPodmanSocket {
311
+
312
+ if _ , err := os .Stat (socketPath ); err != nil {
313
+ if err := os .MkdirAll (filepath .Dir (socketPath ), 0750 ); err != nil {
314
+ t .Fatalf ("failed to create socket directory: %v" , err )
315
+ }
316
+ file , err := os .Create (socketPath )
317
+ if err != nil {
318
+ t .Fatalf ("failed to create default podman socket: %v" , err )
319
+ }
320
+ defer func () {
321
+ if closeErr := file .Close (); closeErr != nil {
322
+ t .Errorf ("failed to close file: %v" , closeErr )
323
+ }
324
+ }()
325
+ socketCreated = true
326
+ }
327
+ }
328
+
329
+ engineHost := GetDefaultContainerEngineHost ()
330
+ if tc .expectedHost != engineHost {
331
+ t .Errorf ("expected container host %s; got %s" , tc .expectedHost , engineHost )
332
+ }
333
+
334
+ if socketCreated {
335
+ if err := os .RemoveAll (filepath .Dir (socketPath )); err != nil {
336
+ t .Errorf ("failed to clean up podman socket: %v" , err )
337
+ }
338
+ }
339
+
340
+ })
341
+ }
342
+ }
343
+
265
344
func TestGetAssembleUser (t * testing.T ) {
266
345
testCases := []struct {
267
346
name string
0 commit comments