@@ -22,13 +22,16 @@ import (
22
22
"fmt"
23
23
"io"
24
24
"io/ioutil"
25
+ "path"
25
26
"strings"
26
27
"testing"
27
28
29
+ "github.com/docker/docker/api/types"
28
30
"github.com/google/go-cmp/cmp"
29
31
"github.com/google/go-cmp/cmp/cmpopts"
30
32
"github.com/google/go-containerregistry/pkg/name"
31
33
v1 "github.com/google/go-containerregistry/pkg/v1"
34
+ "github.com/google/go-containerregistry/pkg/v1/daemon"
32
35
"github.com/google/go-containerregistry/pkg/v1/empty"
33
36
"github.com/google/go-containerregistry/pkg/v1/random"
34
37
"github.com/google/ko/pkg/build"
52
55
fooRef : fooHash ,
53
56
barRef : barHash ,
54
57
}
58
+
59
+ errImageLoad = fmt .Errorf ("ImageLoad() error" )
60
+ errImageTag = fmt .Errorf ("ImageTag() error" )
55
61
)
56
62
63
+ type erroringClient struct {
64
+ daemon.Client
65
+ }
66
+
67
+ func (m * erroringClient ) NegotiateAPIVersion (context.Context ) {}
68
+ func (m * erroringClient ) ImageLoad (context.Context , io.Reader , bool ) (types.ImageLoadResponse , error ) {
69
+ return types.ImageLoadResponse {}, errImageLoad
70
+ }
71
+ func (m * erroringClient ) ImageTag (_ context.Context , _ string , _ string ) error {
72
+ return errImageTag
73
+ }
74
+
57
75
func TestResolveMultiDocumentYAMLs (t * testing.T ) {
58
76
refs := []string {fooRef , barRef }
59
77
hashes := []v1.Hash {fooHash , barHash }
@@ -138,14 +156,14 @@ kind: Bar
138
156
}
139
157
}
140
158
141
- func TestMakeBuilder (t * testing.T ) {
159
+ func TestNewBuilderCanBuild (t * testing.T ) {
142
160
ctx := context .Background ()
143
161
bo := & options.BuildOptions {
144
162
ConcurrentBuilds : 1 ,
145
163
}
146
164
builder , err := NewBuilder (ctx , bo )
147
165
if err != nil {
148
- t .Fatalf ("MakeBuilder (): %v" , err )
166
+ t .Fatalf ("NewBuilder (): %v" , err )
149
167
}
150
168
res , err := builder .Build (ctx , "ko://github.com/google/ko/test" )
151
169
if err != nil {
@@ -158,29 +176,74 @@ func TestMakeBuilder(t *testing.T) {
158
176
fmt .Println (gotDigest .String ())
159
177
}
160
178
161
- func TestMakePublisher (t * testing.T ) {
162
- repo := "registry.example.com/repository"
163
- po := & options.PublishOptions {
164
- DockerRepo : repo ,
165
- PreserveImportPaths : true ,
166
- }
167
- publisher , err := NewPublisher (po )
168
- if err != nil {
169
- t .Fatalf ("MakePublisher(): %v" , err )
170
- }
171
- defer publisher .Close ()
172
- ctx := context .Background ()
179
+ func TestNewPublisherCanPublish (t * testing.T ) {
180
+ dockerRepo := "registry.example.com/repo"
181
+ localDomain := "localdomain.example.com/repo"
173
182
importpath := "github.com/google/ko/test"
174
- importpathWithScheme := build .StrictScheme + importpath
175
- buildResult := empty .Index
176
- ref , err := publisher .Publish (ctx , buildResult , importpathWithScheme )
177
- if err != nil {
178
- t .Fatalf ("publisher.Publish(): %v" , err )
183
+ tests := []struct {
184
+ description string
185
+ wantImageName string
186
+ po * options.PublishOptions
187
+ shouldError bool
188
+ wantError error
189
+ }{
190
+ {
191
+ description : "base import path" ,
192
+ wantImageName : fmt .Sprintf ("%s/%s" , dockerRepo , path .Base (importpath )),
193
+ po : & options.PublishOptions {
194
+ BaseImportPaths : true ,
195
+ DockerRepo : dockerRepo ,
196
+ },
197
+ },
198
+ {
199
+ description : "preserve import path" ,
200
+ wantImageName : fmt .Sprintf ("%s/%s" , dockerRepo , importpath ),
201
+ po : & options.PublishOptions {
202
+ DockerRepo : dockerRepo ,
203
+ PreserveImportPaths : true ,
204
+ },
205
+ },
206
+ {
207
+ description : "override LocalDomain" ,
208
+ wantImageName : fmt .Sprintf ("%s/%s" , localDomain , importpath ),
209
+ po : & options.PublishOptions {
210
+ Local : true ,
211
+ LocalDomain : localDomain ,
212
+ PreserveImportPaths : true ,
213
+ },
214
+ },
215
+ {
216
+ description : "override DockerClient" ,
217
+ wantImageName : strings .ToLower (fmt .Sprintf ("%s/%s" , localDomain , importpath )),
218
+ po : & options.PublishOptions {
219
+ DockerClient : & erroringClient {},
220
+ Local : true ,
221
+ },
222
+ shouldError : true ,
223
+ wantError : errImageLoad ,
224
+ },
179
225
}
180
- gotImageName := ref .Context ().Name ()
181
- wantImageName := strings .ToLower (fmt .Sprintf ("%s/%s" , repo , importpath ))
182
- if gotImageName != wantImageName {
183
- t .Errorf ("got %s, wanted %s" , gotImageName , wantImageName )
226
+ for _ , test := range tests {
227
+ publisher , err := NewPublisher (test .po )
228
+ if err != nil {
229
+ t .Fatalf ("%s: NewPublisher(): %v" , test .description , err )
230
+ }
231
+ defer publisher .Close ()
232
+ ref , err := publisher .Publish (context .Background (), empty .Image , build .StrictScheme + importpath )
233
+ if test .shouldError {
234
+ if err == nil || ! strings .HasSuffix (err .Error (), test .wantError .Error ()) {
235
+ t .Errorf ("%s: got error %v, wanted %v" , test .description , err , test .wantError )
236
+ }
237
+ continue
238
+ }
239
+ if err != nil {
240
+ t .Fatalf ("%s: publisher.Publish(): %v" , test .description , err )
241
+ }
242
+ fmt .Printf ("ref: %+v\n " , ref )
243
+ gotImageName := ref .Context ().Name ()
244
+ if gotImageName != test .wantImageName {
245
+ t .Errorf ("%s: got %s, wanted %s" , test .description , gotImageName , test .wantImageName )
246
+ }
184
247
}
185
248
}
186
249
0 commit comments