Skip to content

tests for local.NewBuilder #2240

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

Merged
merged 3 commits into from
Jun 8, 2019
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
83 changes: 83 additions & 0 deletions pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -245,3 +249,82 @@ func TestLocalRun(t *testing.T) {
})
}
}

type dummyLocalDaemon struct {
docker.LocalDaemon
}

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

tcs := []struct {
name string
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,
},
}

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

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

var getLocalCluster = 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")
}
Expand Down
33 changes: 30 additions & 3 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

Expand All @@ -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
}