From 6f0481318c91dfbbe0f338b3448e5e2bf62f7078 Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Thu, 31 Mar 2022 13:30:17 +0200 Subject: [PATCH] Fix potential issue with `image` unit tests when `PODMAN_CMD` env var is set For some valid reasons, we could want `odo` to use Docker as backend when building images. One way of doing so is to set the `PODMAN_CMD` system environment variable to a command we know does not exist. In such cases, the `image` unit tests will not pass, because they rely on the system environment variables. --- pkg/devfile/image/image.go | 5 ++-- pkg/devfile/image/image_test.go | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pkg/devfile/image/image.go b/pkg/devfile/image/image.go index c80e0e94ea6..6a1f9bc8afd 100644 --- a/pkg/devfile/image/image.go +++ b/pkg/devfile/image/image.go @@ -24,6 +24,7 @@ type Backend interface { } var lookPathCmd = exec.LookPath +var getEnvFunc = os.Getenv // BuildPushImages build all images defined in the devfile with the detected backend // If push is true, also push the images to their registries @@ -89,7 +90,7 @@ func buildPushImage(backend Backend, image *devfile.ImageComponent, devfilePath // or return an error if none are present locally func selectBackend() (Backend, error) { - podmanCmd := os.Getenv("PODMAN_CMD") + podmanCmd := getEnvFunc("PODMAN_CMD") if podmanCmd == "" { podmanCmd = "podman" } @@ -97,7 +98,7 @@ func selectBackend() (Backend, error) { return NewDockerCompatibleBackend(podmanCmd), nil } - dockerCmd := os.Getenv("DOCKER_CMD") + dockerCmd := getEnvFunc("DOCKER_CMD") if dockerCmd == "" { dockerCmd = "docker" } diff --git a/pkg/devfile/image/image_test.go b/pkg/devfile/image/image_test.go index cbb93df690c..c1b6487c524 100644 --- a/pkg/devfile/image/image_test.go +++ b/pkg/devfile/image/image_test.go @@ -2,6 +2,7 @@ package image import ( "errors" + "os" "os/exec" "testing" @@ -116,6 +117,7 @@ func TestBuildPushImage(t *testing.T) { func TestSelectBackend(t *testing.T) { tests := []struct { name string + getEnvFunc func(string) string lookPathCmd func(string) (string, error) wantType string wantErr bool @@ -157,10 +159,53 @@ func TestSelectBackend(t *testing.T) { wantErr: false, wantType: "podman", }, + { + name: "value of PODMAN_CMD envvar is returned if it points to a valid command", + getEnvFunc: func(name string) string { + if name == "PODMAN_CMD" { + return "my-alternate-podman-command" + } + return "" + }, + lookPathCmd: func(name string) (string, error) { + if name == "my-alternate-podman-command" { + return "my-alternate-podman-command", nil + } + return "", errors.New("") + }, + wantErr: false, + wantType: "my-alternate-podman-command", + }, + { + name: "docker if PODMAN_CMD points to an invalid command", + getEnvFunc: func(name string) string { + if name == "PODMAN_CMD" { + return "no-such-command" + } + return "" + }, + lookPathCmd: func(name string) (string, error) { + if name == "docker" { + return "docker", nil + } + return "", errors.New("") + }, + wantErr: false, + wantType: "docker", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if tt.getEnvFunc != nil { + getEnvFunc = tt.getEnvFunc + } else { + getEnvFunc = func(string) string { + //Empty environment + return "" + } + } + defer func() { getEnvFunc = os.Getenv }() lookPathCmd = tt.lookPathCmd defer func() { lookPathCmd = exec.LookPath }() backend, err := selectBackend()