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,84 @@ 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
+ defer func () {
279
+ if rmErr := os .RemoveAll (tmpDir ); rmErr != nil {
280
+ t .Errorf ("failed to clean up xdg temp dir: %v" , err )
281
+ }
282
+ }()
283
+
284
+ testCases := []struct {
285
+ name string
286
+ xdgRuntimeDir string
287
+ createPodmanSocket bool
288
+ expectedHost string
289
+ }{
290
+ {
291
+ name : "rootless podman - socket exists" ,
292
+ xdgRuntimeDir : tmpDir ,
293
+ createPodmanSocket : true ,
294
+ expectedHost : fmt .Sprintf ("unix://%s" , filepath .Join (tmpDir , "podman" , "podman.sock" )),
295
+ },
296
+ {
297
+ name : "rootless podman - socket does not exist" ,
298
+ xdgRuntimeDir : tmpDir ,
299
+ createPodmanSocket : false ,
300
+ expectedHost : client .DefaultDockerHost ,
301
+ },
302
+ {
303
+ name : "docker default" ,
304
+ expectedHost : client .DefaultDockerHost ,
305
+ },
306
+ }
307
+
308
+ for _ , tc := range testCases {
309
+ t .Run (tc .name , func (t * testing.T ) {
310
+ oldXdgDir := os .Getenv ("XDG_RUNTIME_DIR" )
311
+ os .Setenv ("XDG_RUNTIME_DIR" , tc .xdgRuntimeDir )
312
+ defer os .Setenv ("XDG_RUNTIME_DIR" , oldXdgDir )
313
+
314
+ socketCreated := false
315
+ socketPath := filepath .Join (tc .xdgRuntimeDir , "podman" , "podman.sock" )
316
+ if tc .createPodmanSocket {
317
+
318
+ if _ , err := os .Stat (socketPath ); err != nil {
319
+ if err := os .MkdirAll (filepath .Dir (socketPath ), 0750 ); err != nil {
320
+ t .Fatalf ("failed to create socket directory: %v" , err )
321
+ }
322
+ file , err := os .Create (socketPath )
323
+ if err != nil {
324
+ t .Fatalf ("failed to create default podman socket: %v" , err )
325
+ }
326
+ defer func () {
327
+ if closeErr := file .Close (); closeErr != nil {
328
+ t .Errorf ("failed to close file: %v" , closeErr )
329
+ }
330
+ }()
331
+ socketCreated = true
332
+ }
333
+ }
334
+
335
+ engineHost := GetDefaultContainerEngineHost ()
336
+ if tc .expectedHost != engineHost {
337
+ t .Errorf ("expected container host %s; got %s" , tc .expectedHost , engineHost )
338
+ }
339
+
340
+ if socketCreated {
341
+ if err := os .RemoveAll (filepath .Dir (socketPath )); err != nil {
342
+ t .Errorf ("failed to clean up podman socket: %v" , err )
343
+ }
344
+ }
345
+
346
+ })
347
+ }
348
+ }
349
+
265
350
func TestGetAssembleUser (t * testing.T ) {
266
351
testCases := []struct {
267
352
name string
0 commit comments