Skip to content

Commit

Permalink
Propose a test
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed Oct 2, 2024
1 parent 838570b commit bd42108
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
19 changes: 11 additions & 8 deletions internal/component/prometheus/operator/common/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (
type Component struct {
mut sync.RWMutex
config *operator.Arguments
manager *crdManager
manager crdManagerInterface
ls labelstore.LabelStore

onUpdate chan struct{}
opts component.Options
healthMut sync.RWMutex
health component.Health

crdManagerFactory crdManagerFactory

kind string
cluster cluster.Cluster
}
Expand All @@ -44,11 +46,12 @@ func New(o component.Options, args component.Arguments, kind string) (*Component
}
ls := service.(labelstore.LabelStore)
c := &Component{
opts: o,
onUpdate: make(chan struct{}, 1),
kind: kind,
cluster: clusterData,
ls: ls,
opts: o,
onUpdate: make(chan struct{}, 1),
kind: kind,
cluster: clusterData,
ls: ls,
crdManagerFactory: realCrdManagerFactory{},
}
return c, c.Update(args)
}
Expand Down Expand Up @@ -87,7 +90,7 @@ func (c *Component) Run(ctx context.Context) error {
c.reportHealth(err)
case <-c.onUpdate:
c.mut.Lock()
manager := newCrdManager(c.opts, c.cluster, c.opts.Logger, c.config, c.kind, c.ls)
manager := c.crdManagerFactory.New(c.opts, c.cluster, c.opts.Logger, c.config, c.kind, c.ls)
c.manager = manager

// Wait for the old manager to stop.
Expand Down Expand Up @@ -180,7 +183,7 @@ func (c *Component) Handler() http.Handler {
}
ns := parts[1]
name := parts[2]
scs := man.getScrapeConfig(ns, name)
scs := man.GetScrapeConfig(ns, name)
if len(scs) == 0 {
w.WriteHeader(404)
return
Expand Down
19 changes: 18 additions & 1 deletion internal/component/prometheus/operator/common/crdmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ import (
// Generous timeout period for configuring all informers
const informerSyncTimeout = 10 * time.Second

type crdManagerInterface interface {
Run(ctx context.Context) error
ClusteringUpdated()
DebugInfo() interface{}
GetScrapeConfig(ns, name string) []*config.ScrapeConfig
}

type crdManagerFactory interface {
New(opts component.Options, cluster cluster.Cluster, logger log.Logger, args *operator.Arguments, kind string, ls labelstore.LabelStore) crdManagerInterface
}

type realCrdManagerFactory struct{}

func (realCrdManagerFactory) New(opts component.Options, cluster cluster.Cluster, logger log.Logger, args *operator.Arguments, kind string, ls labelstore.LabelStore) crdManagerInterface {
return newCrdManager(opts, cluster, logger, args, kind, ls)
}

// crdManager is all of the fields required to run a crd based component.
// on update, this entire thing should be recreated and restarted
type crdManager struct {
Expand Down Expand Up @@ -237,7 +254,7 @@ func (c *crdManager) DebugInfo() interface{} {
return info
}

func (c *crdManager) getScrapeConfig(ns, name string) []*config.ScrapeConfig {
func (c *crdManager) GetScrapeConfig(ns, name string) []*config.ScrapeConfig {
prefix := fmt.Sprintf("%s/%s/%s", c.kind, ns, name)
matches := []*config.ScrapeConfig{}
for k, v := range c.scrapeConfigs {
Expand Down
32 changes: 32 additions & 0 deletions internal/component/prometheus/operator/common/crdmanager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"context"
"testing"

"golang.org/x/exp/maps"
Expand Down Expand Up @@ -167,3 +168,34 @@ func (m *mockScrapeManager) TargetsActive() map[string][]*scrape.Target {
func (m *mockScrapeManager) ApplyConfig(cfg *config.Config) error {
return nil
}

type crdManagerFactoryHungRun struct{}

func (crdManagerFactoryHungRun) New(opts component.Options, cluster cluster.Cluster, logger log.Logger, args *operator.Arguments, kind string, ls labelstore.LabelStore) crdManagerInterface {
return &crdManagerHungRun{}
}

type crdManagerHungRun struct{}

func (c *crdManagerHungRun) Run(ctx context.Context) error {

}

Check failure on line 182 in internal/component/prometheus/operator/common/crdmanager_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest-xlarge)

missing return

func (c *crdManagerHungRun) ClusteringUpdated() {}

func (c *crdManagerHungRun) DebugInfo() interface{} {
return nil
}

func (c *crdManagerHungRun) GetScrapeConfig(ns, name string) []*config.ScrapeConfig {
return nil
}

func TestRunExit(t *testing.T) {
// Create a Component
// Call component.Run
// Cancel the context.
// Make sure component.Run didn't exit for a few seconds
// Make crdManager.Run exit
// Make sure component.Run exits
}

0 comments on commit bd42108

Please sign in to comment.