@@ -92,7 +92,7 @@ guyM+Ks3c29KlRf3iX35Gt0CAwEAAQ==
9292)
9393
9494func TestGetRepositoryIndexes (t * testing.T ) {
95- prepLayout := func (t * testing.T , cache string , repos []string ) * APK {
95+ prepLayout := func (t * testing.T , tr http. RoundTripper , cache string , repos []string ) * APK {
9696 src := apkfs .NewMemFS ()
9797 err := src .MkdirAll ("etc/apk" , 0o755 )
9898 require .NoError (t , err , "unable to mkdir /etc/apk" )
@@ -113,7 +113,7 @@ func TestGetRepositoryIndexes(t *testing.T) {
113113 require .NoErrorf (t , err , "unable to write repositories" )
114114 }
115115
116- opts := []Option {WithFS (src ), WithIgnoreMknodErrors (ignoreMknodErrors )}
116+ opts := []Option {WithFS (src ), WithIgnoreMknodErrors (ignoreMknodErrors ), WithTransport ( tr ) }
117117 if cache != "" {
118118 opts = append (opts , WithCache (cache , false , NewCache (false )))
119119 }
@@ -124,19 +124,13 @@ func TestGetRepositoryIndexes(t *testing.T) {
124124 return a
125125 }
126126 t .Run ("no cache" , func (t * testing.T ) {
127- a := prepLayout (t , "" , nil )
128- a .SetClient (& http.Client {
129- Transport : & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true },
130- })
127+ a := prepLayout (t , & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true }, "" , nil )
131128 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
132129 require .NoErrorf (t , err , "unable to get indexes" )
133130 require .Greater (t , len (indexes ), 0 , "no indexes found" )
134131 })
135132 t .Run ("RSA256 signed" , func (t * testing.T ) {
136- a := prepLayout (t , "" , nil )
137- a .SetClient (& http.Client {
138- Transport : & testLocalTransport {root : testRSA256IndexPkgDir , basenameOnly : true },
139- })
133+ a := prepLayout (t , & testLocalTransport {root : testRSA256IndexPkgDir , basenameOnly : true }, "" , nil )
140134 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
141135 require .NoErrorf (t , err , "unable to get indexes" )
142136 require .Greater (t , len (indexes ), 0 , "no indexes found" )
@@ -145,22 +139,16 @@ func TestGetRepositoryIndexes(t *testing.T) {
145139 // we use a transport that always returns a 404 so we know we're not hitting the network
146140 // it should fail for a cache hit
147141 tmpDir := t .TempDir ()
148- a := prepLayout (t , tmpDir , nil )
149- a .SetClient (& http.Client {
150- Transport : & testLocalTransport {fail : true },
151- })
142+ a := prepLayout (t , & testLocalTransport {fail : true }, tmpDir , nil )
152143 _ , err := a .GetRepositoryIndexes (context .Background (), false )
153144 require .Error (t , err , "should fail when no cache and no network" )
154145 })
155146 t .Run ("we can fetch, but do not cache indices without etag" , func (t * testing.T ) {
156147 // we use a transport that can read from the network
157148 // it should fail for a cache hit
158149 tmpDir := t .TempDir ()
159- a := prepLayout (t , tmpDir , nil )
150+ a := prepLayout (t , & testLocalTransport { root : testPrimaryPkgDir , basenameOnly : true }, tmpDir , nil )
160151
161- a .SetClient (& http.Client {
162- Transport : & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true },
163- })
164152 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
165153 require .NoErrorf (t , err , "unable to get indexes" )
166154 require .Greater (t , len (indexes ), 0 , "no indexes found" )
@@ -176,19 +164,16 @@ func TestGetRepositoryIndexes(t *testing.T) {
176164 // we use a transport that can read from the network
177165 // it should fail for a cache hit
178166 tmpDir := t .TempDir ()
179- a := prepLayout (t , tmpDir , []string {testAlpineRepos })
167+ a := prepLayout (t , & testLocalTransport {
168+ root : testPrimaryPkgDir ,
169+ basenameOnly : true ,
170+ headers : map [string ][]string {
171+ http .CanonicalHeaderKey ("etag" ): {"an-etag" },
172+ },
173+ }, tmpDir , []string {testAlpineRepos })
180174 // fill the cache
181175 repoDir := filepath .Join (tmpDir , url .QueryEscape (testAlpineRepos ), testArch )
182176
183- a .SetClient (& http.Client {
184- Transport : & testLocalTransport {
185- root : testPrimaryPkgDir ,
186- basenameOnly : true ,
187- headers : map [string ][]string {
188- http .CanonicalHeaderKey ("etag" ): {"an-etag" },
189- },
190- },
191- })
192177 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
193178 require .NoErrorf (t , err , "unable to get indexes" )
194179 require .Greater (t , len (indexes ), 0 , "no indexes found" )
@@ -201,15 +186,12 @@ func TestGetRepositoryIndexes(t *testing.T) {
201186 })
202187 t .Run ("repo url with http basic auth" , func (t * testing.T ) {
203188 tmpDir := t .TempDir ()
204- a := prepLayout (t , tmpDir , []string {"https://user:pass@dl-cdn.alpinelinux.org/alpine/v3.16/main" })
189+ a := prepLayout (t , & testLocalTransport {
190+ root : testPrimaryPkgDir ,
191+ basenameOnly : true ,
192+ requireBasicAuth : true ,
193+ }, tmpDir , []string {"https://user:pass@dl-cdn.alpinelinux.org/alpine/v3.16/main" })
205194
206- a .SetClient (& http.Client {
207- Transport : & testLocalTransport {
208- root : testPrimaryPkgDir ,
209- basenameOnly : true ,
210- requireBasicAuth : true ,
211- },
212- })
213195 ctx := context .Background ()
214196 indexes , err := a .GetRepositoryIndexes (ctx , false )
215197 require .NoErrorf (t , err , "unable to get indexes" )
@@ -219,13 +201,11 @@ func TestGetRepositoryIndexes(t *testing.T) {
219201 // it should succeed for a cache hit
220202 tmpDir := t .TempDir ()
221203 testEtag := "test-etag"
204+ tr := & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true , headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag }}}
222205
223206 // get our APK struct
224- a := prepLayout (t , tmpDir , nil )
207+ a := prepLayout (t , tr , tmpDir , nil )
225208
226- a .SetClient (& http.Client {
227- Transport : & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true , headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag }}},
228- })
229209 // Use the client to fill the cache.
230210 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
231211 require .NoErrorf (t , err , "unable to get indexes" )
@@ -235,9 +215,8 @@ func TestGetRepositoryIndexes(t *testing.T) {
235215
236216 // Update the transport to serve the same etag, but different content to
237217 // verify that we serve from the cache instead of the response.
238- a .SetClient (& http.Client {
239- Transport : & testLocalTransport {root : testAlternatePkgDir , basenameOnly : true , headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag }}},
240- })
218+ tr .root = testAlternatePkgDir
219+
241220 indexes , err = a .GetRepositoryIndexes (context .Background (), false )
242221 require .NoErrorf (t , err , "unable to get indexes" )
243222 require .Greater (t , len (indexes ), 0 , "no indexes found" )
@@ -251,13 +230,11 @@ func TestGetRepositoryIndexes(t *testing.T) {
251230 // it should succeed for a cache hit
252231 tmpDir := t .TempDir ()
253232 testEtag := "test-etag"
233+ tr := & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true , headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag }}}
254234
255235 // get our APK struct
256- a := prepLayout (t , tmpDir , nil )
236+ a := prepLayout (t , tr , tmpDir , nil )
257237
258- a .SetClient (& http.Client {
259- Transport : & testLocalTransport {root : testPrimaryPkgDir , basenameOnly : true , headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag }}},
260- })
261238 // Use the client to fill the cache.
262239 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
263240 require .NoErrorf (t , err , "unable to get indexes" )
@@ -268,9 +245,8 @@ func TestGetRepositoryIndexes(t *testing.T) {
268245 // Update the transport to serve a different etag and different content,
269246 // to verify that when the etag changes we use the data from the
270247 // response.
271- a .SetClient (& http.Client {
272- Transport : & testLocalTransport {root : testAlternatePkgDir , basenameOnly : true , headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag + "change" }}},
273- })
248+ tr .root = testAlternatePkgDir
249+ tr .headers = map [string ][]string {http .CanonicalHeaderKey ("etag" ): {testEtag + "change" }}
274250
275251 indexes , err = a .GetRepositoryIndexes (context .Background (), false )
276252 require .NoErrorf (t , err , "unable to get indexes" )
@@ -288,14 +264,11 @@ func TestGetRepositoryIndexes(t *testing.T) {
288264 eg := errgroup.Group {}
289265 for i := range 100 {
290266 eg .Go (func () error {
291- a := prepLayout (t , tmpDir , nil )
292- a .SetClient (& http.Client {
293- Transport : & testLocalTransport {
294- root : testPrimaryPkgDir ,
295- basenameOnly : true ,
296- headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {fmt .Sprint (i )}},
297- },
298- })
267+ a := prepLayout (t , & testLocalTransport {
268+ root : testPrimaryPkgDir ,
269+ basenameOnly : true ,
270+ headers : map [string ][]string {http .CanonicalHeaderKey ("etag" ): {fmt .Sprint (i )}},
271+ }, tmpDir , nil )
299272 indexes , err := a .GetRepositoryIndexes (context .Background (), false )
300273 require .NoErrorf (t , err , "unable to get indexes" )
301274 require .Greater (t , len (indexes ), 0 , "no indexes found" )
0 commit comments