Skip to content

Commit f931af4

Browse files
committed
Edit test cases to work with new changes on NamespaceSourceQuerier
Signed-off-by: Vu Dinh <[email protected]>
1 parent c7d1099 commit f931af4

File tree

2 files changed

+109
-19
lines changed

2 files changed

+109
-19
lines changed

pkg/controller/registry/resolver/querier_test.go

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ func TestNewNamespaceSourceQuerier(t *testing.T) {
2323
nonEmptySources := map[CatalogKey]client.Interface{
2424
CatalogKey{"test", "ns"}: &fakes.FakeInterface{},
2525
}
26+
27+
emptyClients := map[CatalogKey]*client.Client{}
28+
nonEmptyClients := map[CatalogKey]*client.Client{
29+
CatalogKey{"test", "ns"}: &client.Client{
30+
Registry: &fakes.FakeRegistryClient{},
31+
},
32+
}
2633
type args struct {
2734
sources map[CatalogKey]client.Interface
35+
clients map[CatalogKey]*client.Client
2836
}
2937
tests := []struct {
3038
name string
@@ -35,34 +43,38 @@ func TestNewNamespaceSourceQuerier(t *testing.T) {
3543
name: "nil",
3644
args: args{
3745
sources: nil,
46+
clients: nil,
3847
},
39-
want: &NamespaceSourceQuerier{sources: nil},
48+
want: &NamespaceSourceQuerier{sources: nil, clients: nil},
4049
},
4150
{
4251
name: "empty",
4352
args: args{
4453
sources: emptySources,
54+
clients: emptyClients,
4555
},
46-
want: &NamespaceSourceQuerier{sources: emptySources},
56+
want: &NamespaceSourceQuerier{sources: emptySources, clients: emptyClients},
4757
},
4858
{
4959
name: "nonEmpty",
5060
args: args{
5161
sources: nonEmptySources,
62+
clients: nonEmptyClients,
5263
},
53-
want: &NamespaceSourceQuerier{sources: nonEmptySources},
64+
want: &NamespaceSourceQuerier{sources: nonEmptySources, clients: nonEmptyClients},
5465
},
5566
}
5667
for _, tt := range tests {
5768
t.Run(tt.name, func(t *testing.T) {
58-
require.Equal(t, NewNamespaceSourceQuerier(tt.args.sources), tt.want)
69+
require.Equal(t, NewNamespaceSourceQuerier(tt.args.sources, tt.args.clients), tt.want)
5970
})
6071
}
6172
}
6273

