diff --git a/pkg/commands/resolver_test.go b/pkg/commands/resolver_test.go index f0da739fc9..71f697a3cb 100644 --- a/pkg/commands/resolver_test.go +++ b/pkg/commands/resolver_test.go @@ -264,6 +264,7 @@ func TestNewPublisherCanPublish(t *testing.T) { Local: true, LocalDomain: localDomain, PreserveImportPaths: true, + DockerClient: &kotesting.MockDaemon{}, }, }, { diff --git a/pkg/internal/testing/daemon.go b/pkg/internal/testing/daemon.go new file mode 100644 index 0000000000..8faecff1ef --- /dev/null +++ b/pkg/internal/testing/daemon.go @@ -0,0 +1,45 @@ +// Copyright 2021 Google LLC All Rights Reserved. +// +// 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 testing + +import ( + "context" + "io" + "io/ioutil" + "strings" + + "github.com/docker/docker/api/types" + "github.com/google/go-containerregistry/pkg/v1/daemon" +) + +type MockDaemon struct { + daemon.Client + Tags []string +} + +func (m *MockDaemon) NegotiateAPIVersion(context.Context) {} +func (m *MockDaemon) ImageLoad(context.Context, io.Reader, bool) (types.ImageLoadResponse, error) { + return types.ImageLoadResponse{ + Body: ioutil.NopCloser(strings.NewReader("Loaded")), + }, nil +} + +func (m *MockDaemon) ImageTag(_ context.Context, _ string, tag string) error { + if m.Tags == nil { + m.Tags = []string{} + } + m.Tags = append(m.Tags, tag) + return nil +} diff --git a/pkg/publish/daemon_test.go b/pkg/publish/daemon_test.go index 1209c758aa..1225def87b 100644 --- a/pkg/publish/daemon_test.go +++ b/pkg/publish/daemon_test.go @@ -14,38 +14,18 @@ See the License for the specific language governing permissions and limitations under the License. */ -package publish +package publish_test import ( "context" - "io" - "io/ioutil" "strings" "testing" - "github.com/docker/docker/api/types" - "github.com/google/go-containerregistry/pkg/v1/daemon" "github.com/google/go-containerregistry/pkg/v1/random" + kotesting "github.com/google/ko/pkg/internal/testing" + "github.com/google/ko/pkg/publish" ) -type mockClient struct { - daemon.Client -} - -func (m *mockClient) NegotiateAPIVersion(context.Context) {} -func (m *mockClient) ImageLoad(context.Context, io.Reader, bool) (types.ImageLoadResponse, error) { - return types.ImageLoadResponse{ - Body: ioutil.NopCloser(strings.NewReader("Loaded")), - }, nil -} - -func (m *mockClient) ImageTag(_ context.Context, _ string, tag string) error { - Tags = append(Tags, tag) - return nil -} - -var Tags []string - func TestDaemon(t *testing.T) { importpath := "github.com/google/ko" img, err := random.Image(1024, 1) @@ -53,7 +33,8 @@ func TestDaemon(t *testing.T) { t.Fatalf("random.Image() = %v", err) } - def, err := NewDaemon(md5Hash, []string{}, WithDockerClient(&mockClient{})) + client := &kotesting.MockDaemon{} + def, err := publish.NewDaemon(md5Hash, []string{}, publish.WithDockerClient(client)) if err != nil { t.Fatalf("NewDaemon() = %v", err) } @@ -66,15 +47,14 @@ func TestDaemon(t *testing.T) { } func TestDaemonTags(t *testing.T) { - Tags = nil - importpath := "github.com/google/ko" img, err := random.Image(1024, 1) if err != nil { t.Fatalf("random.Image() = %v", err) } - def, err := NewDaemon(md5Hash, []string{"v2.0.0", "v1.2.3", "production"}, WithDockerClient(&mockClient{})) + client := &kotesting.MockDaemon{} + def, err := publish.NewDaemon(md5Hash, []string{"v2.0.0", "v1.2.3", "production"}, publish.WithDockerClient(client)) if err != nil { t.Fatalf("NewDaemon() = %v", err) } @@ -88,15 +68,13 @@ func TestDaemonTags(t *testing.T) { expected := []string{"ko.local/98b8c7facdad74510a7cae0cd368eb4e:v2.0.0", "ko.local/98b8c7facdad74510a7cae0cd368eb4e:v1.2.3", "ko.local/98b8c7facdad74510a7cae0cd368eb4e:production"} for i, v := range expected { - if Tags[i] != v { - t.Errorf("Expected tag %v got %v", v, Tags[i]) + if client.Tags[i] != v { + t.Errorf("Expected tag %v got %v", v, client.Tags[i]) } } } func TestDaemonDomain(t *testing.T) { - Tags = nil - importpath := "github.com/google/ko" img, err := random.Image(1024, 1) if err != nil { @@ -104,7 +82,8 @@ func TestDaemonDomain(t *testing.T) { } localDomain := "registry.example.com/repository" - def, err := NewDaemon(md5Hash, []string{}, WithLocalDomain(localDomain), WithDockerClient(&mockClient{})) + client := &kotesting.MockDaemon{} + def, err := publish.NewDaemon(md5Hash, []string{}, publish.WithLocalDomain(localDomain), publish.WithDockerClient(client)) if err != nil { t.Fatalf("NewDaemon() = %v", err) } diff --git a/pkg/publish/default_test.go b/pkg/publish/default_test.go index 37dc7ec042..7eb896620d 100644 --- a/pkg/publish/default_test.go +++ b/pkg/publish/default_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package publish +package publish_test import ( "context" @@ -31,6 +31,7 @@ import ( "github.com/google/go-containerregistry/pkg/registry" "github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/ko/pkg/build" + "github.com/google/ko/pkg/publish" ) var ( @@ -56,7 +57,7 @@ func TestDefault(t *testing.T) { } repoName := fmt.Sprintf("%s/%s", u.Host, base) - def, err := NewDefault(repoName) + def, err := publish.NewDefault(repoName) if err != nil { t.Errorf("NewDefault() = %v", err) } @@ -94,7 +95,7 @@ func TestDefaultWithCustomNamer(t *testing.T) { repoName := fmt.Sprintf("%s/%s", u.Host, base) - def, err := NewDefault(repoName, WithNamer(md5Hash)) + def, err := publish.NewDefault(repoName, publish.WithNamer(md5Hash)) if err != nil { t.Errorf("NewDefault() = %v", err) } @@ -126,7 +127,7 @@ func TestDefaultWithTags(t *testing.T) { repoName := fmt.Sprintf("%s/%s", u.Host, base) - def, err := NewDefault(repoName, WithTags([]string{"notLatest", "v1.2.3"})) + def, err := publish.NewDefault(repoName, publish.WithTags([]string{"notLatest", "v1.2.3"})) if err != nil { t.Errorf("NewDefault() = %v", err) } @@ -207,7 +208,7 @@ func TestDefaultWithReleaseTag(t *testing.T) { repoName := fmt.Sprintf("%s/%s", u.Host, base) - def, err := NewDefault(repoName, WithTags([]string{releaseTag})) + def, err := publish.NewDefault(repoName, publish.WithTags([]string{releaseTag})) if err != nil { t.Errorf("NewDefault() = %v", err) } @@ -225,7 +226,7 @@ func TestDefaultWithReleaseTag(t *testing.T) { t.Errorf("Tag v1.2.3 was not created.") } - def, err = NewDefault(repoName, WithTags([]string{releaseTag}), WithTagOnly(true)) + def, err = publish.NewDefault(repoName, publish.WithTags([]string{releaseTag}), publish.WithTagOnly(true)) if err != nil { t.Errorf("NewDefault() = %v", err) } diff --git a/pkg/publish/multi_test.go b/pkg/publish/multi_test.go index c819066de8..fda208f4ad 100644 --- a/pkg/publish/multi_test.go +++ b/pkg/publish/multi_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package publish +package publish_test import ( "context" @@ -22,6 +22,7 @@ import ( "testing" "github.com/google/go-containerregistry/pkg/v1/random" + "github.com/google/ko/pkg/publish" ) func TestMulti(t *testing.T) { @@ -40,7 +41,7 @@ func TestMulti(t *testing.T) { defer fp.Close() defer os.Remove(fp.Name()) - tp := NewTarball(fp.Name(), repoName, md5Hash, []string{}) + tp := publish.NewTarball(fp.Name(), repoName, md5Hash, []string{}) tmp, err := ioutil.TempDir("/tmp", "ko") if err != nil { @@ -48,12 +49,12 @@ func TestMulti(t *testing.T) { } defer os.RemoveAll(tmp) - lp, err := NewLayout(tmp) + lp, err := publish.NewLayout(tmp) if err != nil { t.Errorf("NewLayout() = %v", err) } - p := MultiPublisher(lp, tp) + p := publish.MultiPublisher(lp, tp) if _, err := p.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } @@ -69,7 +70,7 @@ func TestMulti_Zero(t *testing.T) { t.Fatalf("random.Image() = %v", err) } - p := MultiPublisher() // No publishers. + p := publish.MultiPublisher() // No publishers. if _, err := p.Publish(context.Background(), img, "foo"); err == nil { t.Errorf("Publish() got nil error") } diff --git a/pkg/publish/tarball_test.go b/pkg/publish/tarball_test.go index e9c7da2137..c6e59037b8 100644 --- a/pkg/publish/tarball_test.go +++ b/pkg/publish/tarball_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package publish +package publish_test import ( "context" @@ -24,6 +24,7 @@ import ( "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/random" + "github.com/google/ko/pkg/publish" ) func TestTarball(t *testing.T) { @@ -60,7 +61,7 @@ func TestTarball(t *testing.T) { "debug", }} for _, tags := range tagss { - tp := NewTarball(fp.Name(), repoName, md5Hash, tags) + tp := publish.NewTarball(fp.Name(), repoName, md5Hash, tags) if d, err := tp.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), tag.Repository.String()) {