From 76c9cc8412d8a4ff191989f841802243262b8274 Mon Sep 17 00:00:00 2001 From: Bryce Palmer Date: Thu, 21 Sep 2023 14:41:50 -0400 Subject: [PATCH] replace extraTests with a different approach Signed-off-by: Bryce Palmer --- internal/catalogmetadata/cache/cache_test.go | 64 ++++++++++---------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/internal/catalogmetadata/cache/cache_test.go b/internal/catalogmetadata/cache/cache_test.go index 2386c1b65..b3e251605 100644 --- a/internal/catalogmetadata/cache/cache_test.go +++ b/internal/catalogmetadata/cache/cache_test.go @@ -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 ( @@ -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{ { @@ -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", @@ -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", @@ -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) + } } }) }