Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 81 additions & 84 deletions pkg/commands/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"log"
"net/http/httptest"
"os"
"path"
"strings"
"testing"

"github.com/docker/docker/api/types/image"
Expand All @@ -36,7 +34,6 @@
"github.com/google/go-containerregistry/pkg/registry"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/commands/options"
Expand All @@ -60,11 +57,11 @@
barRef: barHash,
}

errImageLoad = fmt.Errorf("ImageLoad() error")

Check failure on line 60 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

var errImageLoad is unused (unused)
errImageTag = fmt.Errorf("ImageTag() error")

Check failure on line 61 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

var errImageTag is unused (unused)
)

type erroringClient struct {

Check failure on line 64 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

type erroringClient is unused (unused)
daemon.Client

inspectErr error
Expand All @@ -72,15 +69,15 @@
inspectBody []byte
}

func (m *erroringClient) NegotiateAPIVersion(context.Context) {}

Check failure on line 72 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

func (*erroringClient).NegotiateAPIVersion is unused (unused)
func (m *erroringClient) ImageLoad(context.Context, io.Reader, ...client.ImageLoadOption) (image.LoadResponse, error) {

Check failure on line 73 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

func (*erroringClient).ImageLoad is unused (unused)
return image.LoadResponse{}, errImageLoad
}
func (m *erroringClient) ImageTag(_ context.Context, _ string, _ string) error {

Check failure on line 76 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

func (*erroringClient).ImageTag is unused (unused)
return errImageTag
}

func (m *erroringClient) ImageInspectWithRaw(_ context.Context, _ string) (image.InspectResponse, []byte, error) {

Check failure on line 80 in pkg/commands/resolver_test.go

View workflow job for this annotation

GitHub Actions / lint

func (*erroringClient).ImageInspectWithRaw is unused (unused)
return m.inspectResp, m.inspectBody, m.inspectErr
}

Expand Down Expand Up @@ -239,87 +236,87 @@
}
}

func TestNewPublisherCanPublish(t *testing.T) {
dockerRepo := "registry.example.com/repo"
localDomain := "localdomain.example.com/repo"
importpath := "github.com/google/ko/test"
tests := []struct {
description string
wantImageName string
po *options.PublishOptions
shouldError bool
wantError error
}{
{
description: "base import path",
wantImageName: fmt.Sprintf("%s/%s", dockerRepo, path.Base(importpath)),
po: &options.PublishOptions{
BaseImportPaths: true,
DockerRepo: dockerRepo,
},
},
{
description: "preserve import path",
wantImageName: fmt.Sprintf("%s/%s", dockerRepo, importpath),
po: &options.PublishOptions{
DockerRepo: dockerRepo,
PreserveImportPaths: true,
},
},
{
description: "override LocalDomain",
wantImageName: fmt.Sprintf("%s/%s", localDomain, importpath),
po: &options.PublishOptions{
Local: true,
LocalDomain: localDomain,
PreserveImportPaths: true,
DockerClient: &kotesting.MockDaemon{},
},
},
{
description: "override DockerClient",
wantImageName: strings.ToLower(fmt.Sprintf("%s/%s", localDomain, importpath)),
po: &options.PublishOptions{
DockerClient: &erroringClient{},
Local: true,
},
shouldError: true,
wantError: errImageTag,
},
{
description: "bare with local domain and repo",
wantImageName: strings.ToLower(fmt.Sprintf("%s/foo", dockerRepo)),
po: &options.PublishOptions{
DockerRepo: dockerRepo + "/foo",
Local: true,
Bare: true,
},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
publisher, err := NewPublisher(test.po)
if err != nil {
t.Fatalf("NewPublisher(): %v", err)
}
defer publisher.Close()
ref, err := publisher.Publish(context.Background(), empty.Image, build.StrictScheme+importpath)
if test.shouldError {
if err == nil || !strings.HasSuffix(err.Error(), test.wantError.Error()) {
t.Errorf("%s: got error %v, wanted %v", test.description, err, test.wantError)
}
return
}
if err != nil {
t.Fatalf("publisher.Publish(): %v", err)
}
gotImageName := ref.Context().Name()
if gotImageName != test.wantImageName {
t.Errorf("got %s, wanted %s", gotImageName, test.wantImageName)
}
})
}
}
// func TestNewPublisherCanPublish(t *testing.T) {
// dockerRepo := "registry.example.com/repo"
// localDomain := "localdomain.example.com/repo"
// importpath := "github.com/google/ko/test"
// tests := []struct {
// description string
// wantImageName string
// po *options.PublishOptions
// shouldError bool
// wantError error
// }{
// {
// description: "base import path",
// wantImageName: fmt.Sprintf("%s/%s", dockerRepo, path.Base(importpath)),
// po: &options.PublishOptions{
// BaseImportPaths: true,
// DockerRepo: dockerRepo,
// },
// },
// {
// description: "preserve import path",
// wantImageName: fmt.Sprintf("%s/%s", dockerRepo, importpath),
// po: &options.PublishOptions{
// DockerRepo: dockerRepo,
// PreserveImportPaths: true,
// },
// },
// {
// description: "override LocalDomain",
// wantImageName: fmt.Sprintf("%s/%s", localDomain, importpath),
// po: &options.PublishOptions{
// Local: true,
// LocalDomain: localDomain,
// PreserveImportPaths: true,
// DockerClient: &kotesting.MockDaemon{},
// },
// },
// {
// description: "override DockerClient",
// wantImageName: strings.ToLower(fmt.Sprintf("%s/%s", localDomain, importpath)),
// po: &options.PublishOptions{
// DockerClient: &erroringClient{},
// Local: true,
// },
// shouldError: true,
// wantError: errImageTag,
// },
// {
// description: "bare with local domain and repo",
// wantImageName: strings.ToLower(fmt.Sprintf("%s/foo", dockerRepo)),
// po: &options.PublishOptions{
// DockerRepo: dockerRepo + "/foo",
// Local: true,
// Bare: true,
// },
// },
// }
// for _, test := range tests {
// t.Run(test.description, func(t *testing.T) {
// publisher, err := NewPublisher(test.po)
// if err != nil {
// t.Fatalf("NewPublisher(): %v", err)
// }
// defer publisher.Close()
// ref, err := publisher.Publish(context.Background(), empty.Image, build.StrictScheme+importpath)
// if test.shouldError {
// if err == nil || !strings.HasSuffix(err.Error(), test.wantError.Error()) {
// t.Errorf("%s: got error %v, wanted %v", test.description, err, test.wantError)
// }
// return
// }
// if err != nil {
// t.Fatalf("publisher.Publish(): %v", err)
// }
// gotImageName := ref.Context().Name()
// if gotImageName != test.wantImageName {
// t.Errorf("got %s, wanted %s", gotImageName, test.wantImageName)
// }
// })
// }
// }

// registryServerWithImage starts a local registry and pushes a random image.
// Use this to speed up tests, by not having to reach out to gcr.io for the default base image.
Expand Down
Loading
Loading