Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anik120 committed Jan 22, 2025
1 parent 8285ca7 commit 478c152
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 331 deletions.
14 changes: 6 additions & 8 deletions catalogd/internal/serverutil/serverutil.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package serverutil

import (
"context"
"crypto/tls"
"fmt"
"io"
Expand All @@ -11,9 +10,9 @@ import (

"github.com/go-logr/logr"
"github.com/gorilla/handlers"
"github.com/klauspost/compress/gzhttp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"

catalogdmetrics "github.com/operator-framework/operator-controller/catalogd/internal/metrics"
Expand Down Expand Up @@ -44,20 +43,19 @@ func AddCatalogServerToManager(mgr ctrl.Manager, cfg CatalogServerConfig, tlsFil
}

shutdownTimeout := 30 * time.Second
handler := cfg.LocalStorage.StorageServerHandler()
handler = gzhttp.GzipHandler(handler)
handler = catalogdmetrics.AddMetricsToHandler(handler)

l := mgr.GetLogger().WithName("catalogd-http-server")
handler := catalogdmetrics.AddMetricsToHandler(cfg.LocalStorage.StorageServerHandler())
handler = logrLoggingHandler(l, handler)

catalogServer := manager.Server{
Name: "catalogs",
OnlyServeWhenLeader: true,
Server: &http.Server{
Addr: cfg.CatalogAddr,
Handler: handler,
BaseContext: func(_ net.Listener) context.Context {
return log.IntoContext(context.Background(), mgr.GetLogger().WithName("http.catalogs"))
},
Addr: cfg.CatalogAddr,
Handler: handler,
ReadTimeout: 5 * time.Second,
// TODO: Revert this to 10 seconds if/when the API
// evolves to have significantly smaller responses
Expand Down
21 changes: 18 additions & 3 deletions catalogd/internal/storage/localdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ type LocalDirV1 struct {
var _ Instance = &LocalDirV1{}

func (s *LocalDirV1) Store(ctx context.Context, catalog string, fsys fs.FS) error {
s.m.Lock()
defer s.m.Unlock()
func() {
s.m.Lock()
defer s.m.Unlock()

if err := os.MkdirAll(s.catalogDir(catalog), 0700); err != nil {
return
}
}()

if err := os.MkdirAll(s.RootDir, 0700); err != nil {
return err
Expand All @@ -60,7 +66,14 @@ func (s *LocalDirV1) Store(ctx context.Context, catalog string, fsys fs.FS) erro
)
for i, f := range storeMetaFuncs {
metaChans = append(metaChans, make(chan *declcfg.Meta, 1))
eg.Go(func() error { return f(tmpCatalogDir, metaChans[i]) })
// Create a new variable scoped to this iteration,
// because the value of i might change by the time the goroutine starts
index := i
// Capture the function in a new variable too
fn := f
eg.Go(func() error {
return fn(tmpCatalogDir, metaChans[index])
})
}
err = declcfg.WalkMetasFS(egCtx, fsys, func(path string, meta *declcfg.Meta, err error) error {
if err != nil {
Expand All @@ -86,6 +99,8 @@ func (s *LocalDirV1) Store(ctx context.Context, catalog string, fsys fs.FS) erro
return err
}

s.m.Lock()
defer s.m.Unlock()
catalogDir := s.catalogDir(catalog)
return errors.Join(
os.RemoveAll(catalogDir),
Expand Down
Loading

0 comments on commit 478c152

Please sign in to comment.