From 425e0f20363b970ceccfc5561d85ee2db4d47800 Mon Sep 17 00:00:00 2001 From: Giuseppe Santoro Date: Thu, 18 Aug 2022 12:44:03 +0100 Subject: [PATCH] Refactor tests in metricbeat in kubernetes module (#32706) * refactoring some tests to avoid duplication Co-authored-by: Andrew Gizas --- .../kubernetes/container/container_test.go | 146 +++++--- .../module/kubernetes/node/node_test.go | 52 ++- metricbeat/module/kubernetes/pod/pod_test.go | 327 +++++++----------- .../kubernetes/util/metrics_repo_test.go | 6 +- 4 files changed, 256 insertions(+), 275 deletions(-) diff --git a/metricbeat/module/kubernetes/container/container_test.go b/metricbeat/module/kubernetes/container/container_test.go index b0d93a9c152e..161051096133 100644 --- a/metricbeat/module/kubernetes/container/container_test.go +++ b/metricbeat/module/kubernetes/container/container_test.go @@ -25,58 +25,73 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" ) +// this file is used for the tests to compare expected result const testFile = "../_meta/test/stats_summary.json" -func TestEventMapping(t *testing.T) { - logger := logp.NewLogger("kubernetes.container") +type ContainerTestSuite struct { + suite.Suite + MetricsRepo *util.MetricsRepo + NodeName string + Namespace string + PodName string + ContainerName string + PodId util.PodId + Logger *logp.Logger + NodeMetrics *util.NodeMetrics + ContainerMetrics *util.ContainerMetrics +} - f, err := os.Open(testFile) - assert.NoError(t, err, "cannot open test file "+testFile) +func (s *ContainerTestSuite) SetupTest() { + s.MetricsRepo = util.NewMetricsRepo() + s.NodeName = "gke-beats-default-pool-a5b33e2e-hdww" + s.Namespace = "default" + s.PodName = "nginx-deployment-2303442956-pcqfc" + s.ContainerName = "nginx" - body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) + s.PodId = util.NewPodId(s.Namespace, s.PodName) - metricsRepo := util.NewMetricsRepo() + s.Logger = logp.NewLogger("kubernetes.container") - nodeName := "gke-beats-default-pool-a5b33e2e-hdww" + s.NodeMetrics = util.NewNodeMetrics() + s.NodeMetrics.CoresAllocatable = util.NewFloat64Metric(2) + s.NodeMetrics.MemoryAllocatable = util.NewFloat64Metric(146227200) - nodeMetrics := util.NewNodeMetrics() - nodeMetrics.CoresAllocatable = util.NewFloat64Metric(2) - nodeMetrics.MemoryAllocatable = util.NewFloat64Metric(146227200) - addNodeMetric(metricsRepo, nodeName, nodeMetrics) + s.ContainerMetrics = util.NewContainerMetrics() + s.ContainerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) +} - namespace := "default" - podName := "nginx-deployment-2303442956-pcqfc" - podId := util.NewPodId(namespace, podName) - containerName := "nginx" +func (s *ContainerTestSuite) ReadTestFile(testFile string) []byte { + f, err := os.Open(testFile) + s.NoError(err, "cannot open test file "+testFile) - containerMetrics := util.NewContainerMetrics() - containerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, containerName, containerMetrics) + body, err := ioutil.ReadAll(f) + s.NoError(err, "cannot read test file "+testFile) - events, err := eventMapping(body, metricsRepo, logger) - assert.NoError(t, err, "error mapping "+testFile) + return body +} - assert.Len(t, events, 1, "got wrong number of events") +func (s *ContainerTestSuite) TestEventMapping() { + s.MetricsRepo.DeleteAllNodeStore() - testCases := map[string]interface{}{ + s.addNodeMetric(s.NodeMetrics) + s.addContainerMetric(s.ContainerName, s.ContainerMetrics) + + body := s.ReadTestFile(testFile) + events, err := eventMapping(body, s.MetricsRepo, s.Logger) + + s.basicTests(events, err) + + cpuMemoryTestCases := map[string]interface{}{ "cpu.usage.core.ns": 43959424, "cpu.usage.nanocores": 11263994, - "logs.available.bytes": int64(98727014400), - "logs.capacity.bytes": int64(101258067968), - "logs.used.bytes": 28672, - "logs.inodes.count": 6258720, - "logs.inodes.free": 6120096, - "logs.inodes.used": 138624, - "memory.available.bytes": 0, "memory.usage.bytes": 1462272, "memory.rss.bytes": 1409024, @@ -90,44 +105,67 @@ func TestEventMapping(t *testing.T) { "memory.usage.node.pct": 0.01, "memory.usage.limit.pct": 0.1, "memory.workingset.limit.pct": 0.09943977591036414, - - "name": "nginx", - - "rootfs.available.bytes": int64(98727014400), - "rootfs.capacity.bytes": int64(101258067968), - "rootfs.used.bytes": 61440, - "rootfs.inodes.used": 21, } - for k, v := range testCases { - testValue(t, events[0], k, v) - } + s.RunMetricsTests(events[0], cpuMemoryTestCases) - containerEcsFields := ecsfields(events[0], logger) + containerEcsFields := ecsfields(events[0], s.Logger) testEcs := map[string]interface{}{ "cpu.usage": 0.005631997, "memory.usage": 0.01, "name": "nginx", } - for k, v := range testEcs { - testValue(t, containerEcsFields, k, v) - } + s.RunMetricsTests(containerEcsFields, testEcs) } -func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { +func (s *ContainerTestSuite) testValue(event mapstr.M, field string, expected interface{}) { data, err := event.GetValue(field) - assert.NoError(t, err, "Could not read field "+field) - assert.EqualValues(t, data, value, "Wrong value for field "+field) + s.NoError(err, "Could not read field "+field) + s.EqualValues(expected, data, "Wrong value for field "+field) } -func addContainerMetric(metricsRepo *util.MetricsRepo, nodeName string, podId util.PodId, containerName string, metrics *util.ContainerMetrics) { - nodeStore, _ := metricsRepo.AddNodeStore(nodeName) - podStore, _ := nodeStore.AddPodStore(podId) +func (s *ContainerTestSuite) addContainerMetric(containerName string, containerMetric *util.ContainerMetrics) { + nodeStore, _ := s.MetricsRepo.AddNodeStore(s.NodeName) + podStore, _ := nodeStore.AddPodStore(s.PodId) containerStore, _ := podStore.AddContainerStore(containerName) - containerStore.SetContainerMetrics(metrics) + containerStore.SetContainerMetrics(containerMetric) } -func addNodeMetric(metricsRepo *util.MetricsRepo, nodeName string, nodeMetrics *util.NodeMetrics) { - nodeStore, _ := metricsRepo.AddNodeStore(nodeName) +func (s *ContainerTestSuite) addNodeMetric(nodeMetrics *util.NodeMetrics) { + nodeStore, _ := s.MetricsRepo.AddNodeStore(s.NodeName) nodeStore.SetNodeMetrics(nodeMetrics) } + +func (s *ContainerTestSuite) basicTests(events []mapstr.M, err error) { + s.NoError(err, "error mapping "+testFile) + + s.Len(events, 1, "got wrong number of events") + + basicTestCases := map[string]interface{}{ + "logs.available.bytes": int64(98727014400), + "logs.capacity.bytes": int64(101258067968), + "logs.used.bytes": 28672, + "logs.inodes.count": 6258720, + "logs.inodes.free": 6120096, + "logs.inodes.used": 138624, + + "name": "nginx", + + "rootfs.available.bytes": int64(98727014400), + "rootfs.capacity.bytes": int64(101258067968), + "rootfs.used.bytes": 61440, + "rootfs.inodes.used": 21, + } + + s.RunMetricsTests(events[0], basicTestCases) +} + +func (s *ContainerTestSuite) RunMetricsTests(event mapstr.M, testCases map[string]interface{}) { + for k, v := range testCases { + s.testValue(event, k, v) + } +} + +func TestContainerTestSuite(t *testing.T) { + suite.Run(t, new(ContainerTestSuite)) +} diff --git a/metricbeat/module/kubernetes/node/node_test.go b/metricbeat/module/kubernetes/node/node_test.go index ae543b735a1d..c2129c9b57bd 100644 --- a/metricbeat/module/kubernetes/node/node_test.go +++ b/metricbeat/module/kubernetes/node/node_test.go @@ -25,27 +25,51 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" ) +// this file is used for the tests to compare expected result const testFile = "../_meta/test/stats_summary.json" -func TestEventMapping(t *testing.T) { - logger := logp.NewLogger("kubernetes.node") +type NodeTestSuite struct { + suite.Suite + Logger *logp.Logger +} + +func (s *NodeTestSuite) SetupTest() { + s.Logger = logp.NewLogger("kubernetes.node") +} +func (s *NodeTestSuite) ReadTestFile(testFile string) []byte { f, err := os.Open(testFile) - assert.NoError(t, err, "cannot open test file "+testFile) + s.NoError(err, "cannot open test file "+testFile) body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) + s.NoError(err, "cannot read test file "+testFile) + + return body +} + +func (s *NodeTestSuite) TestEventMapping() { + body := s.ReadTestFile(testFile) + event, err := eventMapping(body, s.Logger) + + s.basicTests(event, err) +} + +func (s *NodeTestSuite) testValue(event mapstr.M, field string, expected interface{}) { + data, err := event.GetValue(field) + s.NoError(err, "Could not read field "+field) + s.EqualValues(expected, data, "Wrong value for field "+field) +} - event, err := eventMapping(body, logger) - assert.NoError(t, err, "error mapping "+testFile) +func (s *NodeTestSuite) basicTests(event mapstr.M, err error) { + s.NoError(err, "error mapping "+testFile) - testCases := map[string]interface{}{ + basicTestCases := map[string]interface{}{ "cpu.usage.core.ns": int64(4189523881380), "cpu.usage.nanocores": 18691146, @@ -75,13 +99,15 @@ func TestEventMapping(t *testing.T) { "runtime.imagefs.used.bytes": 860204379, } + s.RunMetricsTests(event, basicTestCases) +} + +func (s *NodeTestSuite) RunMetricsTests(event mapstr.M, testCases map[string]interface{}) { for k, v := range testCases { - testValue(t, event, k, v) + s.testValue(event, k, v) } } -func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { - data, err := event.GetValue(field) - assert.NoError(t, err, "Could not read field "+field) - assert.EqualValues(t, data, value, "Wrong value for field "+field) +func TestNodeTestSuite(t *testing.T) { + suite.Run(t, new(NodeTestSuite)) } diff --git a/metricbeat/module/kubernetes/pod/pod_test.go b/metricbeat/module/kubernetes/pod/pod_test.go index 69dc23bb5d50..d8e02c3b7fbf 100644 --- a/metricbeat/module/kubernetes/pod/pod_test.go +++ b/metricbeat/module/kubernetes/pod/pod_test.go @@ -25,57 +25,77 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" + + "github.com/stretchr/testify/suite" ) +// both these two files are used in tests to compare expected result const testFile = "../_meta/test/stats_summary.json" const testFileWithMultipleContainers = "../_meta/test/stats_summary_multiple_containers.json" -func TestEventMapping(t *testing.T) { - logger := logp.NewLogger("kubernetes.pod") +type PodTestSuite struct { + suite.Suite + MetricsRepo *util.MetricsRepo + NodeName string + Namespace string + PodName string + ContainerName string + AnotherContainerName string + PodId util.PodId + Logger *logp.Logger + NodeMetrics *util.NodeMetrics + ContainerMetrics *util.ContainerMetrics + AnotherContainerMetrics *util.ContainerMetrics +} - f, err := os.Open(testFile) - assert.NoError(t, err, "cannot open test file "+testFile) +func (s *PodTestSuite) SetupTest() { + s.MetricsRepo = util.NewMetricsRepo() + s.NodeName = "gke-beats-default-pool-a5b33e2e-hdww" + s.Namespace = "default" + s.PodName = "nginx-deployment-2303442956-pcqfc" + s.ContainerName = "nginx" + s.AnotherContainerName = "sidecar" - body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) + s.PodId = util.NewPodId(s.Namespace, s.PodName) - metricsRepo := util.NewMetricsRepo() + s.Logger = logp.NewLogger("kubernetes.pod") - nodeName := "gke-beats-default-pool-a5b33e2e-hdww" + s.NodeMetrics = util.NewNodeMetrics() + s.NodeMetrics.CoresAllocatable = util.NewFloat64Metric(2) + s.NodeMetrics.MemoryAllocatable = util.NewFloat64Metric(146227200) - nodeMetrics := util.NewNodeMetrics() - nodeMetrics.CoresAllocatable = util.NewFloat64Metric(2) - nodeMetrics.MemoryAllocatable = util.NewFloat64Metric(146227200) - addNodeMetric(metricsRepo, nodeName, nodeMetrics) + s.ContainerMetrics = util.NewContainerMetrics() + s.ContainerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - namespace := "default" - podName := "nginx-deployment-2303442956-pcqfc" - podId := util.NewPodId(namespace, podName) - containerName := "nginx" + s.AnotherContainerMetrics = util.NewContainerMetrics() + s.AnotherContainerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) +} - containerMetrics := util.NewContainerMetrics() - containerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, containerName, containerMetrics) +func (s *PodTestSuite) ReadTestFile(testFile string) []byte { + f, err := os.Open(testFile) + s.NoError(err, "cannot open test file "+testFile) - events, err := eventMapping(body, metricsRepo, logger) - assert.NoError(t, err, "error mapping "+testFile) + body, err := ioutil.ReadAll(f) + s.NoError(err, "cannot read test file "+testFile) - assert.Len(t, events, 1, "got wrong number of events") + return body +} - testCases := map[string]interface{}{ - "name": "nginx-deployment-2303442956-pcqfc", - "uid": "beabc196-2456-11e7-a3ad-42010a840235", +func (s *PodTestSuite) TestEventMapping() { + s.MetricsRepo.DeleteAllNodeStore() - "network.rx.bytes": 107056, - "network.rx.errors": 0, - "network.tx.bytes": 72447, - "network.tx.errors": 0, + s.addNodeMetric(s.NodeMetrics) + s.addContainerMetric(s.ContainerName, s.ContainerMetrics) + + body := s.ReadTestFile(testFile) + events, err := eventMapping(body, s.MetricsRepo, s.Logger) + s.basicTests(events, err) + + cpuMemoryTestCases := map[string]interface{}{ // calculated pct fields: "cpu.usage.nanocores": 11263994, "cpu.usage.node.pct": 0.005631997, @@ -87,99 +107,43 @@ func TestEventMapping(t *testing.T) { "memory.working_set.limit.pct": 0.09943977591036414, } - for k, v := range testCases { - testValue(t, events[0], k, v) - } + s.RunMetricsTests(events[0], cpuMemoryTestCases) } -func TestEventMappingWithZeroNodeMetrics(t *testing.T) { - logger := logp.NewLogger("kubernetes.pod") - - f, err := os.Open(testFile) - assert.NoError(t, err, "cannot open test file "+testFile) - - body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) - - metricsRepo := util.NewMetricsRepo() - - nodeName := "gke-beats-default-pool-a5b33e2e-hdww" +func (s *PodTestSuite) TestEventMappingWithZeroNodeMetrics() { + s.MetricsRepo.DeleteAllNodeStore() nodeMetrics := util.NewNodeMetrics() - addNodeMetric(metricsRepo, nodeName, nodeMetrics) - - namespace := "default" - podName := "nginx-deployment-2303442956-pcqfc" - podId := util.NewPodId(namespace, podName) - containerName := "nginx" - - containerMetrics := util.NewContainerMetrics() - containerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, containerName, containerMetrics) - - events, err := eventMapping(body, metricsRepo, logger) - assert.NoError(t, err, "error mapping "+testFile) + s.addNodeMetric(nodeMetrics) - assert.Len(t, events, 1, "got wrong number of events") + s.addContainerMetric(s.ContainerName, s.ContainerMetrics) - testCases := map[string]interface{}{ - "name": "nginx-deployment-2303442956-pcqfc", - "uid": "beabc196-2456-11e7-a3ad-42010a840235", + body := s.ReadTestFile(testFile) + events, err := eventMapping(body, s.MetricsRepo, s.Logger) - "network.rx.bytes": 107056, - "network.rx.errors": 0, - "network.tx.bytes": 72447, - "network.tx.errors": 0, + s.basicTests(events, err) - // calculated pct fields: + cpuMemoryTestCases := map[string]interface{}{ "cpu.usage.nanocores": 11263994, "memory.usage.bytes": 1462272, "memory.working_set.limit.pct": 0.09943977591036414, } - for k, v := range testCases { - testValue(t, events[0], k, v) - } + s.RunMetricsTests(events[0], cpuMemoryTestCases) } -func TestEventMappingWithNoNodeMetrics(t *testing.T) { - logger := logp.NewLogger("kubernetes.pod") - - f, err := os.Open(testFile) - assert.NoError(t, err, "cannot open test file "+testFile) - - body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) - - metricsRepo := util.NewMetricsRepo() - - nodeName := "gke-beats-default-pool-a5b33e2e-hdww" +func (s *PodTestSuite) TestEventMappingWithNoNodeMetrics() { + s.MetricsRepo.DeleteAllNodeStore() - namespace := "default" - podName := "nginx-deployment-2303442956-pcqfc" - podId := util.NewPodId(namespace, podName) - containerName := "nginx" + s.addContainerMetric(s.ContainerName, s.ContainerMetrics) - containerMetrics := util.NewContainerMetrics() - containerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, containerName, containerMetrics) + body := s.ReadTestFile(testFile) + events, err := eventMapping(body, s.MetricsRepo, s.Logger) - events, err := eventMapping(body, metricsRepo, logger) - assert.NoError(t, err, "error mapping "+testFile) + s.basicTests(events, err) - assert.Len(t, events, 1, "got wrong number of events") - - testCases := map[string]interface{}{ - "name": "nginx-deployment-2303442956-pcqfc", - "uid": "beabc196-2456-11e7-a3ad-42010a840235", - - "network.rx.bytes": 107056, - "network.rx.errors": 0, - "network.tx.bytes": 72447, - "network.tx.errors": 0, - - // calculated pct fields: + cpuMemoryTestCases := map[string]interface{}{ "cpu.usage.nanocores": 11263994, "memory.usage.bytes": 1462272, @@ -187,53 +151,21 @@ func TestEventMappingWithNoNodeMetrics(t *testing.T) { "memory.working_set.limit.pct": 0.09943977591036414, } - for k, v := range testCases { - testValue(t, events[0], k, v) - } + s.RunMetricsTests(events[0], cpuMemoryTestCases) } -func TestEventMappingWithMultipleContainers(t *testing.T) { - logger := logp.NewLogger("kubernetes.pod") - - f, err := os.Open(testFileWithMultipleContainers) - assert.NoError(t, err, "cannot open test file "+testFile) +func (s *PodTestSuite) TestEventMappingWithMultipleContainers() { + s.MetricsRepo.DeleteAllNodeStore() - body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) + s.addNodeMetric(s.NodeMetrics) + s.addContainerMetric(s.ContainerName, s.ContainerMetrics) - metricsRepo := util.NewMetricsRepo() + body := s.ReadTestFile(testFileWithMultipleContainers) // NOTE: different test file + events, err := eventMapping(body, s.MetricsRepo, s.Logger) - nodeName := "gke-beats-default-pool-a5b33e2e-hdww" + s.basicTests(events, err) - nodeMetrics := util.NewNodeMetrics() - nodeMetrics.CoresAllocatable = util.NewFloat64Metric(2) - nodeMetrics.MemoryAllocatable = util.NewFloat64Metric(146227200) - addNodeMetric(metricsRepo, nodeName, nodeMetrics) - - namespace := "default" - podName := "nginx-deployment-2303442956-pcqfc" - podId := util.NewPodId(namespace, podName) - containerName := "nginx" - - containerMetrics := util.NewContainerMetrics() - containerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, containerName, containerMetrics) - - events, err := eventMapping(body, metricsRepo, logger) - assert.NoError(t, err, "error mapping "+testFile) - - assert.Len(t, events, 1, "got wrong number of events") - - testCases := map[string]interface{}{ - "name": "nginx-deployment-2303442956-pcqfc", - "uid": "beabc196-2456-11e7-a3ad-42010a840235", - - "network.rx.bytes": 107056, - "network.rx.errors": 0, - "network.tx.bytes": 72447, - "network.tx.errors": 0, - - // calculated pct fields: + cpuMemoryTestCases := map[string]interface{}{ // Following comments explain what is the difference with the test `TestEventMapping` "cpu.usage.nanocores": 22527988, // 2x usage since 2 container "cpu.usage.node.pct": 0.011263994, // 2x usage since 2 container @@ -245,59 +177,22 @@ func TestEventMappingWithMultipleContainers(t *testing.T) { "memory.working_set.limit.pct": 0.019887955182072828, // similar concept to `memory.usage.limit.pct`. 2x usage but denominator 10x since nodeLimit = 10x containerMemLimit } - for k, v := range testCases { - testValue(t, events[0], k, v) - } + s.RunMetricsTests(events[0], cpuMemoryTestCases) } -func TestEventMappingWithMultipleContainersWithAllMemLimits(t *testing.T) { - logger := logp.NewLogger("kubernetes.pod") - - f, err := os.Open(testFileWithMultipleContainers) - assert.NoError(t, err, "cannot open test file "+testFile) - - body, err := ioutil.ReadAll(f) - assert.NoError(t, err, "cannot read test file "+testFile) - - metricsRepo := util.NewMetricsRepo() - - nodeName := "gke-beats-default-pool-a5b33e2e-hdww" +func (s *PodTestSuite) TestEventMappingWithMultipleContainersWithAllMemLimits() { + s.MetricsRepo.DeleteAllNodeStore() - nodeMetrics := util.NewNodeMetrics() - nodeMetrics.CoresAllocatable = util.NewFloat64Metric(2) - nodeMetrics.MemoryAllocatable = util.NewFloat64Metric(146227200) - addNodeMetric(metricsRepo, nodeName, nodeMetrics) - - namespace := "default" - podName := "nginx-deployment-2303442956-pcqfc" - podId := util.NewPodId(namespace, podName) - containerName := "nginx" - - containerMetrics := util.NewContainerMetrics() - containerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, containerName, containerMetrics) - - anotherContainerName := "sidecar" + s.addNodeMetric(s.NodeMetrics) + s.addContainerMetric(s.ContainerName, s.ContainerMetrics) + s.addContainerMetric(s.AnotherContainerName, s.AnotherContainerMetrics) - anotherContainerMetrics := util.NewContainerMetrics() - anotherContainerMetrics.MemoryLimit = util.NewFloat64Metric(14622720) - addContainerMetric(metricsRepo, nodeName, podId, anotherContainerName, anotherContainerMetrics) + body := s.ReadTestFile(testFileWithMultipleContainers) // NOTE: different test file + events, err := eventMapping(body, s.MetricsRepo, s.Logger) - events, err := eventMapping(body, metricsRepo, logger) - assert.NoError(t, err, "error mapping "+testFile) + s.basicTests(events, err) - assert.Len(t, events, 1, "got wrong number of events") - - testCases := map[string]interface{}{ - "name": "nginx-deployment-2303442956-pcqfc", - "uid": "beabc196-2456-11e7-a3ad-42010a840235", - - "network.rx.bytes": 107056, - "network.rx.errors": 0, - "network.tx.bytes": 72447, - "network.tx.errors": 0, - - // calculated pct fields: + cpuMemoryTestCases := map[string]interface{}{ // Following comments explain what is the difference with the test `TestEventMapping "cpu.usage.nanocores": 22527988, // 2x usage since 2 container "cpu.usage.node.pct": 0.011263994, // 2x usage since 2 container @@ -309,25 +204,51 @@ func TestEventMappingWithMultipleContainersWithAllMemLimits(t *testing.T) { "memory.working_set.limit.pct": 0.09943977591036414, // 2x usage / 2x limit = same value } - for k, v := range testCases { - testValue(t, events[0], k, v) - } + s.RunMetricsTests(events[0], cpuMemoryTestCases) } -func testValue(t *testing.T, event mapstr.M, field string, expected interface{}) { +func (s *PodTestSuite) testValue(event mapstr.M, field string, expected interface{}) { data, err := event.GetValue(field) - assert.NoError(t, err, "Could not read field "+field) - assert.EqualValues(t, expected, data, "Wrong value for field "+field) + s.NoError(err, "Could not read field "+field) + s.EqualValues(expected, data, "Wrong value for field "+field) } -func addContainerMetric(metricsRepo *util.MetricsRepo, nodeName string, podId util.PodId, containerName string, containerMetric *util.ContainerMetrics) { - nodeStore, _ := metricsRepo.AddNodeStore(nodeName) - podStore, _ := nodeStore.AddPodStore(podId) +func (s *PodTestSuite) addContainerMetric(containerName string, containerMetric *util.ContainerMetrics) { + nodeStore, _ := s.MetricsRepo.AddNodeStore(s.NodeName) + podStore, _ := nodeStore.AddPodStore(s.PodId) containerStore, _ := podStore.AddContainerStore(containerName) containerStore.SetContainerMetrics(containerMetric) } -func addNodeMetric(metricsRepo *util.MetricsRepo, nodeName string, nodeMetrics *util.NodeMetrics) { - nodeStore, _ := metricsRepo.AddNodeStore(nodeName) +func (s *PodTestSuite) addNodeMetric(nodeMetrics *util.NodeMetrics) { + nodeStore, _ := s.MetricsRepo.AddNodeStore(s.NodeName) nodeStore.SetNodeMetrics(nodeMetrics) } + +func (s *PodTestSuite) basicTests(events []mapstr.M, err error) { + s.NoError(err, "error mapping "+testFile) + + s.Len(events, 1, "got wrong number of events") + + basicTestCases := map[string]interface{}{ + "name": "nginx-deployment-2303442956-pcqfc", + "uid": "beabc196-2456-11e7-a3ad-42010a840235", + + "network.rx.bytes": 107056, + "network.rx.errors": 0, + "network.tx.bytes": 72447, + "network.tx.errors": 0, + } + + s.RunMetricsTests(events[0], basicTestCases) +} + +func (s *PodTestSuite) RunMetricsTests(events mapstr.M, testCases map[string]interface{}) { + for k, v := range testCases { + s.testValue(events, k, v) + } +} + +func TestPodTestSuite(t *testing.T) { + suite.Run(t, new(PodTestSuite)) +} diff --git a/metricbeat/module/kubernetes/util/metrics_repo_test.go b/metricbeat/module/kubernetes/util/metrics_repo_test.go index e69167c88714..3de7122bca4b 100644 --- a/metricbeat/module/kubernetes/util/metrics_repo_test.go +++ b/metricbeat/module/kubernetes/util/metrics_repo_test.go @@ -338,10 +338,6 @@ func (s *MetricsRepoTestSuite) TestSetContainerMetricsMultiplePods() { addContainerMetric(s.MetricsRepo, s.NodeName, s.PodId, s.ContainerName, s.ContainerMetric) addContainerMetric(s.MetricsRepo, s.NodeName, s.AnotherPodId, s.ContainerName, s.AnotherContainerMetric) - s.Equal(1, len(s.MetricsRepo.NodeNames())) - s.Equal(s.ContainerMetric, GetMetric(s.MetricsRepo, s.NodeName, s.PodId, s.ContainerName)) - s.Equal(s.AnotherContainerMetric, GetMetric(s.MetricsRepo, s.NodeName, s.AnotherPodId, s.ContainerName)) - nodeStore := s.MetricsRepo.GetNodeStore(s.NodeName) s.Equal(2, len(nodeStore.PodIds())) } @@ -373,7 +369,7 @@ func (s *MetricsRepoTestSuite) TestGetContainerMetricsNotFound() { s.Nil(ans.MemoryLimit) } -func TestExampleTestSuite(t *testing.T) { +func TestMetricsRepoTestSuite(t *testing.T) { suite.Run(t, new(MetricsRepoTestSuite)) }