Skip to content

Commit

Permalink
[Metricbeat][Kubernetes] Add test metrics family to scheduler, proxy …
Browse files Browse the repository at this point in the history
…and controller manager (#36375)

* Add test metrics family to scheduler, proxy and controller manager

* Update function name.

* Sort imports

* Sort imports
  • Loading branch information
constanca-m authored Aug 21, 2023
1 parent 25514e7 commit 39b5834
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ CHANGELOG*
/licenses/ @elastic/elastic-agent-data-plane
/metricbeat/ @elastic/elastic-agent-data-plane
/metricbeat/docs/ # Listed without an owner to avoid maintaining doc ownership for each input and module.
/metricbeat/helper/kubernetes @elastic/obs-cloudnative-monitoring
/metricbeat/module/ @elastic/integrations
/metricbeat/module/apache @elastic/obs-infraobs-integrations
/metricbeat/module/beat/ @elastic/infra-monitoring-ui
Expand Down
4 changes: 2 additions & 2 deletions metricbeat/helper/kubernetes/ktest/ktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func GetTestCases(files []string) ptest.TestCases {
return cases
}

// TestStateMetricsFamily
// TestMetricsFamily
// This function reads the metric files and checks if the resource fetched metrics exist in it.
// It only checks the family metric, because if the metric doesn't have any data, we don't have a way
// to know the labels from the file.
// The test fails if the metric does not exist in any of the files.
// A warning is printed if the metric is not present in all of them.
// Nothing happens, otherwise.
func TestStateMetricsFamily(t *testing.T, files []string, mapping *p.MetricsMapping) {
func TestMetricsFamily(t *testing.T, files []string, mapping *p.MetricsMapping) {
metricsFiles := map[string][]string{}
for i := 0; i < len(files); i++ {
content, err := ioutil.ReadFile(files[i])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,33 @@ package controllermanager
import (
"testing"

k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest"
"github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest"
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
_ "github.com/elastic/beats/v7/metricbeat/module/kubernetes"
)

var files = []string{
"./_meta/test/metrics.1.25",
"./_meta/test/metrics.1.26",
"./_meta/test/metrics.1.27",
}

func TestEventMapping(t *testing.T) {
ptest.TestMetricSet(t, "kubernetes", "controllermanager",
ptest.TestCases{
ptest.TestCase{
MetricsFile: "./_meta/test/metrics.1.25",
ExpectedFile: "./_meta/test/metrics.1.25.expected",
},
ptest.TestCase{
MetricsFile: "./_meta/test/metrics.1.26",
ExpectedFile: "./_meta/test/metrics.1.26.expected",
},
ptest.TestCase{
MetricsFile: "./_meta/test/metrics.1.27",
ExpectedFile: "./_meta/test/metrics.1.27.expected",
},
},
)
var testCases ptest.TestCases
for _, file := range files {
testCases = append(testCases, ptest.TestCase{
MetricsFile: file,
ExpectedFile: file + ".expected",
})
}
ptest.TestMetricSet(t, "kubernetes", "controllermanager", testCases)
}

func TestData(t *testing.T) {
mbtest.TestDataFiles(t, "kubernetes", "controllermanager")
}

func TestMetricsFamily(t *testing.T) {
k.TestMetricsFamily(t, files, mapping)
}
36 changes: 19 additions & 17 deletions metricbeat/module/kubernetes/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@ package proxy
import (
"testing"

k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest"
"github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest"

mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
_ "github.com/elastic/beats/v7/metricbeat/module/kubernetes"
)

var files = []string{
"./_meta/test/metrics.1.25",
"./_meta/test/metrics.1.26",
"./_meta/test/metrics.1.27",
}

func TestEventMapping(t *testing.T) {
ptest.TestMetricSet(t, "kubernetes", "proxy",
ptest.TestCases{
{
MetricsFile: "./_meta/test/metrics.1.25",
ExpectedFile: "./_meta/test/metrics.1.25.expected",
},
{
MetricsFile: "./_meta/test/metrics.1.26",
ExpectedFile: "./_meta/test/metrics.1.26.expected",
},
ptest.TestCase{
MetricsFile: "./_meta/test/metrics.1.27",
ExpectedFile: "./_meta/test/metrics.1.27.expected",
},
},
)
var testCases ptest.TestCases
for _, file := range files {
testCases = append(testCases, ptest.TestCase{
MetricsFile: file,
ExpectedFile: file + ".expected",
})
}
ptest.TestMetricSet(t, "kubernetes", "proxy", testCases)
}

func TestData(t *testing.T) {
mbtest.TestDataFiles(t, "kubernetes", "proxy")
}

func TestMetricsFamily(t *testing.T) {
k.TestMetricsFamily(t, files, mapping)
}
36 changes: 19 additions & 17 deletions metricbeat/module/kubernetes/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@ package scheduler
import (
"testing"

k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest"
"github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest"

mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
_ "github.com/elastic/beats/v7/metricbeat/module/kubernetes"
)

var files = []string{
"./_meta/test/metrics.1.25",
"./_meta/test/metrics.1.26",
"./_meta/test/metrics.1.27",
}

func TestEventMapping(t *testing.T) {
ptest.TestMetricSet(t, "kubernetes", "scheduler",
ptest.TestCases{
{
MetricsFile: "./_meta/test/metrics.1.25",
ExpectedFile: "./_meta/test/metrics.1.25.expected",
},
{
MetricsFile: "./_meta/test/metrics.1.26",
ExpectedFile: "./_meta/test/metrics.1.26.expected",
},
ptest.TestCase{
MetricsFile: "./_meta/test/metrics.1.27",
ExpectedFile: "./_meta/test/metrics.1.27.expected",
},
},
)
var testCases ptest.TestCases
for _, file := range files {
testCases = append(testCases, ptest.TestCase{
MetricsFile: file,
ExpectedFile: file + ".expected",
})
}
ptest.TestMetricSet(t, "kubernetes", "scheduler", testCases)
}

func TestData(t *testing.T) {
mbtest.TestDataFiles(t, "kubernetes", "scheduler")
}

func TestMetricsFamily(t *testing.T) {
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
2 changes: 1 addition & 1 deletion metricbeat/module/kubernetes/state_job/state_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
2 changes: 1 addition & 1 deletion metricbeat/module/kubernetes/state_node/state_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
2 changes: 1 addition & 1 deletion metricbeat/module/kubernetes/state_pod/state_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestData(t *testing.T) {
}

func TestMetricsFamily(t *testing.T) {
k.TestStateMetricsFamily(t, files, mapping)
k.TestMetricsFamily(t, files, mapping)
}

0 comments on commit 39b5834

Please sign in to comment.