From 2b0cccf584a342548aa4449e7c9b7efc94516fcc Mon Sep 17 00:00:00 2001 From: harshit-splunk Date: Wed, 2 Feb 2022 16:03:42 +0530 Subject: [PATCH] added test for multi-container pod --- test/README.md | 6 ++-- .../metric_data/selector.conf | 20 ----------- .../k8s_metrics_tests/metric_data/selector.md | 19 ++++++++++ .../test_metric_aggr_plugin.py | 36 +++++++++++++++++++ test/test_setup.yaml | 18 +++++++++- 5 files changed, 76 insertions(+), 23 deletions(-) delete mode 100644 test/k8s_metrics_tests/metric_data/selector.conf create mode 100644 test/k8s_metrics_tests/metric_data/selector.md diff --git a/test/README.md b/test/README.md index 8db50402..2ce1296b 100644 --- a/test/README.md +++ b/test/README.md @@ -26,5 +26,7 @@ # How metric tests works Test collects test data from metric_data, each data contains metric name, field selector for metric and list of asserions. -For example, for `kube.node.uptime` test, selector value is `node`. So test will search `| mstats max(kube.node.uptime) where index=ci_metric by node`. It will collect uptime value for each node and test will assert each values to be greater than zero. -All selector fields are listed in [selector.conf](https://github.com/splunk/splunk-connect-for-kubernetes/blob/develop/test/k8s_metrics_tests/metric_data/selector.conf) file. \ No newline at end of file + +Generic search query used in metric tests: `| mstats max() where index= by ` + +All selector fields are listed in [selector.md](https://github.com/splunk/splunk-connect-for-kubernetes/blob/develop/test/k8s_metrics_tests/metric_data/selector.md) \ No newline at end of file diff --git a/test/k8s_metrics_tests/metric_data/selector.conf b/test/k8s_metrics_tests/metric_data/selector.conf deleted file mode 100644 index 0d098efd..00000000 --- a/test/k8s_metrics_tests/metric_data/selector.conf +++ /dev/null @@ -1,20 +0,0 @@ -# This file is for documentation purposes only. -# It contains which selector field used in each test -[stats] -node=node - -[summary] -node=node -container=container-name -pod=pod-name - -[cAdvisor] -container=container_name -pod=pod_name - -[aggregated] -container=name -pod=name -namespace=names -cluster=cluster_name -node=node \ No newline at end of file diff --git a/test/k8s_metrics_tests/metric_data/selector.md b/test/k8s_metrics_tests/metric_data/selector.md new file mode 100644 index 00000000..a8e59190 --- /dev/null +++ b/test/k8s_metrics_tests/metric_data/selector.md @@ -0,0 +1,19 @@ +This file is for documentation purposes only. It contains which selector field used in each test + + + +| Metric type | selector for stats | selector for summary | selector for cAdvisor | selector for aggregated | +| ----------- | ------------------ | -------------------- | --------------------- | ----------------------- | +| container | - | container-name | container_name | name | +| pod | - | pod-name | pod_name | name | +| node | node | node | - | node | +| namespace | - | - | - | name | +| cluster | - | - | - | name | + +Selector field suggest unique identifier field for given metric. +Here, `"-"` means the given type of metric doesn't exist in metric source(i.e stats/summary/cAdvisor) + +For example, `kube.container.uptime` summary metric suggest container uptime. Each containers will have different uptime. So, we can group all `kube.container.uptime` by container's name. Summary scrapper will set the container's name as `container-name`. For this metric, since it is container metric and it's coming from summary api, the selector field will be `container-name`. + +Test will search following query in splunk for example test: `| mstats max(kube.container.cpu.uptime) where index= by container-name` + diff --git a/test/k8s_metrics_tests/test_metric_aggr_plugin.py b/test/k8s_metrics_tests/test_metric_aggr_plugin.py index c64bd6af..5a501c72 100644 --- a/test/k8s_metrics_tests/test_metric_aggr_plugin.py +++ b/test/k8s_metrics_tests/test_metric_aggr_plugin.py @@ -185,3 +185,39 @@ def test_namespace_limit_and_requests(setup, index_metrics): + actual_data["splunk-fluentd-k8s-objects"] + actual_data["splunk-fluentd-k8s-metrics-agg"] ) + +def test_multi_container_pod_limit_and_request(setup, index_metrics): + selector = "name" + for resource in ["cpu", "memory"]: + for metric in ["limit", "request"]: + logger.info( + "testing {} metric".format(f"kube.container.{resource}.{metric} for multi-container pods") + ) + container_events = collect_metric_from_splunk( + f"kube.container.{resource}.{metric}", + index_metrics, + selector, + url=setup["splunkd_url"], + user=setup["splunk_user"], + password=setup["splunk_password"], + func="avg", + ) + if not "pod-wo-index-wo-ns-index-dup" in container_events: + pytest.fail("pod 'pod-wo-index-wo-ns-index-dup' not found" ) + + assert container_events["pod-wo-index-wo-ns-index"] == 50 + assert container_events["pod-wo-index-wo-ns-index-dup"] == 50 + + pod_events = collect_metric_from_splunk( + f"kube.pod.{resource}.{metric}", + index_metrics, + selector, + url=setup["splunkd_url"], + user=setup["splunk_user"], + password=setup["splunk_password"], + func="avg", + ) + + for pod_name, metric_value in pod_events.items(): + if "pod-wo-index-wo-ns-index" in pod_name: + assert metric_value == 100, f"{resource}.{metric} of pod '{pod_name}' should be 100" diff --git a/test/test_setup.yaml b/test/test_setup.yaml index dcfd1663..1580eba1 100644 --- a/test/test_setup.yaml +++ b/test/test_setup.yaml @@ -145,4 +145,20 @@ spec: spec: containers: - name: pod-wo-index-wo-ns-index - image: rock1017/log-generator:2.2.6 \ No newline at end of file + image: rock1017/log-generator:2.2.6 + resources: + limits: + cpu: 50m + memory: 50Mi + requests: + cpu: 50m + memory: 50Mi + - name: pod-wo-index-wo-ns-index-dup + image: rock1017/log-generator:2.2.6 + resources: + limits: + cpu: 50m + memory: 50Mi + requests: + cpu: 50m + memory: 50Mi \ No newline at end of file