Skip to content

Commit 76c9cc8

Browse files
committed
replace extraTests with a different approach
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
1 parent a6df199 commit 76c9cc8

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

internal/catalogmetadata/cache/cache_test.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
1717

1818
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
19-
"github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
2019
)
2120

2221
const (
@@ -53,12 +52,13 @@ const (
5352
func TestCache(t *testing.T) {
5453
t.Run("FetchCatalogContents", func(t *testing.T) {
5554
type test struct {
56-
name string
57-
catalog *catalogd.Catalog
58-
contents []byte
59-
wantErr bool
60-
tripper *MockTripper
61-
extraTests func(t *testing.T, c client.Fetcher, ctx context.Context, tst test)
55+
name string
56+
catalog *catalogd.Catalog
57+
contents []byte
58+
wantErr bool
59+
tripper *MockTripper
60+
testCaching bool
61+
shouldHitCache bool
6262
}
6363
for _, tt := range []test{
6464
{
@@ -94,17 +94,10 @@ func TestCache(t *testing.T) {
9494
},
9595
},
9696
},
97-
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
98-
tripper: &MockTripper{},
99-
extraTests: func(t *testing.T, c client.Fetcher, ctx context.Context, tst test) {
100-
tst.tripper.content = append(tst.tripper.content, []byte(`{"schema": "olm.package", "name": "foobar"}`)...)
101-
rc, err := c.FetchCatalogContents(ctx, tst.catalog)
102-
assert.NoError(t, err)
103-
defer rc.Close()
104-
data, err := io.ReadAll(rc)
105-
assert.NoError(t, err)
106-
assert.Equal(t, tst.contents, data)
107-
},
97+
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
98+
tripper: &MockTripper{},
99+
testCaching: true,
100+
shouldHitCache: true,
108101
},
109102
{
110103
name: "cached update fetch with changes",
@@ -121,19 +114,10 @@ func TestCache(t *testing.T) {
121114
},
122115
},
123116
},
124-
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
125-
tripper: &MockTripper{},
126-
extraTests: func(t *testing.T, c client.Fetcher, ctx context.Context, tst test) {
127-
tst.catalog.Status.ResolvedSource.Image.Ref = "fake/catalog@sha256:shafake"
128-
tst.tripper.content = append(tst.tripper.content, []byte(`{"schema": "olm.package", "name": "foobar"}`)...)
129-
rc, err := c.FetchCatalogContents(ctx, tst.catalog)
130-
assert.NoError(t, err)
131-
defer rc.Close()
132-
data, err := io.ReadAll(rc)
133-
assert.NoError(t, err)
134-
assert.Equal(t, tst.tripper.content, data)
135-
assert.NotEqual(t, tst.contents, data)
136-
},
117+
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
118+
tripper: &MockTripper{},
119+
testCaching: true,
120+
shouldHitCache: false,
137121
},
138122
{
139123
name: "fetch error",
@@ -231,8 +215,22 @@ func TestCache(t *testing.T) {
231215
assert.Error(t, err)
232216
}
233217

234-
if tt.extraTests != nil {
235-
tt.extraTests(t, c, ctx, tt)
218+
if tt.testCaching {
219+
if !tt.shouldHitCache {
220+
tt.catalog.Status.ResolvedSource.Image.Ref = "fake/catalog@sha256:shafake"
221+
}
222+
tt.tripper.content = append(tt.tripper.content, []byte(`{"schema": "olm.package", "name": "foobar"}`)...)
223+
rc, err := c.FetchCatalogContents(ctx, tt.catalog)
224+
assert.NoError(t, err)
225+
defer rc.Close()
226+
data, err := io.ReadAll(rc)
227+
assert.NoError(t, err)
228+
if !tt.shouldHitCache {
229+
assert.Equal(t, tt.tripper.content, data)
230+
assert.NotEqual(t, tt.contents, data)
231+
} else {
232+
assert.Equal(t, tt.contents, data)
233+
}
236234
}
237235
})
238236
}

0 commit comments

Comments
 (0)