6374
func TestNamespaceSourceQuerier_Queryable(t *testing.T) {
6475
type fields struct {
6576
sources map[CatalogKey]client.Interface
77+
clients map[CatalogKey]*client.Client
6678
}
6779
tests := []struct {
6880
name string
@@ -73,13 +85,15 @@ func TestNamespaceSourceQuerier_Queryable(t *testing.T) {
7385
name: "nil",
7486
fields: fields{
7587
sources: nil,
88+
clients: nil,
7689
},
7790
error: fmt.Errorf("no catalog sources available"),
7891
},
7992
{
8093
name: "empty",
8194
fields: fields{
8295
sources: map[CatalogKey]client.Interface{},
96+
clients: map[CatalogKey]*client.Client{},
8397
},
8498
error: fmt.Errorf("no catalog sources available"),
8599
},
@@ -89,6 +103,11 @@ func TestNamespaceSourceQuerier_Queryable(t *testing.T) {
89103
sources: map[CatalogKey]client.Interface{
90104
CatalogKey{"test", "ns"}: &fakes.FakeInterface{},
91105
},
106+
clients: map[CatalogKey]*client.Client{
107+
CatalogKey{"test", "ns"}: &client.Client{
108+
Registry: &fakes.FakeRegistryClient{},
109+
},
110+
},
92111
},
93112
error: nil,
94113
},
@@ -218,6 +237,7 @@ func TestNamespaceSourceQuerier_FindProvider(t *testing.T) {
218237
func TestNamespaceSourceQuerier_FindPackage(t *testing.T) {
219238
initialSource := fakes.FakeInterface{}
220239
otherSource := fakes.FakeInterface{}
240+
clients := map[CatalogKey]*client.Client{}
221241
initalBundle := &api.Bundle{CsvName: "test", PackageName: "testPkg", ChannelName: "testChannel"}
222242
startingBundle := &api.Bundle{CsvName: "starting-test", PackageName: "testPkg", ChannelName: "testChannel"}
223243
otherBundle := &api.Bundle{CsvName: "other", PackageName: "otherPkg", ChannelName: "otherChannel"}
@@ -248,6 +268,7 @@ func TestNamespaceSourceQuerier_FindPackage(t *testing.T) {
248268

249269
type fields struct {
250270
sources map[CatalogKey]client.Interface
271+
clients map[CatalogKey]*client.Client
251272
}
252273
type args struct {
253274
pkgName string
@@ -268,37 +289,37 @@ func TestNamespaceSourceQuerier_FindPackage(t *testing.T) {
268289
}{
269290
{
270291
name: "Initial/Found",
271-
fields: fields{sources: sources},
292+
fields: fields{sources: sources, clients: clients},
272293
args: args{"test", "testChannel", "", CatalogKey{"initial", "ns"}},
273294
out: out{bundle: initalBundle, key: &initialKey, err: nil},
274295
},
275296
{
276297
name: "Initial/CatalogNotFound",
277-
fields: fields{sources: sources},
298+
fields: fields{sources: sources, clients: clients},
278299
args: args{"test", "testChannel", "", CatalogKey{"absent", "found"}},
279300
out: out{bundle: nil, key: nil, err: fmt.Errorf("CatalogSource {absent found} not found")},
280301
},
281302
{
282303
name: "Initial/StartingCSVFound",
283-
fields: fields{sources: sources},
304+
fields: fields{sources: sources, clients: clients},
284305
args: args{"test", "testChannel", "starting-test", CatalogKey{"initial", "ns"}},
285306
out: out{bundle: startingBundle, key: &initialKey, err: nil},
286307
},
287308
{
288309
name: "Initial/StartingCSVNotFound",
289-
fields: fields{sources: sources},
310+
fields: fields{sources: sources, clients: clients},
290311
args: args{"test", "testChannel", "non-existent", CatalogKey{"initial", "ns"}},
291312
out: out{bundle: nil, key: nil, err: fmt.Errorf("not found")},
292313
},
293314
{
294315
name: "Other/Found",
295-
fields: fields{sources: sources},
316+
fields: fields{sources: sources, clients: clients},
296317
args: args{"other", "testChannel", "", CatalogKey{"", ""}},
297318
out: out{bundle: otherBundle, key: &otherKey, err: nil},
298319
},
299320
{
300321
name: "NotFound",
301-
fields: fields{sources: sources},
322+
fields: fields{sources: sources, clients: clients},
302323
args: args{"nope", "not", "", CatalogKey{"", ""}},
303324
out: out{bundle: nil, err: fmt.Errorf("nope/not not found in any available CatalogSource")},
304325
},
@@ -307,6 +328,7 @@ func TestNamespaceSourceQuerier_FindPackage(t *testing.T) {
307328
t.Run(tt.name, func(t *testing.T) {
308329
q := &NamespaceSourceQuerier{
309330
sources: tt.fields.sources,
331+
clients: tt.fields.clients,
310332
}
311333
var got *api.Bundle
312334
var key *CatalogKey
@@ -330,6 +352,7 @@ func TestNamespaceSourceQuerier_FindReplacement(t *testing.T) {
330352
replacementSource := fakes.FakeInterface{}
331353
replacementAndLatestSource := fakes.FakeInterface{}
332354
replacementAndNoAnnotationLatestSource := fakes.FakeInterface{}
355+
clients := map[CatalogKey]*client.Client{}
333356

334357
latestVersion := semver.MustParse("1.0.0-1556661308")
335358
csv := v1alpha1.ClusterServiceVersion{
@@ -417,6 +440,7 @@ func TestNamespaceSourceQuerier_FindReplacement(t *testing.T) {
417440

418441
type fields struct {
419442
sources map[CatalogKey]client.Interface
443+
clients map[CatalogKey]*client.Client
420444
}
421445
type args struct {
422446
currentVersion *semver.Version
@@ -438,43 +462,43 @@ func TestNamespaceSourceQuerier_FindReplacement(t *testing.T) {
438462
}{
439463
{
440464
name: "FindsLatestInPrimaryCatalog",
441-
fields: fields{sources: sources},
465+
fields: fields{sources: sources, clients: clients},
442466
args: args{&startVersion, "testPkg", "testChannel", "test.v1", initialKey},
443467
out: out{bundle: latestBundle, key: &initialKey, err: nil},
444468
},
445469
{
446470
name: "FindsLatestInSecondaryCatalog",
447-
fields: fields{sources: sources},
471+
fields: fields{sources: sources, clients: clients},
448472
args: args{&startVersion, "testPkg", "testChannel", "test.v1", otherKey},
449473
out: out{bundle: latestBundle, key: &otherKey, err: nil},
450474
},
451475
{
452476
name: "PrefersLatestToReplaced/SameCatalog",
453-
fields: fields{sources: sources},
477+
fields: fields{sources: sources, clients: clients},
454478
args: args{&startVersion, "testPkg", "testChannel", "test.v1", replacementAndLatestKey},
455479
out: out{bundle: latestBundle, key: &replacementAndLatestKey, err: nil},
456480
},
457481
{
458482
name: "PrefersLatestToReplaced/OtherCatalog",
459-
fields: fields{sources: sources},
483+
fields: fields{sources: sources, clients: clients},
460484
args: args{&startVersion, "testPkg", "testChannel", "test.v1", initialKey},
461485
out: out{bundle: latestBundle, key: &initialKey, err: nil},
462486
},
463487
{
464488
name: "IgnoresLatestWithoutAnnotation",
465-
fields: fields{sources: sources},
489+
fields: fields{sources: sources, clients: clients},
466490
args: args{&startVersion, "testPkg", "testChannel", "test.v1", replacementAndNoAnnotationLatestKey},
467491
out: out{bundle: nextBundle, key: &replacementAndNoAnnotationLatestKey, err: nil},
468492
},
469493
{
470494
name: "IgnoresLatestNotInRange",
471-
fields: fields{sources: sources},
495+
fields: fields{sources: sources, clients: clients},
472496
args: args{&notInRange, "testPkg", "testChannel", "test.v1", replacementAndLatestKey},
473497
out: out{bundle: nextBundle, key: &replacementAndLatestKey, err: nil},
474498
},
475499
{
476500
name: "IgnoresLatestAtLatest",
477-
fields: fields{sources: sources},
501+
fields: fields{sources: sources, clients: clients},
478502
args: args{&latestVersion, "testPkg", "testChannel", "test.v1", otherKey},
479503
out: out{bundle: nil, key: nil, err: nil},
480504
},
@@ -483,6 +507,7 @@ func TestNamespaceSourceQuerier_FindReplacement(t *testing.T) {
483507
t.Run(tt.name, func(t *testing.T) {
484508
q := &NamespaceSourceQuerier{
485509
sources: tt.fields.sources,
510+
clients: tt.fields.clients,
486511
}
487512
var got *api.Bundle
488513
var key *CatalogKey

pkg/controller/registry/resolver/util_test.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,12 @@ func withReplaces(operator *Operator, replaces string) *Operator {
313313
// NewFakeSourceQuerier builds a querier that talks to fake registry stubs for testing
314314
func NewFakeSourceQuerier(bundlesByCatalog map[CatalogKey][]*api.Bundle) *NamespaceSourceQuerier {
315315
sources := map[CatalogKey]client.Interface{}
316+
clients := map[CatalogKey]*client.Client{}
316317
for catKey, bundles := range bundlesByCatalog {
317318
source := &fakes.FakeInterface{}
319+
client := &client.Client{
320+
Registry: &fakes.FakeRegistryClient{},
321+
}
318322
source.GetBundleThatProvidesStub = func(ctx context.Context, groupOrName, version, kind string) (*api.Bundle, error) {
319323
for _, b := range bundles {
320324
apis := b.GetProvidedApis()
@@ -356,16 +360,77 @@ func NewFakeSourceQuerier(bundlesByCatalog map[CatalogKey][]*api.Bundle) *Namesp
356360
return b, nil
357361
}
358362
}
363+
359364
return nil, fmt.Errorf("no bundle found")
360365
}
366+
clients[catKey] = client
361367
sources[catKey] = source
362368
}
363-
return NewNamespaceSourceQuerier(sources)
369+
return NewNamespaceSourceQuerier(sources, clients)
370+
}
371+
372+
// SortBundleInPackageChannel will sort into map of package-channel key and list
373+
// sorted (oldest to latest version) of bundles as value
374+
func SortBundleInPackageChannel(bundles []*api.Bundle) map[string][]*api.Bundle {
375+
sorted := map[string][]*api.Bundle{}
376+
var initialReplaces string
377+
for _, v := range bundles {
378+
pkgChanKey := v.PackageName + "/" + v.ChannelName
379+
b, ok := sorted[pkgChanKey]
380+
if ok {
381+
b = append(b, v)
382+
sorted[pkgChanKey] = b
383+
} else {
384+
blist := []*api.Bundle{}
385+
blist = append(blist, v)
386+
sorted[pkgChanKey] = blist
387+
}
388+
}
389+
390+
for k, v := range sorted {
391+
resorted := []*api.Bundle{}
392+
bundleMap := make(map[string]*api.Bundle)
393+
// Find the first (oldest) bundle in upgrade graph
394+
for _, bundle := range v {
395+
csv, err := V1alpha1CSVFromBundle(bundle)
396+
if err != nil {
397+
continue
398+
}
399+
400+
if replaces := csv.Spec.Replaces; replaces == "" {
401+
initialReplaces = bundle.CsvName
402+
resorted = append(resorted, bundle)
403+
} else {
404+
bundleMap[replaces] = bundle
405+
}
406+
}
407+
resorted = SortBundleInChannel(initialReplaces, bundleMap, resorted)
408+
sorted[k] = resorted
409+
}
410+
return sorted
411+
}
412+
413+
// SortBundleInChannel recursively sorts a list of bundles to form a update graph
414+
// of a specific channel. The first item in the returned list is the start
415+
// (oldest version) of the upgrade graph and the last item is the head
416+
// (latest version) of a channel
417+
func SortBundleInChannel(replaces string, replacedBundle map[string]*api.Bundle, updated []*api.Bundle) []*api.Bundle {
418+
bundle, ok := replacedBundle[replaces]
419+
if ok {
420+
csv, err := V1alpha1CSVFromBundle(bundle)
421+
if err != nil {
422+
return updated
423+
}
424+
updated = append(updated, bundle)
425+
return SortBundleInChannel(csv.Spec.Replaces, replacedBundle, updated)
426+
}
427+
return updated
364428
}
365429

366430
// NewFakeSourceQuerier builds a querier that talks to fake registry stubs for testing
367431
func NewFakeSourceQuerierCustomReplacement(catKey CatalogKey, bundle *api.Bundle) *NamespaceSourceQuerier {
368432
sources := map[CatalogKey]client.Interface{}
433+
clients := map[CatalogKey]*client.Client{}
369434
source := &fakes.FakeInterface{}
370435
source.GetBundleThatProvidesStub = func(ctx context.Context, groupOrName, version, kind string) (*api.Bundle, error) {
371436
return nil, fmt.Errorf("no bundle found")
@@ -380,5 +445,5 @@ func NewFakeSourceQuerierCustomReplacement(catKey CatalogKey, bundle *api.Bundle
380445
return bundle, nil
381446
}
382447
sources[catKey] = source
383-
return NewNamespaceSourceQuerier(sources)
448+
return NewNamespaceSourceQuerier(sources, clients)
384449
}

0 commit comments

Comments
 (0)