Skip to content

Commit

Permalink
refactor some agent receivers unit tests into a table test
Browse files Browse the repository at this point in the history
  • Loading branch information
shorim committed Oct 10, 2024
1 parent 4d9226a commit 4fc95bb
Showing 1 changed file with 82 additions and 184 deletions.
266 changes: 82 additions & 184 deletions internal/otelcollector/config/metric/agent/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,198 +28,96 @@ func TestReceivers(t *testing.T) {
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)
})

t.Run("runtime input enabled with default resources metrics enabled", func(t *testing.T) {
collectorConfig := sut.Build([]telemetryv1alpha1.MetricPipeline{
testutils.NewMetricPipelineBuilder().WithRuntimeInput(true).Build(),
}, BuildOptions{})

require.Nil(t, collectorConfig.Receivers.PrometheusAppPods)
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)

expectedKubeletStatsReceiver := KubeletStatsReceiver{
CollectionInterval: "30s",
AuthType: "serviceAccount",
Endpoint: "https://${MY_NODE_NAME}:10250",
InsecureSkipVerify: true,
MetricGroups: []MetricGroupType{"container", "pod"},
Metrics: KubeletStatsMetricsConfig{
ContainerCPUUsage: MetricConfig{Enabled: true},
ContainerCPUUtilization: MetricConfig{Enabled: false},
K8sPodCPUUsage: MetricConfig{Enabled: true},
K8sPodCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUUsage: MetricConfig{Enabled: true},
K8sNodeCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUTime: MetricConfig{Enabled: false},
K8sNodeMemoryMajorPageFaults: MetricConfig{Enabled: false},
K8sNodeMemoryPageFaults: MetricConfig{Enabled: false},
K8sNodeNetworkIO: MetricConfig{Enabled: false},
K8sNodeNetworkErrors: MetricConfig{Enabled: false},
},
ExtraMetadataLabels: []string{
"k8s.volume.type",
},
}
require.Equal(t, expectedKubeletStatsReceiver, *collectorConfig.Receivers.KubeletStats)
})

