Skip to content

Commit 192cbce

Browse files
committed
code cleanup
1 parent 3b6b263 commit 192cbce

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

catalogd/internal/serverutil/serverutil.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func logrLoggingHandler(l logr.Logger, handler http.Handler) http.Handler {
9595
}
9696

9797
func storageServerHandlerWrapped(mgr ctrl.Manager, cfg CatalogServerConfig) http.Handler {
98-
9998
handler := cfg.LocalStorage.StorageServerHandler()
10099
handler = gzhttp.GzipHandler(handler)
101100
handler = catalogdmetrics.AddMetricsToHandler(handler)

catalogd/internal/serverutil/serverutil_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ type mockStorageInstance struct {
234234

235235
func (m *mockStorageInstance) StorageServerHandler() http.Handler {
236236
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
237-
w.Write([]byte(m.content))
237+
_, err := w.Write([]byte(m.content))
238+
if err != nil {
239+
http.Error(w, err.Error(), http.StatusInternalServerError)
240+
}
238241
})
239242
}
240243

catalogd/internal/storage/http_precoditions_check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424

2525
// checkPreconditions evaluates request preconditions and reports whether a precondition
2626
// resulted in sending StatusNotModified or StatusPreconditionFailed.
27-
func checkPreconditions(w http.ResponseWriter, r *http.Request, modtime time.Time) (done bool) {
27+
func checkPreconditions(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
2828
// This function carefully follows RFC 7232 section 6.
2929
ch := checkIfMatch(r)
3030
if ch == condNone {

catalogd/internal/storage/index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (i *index) getSectionSet(schema, packageName, name string) sets.Set[section
119119
}
120120
}
121121

122-
func newIndex(metasChan <-chan *declcfg.Meta) (*index, error) {
122+
func newIndex(metasChan <-chan *declcfg.Meta) *index {
123123
idx := &index{
124124
BySchema: make(map[string][]section),
125125
ByPackage: make(map[string][]section),
@@ -142,5 +142,5 @@ func newIndex(metasChan <-chan *declcfg.Meta) (*index, error) {
142142
idx.ByName[meta.Name] = append(idx.ByName[meta.Name], s)
143143
}
144144
}
145-
return idx, nil
145+
return idx
146146
}

catalogd/internal/storage/localdir.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ type LocalDirV1 struct {
4242
}
4343

4444
var (
45-
_ Instance = &LocalDirV1{}
46-
ErrInvalidParams = errors.New("invalid parameters")
45+
_ Instance = (*LocalDirV1)(nil)
46+
errInvalidParams = errors.New("invalid parameters")
4747
)
4848

4949
func (s *LocalDirV1) Store(ctx context.Context, catalog string, fsys fs.FS) error {
@@ -169,10 +169,7 @@ func storeCatalogData(catalogDir string, metas <-chan *declcfg.Meta) error {
169169
}
170170

171171
func storeIndexData(catalogDir string, metas <-chan *declcfg.Meta) error {
172-
idx, err := newIndex(metas)
173-
if err != nil {
174-
return err
175-
}
172+
idx := newIndex(metas)
176173

177174
f, err := os.Create(catalogIndexFilePath(catalogDir))
178175
if err != nil {
@@ -209,7 +206,7 @@ func (s *LocalDirV1) handleV1All(w http.ResponseWriter, r *http.Request) {
209206
httpError(w, err)
210207
return
211208
}
212-
serveJsonLines(w, r, catalogStat.ModTime(), catalogFile)
209+
serveJSONLines(w, r, catalogStat.ModTime(), catalogFile)
213210
}
214211

215212
func (s *LocalDirV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
@@ -236,7 +233,7 @@ func (s *LocalDirV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
236233

237234
if schema == "" && pkg == "" && name == "" {
238235
// If no parameters are provided, return the entire catalog (this is the same as /api/v1/all)
239-
serveJsonLines(w, r, catalogStat.ModTime(), catalogFile)
236+
serveJSONLines(w, r, catalogStat.ModTime(), catalogFile)
240237
return
241238
}
242239
idx, err := s.getIndex(catalog)
@@ -249,7 +246,7 @@ func (s *LocalDirV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
249246
httpError(w, fs.ErrNotExist)
250247
return
251248
}
252-
serveJsonLinesQuery(w, indexReader)
249+
serveJSONLinesQuery(w, indexReader)
253250
}
254251

255252
func (s *LocalDirV1) catalogData(catalog string) (*os.File, os.FileInfo, error) {
@@ -271,20 +268,20 @@ func httpError(w http.ResponseWriter, err error) {
271268
code = http.StatusNotFound
272269
case errors.Is(err, fs.ErrPermission):
273270
code = http.StatusForbidden
274-
case errors.Is(err, ErrInvalidParams):
271+
case errors.Is(err, errInvalidParams):
275272
code = http.StatusBadRequest
276273
default:
277274
code = http.StatusInternalServerError
278275
}
279276
http.Error(w, fmt.Sprintf("%d %s", code, http.StatusText(code)), code)
280277
}
281278

282-
func serveJsonLines(w http.ResponseWriter, r *http.Request, modTime time.Time, rs io.ReadSeeker) {
279+
func serveJSONLines(w http.ResponseWriter, r *http.Request, modTime time.Time, rs io.ReadSeeker) {
283280
w.Header().Add("Content-Type", "application/jsonl")
284281
http.ServeContent(w, r, "", modTime, rs)
285282
}
286283

287-
func serveJsonLinesQuery(w http.ResponseWriter, rs io.Reader) {
284+
func serveJSONLinesQuery(w http.ResponseWriter, rs io.Reader) {
288285
w.Header().Add("Content-Type", "application/jsonl")
289286
_, err := io.Copy(w, rs)
290287
if err != nil {

catalogd/internal/storage/localdir_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,24 @@ func TestQueryEndpoint(t *testing.T) {
310310
expectedStatusCode: http.StatusOK,
311311
expectedContent: `{"image":"quaydock.io/namespace/bundle:0.0.3","name":"bundle.v0.0.1","package":"webhook_operator_test","properties":[{"type":"olm.bundle.object","value":{"data":"dW5pbXBvcnRhbnQK"}},{"type":"some.other","value":{"data":"arbitrary-info"}}],"relatedImages":[{"image":"testimage:latest","name":"test"}],"schema":"olm.bundle"}`,
312312
},
313-
// {
314-
// name: "valid query for package schema for a package that does not exist",
315-
// queryParams: "?schema=olm.package&name=not-present",
316-
// expectedStatusCode: http.StatusOK,
317-
// expectedContent: "",
318-
// },
313+
{
314+
name: "valid query for package schema for a package that does not exist",
315+
queryParams: "?schema=olm.package&name=not-present",
316+
expectedStatusCode: http.StatusOK,
317+
expectedContent: "",
318+
},
319319
{
320320
name: "valid query with package and name",
321321
queryParams: "?package=webhook_operator_test&name=bundle.v0.0.1",
322322
expectedStatusCode: http.StatusOK,
323323
expectedContent: `{"image":"quaydock.io/namespace/bundle:0.0.3","name":"bundle.v0.0.1","package":"webhook_operator_test","properties":[{"type":"olm.bundle.object","value":{"data":"dW5pbXBvcnRhbnQK"}},{"type":"some.other","value":{"data":"arbitrary-info"}}],"relatedImages":[{"image":"testimage:latest","name":"test"}],"schema":"olm.bundle"}`,
324324
},
325-
// {
326-
// name: "invalid query with non-existent schema",
327-
// queryParams: "?schema=non_existent_schema",
328-
// expectedStatusCode: http.StatusNotFound,
329-
// expectedContent: "400 Bad Request",
330-
// },
325+
{
326+
name: "query with non-existent schema",
327+
queryParams: "?schema=non_existent_schema",
328+
expectedStatusCode: http.StatusOK,
329+
expectedContent: "",
330+
},
331331
{
332332
name: "cached response with If-Modified-Since",
333333
queryParams: "?schema=olm.package",
@@ -346,6 +346,7 @@ func TestQueryEndpoint(t *testing.T) {
346346
// for the actual request
347347
resp, err := http.DefaultClient.Do(req)
348348
require.NoError(t, err)
349+
resp.Body.Close()
349350
req.Header.Set("If-Modified-Since", resp.Header.Get("Last-Modified"))
350351
}
351352
resp, err := http.DefaultClient.Do(req)
@@ -409,7 +410,7 @@ func TestServerLoadHandling(t *testing.T) {
409410
}
410411
for _, resp := range responses {
411412
require.Equal(t, http.StatusOK, resp.StatusCode)
412-
require.Equal(t, resp.Header.Get("Content-Type"), "application/jsonl")
413+
require.Equal(t, "application/jsonl", resp.Header.Get("Content-Type"))
413414
resp.Body.Close()
414415
}
415416
},

0 commit comments

Comments
 (0)