Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <[email protected]>
  • Loading branch information
jannfis committed Oct 30, 2024
1 parent 4457537 commit 85dea68
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
24 changes: 16 additions & 8 deletions internal/informer/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,27 @@ type GenericInformer struct {
fieldSelector string
// mutex should be owned before accessing namespaces
namespaces map[string]interface{}
metrics metrics
// metrics is the metrics provider for this informer
metrics metrics
}

// metrics is a simplified and specialized metrics provider for the informer
type metrics struct {
added metricCounter
updated metricCounter
removed metricCounter
watched metricGauge
added metricsCounter
updated metricsCounter
removed metricsCounter
watched metricsGauge
}
type metricCounter interface {

// metricsCounter is an interface for a metrics implementation of type counter.
// The reason we use an oversimplified abstraction is for testing purposes.
type metricsCounter interface {
Inc()
}
type metricGauge interface {

// metricsGauge is an interface for a metrics implementation of type gauge.
// The reason we use an oversimplified abstraction is for testing purposes.
type metricsGauge interface {
Inc()
Dec()
Set(float64)
Expand Down Expand Up @@ -141,7 +149,7 @@ func WithFieldSelector(sel string) InformerOption {
}

// WithMetrics sets the metrics functions to use with this informer.
func WithMetrics(added, updated, removed metricCounter, watched metricGauge) InformerOption {
func WithMetrics(added, updated, removed metricsCounter, watched metricsGauge) InformerOption {
return func(i *GenericInformer) error {
i.metrics.added = added
i.metrics.updated = updated
Expand Down
20 changes: 16 additions & 4 deletions internal/informer/informer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,18 @@ func Test_NamespaceRestrictions(t *testing.T) {
Namespace: "argocd",
},
}

t.Run("Namespace is allowed", func(t *testing.T) {
ctl := fakeGenericInformer(t)
assert.True(t, ctl.i.isNamespaceAllowed(app))
ctl = fakeGenericInformer(t, WithNamespaces("argocd"))
assert.True(t, ctl.i.isNamespaceAllowed(app))
})

t.Run("Namespace is not allowed", func(t *testing.T) {
ctl := fakeGenericInformer(t, WithNamespaces("argocd"))
app := app.DeepCopy()
app.Namespace = "foobar"
assert.False(t, ctl.i.isNamespaceAllowed(app))
})

t.Run("Adding a namespace works", func(t *testing.T) {
ctl := fakeGenericInformer(t, WithNamespaces("foobar"))
assert.False(t, ctl.i.isNamespaceAllowed(app))
Expand All @@ -204,7 +201,6 @@ func Test_NamespaceRestrictions(t *testing.T) {
err = ctl.i.RemoveNamespace("argocd")
require.Error(t, err)
})

t.Run("Prevent resources in non-allowed namespaces to execute add handlers", func(t *testing.T) {
ctl := fakeGenericInformer(t, WithNamespaces("argocd"))
err := ctl.i.Start(context.TODO())
Expand All @@ -219,3 +215,19 @@ func Test_NamespaceRestrictions(t *testing.T) {
assert.False(t, added)
})
}

func Test_watchAndListNamespaces(t *testing.T) {
t.Run("Watch namespace is empty when no namespace is given", func(t *testing.T) {
ctl := fakeGenericInformer(t)
assert.Empty(t, ctl.i.watchAndListNamespace())
})
t.Run("Watch namespace is same as single namespace constraint", func(t *testing.T) {
ctl := fakeGenericInformer(t, WithNamespaces("argocd"))
assert.Equal(t, "argocd", ctl.i.watchAndListNamespace())
})

t.Run("Watch namespace is empty when multiple namespaces are given", func(t *testing.T) {
ctl := fakeGenericInformer(t, WithNamespaces("argocd", "foobar"))
assert.Empty(t, ctl.i.watchAndListNamespace())
})
}

0 comments on commit 85dea68

Please sign in to comment.