Skip to content

Commit

Permalink
Fix potential issue with image unit tests when PODMAN_CMD env var…
Browse files Browse the repository at this point in the history
… is set (#5614)

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.
  • Loading branch information
rm3l authored Apr 1, 2022
1 parent 3759980 commit 851b2f3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/devfile/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,15 +90,15 @@ 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"
}
if _, err := lookPathCmd(podmanCmd); err == nil {
return NewDockerCompatibleBackend(podmanCmd), nil
}

dockerCmd := os.Getenv("DOCKER_CMD")
dockerCmd := getEnvFunc("DOCKER_CMD")
if dockerCmd == "" {
dockerCmd = "docker"
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/devfile/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package image

import (
"errors"
"os"
"os/exec"
"testing"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 851b2f3

Please sign in to comment.