Skip to content

Commit

Permalink
replace extraTests with a different approach
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
  • Loading branch information
everettraven committed Sep 21, 2023
1 parent a6df199 commit 76c9cc8
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions internal/catalogmetadata/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"

"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
"github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
)

const (
Expand Down Expand Up @@ -53,12 +52,13 @@ const (
func TestCache(t *testing.T) {
t.Run("FetchCatalogContents", func(t *testing.T) {
type test struct {
name string
catalog *catalogd.Catalog
contents []byte
wantErr bool
tripper *MockTripper
extraTests func(t *testing.T, c client.Fetcher, ctx context.Context, tst test)
name string
catalog *catalogd.Catalog
contents []byte
wantErr bool
tripper *MockTripper
testCaching bool
shouldHitCache bool
}
for _, tt := range []test{
{
Expand Down Expand Up @@ -94,17 +94,10 @@ func TestCache(t *testing.T) {
},
},
},
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
tripper: &MockTripper{},
extraTests: func(t *testing.T, c client.Fetcher, ctx context.Context, tst test) {
tst.tripper.content = append(tst.tripper.content, []byte(`{"schema": "olm.package", "name": "foobar"}`)...)
rc, err := c.FetchCatalogContents(ctx, tst.catalog)
assert.NoError(t, err)
defer rc.Close()
data, err := io.ReadAll(rc)
assert.NoError(t, err)
assert.Equal(t, tst.contents, data)
},
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
tripper: &MockTripper{},
testCaching: true,
shouldHitCache: true,
},
{
name: "cached update fetch with changes",
Expand All @@ -121,19 +114,10 @@ func TestCache(t *testing.T) {
},
},
},
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
tripper: &MockTripper{},
extraTests: func(t *testing.T, c client.Fetcher, ctx context.Context, tst test) {
tst.catalog.Status.ResolvedSource.Image.Ref = "fake/catalog@sha256:shafake"
tst.tripper.content = append(tst.tripper.content, []byte(`{"schema": "olm.package", "name": "foobar"}`)...)
rc, err := c.FetchCatalogContents(ctx, tst.catalog)
assert.NoError(t, err)
defer rc.Close()
data, err := io.ReadAll(rc)
assert.NoError(t, err)
assert.Equal(t, tst.tripper.content, data)
assert.NotEqual(t, tst.contents, data)
},
contents: []byte(strings.Join([]string{package1, bundle1, stableChannel}, "\n")),
tripper: &MockTripper{},
testCaching: true,
shouldHitCache: false,
},
{
name: "fetch error",
Expand Down Expand Up @@ -231,8 +215,22 @@ func TestCache(t *testing.T) {
assert.Error(t, err)
}

if tt.extraTests != nil {
tt.extraTests(t, c, ctx, tt)
if tt.testCaching {
if !tt.shouldHitCache {
tt.catalog.Status.ResolvedSource.Image.Ref = "fake/catalog@sha256:shafake"
}
tt.tripper.content = append(tt.tripper.content, []byte(`{"schema": "olm.package", "name": "foobar"}`)...)
rc, err := c.FetchCatalogContents(ctx, tt.catalog)
assert.NoError(t, err)
defer rc.Close()
data, err := io.ReadAll(rc)
assert.NoError(t, err)
if !tt.shouldHitCache {
assert.Equal(t, tt.tripper.content, data)
assert.NotEqual(t, tt.contents, data)
} else {
assert.Equal(t, tt.contents, data)
}
}
})
}
Expand Down

0 comments on commit 76c9cc8

Please sign in to comment.