1
0
mirror of https://github.com/ko-build/ko.git synced 2025-07-12 23:50:31 +02:00

Do not require docker installed for tests (#438)

This commit is contained in:
jonjohnsonjr
2021-09-20 04:07:56 -07:00
committed by GitHub
parent e73be50bbe
commit 45467f076b
6 changed files with 73 additions and 45 deletions

View File

@ -264,6 +264,7 @@ func TestNewPublisherCanPublish(t *testing.T) {
Local: true, Local: true,
LocalDomain: localDomain, LocalDomain: localDomain,
PreserveImportPaths: true, PreserveImportPaths: true,
DockerClient: &kotesting.MockDaemon{},
}, },
}, },
{ {

View File

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

View File

@ -14,38 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package publish package publish_test
import ( import (
"context" "context"
"io"
"io/ioutil"
"strings" "strings"
"testing" "testing"
"github.com/docker/docker/api/types"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/random" "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) { func TestDaemon(t *testing.T) {
importpath := "github.com/google/ko" importpath := "github.com/google/ko"
img, err := random.Image(1024, 1) img, err := random.Image(1024, 1)
@ -53,7 +33,8 @@ func TestDaemon(t *testing.T) {
t.Fatalf("random.Image() = %v", err) 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 { if err != nil {
t.Fatalf("NewDaemon() = %v", err) t.Fatalf("NewDaemon() = %v", err)
} }
@ -66,15 +47,14 @@ func TestDaemon(t *testing.T) {
} }
func TestDaemonTags(t *testing.T) { func TestDaemonTags(t *testing.T) {
Tags = nil
importpath := "github.com/google/ko" importpath := "github.com/google/ko"
img, err := random.Image(1024, 1) img, err := random.Image(1024, 1)
if err != nil { if err != nil {
t.Fatalf("random.Image() = %v", err) 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 { if err != nil {
t.Fatalf("NewDaemon() = %v", err) 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"} expected := []string{"ko.local/98b8c7facdad74510a7cae0cd368eb4e:v2.0.0", "ko.local/98b8c7facdad74510a7cae0cd368eb4e:v1.2.3", "ko.local/98b8c7facdad74510a7cae0cd368eb4e:production"}
for i, v := range expected { for i, v := range expected {
if Tags[i] != v { if client.Tags[i] != v {
t.Errorf("Expected tag %v got %v", v, Tags[i]) t.Errorf("Expected tag %v got %v", v, client.Tags[i])
} }
} }
} }
func TestDaemonDomain(t *testing.T) { func TestDaemonDomain(t *testing.T) {
Tags = nil
importpath := "github.com/google/ko" importpath := "github.com/google/ko"
img, err := random.Image(1024, 1) img, err := random.Image(1024, 1)
if err != nil { if err != nil {
@ -104,7 +82,8 @@ func TestDaemonDomain(t *testing.T) {
} }
localDomain := "registry.example.com/repository" 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 { if err != nil {
t.Fatalf("NewDaemon() = %v", err) t.Fatalf("NewDaemon() = %v", err)
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package publish package publish_test
import ( import (
"context" "context"
@ -31,6 +31,7 @@ import (
"github.com/google/go-containerregistry/pkg/registry" "github.com/google/go-containerregistry/pkg/registry"
"github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/ko/pkg/build" "github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
) )
var ( var (
@ -56,7 +57,7 @@ func TestDefault(t *testing.T) {
} }
repoName := fmt.Sprintf("%s/%s", u.Host, base) repoName := fmt.Sprintf("%s/%s", u.Host, base)
def, err := NewDefault(repoName) def, err := publish.NewDefault(repoName)
if err != nil { if err != nil {
t.Errorf("NewDefault() = %v", err) t.Errorf("NewDefault() = %v", err)
} }
@ -94,7 +95,7 @@ func TestDefaultWithCustomNamer(t *testing.T) {
repoName := fmt.Sprintf("%s/%s", u.Host, base) 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 { if err != nil {
t.Errorf("NewDefault() = %v", err) t.Errorf("NewDefault() = %v", err)
} }
@ -126,7 +127,7 @@ func TestDefaultWithTags(t *testing.T) {
repoName := fmt.Sprintf("%s/%s", u.Host, base) 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 { if err != nil {
t.Errorf("NewDefault() = %v", err) t.Errorf("NewDefault() = %v", err)
} }
@ -207,7 +208,7 @@ func TestDefaultWithReleaseTag(t *testing.T) {
repoName := fmt.Sprintf("%s/%s", u.Host, base) 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 { if err != nil {
t.Errorf("NewDefault() = %v", err) t.Errorf("NewDefault() = %v", err)
} }
@ -225,7 +226,7 @@ func TestDefaultWithReleaseTag(t *testing.T) {
t.Errorf("Tag v1.2.3 was not created.") 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 { if err != nil {
t.Errorf("NewDefault() = %v", err) t.Errorf("NewDefault() = %v", err)
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package publish package publish_test
import ( import (
"context" "context"
@ -22,6 +22,7 @@ import (
"testing" "testing"
"github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/ko/pkg/publish"
) )
func TestMulti(t *testing.T) { func TestMulti(t *testing.T) {
@ -40,7 +41,7 @@ func TestMulti(t *testing.T) {
defer fp.Close() defer fp.Close()
defer os.Remove(fp.Name()) 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") tmp, err := ioutil.TempDir("/tmp", "ko")
if err != nil { if err != nil {
@ -48,12 +49,12 @@ func TestMulti(t *testing.T) {
} }
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
lp, err := NewLayout(tmp) lp, err := publish.NewLayout(tmp)
if err != nil { if err != nil {
t.Errorf("NewLayout() = %v", err) t.Errorf("NewLayout() = %v", err)
} }
p := MultiPublisher(lp, tp) p := publish.MultiPublisher(lp, tp)
if _, err := p.Publish(context.Background(), img, importpath); err != nil { if _, err := p.Publish(context.Background(), img, importpath); err != nil {
t.Errorf("Publish() = %v", err) t.Errorf("Publish() = %v", err)
} }
@ -69,7 +70,7 @@ func TestMulti_Zero(t *testing.T) {
t.Fatalf("random.Image() = %v", err) 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 { if _, err := p.Publish(context.Background(), img, "foo"); err == nil {
t.Errorf("Publish() got nil error") t.Errorf("Publish() got nil error")
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package publish package publish_test
import ( import (
"context" "context"
@ -24,6 +24,7 @@ import (
"github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/ko/pkg/publish"
) )
func TestTarball(t *testing.T) { func TestTarball(t *testing.T) {
@ -60,7 +61,7 @@ func TestTarball(t *testing.T) {
"debug", "debug",
}} }}
for _, tags := range tagss { 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 { if d, err := tp.Publish(context.Background(), img, importpath); err != nil {
t.Errorf("Publish() = %v", err) t.Errorf("Publish() = %v", err)
} else if !strings.HasPrefix(d.String(), tag.Repository.String()) { } else if !strings.HasPrefix(d.String(), tag.Repository.String()) {