Skip to content

Commit

Permalink
Improve fake commands handling
Browse files Browse the repository at this point in the history
 + avoid cases where the parent test would be mark
in error instead of the child test. See GoogleContainerTools#2744
 + simplify usage
 + make sure every usage look similar

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Sep 2, 2019
1 parent 4899ae5 commit a7c14be
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 242 deletions.
5 changes: 4 additions & 1 deletion pkg/skaffold/build/bazel/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func TestGetDependencies(t *testing.T) {
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, t.FakeRunOut(test.expectedQuery, test.output))
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
test.expectedQuery,
test.output,
))
t.NewTempDir().WriteFiles(test.files).Chdir()

deps, err := GetDependencies(context.Background(), test.workspace, &latest.BazelArtifact{
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/custom/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestGetDependenciesDockerfile(t *testing.T) {

func TestGetDependenciesCommand(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, t.FakeRunOut(
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"echo [\"file1\",\"file2\",\"file3\"]",
"[\"file1\",\"file2\",\"file3\"]",
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/bazel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func TestBazelBin(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, t.FakeRunOut(
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"bazel info bazel-bin --arg1 --arg2",
"/absolute/path/bin\n",
))
Expand Down
5 changes: 4 additions & 1 deletion pkg/skaffold/build/local/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func TestDockerCLIBuild(t *testing.T) {
testutil.Run(t, test.description, func(t *testutil.T) {
t.NewTempDir().Chdir()
dockerfilePath, _ := filepath.Abs("Dockerfile")
t.Override(&util.DefaultExecCommand, t.NewFakeCmd().WithRunEnv("docker build . --file "+dockerfilePath+" -t tag --force-rm", test.expectedEnv))
t.Override(&util.DefaultExecCommand, testutil.CmdRunEnv(
"docker build . --file "+dockerfilePath+" -t tag --force-rm",
test.expectedEnv,
))
t.Override(&util.OSEnviron, func() []string { return []string{"KEY=VALUE"} })
t.Override(&docker.NewAPIClient, func(*runcontext.RunContext) (docker.LocalDaemon, error) {
return docker.NewLocalDaemon(&testutil.FakeAPIClient{}, test.extraEnv, false, nil), nil
Expand Down
50 changes: 34 additions & 16 deletions pkg/skaffold/build/local/jib_gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,38 @@ func TestBuildJibGradleToDocker(t *testing.T) {
tests := []struct {
description string
artifact *latest.JibGradleArtifact
cmd util.Command
commands util.Command
shouldErr bool
expectedError string
}{
{
description: "build",
artifact: &latest.JibGradleArtifact{},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jibDockerBuild --image=img:tag"),
commands: testutil.CmdRun(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion=" + jib.MinimumJibGradleVersion + " :jibDockerBuild --image=img:tag",
),
},
{
description: "build with additional flags",
artifact: &latest.JibGradleArtifact{Flags: []string{"--flag1", "--flag2"}},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jibDockerBuild --image=img:tag --flag1 --flag2"),
commands: testutil.CmdRun(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion=" + jib.MinimumJibGradleVersion + " :jibDockerBuild --image=img:tag --flag1 --flag2",
),
},
{
description: "build with project",
artifact: &latest.JibGradleArtifact{Project: "project"},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :project:jibDockerBuild --image=img:tag"),
commands: testutil.CmdRun(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion=" + jib.MinimumJibGradleVersion + " :project:jibDockerBuild --image=img:tag",
),
},
{
description: "fail build",
artifact: &latest.JibGradleArtifact{},
cmd: testutil.FakeRunErr(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jibDockerBuild --image=img:tag", errors.New("BUG")),
description: "fail build",
artifact: &latest.JibGradleArtifact{},
commands: testutil.CmdRunErr(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jibDockerBuild --image=img:tag",
errors.New("BUG"),
),
shouldErr: true,
expectedError: "gradle build failed",
},
Expand All @@ -66,7 +75,7 @@ func TestBuildJibGradleToDocker(t *testing.T) {
testutil.Run(t, test.description, func(t *testutil.T) {
api := (&testutil.FakeAPIClient{}).Add("img:tag", "imageID")

t.Override(&util.DefaultExecCommand, test.cmd)
t.Override(&util.DefaultExecCommand, test.commands)
t.Override(&docker.NewAPIClient, func(*runcontext.RunContext) (docker.LocalDaemon, error) {
return docker.NewLocalDaemon(api, nil, false, nil), nil
})
Expand All @@ -92,37 +101,46 @@ func TestBuildJibGradleToRegistry(t *testing.T) {
tests := []struct {
description string
artifact *latest.JibGradleArtifact
cmd util.Command
commands util.Command
shouldErr bool
expectedError string
}{
{
description: "remote build",
artifact: &latest.JibGradleArtifact{},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jib --image=img:tag"),
commands: testutil.CmdRun(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion=" + jib.MinimumJibGradleVersion + " :jib --image=img:tag",
),
},
{
description: "build with additional flags",
artifact: &latest.JibGradleArtifact{Flags: []string{"--flag1", "--flag2"}},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jib --image=img:tag --flag1 --flag2"),
commands: testutil.CmdRun(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion=" + jib.MinimumJibGradleVersion + " :jib --image=img:tag --flag1 --flag2",
),
},
{
description: "build with project",
artifact: &latest.JibGradleArtifact{Project: "project"},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :project:jib --image=img:tag"),
commands: testutil.CmdRun(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion=" + jib.MinimumJibGradleVersion + " :project:jib --image=img:tag",
),
},
{
description: "fail build",
artifact: &latest.JibGradleArtifact{},
cmd: testutil.FakeRunErr(t, "gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jib --image=img:tag", errors.New("BUG")),
description: "fail build",
artifact: &latest.JibGradleArtifact{},
commands: testutil.CmdRunErr(
"gradle -Djib.console=plain _skaffoldFailIfJibOutOfDate -Djib.requiredVersion="+jib.MinimumJibGradleVersion+" :jib --image=img:tag",
errors.New("BUG"),
),
shouldErr: true,
expectedError: "gradle build failed",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, test.cmd)
t.Override(&util.DefaultExecCommand, test.commands)
t.Override(&docker.RemoteDigest, func(identifier string, _ map[string]bool) (string, error) {
if identifier == "img:tag" {
return "digest", nil
Expand Down
58 changes: 40 additions & 18 deletions pkg/skaffold/build/local/jib_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,45 @@ func TestBuildJibMavenToDocker(t *testing.T) {
tests := []struct {
description string
artifact *latest.JibMavenArtifact
cmd util.Command
commands util.Command
shouldErr bool
expectedError string
}{
{
description: "build",
artifact: &latest.JibMavenArtifact{},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag",
),
},
{
description: "build with additional flags",
artifact: &latest.JibMavenArtifact{Flags: []string{"--flag1", "--flag2"}},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --flag1 --flag2 --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --flag1 --flag2 --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag",
),
},
{
description: "build with module",
artifact: &latest.JibMavenArtifact{Module: "module"},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --projects module --also-make package jib:dockerBuild -Djib.containerize=module -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --projects module --also-make package jib:dockerBuild -Djib.containerize=module -Dimage=img:tag",
),
},
{
description: "build with module and profile",
artifact: &latest.JibMavenArtifact{Module: "module", Profile: "profile"},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --activate-profiles profile --projects module --also-make package jib:dockerBuild -Djib.containerize=module -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --activate-profiles profile --projects module --also-make package jib:dockerBuild -Djib.containerize=module -Dimage=img:tag",
),
},
{
description: "fail build",
artifact: &latest.JibMavenArtifact{},
cmd: testutil.FakeRunErr(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag", errors.New("BUG")),
description: "fail build",
artifact: &latest.JibMavenArtifact{},
commands: testutil.CmdRunErr(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag",
errors.New("BUG"),
),
shouldErr: true,
expectedError: "maven build failed",
},
Expand All @@ -73,7 +84,7 @@ func TestBuildJibMavenToDocker(t *testing.T) {
t.Override(&docker.NewAPIClient, func(*runcontext.RunContext) (docker.LocalDaemon, error) {
return docker.NewLocalDaemon(api, nil, false, nil), nil
})
t.Override(&util.DefaultExecCommand, test.cmd)
t.Override(&util.DefaultExecCommand, test.commands)

builder, err := NewBuilder(stubRunContext(latest.LocalBuild{
Push: util.BoolPtr(false),
Expand All @@ -96,42 +107,53 @@ func TestBuildJibMavenToRegistry(t *testing.T) {
tests := []struct {
description string
artifact *latest.JibMavenArtifact
cmd util.Command
commands util.Command
shouldErr bool
expectedError string
}{
{
description: "build",
artifact: &latest.JibMavenArtifact{},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --non-recursive prepare-package jib:build -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --non-recursive prepare-package jib:build -Dimage=img:tag",
),
},
{
description: "build with additional flags",
artifact: &latest.JibMavenArtifact{Flags: []string{"--flag1", "--flag2"}},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --flag1 --flag2 --non-recursive prepare-package jib:build -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --flag1 --flag2 --non-recursive prepare-package jib:build -Dimage=img:tag",
),
},
{
description: "build with module",
artifact: &latest.JibMavenArtifact{Module: "module"},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --projects module --also-make package jib:build -Djib.containerize=module -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --projects module --also-make package jib:build -Djib.containerize=module -Dimage=img:tag",
),
},
{
description: "build with module and profile",
artifact: &latest.JibMavenArtifact{Module: "module", Profile: "profile"},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --activate-profiles profile --projects module --also-make package jib:build -Djib.containerize=module -Dimage=img:tag"),
commands: testutil.CmdRun(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion=" + jib.MinimumJibMavenVersion + " --activate-profiles profile --projects module --also-make package jib:build -Djib.containerize=module -Dimage=img:tag",
),
},
{
description: "fail build",
artifact: &latest.JibMavenArtifact{},
cmd: testutil.FakeRunErr(t, "mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --non-recursive prepare-package jib:build -Dimage=img:tag", errors.New("BUG")),
description: "fail build",
artifact: &latest.JibMavenArtifact{},
commands: testutil.CmdRunErr(
"mvn -Djib.console=plain jib:_skaffold-fail-if-jib-out-of-date -Djib.requiredVersion="+jib.MinimumJibMavenVersion+" --non-recursive prepare-package jib:build -Dimage=img:tag",
errors.New("BUG"),
),
shouldErr: true,
expectedError: "maven build failed",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, test.cmd)
t.Override(&util.DefaultExecCommand, test.commands)
t.Override(&docker.RemoteDigest, func(identifier string, _ map[string]bool) (string, error) {
if identifier == "img:tag" {
return "digest", nil
Expand Down
Loading

0 comments on commit a7c14be

Please sign in to comment.