Skip to content

Commit

Permalink
Merge pull request #2570 from dgageot/faster-tests
Browse files Browse the repository at this point in the history
Faster tests
  • Loading branch information
tejal29 authored Jul 31, 2019
2 parents 323beec + e250960 commit a58b312
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 135 deletions.
5 changes: 5 additions & 0 deletions cmd/skaffold/app/cmd/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/update"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/blang/semver"
)

func TestCreateNewRunner(t *testing.T) {
Expand Down Expand Up @@ -81,6 +83,9 @@ func TestCreateNewRunner(t *testing.T) {
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&update.GetLatestAndCurrentVersion, func() (semver.Version, semver.Version, error) {
return semver.Version{}, semver.Version{}, nil
})
t.NewTempDir().
Write("skaffold.yaml", fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, test.config)).
Chdir()
Expand Down
8 changes: 7 additions & 1 deletion pkg/skaffold/build/cache/retrieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/docker/docker/api/types"
)

type depLister struct {
Expand Down Expand Up @@ -83,6 +84,11 @@ func (b *mockBuilder) BuildAndTest(ctx context.Context, out io.Writer, tags tag.
return built, nil
}

type stubAuth struct{}

func (t stubAuth) GetAuthConfig(string) (types.AuthConfig, error) { return types.AuthConfig{}, nil }
func (t stubAuth) GetAllAuthConfigs() (map[string]types.AuthConfig, error) { return nil, nil }

func TestCacheBuildLocal(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
tmpDir := t.NewTempDir().
Expand Down Expand Up @@ -183,7 +189,7 @@ func TestCacheBuildRemote(t *testing.T) {
t.Override(&docker.NewAPIClient, func(*runcontext.RunContext) (docker.LocalDaemon, error) {
return dockerDaemon, nil
})

t.Override(&docker.DefaultAuthHelper, stubAuth{})
t.Override(&docker.RemoteDigest, func(ref string, _ map[string]bool) (string, error) {
switch ref {
case "artifact1:tag1":
Expand Down
108 changes: 47 additions & 61 deletions pkg/skaffold/build/tag/git_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha string
createGitRepo func(string)
subDir string
shouldErr bool
}{
{
description: "clean worktree without tag",
Expand All @@ -54,7 +53,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial")
},
Expand All @@ -68,11 +67,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:bc69d50",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial").
tag("v1").
write("other.go", []byte("other")).
write("other.go", "other").
add("other.go").
commit("second commit").
tag("v2")
Expand All @@ -87,10 +86,10 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("other code")).
write("source.go", "other code").
add("source.go").
commit("initial").
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("updated code").
tag("v1")
Expand All @@ -105,10 +104,10 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial").
write("source.go", []byte("updated code"))
write("source.go", "updated code")
},
},
{
Expand All @@ -120,11 +119,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial").
tag("v1").
write("source.go", []byte("updated code"))
write("source.go", "updated code")
},
},
{
Expand All @@ -136,10 +135,10 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial").
write("new.go", []byte("new code"))
write("new.go", "new code")
},
},
{
Expand All @@ -151,11 +150,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:81eea36",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial").
tag("v1").
write("source.go", []byte("updated code")).
write("source.go", "updated code").
add("source.go").
commit("changes")
},
Expand All @@ -169,8 +168,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:039c20a-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source1.go", []byte("code1")).
write("source2.go", []byte("code2")).
write("source1.go", "code1").
write("source2.go", "code2").
add("source1.go", "source2.go").
commit("initial").
delete("source1.go")
Expand All @@ -185,7 +184,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
write("source.go", "code").
add("source.go").
commit("initial").
rename("source.go", "source2.go")
Expand Down Expand Up @@ -214,8 +213,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("artifact1").write("artifact1/source.go", []byte("code")).
mkdir("artifact2").write("artifact2/source.go", []byte("other code")).
mkdir("artifact1").write("artifact1/source.go", "code").
mkdir("artifact2").write("artifact2/source.go", "other code").
add("artifact1/source.go", "artifact2/source.go").
commit("initial").tag("v1")
},
Expand All @@ -230,8 +229,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:36651c8",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("artifact1").write("artifact1/source.go", []byte("code")).
mkdir("artifact2").write("artifact2/source.go", []byte("other code")).
mkdir("artifact1").write("artifact1/source.go", "code").
mkdir("artifact2").write("artifact2/source.go", "other code").
add("artifact1/source.go", "artifact2/source.go").
commit("initial").tag("v1")
},
Expand All @@ -246,11 +245,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("artifact1").write("artifact1/source.go", []byte("code")).
mkdir("artifact2").write("artifact2/source.go", []byte("other code")).
mkdir("artifact1").write("artifact1/source.go", "code").
mkdir("artifact2").write("artifact2/source.go", "other code").
add("artifact1/source.go", "artifact2/source.go").
commit("initial").tag("v1").
write("artifact2/source.go", []byte("updated code"))
write("artifact2/source.go", "updated code")
},
subDir: "artifact1",
},
Expand All @@ -263,11 +262,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:36651c8-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("artifact1").write("artifact1/source.go", []byte("code")).
mkdir("artifact2").write("artifact2/source.go", []byte("other code")).
mkdir("artifact1").write("artifact1/source.go", "code").
mkdir("artifact2").write("artifact2/source.go", "other code").
add("artifact1/source.go", "artifact2/source.go").
commit("initial").tag("v1").
write("artifact2/source.go", []byte("updated code"))
write("artifact2/source.go", "updated code")
},
subDir: "artifact2",
},
Expand All @@ -280,11 +279,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
variantAbbrevTreeSha: "test:3bed02c",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("artifact1").write("artifact1/source.go", []byte("code")).
mkdir("artifact2").write("artifact2/source.go", []byte("other code")).
mkdir("artifact1").write("artifact1/source.go", "code").
mkdir("artifact2").write("artifact2/source.go", "other code").
add("artifact1/source.go", "artifact2/source.go").
commit("initial").
write("artifact2/source.go", []byte("updated code")).
write("artifact2/source.go", "updated code").
add("artifact2/source.go").
commit("update artifact2")
},
Expand Down Expand Up @@ -314,41 +313,28 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
},
}

