|
4 | 4 | "math" |
5 | 5 | "math/rand" |
6 | 6 | "reflect" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 | "testing/quick" |
9 | 10 |
|
@@ -31,17 +32,25 @@ func unstructuredCSV() *unstructured.Unstructured { |
31 | 32 | } |
32 | 33 |
|
33 | 34 | type inputConfig struct { |
34 | | - pkg string |
35 | | - name string |
36 | | - version string |
37 | | - replaces string |
| 35 | + pkg string |
| 36 | + name string |
| 37 | + version string |
| 38 | + replaces string |
| 39 | + defaultChannel string |
| 40 | + channels []string |
38 | 41 | } |
39 | 42 |
|
40 | 43 | func newImageInput(config *inputConfig) *registry.ImageInput { |
41 | 44 | image := ®istry.ImageInput{ |
42 | 45 | Bundle: ®istry.Bundle{ |
43 | | - Package: config.pkg, |
44 | | - Name: config.name, |
| 46 | + Package: config.pkg, |
| 47 | + Name: config.name, |
| 48 | + Channels: config.channels, |
| 49 | + Annotations: ®istry.Annotations{ |
| 50 | + PackageName: config.pkg, |
| 51 | + Channels: strings.Join(config.channels, ","), |
| 52 | + DefaultChannelName: config.defaultChannel, |
| 53 | + }, |
45 | 54 | }, |
46 | 55 | } |
47 | 56 |
|
@@ -280,6 +289,125 @@ func TestReplacesTakesPrecedence(t *testing.T) { |
280 | 289 | } |
281 | 290 | } |
282 | 291 |
|
| 292 | +func TestDefaultChannelAffectsOrder(t *testing.T) { |
| 293 | + type args struct { |
| 294 | + input []*registry.ImageInput |
| 295 | + } |
| 296 | + type expect struct { |
| 297 | + err bool |
| 298 | + ordered []string |
| 299 | + } |
| 300 | + for _, tt := range []struct { |
| 301 | + description string |
| 302 | + args args |
| 303 | + expect expect |
| 304 | + }{ |
| 305 | + { |
| 306 | + description: "BundleDefiningDefaultChannelIsFirst", |
| 307 | + args: args{ |
| 308 | + input: []*registry.ImageInput{ |
| 309 | + newImageInput(&inputConfig{ |
| 310 | + name: "a", |
| 311 | + version: "1.0.0", |
| 312 | + channels: []string{"stable"}, |
| 313 | + defaultChannel: "alpha", |
| 314 | + }), |
| 315 | + newImageInput(&inputConfig{ |
| 316 | + name: "b", |
| 317 | + version: "1.1.0", |
| 318 | + channels: []string{"alpha", "stable"}, |
| 319 | + defaultChannel: "alpha", |
| 320 | + }), |
| 321 | + }, |
| 322 | + }, |
| 323 | + expect: expect{ |
| 324 | + ordered: []string{"b", "a"}, |
| 325 | + }, |
| 326 | + }, |
| 327 | + { |
| 328 | + description: "ConflictingReplacesAndDefaultChannelReturnsError", |
| 329 | + args: args{ |
| 330 | + input: []*registry.ImageInput{ |
| 331 | + newImageInput(&inputConfig{ |
| 332 | + name: "a", |
| 333 | + version: "1.0.0", |
| 334 | + channels: []string{"stable"}, |
| 335 | + defaultChannel: "alpha", |
| 336 | + }), |
| 337 | + newImageInput(&inputConfig{ |
| 338 | + name: "b", |
| 339 | + version: "1.1.0", |
| 340 | + replaces: "a", |
| 341 | + channels: []string{"alpha", "stable"}, |
| 342 | + defaultChannel: "alpha", |
| 343 | + }), |
| 344 | + }, |
| 345 | + }, |
| 346 | + expect: expect{ |
| 347 | + err: true, |
| 348 | + }, |
| 349 | + }, |
| 350 | + } { |
| 351 | + t.Run(tt.description, func(t *testing.T) { |
| 352 | + addedChannels := map[string]registry.Channel{} |
| 353 | + loader := ®istryfakes.FakeGraphLoader{ |
| 354 | + GenerateStub: func(pkg string) (*registry.Package, error) { |
| 355 | + return ®istry.Package{ |
| 356 | + Name: pkg, |
| 357 | + Channels: addedChannels, |
| 358 | + }, nil |
| 359 | + }, |
| 360 | + } |
| 361 | + |
| 362 | + stream, err := registry.NewReplacesInputStream(loader, tt.args.input) |
| 363 | + if err != nil { |
| 364 | + t.Error(err) |
| 365 | + return |
| 366 | + } |
| 367 | + |
| 368 | + if tt.expect.err { |
| 369 | + _, err := stream.Next() |
| 370 | + if err == nil { |
| 371 | + t.Error("expected next to return error, returned nil instead") |
| 372 | + } |
| 373 | + return |
| 374 | + } |
| 375 | + |
| 376 | + for _, expected := range tt.expect.ordered { |
| 377 | + next, err := stream.Next() |
| 378 | + if err != nil { |
| 379 | + t.Errorf("next returned unexpected error %s", err) |
| 380 | + return |
| 381 | + } |
| 382 | + if next == nil { |
| 383 | + t.Errorf("next returned unexpected nil, expecting %s", expected) |
| 384 | + return |
| 385 | + } |
| 386 | + |
| 387 | + name := next.Bundle.Name |
| 388 | + if name != expected { |
| 389 | + t.Errorf("next returned unexpected bundle %s, expecting %s", name, expected) |
| 390 | + return |
| 391 | + } |
| 392 | + |
| 393 | + // Simulate an add |
| 394 | + for _, channel := range next.Bundle.Channels { |
| 395 | + added := addedChannels[channel] |
| 396 | + if added.Nodes == nil { |
| 397 | + added.Nodes = map[registry.BundleKey]map[registry.BundleKey]struct{}{} |
| 398 | + } |
| 399 | + added.Nodes[registry.BundleKey{CsvName: name}] = nil |
| 400 | + addedChannels[channel] = added |
| 401 | + } |
| 402 | + } |
| 403 | + |
| 404 | + if !stream.Empty() { |
| 405 | + t.Errorf("stream still contains content, expected end of content") |
| 406 | + } |
| 407 | + }) |
| 408 | + } |
| 409 | +} |
| 410 | + |
283 | 411 | type missingReplaces struct { |
284 | 412 | input []*registry.ImageInput |
285 | 413 | valid int |
|
0 commit comments