Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential issue with image.TestSelectBackend unit tests when PODMAN_CMD env var is set #5614

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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