Skip to content

Commit

Permalink
Add test for prometheus selectors in conversion (#2724)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
  • Loading branch information
pavolloffay authored Mar 6, 2024
1 parent 249bfd4 commit 2e5339c
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions apis/v1alpha1/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,65 @@ func Test_tov1beta1AndBack(t *testing.T) {
assert.Equal(t, colalpha1, colalpha1Converted)
}

func Test_tov1beta1AndBack_prometheus_selectors(t *testing.T) {
t.Run("nil-selectors", func(t *testing.T) {
colalpha1 := OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
TargetAllocator: OpenTelemetryTargetAllocator{
PrometheusCR: OpenTelemetryTargetAllocatorPrometheusCR{
// nil or empty map means select everything
PodMonitorSelector: nil,
ServiceMonitorSelector: nil,
},
},
},
}

colbeta1 := v1beta1.OpenTelemetryCollector{}
err := colalpha1.ConvertTo(&colbeta1)
require.NoError(t, err)

// nil LabelSelector means select nothing
// empty LabelSelector mean select everything
assert.NotNil(t, colbeta1.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector)
assert.NotNil(t, colbeta1.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector)
assert.Equal(t, 0, len(colalpha1.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector))
assert.Equal(t, 0, len(colalpha1.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector))

err = colalpha1.ConvertFrom(&colbeta1)
require.NoError(t, err)
assert.Nil(t, colalpha1.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector)
assert.Nil(t, colalpha1.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector)
})
t.Run("empty-selectors", func(t *testing.T) {
colalpha1 := OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
TargetAllocator: OpenTelemetryTargetAllocator{
PrometheusCR: OpenTelemetryTargetAllocatorPrometheusCR{
// nil or empty map means select everything
PodMonitorSelector: map[string]string{},
ServiceMonitorSelector: map[string]string{},
},
},
},
}

colbeta1 := v1beta1.OpenTelemetryCollector{}
err := colalpha1.ConvertTo(&colbeta1)
require.NoError(t, err)

// nil LabelSelector means select nothing
// empty LabelSelector mean select everything
assert.NotNil(t, colbeta1.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector)
assert.NotNil(t, colbeta1.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector)

err = colalpha1.ConvertFrom(&colbeta1)
require.NoError(t, err)
assert.Equal(t, map[string]string{}, colalpha1.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector)
assert.Equal(t, map[string]string{}, colalpha1.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector)
})
}

func createTA() OpenTelemetryTargetAllocator {
replicas := int32(2)
runAsNonRoot := true
Expand Down

0 comments on commit 2e5339c

Please sign in to comment.