Skip to content

Commit

Permalink
Do not require docker installed for tests (ko-build#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr authored Sep 20, 2021
1 parent e73be50 commit 45467f0
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 45 deletions.
1 change: 1 addition & 0 deletions pkg/commands/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func TestNewPublisherCanPublish(t *testing.T) {
Local: true,
LocalDomain: localDomain,
PreserveImportPaths: true,
DockerClient: &kotesting.MockDaemon{},
},
},
{
Expand Down
45 changes: 45 additions & 0 deletions pkg/internal/testing/daemon.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 11 additions & 32 deletions pkg/publish/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,27 @@ 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)
if err != nil {
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)
}
Expand All @@ -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)
}
Expand All @@ -88,23 +68,22 @@ 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 {
t.Fatalf("random.Image() = %v", err)
}

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)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/publish/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/publish/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -40,20 +41,20 @@ 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 {
t.Fatal(err)
}
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)
}
Expand All @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/publish/tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 45467f0

Please sign in to comment.