From dfdd982932a40f787ef5feed753ed63356bfa6d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constan=C3=A7a=20Manteigas?= <113898685+constanca-m@users.noreply.github.com> Date: Tue, 5 Dec 2023 09:49:07 +0100 Subject: [PATCH] [Kubernetes] Update KSM location to avoid updating all state tests (#37240) * Update KSM loc. * Update KSM loc. * Update KSM loc. * Update KSM loc. * refactor code * Update metricbeat/module/kubernetes/_meta/test/README.md Co-authored-by: Andrew Gizas * Update metricbeat/module/kubernetes/_meta/test/README.md Co-authored-by: Andrew Gizas * Update metricbeat/module/kubernetes/_meta/test/README.md Co-authored-by: Andrew Gizas * Fix testdata path file --------- Co-authored-by: Andrew Gizas --- metricbeat/helper/kubernetes/ktest/ktest.go | 54 ++++++++++++++----- .../_meta/test/{ => KSM}/ksm.v2.10.0.plain | 0 .../_meta/test/{ => KSM}/ksm.v2.8.2.plain | 0 .../_meta/test/{ => KSM}/ksm.v2.9.2.plain | 0 .../module/kubernetes/_meta/test/README.md | 20 ++++--- .../controllermanager_test.go | 2 +- .../module/kubernetes/proxy/proxy_test.go | 2 +- .../kubernetes/scheduler/scheduler_test.go | 2 +- .../state_container/_meta/testdata/config.yml | 2 +- .../state_container/state_container_test.go | 15 +++--- .../state_cronjob/_meta/testdata/config.yml | 2 +- .../state_cronjob/state_cronjob_test.go | 15 +++--- .../state_daemonset/_meta/testdata/config.yml | 2 +- .../state_daemonset/state_daemonset_test.go | 15 +++--- .../_meta/testdata/config.yml | 2 +- .../state_deployment/state_deployment_test.go | 15 +++--- .../state_job/_meta/testdata/config.yml | 2 +- .../kubernetes/state_job/state_job_test.go | 15 +++--- .../state_namespace/_meta/testdata/config.yml | 2 +- .../state_namespace/state_namespace_test.go | 15 +++--- .../state_node/_meta/testdata/config.yml | 2 +- .../kubernetes/state_node/state_node_test.go | 15 +++--- .../_meta/testdata/config.yml | 2 +- .../state_persistentvolume_test.go | 15 +++--- .../_meta/testdata/config.yml | 2 +- .../state_persistentvolumeclaim_test.go | 15 +++--- .../state_pod/_meta/testdata/config.yml | 2 +- .../kubernetes/state_pod/state_pod_test.go | 15 +++--- .../_meta/testdata/config.yml | 2 +- .../state_replicaset/state_replicaset_test.go | 15 +++--- .../_meta/testdata/config.yml | 2 +- .../state_resourcequota_test.go | 15 +++--- .../state_service/_meta/testdata/config.yml | 2 +- .../state_service/state_service_test.go | 16 +++--- .../_meta/testdata/config.yml | 2 +- .../state_statefulset_test.go | 15 +++--- .../_meta/testdata/config.yml | 2 +- .../state_storageclass_test.go | 15 +++--- 38 files changed, 191 insertions(+), 145 deletions(-) rename metricbeat/module/kubernetes/_meta/test/{ => KSM}/ksm.v2.10.0.plain (100%) rename metricbeat/module/kubernetes/_meta/test/{ => KSM}/ksm.v2.8.2.plain (100%) rename metricbeat/module/kubernetes/_meta/test/{ => KSM}/ksm.v2.9.2.plain (100%) diff --git a/metricbeat/helper/kubernetes/ktest/ktest.go b/metricbeat/helper/kubernetes/ktest/ktest.go index a8de05cbd05..2cf1add744e 100644 --- a/metricbeat/helper/kubernetes/ktest/ktest.go +++ b/metricbeat/helper/kubernetes/ktest/ktest.go @@ -18,7 +18,8 @@ package ktest import ( - "io/ioutil" + "os" + "path/filepath" "strings" "testing" @@ -26,38 +27,53 @@ import ( "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -// GetTestCases Build test cases from the files and returns them -func GetTestCases(files []string) ptest.TestCases { +func getFiles(folder string) ([]string, error) { + var files []string + entries, err := os.ReadDir(folder) + if err != nil { + return nil, err + } + for _, e := range entries { + files = append(files, filepath.Join(folder, e.Name())) + } + return files, nil +} + +// GetTestCases Build test cases based on the files from folder, and the expected files in the expectedFolder +func GetTestCases(folder string, expectedFolder string) (ptest.TestCases, error) { var cases ptest.TestCases - for i := 0; i < len(files); i++ { + + files, err := getFiles(folder) + if err != nil { + return nil, err + } + + for _, file := range files { cases = append(cases, struct { MetricsFile string ExpectedFile string }{ - // the metrics file is inside the parent directory, while the expected file is in the current directory - // Example: - // Metricsfile: ../_meta/test/ksm.v2.4.2 - // ExpectedFile: ./_meta/test/ksm.v2.4.2.expected - MetricsFile: files[i], - ExpectedFile: files[i][1:] + ".expected", + MetricsFile: file, + // the expected file will have the path /filename.expected + ExpectedFile: filepath.Join(expectedFolder, filepath.Base(file)+".expected"), }, ) } - return cases + return cases, nil } -// TestMetricsFamily +// TestMetricsFamilyFromFiles // 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 TestMetricsFamily(t *testing.T, files []string, mapping *p.MetricsMapping) { +func TestMetricsFamilyFromFiles(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]) + content, err := os.ReadFile(files[i]) if err != nil { t.Fatalf("Unknown file %s.", files[i]) } @@ -79,3 +95,13 @@ func TestMetricsFamily(t *testing.T, files []string, mapping *p.MetricsMapping) } } + +// TestMetricsFamilyFromFolder is the same as TestMetricsFamilyFromFiles, but for folder +func TestMetricsFamilyFromFolder(t *testing.T, folder string, mapping *p.MetricsMapping) { + files, err := getFiles(folder) + if err != nil { + t.Fatalf(err.Error()) + } + + TestMetricsFamilyFromFiles(t, files, mapping) +} diff --git a/metricbeat/module/kubernetes/_meta/test/ksm.v2.10.0.plain b/metricbeat/module/kubernetes/_meta/test/KSM/ksm.v2.10.0.plain similarity index 100% rename from metricbeat/module/kubernetes/_meta/test/ksm.v2.10.0.plain rename to metricbeat/module/kubernetes/_meta/test/KSM/ksm.v2.10.0.plain diff --git a/metricbeat/module/kubernetes/_meta/test/ksm.v2.8.2.plain b/metricbeat/module/kubernetes/_meta/test/KSM/ksm.v2.8.2.plain similarity index 100% rename from metricbeat/module/kubernetes/_meta/test/ksm.v2.8.2.plain rename to metricbeat/module/kubernetes/_meta/test/KSM/ksm.v2.8.2.plain diff --git a/metricbeat/module/kubernetes/_meta/test/ksm.v2.9.2.plain b/metricbeat/module/kubernetes/_meta/test/KSM/ksm.v2.9.2.plain similarity index 100% rename from metricbeat/module/kubernetes/_meta/test/ksm.v2.9.2.plain rename to metricbeat/module/kubernetes/_meta/test/KSM/ksm.v2.9.2.plain diff --git a/metricbeat/module/kubernetes/_meta/test/README.md b/metricbeat/module/kubernetes/_meta/test/README.md index f2f4ed01345..1fdb9f8088a 100644 --- a/metricbeat/module/kubernetes/_meta/test/README.md +++ b/metricbeat/module/kubernetes/_meta/test/README.md @@ -1,18 +1,24 @@ # Kube State Metrics metrics files -Each KSM metrics files has the **name format `ksm.v.plain`**. **The `` should be compatible with the Kubernetes versions we support**. Check the compatibility in the official repository [here](https://github.com/kubernetes/kube-state-metrics#compatibility-matrix). +Each KSM metrics file used for the tests of each `state_*` metricset can be found inside `KSM` directory. Each `state_*` metricset directory will have corresponding `_meta/test*` subfolder with expected files. -**These files are being used in the metricsets that fetch these metrics**: all the `state_*` ones. As of the time of this commit (21.feb.2023), there are two folders that are in use for the `test` file present in each metricset: `test` and `testdata`. Both **these folders require these KSM metrics files** to generate the expected ones. You can check the test file by running `go test -data` to generate the expected files, or simply `go test .` to check against the already present generated files. +Each file has the name format `ksm.v.plain`. The `` should be compatible with the Kubernetes versions we support. Check the compatibility in the official repository [here](https://github.com/kubernetes/kube-state-metrics#compatibility-matrix). -> **_NOTE:_** The expected files inside these two folders are not deleted when running the tests. Remember to delete them if they are from an old version. - -**The name of these files needs to end with the suffix `.plain`**. The reason for this is that the `config.yml` file inside each `state_*` metricset `testdata` requires the suffix `.plain` for the metrics files: +It's mandatory for the name of these files to end with the suffix `.plain`. The reason for this is that the `config.yml` file inside each `state_*` metricset `testdata` requires the suffix `.plain` for the metrics files: ```yaml type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" ``` -Since all metricsets use a `data.json` file for the documentation - this file contains an example of the metrics fields -, **it is necessary that one of these files has the name `docs.plain`**. This file should have the **same content as one of the metrics file of a KSM version** we support. This means that one of the files is duplicated, for example: `ksm.v2.7.0.plain` has the same content as `docs.plain`. This is not a mistake, as having the `ksm.v2.7.0.plain` tells us the metrics of that specific version. + +When you update the KSM directory files, remember to run `go test -data` inside each `state_*` metricset directory to generate the expected files. To check against the expected files already present, you can just run `go test .`. + +> **TIP**: To run tests and generate the expected files for all state metricsets you can run `go test ./state... --data`. Navigate to `/elastic/beats/metricbeat/module/kubernetes/` to run this command. + +> **_NOTE:_** The expected files inside the two folders of each `state_*` mericset (`_meta/test` and `_meta/testdata`) are not deleted when running the tests. Remember to delete them if they are from an old version. + + +Since all metricsets use a `data.json` file for the documentation - this file contains an example of the metrics fields -, it is necessary that one of these files has the name `docs.plain`. This file needs to be inside `kubernetes/_meta/test` directory. This file should have the same content as one of the metrics file of a KSM version we support. diff --git a/metricbeat/module/kubernetes/controllermanager/controllermanager_test.go b/metricbeat/module/kubernetes/controllermanager/controllermanager_test.go index c33ea3cda44..b3c9f47d8d5 100644 --- a/metricbeat/module/kubernetes/controllermanager/controllermanager_test.go +++ b/metricbeat/module/kubernetes/controllermanager/controllermanager_test.go @@ -50,5 +50,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFiles(t, files, mapping) } diff --git a/metricbeat/module/kubernetes/proxy/proxy_test.go b/metricbeat/module/kubernetes/proxy/proxy_test.go index 05c06c0539c..cab5a6fe4ed 100644 --- a/metricbeat/module/kubernetes/proxy/proxy_test.go +++ b/metricbeat/module/kubernetes/proxy/proxy_test.go @@ -51,5 +51,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFiles(t, files, mapping) } diff --git a/metricbeat/module/kubernetes/scheduler/scheduler_test.go b/metricbeat/module/kubernetes/scheduler/scheduler_test.go index 0e5e7589037..da60e1b3d1f 100644 --- a/metricbeat/module/kubernetes/scheduler/scheduler_test.go +++ b/metricbeat/module/kubernetes/scheduler/scheduler_test.go @@ -51,5 +51,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFiles(t, files, mapping) } diff --git a/metricbeat/module/kubernetes/state_container/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_container/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_container/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_container/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_container/state_container_test.go b/metricbeat/module/kubernetes/state_container/state_container_test.go index ead1d8f98ab..40876a85c81 100644 --- a/metricbeat/module/kubernetes/state_container/state_container_test.go +++ b/metricbeat/module/kubernetes/state_container/state_container_test.go @@ -22,21 +22,22 @@ package state_container import ( "testing" + "github.com/stretchr/testify/require" + 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" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_container" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -44,5 +45,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_cronjob/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_cronjob/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_cronjob/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_cronjob/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_test.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_test.go index 37b63c98f00..55ba461ff59 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_test.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_test.go @@ -22,22 +22,23 @@ package state_cronjob import ( "testing" + "github.com/stretchr/testify/require" + mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_cronjob" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -45,5 +46,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_daemonset/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_daemonset/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_daemonset/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_daemonset/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_daemonset/state_daemonset_test.go b/metricbeat/module/kubernetes/state_daemonset/state_daemonset_test.go index e096a46b96b..546e4559cdb 100644 --- a/metricbeat/module/kubernetes/state_daemonset/state_daemonset_test.go +++ b/metricbeat/module/kubernetes/state_daemonset/state_daemonset_test.go @@ -22,6 +22,8 @@ package state_daemonset import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -30,16 +32,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_daemonset" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -47,5 +48,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_deployment/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_deployment/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_deployment/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_deployment/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_deployment/state_deployment_test.go b/metricbeat/module/kubernetes/state_deployment/state_deployment_test.go index 11fd42c1ab0..499c3a64915 100644 --- a/metricbeat/module/kubernetes/state_deployment/state_deployment_test.go +++ b/metricbeat/module/kubernetes/state_deployment/state_deployment_test.go @@ -22,6 +22,8 @@ package state_deployment import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -29,16 +31,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_deployment" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -46,5 +47,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_job/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_job/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_job/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_job/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_job/state_job_test.go b/metricbeat/module/kubernetes/state_job/state_job_test.go index bff86841ce1..8aa33a3cfd4 100644 --- a/metricbeat/module/kubernetes/state_job/state_job_test.go +++ b/metricbeat/module/kubernetes/state_job/state_job_test.go @@ -22,6 +22,8 @@ package state_job import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -29,16 +31,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_job" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -46,5 +47,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_namespace/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_namespace/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_namespace/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_namespace/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_namespace/state_namespace_test.go b/metricbeat/module/kubernetes/state_namespace/state_namespace_test.go index dff8eaefe95..902950c6d91 100644 --- a/metricbeat/module/kubernetes/state_namespace/state_namespace_test.go +++ b/metricbeat/module/kubernetes/state_namespace/state_namespace_test.go @@ -22,6 +22,8 @@ package state_namespace import ( "testing" + "github.com/stretchr/testify/require" + 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" @@ -29,16 +31,15 @@ import ( "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = util.NamespaceResource func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -46,5 +47,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_node/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_node/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_node/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_node/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_node/state_node_test.go b/metricbeat/module/kubernetes/state_node/state_node_test.go index da61a879830..5d95614c8ec 100644 --- a/metricbeat/module/kubernetes/state_node/state_node_test.go +++ b/metricbeat/module/kubernetes/state_node/state_node_test.go @@ -22,6 +22,8 @@ package state_node import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -30,16 +32,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_node" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -47,5 +48,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_persistentvolume/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_persistentvolume/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_persistentvolume/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_persistentvolume/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_persistentvolume/state_persistentvolume_test.go b/metricbeat/module/kubernetes/state_persistentvolume/state_persistentvolume_test.go index 01ac39455f7..80a6ce5f8f6 100644 --- a/metricbeat/module/kubernetes/state_persistentvolume/state_persistentvolume_test.go +++ b/metricbeat/module/kubernetes/state_persistentvolume/state_persistentvolume_test.go @@ -22,22 +22,23 @@ package state_persistentvolume import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_persistentvolume" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -45,5 +46,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_persistentvolumeclaim/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_persistentvolumeclaim/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_persistentvolumeclaim/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_persistentvolumeclaim/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_persistentvolumeclaim/state_persistentvolumeclaim_test.go b/metricbeat/module/kubernetes/state_persistentvolumeclaim/state_persistentvolumeclaim_test.go index 6111db302ec..a4d3d5fbe48 100644 --- a/metricbeat/module/kubernetes/state_persistentvolumeclaim/state_persistentvolumeclaim_test.go +++ b/metricbeat/module/kubernetes/state_persistentvolumeclaim/state_persistentvolumeclaim_test.go @@ -22,22 +22,23 @@ package state_persistentvolumeclaim import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_persistentvolumeclaim" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -45,5 +46,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_pod/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_pod/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_pod/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_pod/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_pod/state_pod_test.go b/metricbeat/module/kubernetes/state_pod/state_pod_test.go index 0c9e5e9eeda..51e30ec9903 100644 --- a/metricbeat/module/kubernetes/state_pod/state_pod_test.go +++ b/metricbeat/module/kubernetes/state_pod/state_pod_test.go @@ -22,6 +22,8 @@ package state_pod import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -30,16 +32,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_pod" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -47,5 +48,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_replicaset/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_replicaset/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_replicaset/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_replicaset/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_replicaset/state_replicaset_test.go b/metricbeat/module/kubernetes/state_replicaset/state_replicaset_test.go index 626db74d92a..db9de50ed09 100644 --- a/metricbeat/module/kubernetes/state_replicaset/state_replicaset_test.go +++ b/metricbeat/module/kubernetes/state_replicaset/state_replicaset_test.go @@ -22,6 +22,8 @@ package state_replicaset import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -30,16 +32,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_replicaset" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -47,5 +48,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_resourcequota/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_resourcequota/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_resourcequota/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_resourcequota/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_resourcequota/state_resourcequota_test.go b/metricbeat/module/kubernetes/state_resourcequota/state_resourcequota_test.go index 040b98c13df..102aa590ae0 100644 --- a/metricbeat/module/kubernetes/state_resourcequota/state_resourcequota_test.go +++ b/metricbeat/module/kubernetes/state_resourcequota/state_resourcequota_test.go @@ -22,22 +22,23 @@ package state_resourcequota import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_resourcequota" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -45,5 +46,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_service/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_service/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_service/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_service/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_service/state_service_test.go b/metricbeat/module/kubernetes/state_service/state_service_test.go index 8c634596968..96e7e052254 100644 --- a/metricbeat/module/kubernetes/state_service/state_service_test.go +++ b/metricbeat/module/kubernetes/state_service/state_service_test.go @@ -22,23 +22,23 @@ package state_service import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_service" func TestEventMapping(t *testing.T) { - - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -46,5 +46,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_statefulset/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_statefulset/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_statefulset/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_statefulset/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_statefulset/state_statefulset_test.go b/metricbeat/module/kubernetes/state_statefulset/state_statefulset_test.go index 560c2b754c2..20c7c37ebd0 100644 --- a/metricbeat/module/kubernetes/state_statefulset/state_statefulset_test.go +++ b/metricbeat/module/kubernetes/state_statefulset/state_statefulset_test.go @@ -22,6 +22,8 @@ package state_statefulset import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" @@ -31,16 +33,15 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/kubernetes" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_statefulset" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -48,5 +49,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) } diff --git a/metricbeat/module/kubernetes/state_storageclass/_meta/testdata/config.yml b/metricbeat/module/kubernetes/state_storageclass/_meta/testdata/config.yml index a44a8c52469..c56430984eb 100644 --- a/metricbeat/module/kubernetes/state_storageclass/_meta/testdata/config.yml +++ b/metricbeat/module/kubernetes/state_storageclass/_meta/testdata/config.yml @@ -1,4 +1,4 @@ type: http url: "/metrics" suffix: plain -path: "../_meta/test" +path: "../_meta/test/KSM" diff --git a/metricbeat/module/kubernetes/state_storageclass/state_storageclass_test.go b/metricbeat/module/kubernetes/state_storageclass/state_storageclass_test.go index 572d4f6a379..7e9e0d80780 100644 --- a/metricbeat/module/kubernetes/state_storageclass/state_storageclass_test.go +++ b/metricbeat/module/kubernetes/state_storageclass/state_storageclass_test.go @@ -22,22 +22,23 @@ package state_storageclass import ( "testing" + "github.com/stretchr/testify/require" + k "github.com/elastic/beats/v7/metricbeat/helper/kubernetes/ktest" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/helper/prometheus/ptest" ) -var files = []string{ - "../_meta/test/ksm.v2.8.2.plain", - "../_meta/test/ksm.v2.9.2.plain", - "../_meta/test/ksm.v2.10.0.plain", -} +var filesFolder = "../_meta/test/KSM" +var expectedFolder = "./_meta/test" const name = "state_storageclass" func TestEventMapping(t *testing.T) { - ptest.TestMetricSet(t, "kubernetes", name, k.GetTestCases(files)) + testCases, err := k.GetTestCases(filesFolder, expectedFolder) + require.Equal(t, err, nil) + ptest.TestMetricSet(t, "kubernetes", name, testCases) } func TestData(t *testing.T) { @@ -45,5 +46,5 @@ func TestData(t *testing.T) { } func TestMetricsFamily(t *testing.T) { - k.TestMetricsFamily(t, files, mapping) + k.TestMetricsFamilyFromFolder(t, filesFolder, mapping) }