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

tests for local.NewBuilder #2240

Merged
merged 3 commits into from
Jun 8, 2019
Merged
Changes from 1 commit
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
Next Next commit
tests for local.NewBuilder
balopat committed Jun 8, 2019
commit 145d2eae2a2971d69323b9505fe92451860242c1
84 changes: 84 additions & 0 deletions pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
@@ -21,6 +21,10 @@ import (
"io/ioutil"
"testing"

"github.com/pkg/errors"

"github.com/google/go-cmp/cmp"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
@@ -245,3 +249,83 @@ func TestLocalRun(t *testing.T) {
})
}
}

type dummyLocalDaemon struct {
docker.LocalDaemon
}

func TestNewBuilder(t *testing.T) {
dummyDaemon := dummyLocalDaemon{}

tcs := []struct {
name string
runCtx *runcontext.RunContext
shouldErr bool
expectedBuilder *Builder
localClusterFn func() (bool, error)
localDockerFn func(*runcontext.RunContext) (docker.LocalDaemon, error)
}{
{
name: "failed to get docker client",
localDockerFn: func(runContext *runcontext.RunContext) (daemon docker.LocalDaemon, e error) {
e = errors.New("dummy docker error")
return
},
shouldErr: true,
}, {
name: "pushImages becomes !localCluster when local:push is not defined",
localDockerFn: func(runContext *runcontext.RunContext) (daemon docker.LocalDaemon, e error) {
daemon = dummyDaemon
return
},
localClusterFn: func() (b bool, e error) {
b = false
return
},
shouldErr: false,
expectedBuilder: &Builder{
cfg: &latest.LocalBuild{},
kubeContext: "",
localDocker: dummyDaemon,
localCluster: false,
pushImages: true,
skipTests: false,
prune: true,
insecureRegistries: nil,
},
},
}
for _, tc := range tcs {
testutil.Run(t, tc.name, func(t *testutil.T) {
if tc.localDockerFn != nil {
t.Override(&getLocalDocker, tc.localDockerFn)
}
if tc.localClusterFn != nil {
t.Override(&getLocalCluster, tc.localClusterFn)
}
builder, err := NewBuilder(dummyRunContext())
t.CheckError(tc.shouldErr, err)
if !tc.shouldErr {
t.CheckDeepEqual(tc.expectedBuilder, builder, cmp.AllowUnexported(Builder{}, dummyDaemon))
}
})
}
}

func dummyRunContext() *runcontext.RunContext {
return &runcontext.RunContext{
Cfg: &latest.Pipeline{
Build: latest.BuildConfig{
BuildType: latest.BuildType{
LocalBuild: &latest.LocalBuild{},
},
},
},
Opts: &config.SkaffoldOptions{
NoPrune: false,
CacheArtifacts: false,
SkipTests: false,
},
}

}
15 changes: 13 additions & 2 deletions pkg/skaffold/build/local/types.go
Original file line number Diff line number Diff line change
@@ -44,14 +44,25 @@ type Builder struct {
insecureRegistries map[string]bool
}

// external dependencies are wrapped
// into private functions for testability

var getLocalCluster = func() (bool, error) {
return configutil.GetLocalCluster()
}

var getLocalDocker = func(runCtx *runcontext.RunContext) (docker.LocalDaemon, error) {
return docker.NewAPIClient(runCtx.Opts.Prune(), runCtx.InsecureRegistries)
}

// NewBuilder returns an new instance of a local Builder.
func NewBuilder(runCtx *runcontext.RunContext) (*Builder, error) {
localDocker, err := docker.NewAPIClient(runCtx.Opts.Prune(), runCtx.InsecureRegistries)
localDocker, err := getLocalDocker(runCtx)
if err != nil {
return nil, errors.Wrap(err, "getting docker client")
}

localCluster, err := configutil.GetLocalCluster()
localCluster, err := getLocalCluster()
if err != nil {
return nil, errors.Wrap(err, "getting localCluster")
}
33 changes: 30 additions & 3 deletions testutil/util.go
Original file line number Diff line number Diff line change
@@ -43,7 +43,11 @@ func (t *T) FakeRunOutErr(command string, output string, err error) *FakeCmd {
}

func (t *T) Override(dest, tmp interface{}) {
teardown := Override(t.T, dest, tmp)
teardown, err := override(t.T, dest, tmp)
if err != nil {
t.Errorf("temporary override value is invalid: %v", err)
return
}
t.teardownActions = append(t.teardownActions, teardown)
}

@@ -234,11 +238,27 @@ func ServeFile(t *testing.T, content []byte) (url string, tearDown func()) {
// Returns the function to call to restore the variable
// to its original state.
func Override(t *testing.T, dest, tmp interface{}) func() {
f, err := override(t, dest, tmp)
if err != nil {
t.Errorf("temporary value is invalid: %v", err)
}
return f
}

func override(t *testing.T, dest, tmp interface{}) (f func(), err error) {
t.Helper()

defer func() {
if r := recover(); r != nil {
t.Error("temporary value is of invalid type")
f = nil
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}
}
}()

@@ -257,5 +277,12 @@ func Override(t *testing.T, dest, tmp interface{}) func() {
}
dValue.Set(tmpV)

return func() { dValue.Set(curValue) }
return func() {
defer func() {
if r := recover(); r != nil {
t.Error("panic while restoring original value")
}
}()
dValue.Set(curValue)
}, nil
}