Skip to content

Commit

Permalink
Increase test coverage on Jib Builder (#2383)
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot authored Jul 1, 2019
1 parent 23da73c commit e20185f
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
85 changes: 85 additions & 0 deletions pkg/skaffold/build/local/jib_gradle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package local

import (
"context"
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/pkg/errors"
)

func TestBuildJibGradle(t *testing.T) {
var tests = []struct {
description string
artifact *latest.JibGradleArtifact
cmd util.Command
shouldErr bool
expectedError string
}{
{
description: "local build",
artifact: &latest.JibGradleArtifact{},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain :jibDockerBuild --image=img:tag"),
},
{
description: "local build with additional flags",
artifact: &latest.JibGradleArtifact{Flags: []string{"--flag1", "--flag2"}},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain :jibDockerBuild --image=img:tag --flag1 --flag2"),
},
{
description: "local build with project",
artifact: &latest.JibGradleArtifact{Project: "project"},
cmd: testutil.FakeRun(t, "gradle -Djib.console=plain :project:jibDockerBuild --image=img:tag"),
},
{
description: "fail local build",
artifact: &latest.JibGradleArtifact{},
cmd: testutil.FakeRunErr(t, "gradle -Djib.console=plain :jibDockerBuild --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)

api := &testutil.FakeAPIClient{
TagToImageID: map[string]string{"img:tag": "abacab"},
}

builder := &Builder{
pushImages: false,
localDocker: docker.NewLocalDaemon(api, nil, false, map[string]bool{}),
}
result, err := builder.buildJibGradle(context.Background(), ioutil.Discard, ".", test.artifact, "img:tag")

t.CheckError(test.shouldErr, err)
if test.shouldErr {
t.CheckErrorContains(test.expectedError, err)
} else {
t.CheckDeepEqual("abacab", result)
}
})
}
}
75 changes: 75 additions & 0 deletions pkg/skaffold/build/local/jib_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,88 @@ package local

import (
"context"
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/pkg/errors"
)

func TestBuildJibMaven(t *testing.T) {
var tests = []struct {
description string
artifact *latest.JibMavenArtifact
cmd util.Command
shouldErr bool
expectedError string
}{
{
description: "local build",
artifact: &latest.JibMavenArtifact{},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag"),
},
{
description: "local build with additional flags",
artifact: &latest.JibMavenArtifact{Flags: []string{"--flag1", "--flag2"}},
cmd: testutil.FakeRun(t, "mvn -Djib.console=plain --flag1 --flag2 --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag"),
},
{
description: "local build with module",
artifact: &latest.JibMavenArtifact{Module: "module"},
cmd: testutil.
FakeRunOut(t, "mvn --quiet --projects module jib:_skaffold-package-goals", "dockerBuild").
WithRun("mvn -Djib.console=plain --projects module --also-make package -Dimage=img:tag"),
},
{
description: "local build with module and profile",
artifact: &latest.JibMavenArtifact{Module: "module", Profile: "profile"},
cmd: testutil.
FakeRunOut(t, "mvn --quiet --projects module jib:_skaffold-package-goals --activate-profiles profile", "dockerBuild").
WithRun("mvn -Djib.console=plain --activate-profiles profile --projects module --also-make package -Dimage=img:tag"),
},
{
description: "fail local build",
artifact: &latest.JibMavenArtifact{},
cmd: testutil.FakeRunErr(t, "mvn -Djib.console=plain --non-recursive prepare-package jib:dockerBuild -Dimage=img:tag", errors.New("BUG")),
shouldErr: true,
expectedError: "maven build failed",
},
{
description: "fail to check package goals",
artifact: &latest.JibMavenArtifact{Module: "module"},
cmd: testutil.FakeRunOutErr(t, "mvn --quiet --projects module jib:_skaffold-package-goals", "", errors.New("BUG")),
shouldErr: true,
expectedError: "could not obtain jib package goals",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, test.cmd)

api := &testutil.FakeAPIClient{
TagToImageID: map[string]string{"img:tag": "abacab"},
}

builder := &Builder{
pushImages: false,
localDocker: docker.NewLocalDaemon(api, nil, false, map[string]bool{}),
}
result, err := builder.buildJibMaven(context.Background(), ioutil.Discard, ".", test.artifact, "img:tag")

t.CheckError(test.shouldErr, err)
if test.shouldErr {
t.CheckErrorContains(test.expectedError, err)
} else {
t.CheckDeepEqual("abacab", result)
}
})
}
}

func TestMavenVerifyJibPackageGoal(t *testing.T) {
var tests = []struct {
description string
Expand Down
4 changes: 4 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type T struct {
teardownActions []func()
}

func (t *T) FakeRun(command string) *FakeCmd {
return FakeRun(t.T, command)
}

func (t *T) FakeRunOut(command string, output string) *FakeCmd {
return FakeRunOut(t.T, command, output)
}
Expand Down

0 comments on commit e20185f

Please sign in to comment.