From d90c33f6cb47e29ea2a8600abf39e127a2e921b7 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 30 Jul 2019 09:19:01 +0200 Subject: [PATCH 1/2] Faster tests + Optimize all tests that take over .6s + Use t.Override in placed where it should be used + Remove TestRemoteDigest that was not really testing anything + Run git tests in parallel Signed-off-by: David Gageot --- cmd/skaffold/app/cmd/runner_test.go | 5 +++ pkg/skaffold/build/cache/retrieve_test.go | 8 +++- pkg/skaffold/build/tag/git_commit_test.go | 2 + pkg/skaffold/deploy/status_check_test.go | 21 ++++------ pkg/skaffold/docker/remote_test.go | 38 ------------------- .../portforward/pod_forwarder_test.go | 15 +++++--- .../portforward/resource_forwarder_test.go | 4 +- pkg/skaffold/kubernetes/wait_test.go | 8 ++-- pkg/skaffold/runner/deploy_test.go | 6 +-- pkg/skaffold/runner/runner_test.go | 11 +++++- pkg/skaffold/update/update.go | 9 ++++- 11 files changed, 53 insertions(+), 74 deletions(-) delete mode 100644 pkg/skaffold/docker/remote_test.go diff --git a/cmd/skaffold/app/cmd/runner_test.go b/cmd/skaffold/app/cmd/runner_test.go index 1f770d7e1db..8625230f3f4 100644 --- a/cmd/skaffold/app/cmd/runner_test.go +++ b/cmd/skaffold/app/cmd/runner_test.go @@ -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) { @@ -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() diff --git a/pkg/skaffold/build/cache/retrieve_test.go b/pkg/skaffold/build/cache/retrieve_test.go index 1418b6e2914..68aebcefd5d 100644 --- a/pkg/skaffold/build/cache/retrieve_test.go +++ b/pkg/skaffold/build/cache/retrieve_test.go @@ -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 { @@ -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(). @@ -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": diff --git a/pkg/skaffold/build/tag/git_commit_test.go b/pkg/skaffold/build/tag/git_commit_test.go index 8133866b3e9..3b1edb688f3 100644 --- a/pkg/skaffold/build/tag/git_commit_test.go +++ b/pkg/skaffold/build/tag/git_commit_test.go @@ -331,6 +331,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) { 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) diff --git a/pkg/skaffold/deploy/status_check_test.go b/pkg/skaffold/deploy/status_check_test.go index e0d194ab122..f9891790a0a 100644 --- a/pkg/skaffold/deploy/status_check_test.go +++ b/pkg/skaffold/deploy/status_check_test.go @@ -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", @@ -219,7 +219,7 @@ func TestPollDeploymentRolloutStatus(t *testing.T) { }, shouldErr: true, exactCalls: 1, - duration: 500, + duration: 50, }, { description: "rollout returns success before time out", @@ -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, }, { @@ -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) @@ -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) } }) } diff --git a/pkg/skaffold/docker/remote_test.go b/pkg/skaffold/docker/remote_test.go deleted file mode 100644 index 7483c9fd619..00000000000 --- a/pkg/skaffold/docker/remote_test.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -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 docker - -import ( - "strings" - "testing" -) - -func TestRemoteDigest(t *testing.T) { - validReferences := []string{ - "python", - "python:3-slim", - } - - for _, ref := range validReferences { - _, err := RemoteDigest(ref, nil) - - // Ignore networking errors - if err != nil && strings.Contains(err.Error(), "could not parse") { - t.Errorf("unable to parse %q: %v", ref, err) - } - } -} diff --git a/pkg/skaffold/kubernetes/portforward/pod_forwarder_test.go b/pkg/skaffold/kubernetes/portforward/pod_forwarder_test.go index 3fca04561b6..f7173c385ee 100644 --- a/pkg/skaffold/kubernetes/portforward/pod_forwarder_test.go +++ b/pkg/skaffold/kubernetes/portforward/pod_forwarder_test.go @@ -437,7 +437,7 @@ func TestAutomaticPortForwardPod(t *testing.T) { event.InitializeState(latest.BuildConfig{}) taken := map[int]struct{}{} - forwardingTimeoutTime = time.Second + t.Override(&forwardingTimeoutTime, 500*time.Millisecond) t.Override(&retrieveAvailablePort, mockRetrieveAvailablePort(taken, test.availablePorts)) entryManager := EntryManager{ @@ -481,14 +481,17 @@ func TestStartPodForwarder(t *testing.T) { description: "pod modified event", entryExpected: true, event: watch.Modified, - }, { + }, + { description: "pod error event", event: watch.Error, - }, { + }, + { description: "event isn't for a pod", obj: &v1.Service{}, event: watch.Modified, - }, { + }, + { description: "event is deleted", event: watch.Deleted, }, @@ -551,8 +554,8 @@ func TestStartPodForwarder(t *testing.T) { fakeWatcher.Action(test.event, obj) - // poll for 2 seconds for the pod resource to be forwarded - err := wait.PollImmediate(time.Second, 2*time.Second, func() (bool, error) { + // wait for the pod resource to be forwarded + err := wait.PollImmediate(10*time.Millisecond, 100*time.Millisecond, func() (bool, error) { _, ok := fakeForwarder.forwardedResources.Load("mycontainer-default-myport-8080") return ok, nil }) diff --git a/pkg/skaffold/kubernetes/portforward/resource_forwarder_test.go b/pkg/skaffold/kubernetes/portforward/resource_forwarder_test.go index fc6242e616d..43ea2bec0b3 100644 --- a/pkg/skaffold/kubernetes/portforward/resource_forwarder_test.go +++ b/pkg/skaffold/kubernetes/portforward/resource_forwarder_test.go @@ -131,7 +131,7 @@ func TestStart(t *testing.T) { t.Fatalf("error starting resource forwarder: %v", err) } // poll up to 10 seconds for the resources to be forwarded - err := wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) { + err := wait.PollImmediate(100*time.Millisecond, 10*time.Second, func() (bool, error) { return len(test.expected) == fakeForwarder.forwardedResources.Length(), nil }) if err != nil { @@ -245,7 +245,7 @@ func TestUserDefinedResources(t *testing.T) { t.Fatalf("error starting resource forwarder: %v", err) } // poll up to 10 seconds for the resources to be forwarded - err := wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) { + err := wait.PollImmediate(100*time.Millisecond, 10*time.Second, func() (bool, error) { return len(expected) == fakeForwarder.forwardedResources.Length(), nil }) if err != nil { diff --git a/pkg/skaffold/kubernetes/wait_test.go b/pkg/skaffold/kubernetes/wait_test.go index 10fc4459685..3a3497956c6 100644 --- a/pkg/skaffold/kubernetes/wait_test.go +++ b/pkg/skaffold/kubernetes/wait_test.go @@ -22,10 +22,8 @@ import ( "time" "github.com/GoogleContainerTools/skaffold/testutil" - - "k8s.io/apimachinery/pkg/watch" - v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/watch" fakekubeclientset "k8s.io/client-go/kubernetes/fake" ) @@ -60,7 +58,7 @@ func TestWaitForPodSucceeded(t *testing.T) { errChan := make(chan error) go func() { - errChan <- WaitForPodSucceeded(context.TODO(), fakePods, "", 5*time.Second) + errChan <- WaitForPodSucceeded(context.TODO(), fakePods, "", 50*time.Millisecond) }() for _, phase := range test.phases { @@ -72,7 +70,7 @@ func TestWaitForPodSucceeded(t *testing.T) { Phase: phase, }, }) - time.Sleep(time.Second) + time.Sleep(10 * time.Millisecond) } err := <-errChan diff --git a/pkg/skaffold/runner/deploy_test.go b/pkg/skaffold/runner/deploy_test.go index 391005588b8..8b59da8dcee 100644 --- a/pkg/skaffold/runner/deploy_test.go +++ b/pkg/skaffold/runner/deploy_test.go @@ -60,14 +60,10 @@ func TestDeploy(t *testing.T) { dummyStatusCheck := func(ctx context.Context, l *deploy.DefaultLabeller, runCtx *runcontext.RunContext) error { return nil } - originalStatusCheck := deploy.StatusCheck for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { t.SetupFakeKubernetesContext(api.Config{CurrentContext: "cluster1"}) - // Figure out why i can't use t.Override. - // Using t.Override throws an error "reflect: call of reflect.Value.Elem on func Value" - statusCheck = dummyStatusCheck - defer func() { statusCheck = originalStatusCheck }() + t.Override(&statusCheck, dummyStatusCheck) runner := createRunner(t, test.testBench, nil) runner.runCtx.Opts.StatusCheck = test.statusCheck diff --git a/pkg/skaffold/runner/runner_test.go b/pkg/skaffold/runner/runner_test.go index 8d1e136b11d..e9d33d2f6ba 100644 --- a/pkg/skaffold/runner/runner_test.go +++ b/pkg/skaffold/runner/runner_test.go @@ -199,7 +199,16 @@ func (r *SkaffoldRunner) WithMonitor(m filemon.Monitor) *SkaffoldRunner { } func createRunner(t *testutil.T, testBench *TestBench, monitor filemon.Monitor) *SkaffoldRunner { - cfg := &latest.SkaffoldConfig{} + cfg := &latest.SkaffoldConfig{ + Pipeline: latest.Pipeline{ + Build: latest.BuildConfig{ + TagPolicy: latest.TagPolicy{ + // Use the fastest tagger + ShaTagger: &latest.ShaTagger{}, + }, + }, + }, + } defaults.Set(cfg) runCtx := &runcontext.RunContext{ diff --git a/pkg/skaffold/update/update.go b/pkg/skaffold/update/update.go index eb706cf6905..ccac116bde8 100644 --- a/pkg/skaffold/update/update.go +++ b/pkg/skaffold/update/update.go @@ -28,6 +28,11 @@ import ( "github.com/pkg/errors" ) +// Fot testing +var ( + GetLatestAndCurrentVersion = getLatestAndCurrentVersion +) + const latestVersionURL = "https://storage.googleapis.com/skaffold/releases/latest/VERSION" // IsUpdateCheckEnabled returns whether or not the update check is enabled @@ -42,9 +47,9 @@ func IsUpdateCheckEnabled() bool { return v == "" || strings.ToLower(v) == "true" } -// GetLatestAndCurrentVersion uses a VERSION file stored on GCS to determine the latest released version +// getLatestAndCurrentVersion uses a VERSION file stored on GCS to determine the latest released version // and returns it with the current version of Skaffold -func GetLatestAndCurrentVersion() (semver.Version, semver.Version, error) { +func getLatestAndCurrentVersion() (semver.Version, semver.Version, error) { none := semver.Version{} resp, err := http.Get(latestVersionURL) if err != nil { From e250960f75838ec1be7fea8d5b2d2c35aec91ce9 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 30 Jul 2019 11:57:05 +0200 Subject: [PATCH 2/2] Simplify GitTagger tests Signed-off-by: David Gageot --- pkg/skaffold/build/tag/git_commit_test.go | 106 +++++++++------------- 1 file changed, 45 insertions(+), 61 deletions(-) diff --git a/pkg/skaffold/build/tag/git_commit_test.go b/pkg/skaffold/build/tag/git_commit_test.go index 3b1edb688f3..ed40678180e 100644 --- a/pkg/skaffold/build/tag/git_commit_test.go +++ b/pkg/skaffold/build/tag/git_commit_test.go @@ -43,7 +43,6 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) { variantAbbrevTreeSha string createGitRepo func(string) subDir string - shouldErr bool }{ { description: "clean worktree without tag", @@ -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") }, @@ -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") @@ -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") @@ -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") }, }, { @@ -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") }, }, { @@ -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") }, }, { @@ -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") }, @@ -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") @@ -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") @@ -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") }, @@ -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") }, @@ -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", }, @@ -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", }, @@ -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") }, @@ -314,21 +313,6 @@ 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() @@ -337,20 +321,20 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) { 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) + } }) } } @@ -384,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 }