tTags, err := NewGitCommit("Tags")
testutil.CheckError(t, false, err)

tCommit, err := NewGitCommit("CommitSha")
testutil.CheckError(t, false, err)

tAbbrevC, err := NewGitCommit("AbbrevCommitSha")
testutil.CheckError(t, false, err)

tTree, err := NewGitCommit("TreeSha")
testutil.CheckError(t, false, err)

tAbbrevT, err := NewGitCommit("AbbrevTreeSha")
testutil.CheckError(t, false, err)

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Parallel()

tmpDir := t.NewTempDir()
test.createGitRepo(tmpDir.Root())
workspace := tmpDir.Path(test.subDir)

name, err := tTags.GenerateFullyQualifiedImageName(workspace, "test")
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.variantTags, name)

name, err = tCommit.GenerateFullyQualifiedImageName(workspace, "test")
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.variantCommitSha, name)

name, err = tAbbrevC.GenerateFullyQualifiedImageName(workspace, "test")
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.variantAbbrevCommitSha, name)

name, err = tTree.GenerateFullyQualifiedImageName(workspace, "test")
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.variantTreeSha, name)

name, err = tAbbrevT.GenerateFullyQualifiedImageName(workspace, "test")
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.variantAbbrevTreeSha, name)
for variant, expectedTag := range map[string]string{
"Tags": test.variantTags,
"CommitSha": test.variantCommitSha,
"AbbrevCommitSha": test.variantAbbrevCommitSha,
"TreeSha": test.variantTreeSha,
"AbbrevTreeSha": test.variantAbbrevTreeSha,
} {
tagger, err := NewGitCommit(variant)
t.CheckNoError(err)

tag, err := tagger.GenerateFullyQualifiedImageName(workspace, "test")
t.CheckNoError(err)
t.CheckDeepEqual(expectedTag, tag)
}
})
}
}
Expand Down Expand Up @@ -382,8 +368,8 @@ func (g *gitRepo) mkdir(folder string) *gitRepo {
return g
}

func (g *gitRepo) write(file string, content []byte) *gitRepo {
err := ioutil.WriteFile(filepath.Join(g.dir, file), content, os.ModePerm)
func (g *gitRepo) write(file string, content string) *gitRepo {
err := ioutil.WriteFile(filepath.Join(g.dir, file), []byte(content), os.ModePerm)
failNowIfError(g.t, err)
return g
}
Expand Down
21 changes: 7 additions & 14 deletions pkg/skaffold/deploy/status_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestPollDeploymentRolloutStatus(t *testing.T) {
responses: []string{"dep successfully rolled out"},
},
exactCalls: 1,
duration: 500,
duration: 50,
},
{
description: "rollout returns error in the first attempt",
Expand All @@ -219,7 +219,7 @@ func TestPollDeploymentRolloutStatus(t *testing.T) {
},
shouldErr: true,
exactCalls: 1,
duration: 500,
duration: 50,
},
{
description: "rollout returns success before time out",
Expand All @@ -229,7 +229,7 @@ func TestPollDeploymentRolloutStatus(t *testing.T) {
"Waiting for rollout to finish: 0 of 1 updated replicas are available...",
"deployment.apps/dep successfully rolled out"},
},
duration: 800,
duration: 80,
exactCalls: 3,
},
{
Expand All @@ -240,22 +240,15 @@ func TestPollDeploymentRolloutStatus(t *testing.T) {
"Waiting for rollout to finish: 1 of 3 updated replicas are available...",
"Waiting for rollout to finish: 2 of 3 updated replicas are available..."},
},
duration: 1000,
duration: 100,
shouldErr: true,
timedOut: true,
},
}
originalPollingPeriod := defaultPollPeriodInMilliseconds
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
mock := test.mock
// Figure out why i can't use t.Override.
// Using t.Override throws an error "reflect: call of reflect.Value.Elem on func Value"
executeRolloutStatus = mock.Executefunc
defer func() { executeRolloutStatus = getRollOutStatus }()

defaultPollPeriodInMilliseconds = 100
defer func() { defaultPollPeriodInMilliseconds = originalPollingPeriod }()
t.Override(&executeRolloutStatus, test.mock.Executefunc)
t.Override(&defaultPollPeriodInMilliseconds, 10)

actual := &sync.Map{}
pollDeploymentRolloutStatus(context.Background(), &kubectl.CLI{}, "dep", time.Duration(test.duration)*time.Millisecond, actual)
Expand All @@ -267,7 +260,7 @@ func TestPollDeploymentRolloutStatus(t *testing.T) {
t.CheckError(test.shouldErr, err)
// Check number of calls only if command did not timeout since there could be n-1 or n or n+1 calls when command timed out
if !test.timedOut {
t.CheckDeepEqual(test.exactCalls, mock.called)
t.CheckDeepEqual(test.exactCalls, test.mock.called)
}
})
}
Expand Down
Loading

0 comments on commit a58b312

Please sign in to comment.