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