t.Run("runtime input enabled with only pod metrics enabled", func(t *testing.T) {
collectorConfig := sut.Build([]telemetryv1alpha1.MetricPipeline{
testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(false).
WithRuntimeInputPodMetrics(true).
WithRuntimeInputNodeMetrics(false).
WithRuntimeInputVolumeMetrics(false).
Build(),
}, BuildOptions{})

require.Nil(t, collectorConfig.Receivers.PrometheusAppPods)
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)

expectedKubeletStatsReceiver := KubeletStatsReceiver{
CollectionInterval: "30s",
AuthType: "serviceAccount",
Endpoint: "https://${MY_NODE_NAME}:10250",
InsecureSkipVerify: true,
MetricGroups: []MetricGroupType{"pod"},
Metrics: KubeletStatsMetricsConfig{
ContainerCPUUsage: MetricConfig{Enabled: true},
ContainerCPUUtilization: MetricConfig{Enabled: false},
K8sPodCPUUsage: MetricConfig{Enabled: true},
K8sPodCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUUsage: MetricConfig{Enabled: true},
K8sNodeCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUTime: MetricConfig{Enabled: false},
K8sNodeMemoryMajorPageFaults: MetricConfig{Enabled: false},
K8sNodeMemoryPageFaults: MetricConfig{Enabled: false},
K8sNodeNetworkIO: MetricConfig{Enabled: false},
K8sNodeNetworkErrors: MetricConfig{Enabled: false},
},
ExtraMetadataLabels: []string{
"k8s.volume.type",
t.Run("runtime input enabled", func(t *testing.T) {
tests := []struct {
name string
pipeline telemetryv1alpha1.MetricPipeline
expectedMetricGroups []MetricGroupType
}{
{
name: "default resources enabled",
pipeline: testutils.NewMetricPipelineBuilder().WithRuntimeInput(true).Build(),
expectedMetricGroups: []MetricGroupType{"container", "pod"},
},
}
require.Equal(t, expectedKubeletStatsReceiver, *collectorConfig.Receivers.KubeletStats)
})

t.Run("runtime input enabled with only container metrics enabled", func(t *testing.T) {
collectorConfig := sut.Build([]telemetryv1alpha1.MetricPipeline{
testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(true).
WithRuntimeInputPodMetrics(false).
WithRuntimeInputNodeMetrics(false).
WithRuntimeInputVolumeMetrics(false).
Build(),
}, BuildOptions{})

require.Nil(t, collectorConfig.Receivers.PrometheusAppPods)
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)

expectedKubeletStatsReceiver := KubeletStatsReceiver{
CollectionInterval: "30s",
AuthType: "serviceAccount",
Endpoint: "https://${MY_NODE_NAME}:10250",
InsecureSkipVerify: true,
MetricGroups: []MetricGroupType{"container"},
Metrics: KubeletStatsMetricsConfig{
ContainerCPUUsage: MetricConfig{Enabled: true},
ContainerCPUUtilization: MetricConfig{Enabled: false},
K8sPodCPUUsage: MetricConfig{Enabled: true},
K8sPodCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUUsage: MetricConfig{Enabled: true},
K8sNodeCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUTime: MetricConfig{Enabled: false},
K8sNodeMemoryMajorPageFaults: MetricConfig{Enabled: false},
K8sNodeMemoryPageFaults: MetricConfig{Enabled: false},
K8sNodeNetworkIO: MetricConfig{Enabled: false},
K8sNodeNetworkErrors: MetricConfig{Enabled: false},
{
name: "only pod metrics enabled",
pipeline: testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(false).
WithRuntimeInputPodMetrics(true).
WithRuntimeInputNodeMetrics(false).
WithRuntimeInputVolumeMetrics(false).
Build(),
expectedMetricGroups: []MetricGroupType{"pod"},
},
ExtraMetadataLabels: []string{
"k8s.volume.type",
{
name: "only container metrics enabled",
pipeline: testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(true).
WithRuntimeInputPodMetrics(false).
WithRuntimeInputNodeMetrics(false).
WithRuntimeInputVolumeMetrics(false).
Build(),
expectedMetricGroups: []MetricGroupType{"container"},
},
}
require.Equal(t, expectedKubeletStatsReceiver, *collectorConfig.Receivers.KubeletStats)
})

t.Run("runtime input enabled with only node metrics enabled", func(t *testing.T) {
collectorConfig := sut.Build([]telemetryv1alpha1.MetricPipeline{
testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(false).
WithRuntimeInputPodMetrics(false).
WithRuntimeInputNodeMetrics(true).
WithRuntimeInputVolumeMetrics(false).
Build(),
}, BuildOptions{})

require.Nil(t, collectorConfig.Receivers.PrometheusAppPods)
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)

expectedKubeletStatsReceiver := KubeletStatsReceiver{
CollectionInterval: "30s",
AuthType: "serviceAccount",
Endpoint: "https://${MY_NODE_NAME}:10250",
InsecureSkipVerify: true,
MetricGroups: []MetricGroupType{"node"},
Metrics: KubeletStatsMetricsConfig{
ContainerCPUUsage: MetricConfig{Enabled: true},
ContainerCPUUtilization: MetricConfig{Enabled: false},
K8sPodCPUUsage: MetricConfig{Enabled: true},
K8sPodCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUUsage: MetricConfig{Enabled: true},
K8sNodeCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUTime: MetricConfig{Enabled: false},
K8sNodeMemoryMajorPageFaults: MetricConfig{Enabled: false},
K8sNodeMemoryPageFaults: MetricConfig{Enabled: false},
K8sNodeNetworkIO: MetricConfig{Enabled: false},
K8sNodeNetworkErrors: MetricConfig{Enabled: false},
{
name: "only node metrics enabled",
pipeline: testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(false).
WithRuntimeInputPodMetrics(false).
WithRuntimeInputNodeMetrics(true).
WithRuntimeInputVolumeMetrics(false).
Build(),
expectedMetricGroups: []MetricGroupType{"node"},
},
ExtraMetadataLabels: []string{
"k8s.volume.type",
{
name: "only volume metrics enabled",
pipeline: testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(false).
WithRuntimeInputPodMetrics(false).
WithRuntimeInputNodeMetrics(false).
WithRuntimeInputVolumeMetrics(true).
Build(),
expectedMetricGroups: []MetricGroupType{"volume"},
},
}
require.Equal(t, expectedKubeletStatsReceiver, *collectorConfig.Receivers.KubeletStats)
})

t.Run("runtime input enabled with only volume metrics enabled", func(t *testing.T) {
collectorConfig := sut.Build([]telemetryv1alpha1.MetricPipeline{
testutils.NewMetricPipelineBuilder().
WithRuntimeInput(true).
WithRuntimeInputContainerMetrics(false).
WithRuntimeInputPodMetrics(false).
WithRuntimeInputNodeMetrics(false).
WithRuntimeInputVolumeMetrics(true).
Build(),
}, BuildOptions{})

require.Nil(t, collectorConfig.Receivers.PrometheusAppPods)
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)

expectedKubeletStatsReceiver := KubeletStatsReceiver{
CollectionInterval: "30s",
AuthType: "serviceAccount",
Endpoint: "https://${MY_NODE_NAME}:10250",
InsecureSkipVerify: true,
MetricGroups: []MetricGroupType{"volume"},
Metrics: KubeletStatsMetricsConfig{
ContainerCPUUsage: MetricConfig{Enabled: true},
ContainerCPUUtilization: MetricConfig{Enabled: false},
K8sPodCPUUsage: MetricConfig{Enabled: true},
K8sPodCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUUsage: MetricConfig{Enabled: true},
K8sNodeCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUTime: MetricConfig{Enabled: false},
K8sNodeMemoryMajorPageFaults: MetricConfig{Enabled: false},
K8sNodeMemoryPageFaults: MetricConfig{Enabled: false},
K8sNodeNetworkIO: MetricConfig{Enabled: false},
K8sNodeNetworkErrors: MetricConfig{Enabled: false},
},
ExtraMetadataLabels: []string{
"k8s.volume.type",
},
for _, test := range tests {
collectorConfig := sut.Build([]telemetryv1alpha1.MetricPipeline{
test.pipeline,
}, BuildOptions{})

require.Nil(t, collectorConfig.Receivers.PrometheusAppPods)
require.Nil(t, collectorConfig.Receivers.PrometheusIstio)

expectedKubeletStatsReceiver := KubeletStatsReceiver{
CollectionInterval: "30s",
AuthType: "serviceAccount",
Endpoint: "https://${MY_NODE_NAME}:10250",
InsecureSkipVerify: true,
MetricGroups: test.expectedMetricGroups,
Metrics: KubeletStatsMetricsConfig{
ContainerCPUUsage: MetricConfig{Enabled: true},
ContainerCPUUtilization: MetricConfig{Enabled: false},
K8sPodCPUUsage: MetricConfig{Enabled: true},
K8sPodCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUUsage: MetricConfig{Enabled: true},
K8sNodeCPUUtilization: MetricConfig{Enabled: false},
K8sNodeCPUTime: MetricConfig{Enabled: false},
K8sNodeMemoryMajorPageFaults: MetricConfig{Enabled: false},
K8sNodeMemoryPageFaults: MetricConfig{Enabled: false},
K8sNodeNetworkIO: MetricConfig{Enabled: false},
K8sNodeNetworkErrors: MetricConfig{Enabled: false},
},
ExtraMetadataLabels: []string{
"k8s.volume.type",
},
}
require.Equal(t, expectedKubeletStatsReceiver, *collectorConfig.Receivers.KubeletStats)
}
require.Equal(t, expectedKubeletStatsReceiver, *collectorConfig.Receivers.KubeletStats)
})

t.Run("prometheus input enabled", func(t *testing.T) {
Expand Down

0 comments on commit 4fc95bb

Please sign in to comment.