From 51cf27a38e6215f0c586aa4e091c9a3749f05246 Mon Sep 17 00:00:00 2001 From: Nacho Barrientos <nacho.barrientos@cern.ch> Date: Mon, 3 Mar 2025 15:24:19 +0100 Subject: [PATCH] [MONIT-4147] Add some unit tests --- .gitlab-ci.yml | 6 ++ .helmignore | 3 +- tests/fluentbit-logs/configmap.yaml | 51 ++++++++++++++++ tests/fluentbit-logs/daemonset.yaml | 38 ++++++++++++ tests/fluentbit-metrics/configmap.yaml | 35 +++++++++++ tests/fluentbit-metrics/statefulset.yaml | 29 +++++++++ tests/prometheus/prometheus.yaml | 75 ++++++++++++++++++++++++ tests/prometheus/remotewritesecret.yaml | 36 ++++++++++++ 8 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 tests/fluentbit-logs/configmap.yaml create mode 100644 tests/fluentbit-logs/daemonset.yaml create mode 100644 tests/fluentbit-metrics/configmap.yaml create mode 100644 tests/fluentbit-metrics/statefulset.yaml create mode 100644 tests/prometheus/prometheus.yaml create mode 100644 tests/prometheus/remotewritesecret.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5d1c07a..6ebd77d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -42,6 +42,12 @@ helm_lint: - helm dep update . - helm lint --strict . +unittest: + stage: test + image: registry.cern.ch/docker.io/helmunittest/helm-unittest:3.17.0-0.7.2 + script: + - helm unittest -f 'tests/**/*.yaml' . + version_test: stage: test rules: diff --git a/.helmignore b/.helmignore index df65d10..f72da33 100644 --- a/.helmignore +++ b/.helmignore @@ -27,4 +27,5 @@ README.md .idea/ *.tmproj .vscode/ -config \ No newline at end of file +config +tests/ \ No newline at end of file diff --git a/tests/fluentbit-logs/configmap.yaml b/tests/fluentbit-logs/configmap.yaml new file mode 100644 index 0000000..1627cb7 --- /dev/null +++ b/tests/fluentbit-logs/configmap.yaml @@ -0,0 +1,51 @@ +suite: test fluentbit-logs configmap +templates: + - fluentbit-logs/configmap.yaml +tests: + - it: should deploy nothing by default + asserts: + - containsDocument: + kind: ConfigMap + apiVersion: "apps/v1" + name: it-monit-logs-collector-fluentbit + not: true + - it: should fail to deploy if enabled due to missing cluster name + set: + logs.enabled: true + logs.fluentbit.enabled: true + asserts: + - failedTemplate: + errorMessage: "kubernetes.clusterName is missing" + - it: should fail to deploy if enabled due to missing tenant details + set: + logs.enabled: true + logs.fluentbit.enabled: true + kubernetes.clusterName: test + asserts: + - failedTemplate: + errorMessage: "Tenant name is required" + - it: should fail to deploy if enabled when tenant name available but no tenant password + set: + logs.enabled: true + logs.fluentbit.enabled: true + tenant.name: test + kubernetes.clusterName: test + asserts: + - failedTemplate: + errorMessage: "Tenant password is required" + - it: should deploy if logs processing is enabled and required values are fed + set: + tenant.name: test + tenant.password: test + kubernetes.clusterName: test + logs.enabled: true + logs.fluentbit.enabled: true + asserts: + - containsDocument: + kind: ConfigMap + apiVersion: v1 + name: it-monit-logs-collector-fluentbit + - exists: + path: data["custom_parsers.conf"] + - exists: + path: data["fluent-bit.conf"] diff --git a/tests/fluentbit-logs/daemonset.yaml b/tests/fluentbit-logs/daemonset.yaml new file mode 100644 index 0000000..4669c91 --- /dev/null +++ b/tests/fluentbit-logs/daemonset.yaml @@ -0,0 +1,38 @@ +suite: test fluentbit-logs daemonset +templates: + - fluentbit-logs/daemonset.yaml +tests: + - it: should not be deployed by default + asserts: + - containsDocument: + kind: DaemonSet + apiVersion: "apps/v1" + name: it-monit-logs-collector-fluentbit + not: true + - it: should be deployed if logs.enabled and logs.fluentbit.enabled is true + set: + logs.enabled: true + logs.fluentbit.enabled: true + asserts: + - containsDocument: + kind: DaemonSet + apiVersion: "apps/v1" + name: it-monit-logs-collector-fluentbit + - it: should not be deployed if logs.enabled is false + set: + logs.enabled: false + asserts: + - containsDocument: + kind: DaemonSet + apiVersion: "apps/v1" + not: true + - it: should not be deployed if logs.enabled is true and logs.fluentbit.enabled is false + set: + logs.enabled: true + logs.fluentbit.enabled: false + asserts: + - containsDocument: + kind: DaemonSet + apiVersion: "apps/v1" + name: it-monit-logs-collector-fluentbit + not: true diff --git a/tests/fluentbit-metrics/configmap.yaml b/tests/fluentbit-metrics/configmap.yaml new file mode 100644 index 0000000..82e9a84 --- /dev/null +++ b/tests/fluentbit-metrics/configmap.yaml @@ -0,0 +1,35 @@ +suite: test fluentbit-metrics configmap +templates: + - fluentbit-metrics/configmap.yaml +tests: + - it: should fail to deploy by default due to missing tenant details + asserts: + - failedTemplate: + errorMessage: "Tenant name is required" + - it: should fail to deploy if tenant name available but no tenant password + set: + tenant.name: test + asserts: + - failedTemplate: + errorMessage: "Tenant password is required" + - it: should deploy by default when required values are fed + set: + tenant.name: test + tenant.password: test + asserts: + - containsDocument: + kind: ConfigMap + apiVersion: v1 + - it: should deploy if metrics processing is enabled and required values are fed + set: + tenant.name: test + tenant.password: test + metrics.enabled: true + metrics.fluentbit.enabled: true + asserts: + - containsDocument: + kind: ConfigMap + apiVersion: v1 + name: it-monit-metrics-collector-fluentbit + - exists: + path: data["fluent-bit.yaml"] diff --git a/tests/fluentbit-metrics/statefulset.yaml b/tests/fluentbit-metrics/statefulset.yaml new file mode 100644 index 0000000..56f19e0 --- /dev/null +++ b/tests/fluentbit-metrics/statefulset.yaml @@ -0,0 +1,29 @@ +suite: test fluentbit-metrics statefulset +templates: + - fluentbit-metrics/statefulset.yaml +tests: + - it: should be deployed by default + asserts: + - containsDocument: + kind: StatefulSet + apiVersion: "apps/v1" + name: it-monit-metrics-collector-fluentbit + - it: should not be deployed if metrics.enabled is false + set: + metrics.enabled: false + asserts: + - containsDocument: + kind: StatefulSet + apiVersion: "apps/v1" + name: it-monit-metrics-collector-fluentbit + not: true + - it: should not be deployed if metrics.enabled is true and metrics.fluentbit.enabled is false + set: + metrics.enabled: true + metrics.fluentbit.enabled: false + asserts: + - containsDocument: + kind: StatefulSet + apiVersion: "apps/v1" + name: it-monit-metrics-collector-fluentbit + not: true diff --git a/tests/prometheus/prometheus.yaml b/tests/prometheus/prometheus.yaml new file mode 100644 index 0000000..a019b61 --- /dev/null +++ b/tests/prometheus/prometheus.yaml @@ -0,0 +1,75 @@ +suite: test prometheus prometheus +templates: + - prometheus/prometheus.yaml +tests: + - it: should not be deployed if metrics are disabled + set: + metrics.enabled: false + asserts: + - containsDocument: + kind: Prometheus + apiVersion: "monitoring.coreos.com/v1" + name: it-monit-metrics-collector-prometheus + not: true + - it: should not be deployed if Prometheus is disabled + set: + metrics.enabled: true + metrics.prometheus.enabled: false + asserts: + - containsDocument: + kind: Prometheus + apiVersion: "monitoring.coreos.com/v1" + name: it-monit-metrics-collector-prometheus + not: true + - it: should be deployed with cluster-local remote write by default + set: + kubernetes.clusterName: test + asserts: + - containsDocument: + kind: Prometheus + apiVersion: "monitoring.coreos.com/v1" + name: it-monit-metrics-collector-prometheus + - lengthEqual: + path: spec.remoteWrite + count: 1 + - equal: + path: spec.remoteWrite[0].url + value: "http://it-monit-metrics-fluentbit:8080/api/prom/push" + - it: should be deployed with no remoteWrites if no fluentbit is available + set: + kubernetes.clusterName: test + metrics.fluentbit.enabled: false + asserts: + - containsDocument: + kind: Prometheus + apiVersion: "monitoring.coreos.com/v1" + name: it-monit-metrics-collector-prometheus + - lengthEqual: + path: spec.remoteWrite + count: 0 + - it: should be deployed with external remote write if configured + set: + kubernetes.clusterName: test + metrics.fluentbit.enabled: false + metrics.prometheus.server.remoteWrite.endpoint: "http://foo:123" + asserts: + - containsDocument: + kind: Prometheus + apiVersion: "monitoring.coreos.com/v1" + name: it-monit-metrics-collector-prometheus + - contains: + path: spec.remoteWrite + content: + url: "http://foo:123" + tlsConfig: + insecureSkipVerify: true + basicAuth: + username: + name: it-monit-metrics-collector-prometheus + key: username + password: + name: it-monit-metrics-collector-prometheus + key: password + - lengthEqual: + path: spec.remoteWrite + count: 1 diff --git a/tests/prometheus/remotewritesecret.yaml b/tests/prometheus/remotewritesecret.yaml new file mode 100644 index 0000000..1db96d7 --- /dev/null +++ b/tests/prometheus/remotewritesecret.yaml @@ -0,0 +1,36 @@ +suite: test prometheus remotewritesecret +templates: + - prometheus/remotewritesecret.yaml +tests: + - it: should be deployed with remote write specific creds if they're provided + set: + kubernetes.clusterName: test + tenant.name: test + tenant.password: tset + metrics.prometheus.server.remoteWrite.endpoint: "http://foo:123" + metrics.prometheus.server.remoteWrite.username: higgs + metrics.prometheus.server.remoteWrite.password: boson + asserts: + - containsDocument: + kind: Secret + apiVersion: "v1" + - equal: + path: data + value: + username: "aGlnZ3M=" # higgs + password: "Ym9zb24=" # boson + - it: should be deployed with tenant creds as user/password if provided + set: + kubernetes.clusterName: test + tenant.name: test + tenant.password: tset + metrics.prometheus.server.remoteWrite.endpoint: "http://foo:123" + asserts: + - containsDocument: + kind: Secret + apiVersion: "v1" + - equal: + path: data + value: + username: "dGVzdA==" # test + password: "dHNldA==" # tset -- GitLab