@@ -16,7 +16,6 @@ import (
16
16
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
17
17
18
18
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
19
- "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
20
19
)
21
20
22
21
const (
@@ -53,12 +52,13 @@ const (
53
52
func TestCache (t * testing.T ) {
54
53
t .Run ("FetchCatalogContents" , func (t * testing.T ) {
55
54
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
62
62
}
63
63
for _ , tt := range []test {
64
64
{
@@ -94,17 +94,10 @@ func TestCache(t *testing.T) {
94
94
},
95
95
},
96
96
},
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 ,
108
101
},
109
102
{
110
103
name : "cached update fetch with changes" ,
@@ -121,19 +114,10 @@ func TestCache(t *testing.T) {
121
114
},
122
115
},
123
116
},
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 ,
137
121
},
138
122
{
139
123
name : "fetch error" ,
@@ -231,8 +215,22 @@ func TestCache(t *testing.T) {
231
215
assert .Error (t , err )
232
216
}
233
217
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
+ }
236
234
}
237
235
})
238
236
}
0 commit comments