X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fdevops.git;a=blobdiff_plain;f=installers%2Fcharm%2Fprometheus%2Ftests%2Ftest_charm.py;h=b1848bdd98245b1d5797ce84636f3b067580dccd;hp=87d7bc5c2a0e60c331da12fa9190c914372f3a3f;hb=49379ced23b5e344a773ce77ac9cb59c1864e19b;hpb=e5fd208248a496cd798a66cb27216e83f2852a76 diff --git a/installers/charm/prometheus/tests/test_charm.py b/installers/charm/prometheus/tests/test_charm.py index 87d7bc5c..b1848bdd 100644 --- a/installers/charm/prometheus/tests/test_charm.py +++ b/installers/charm/prometheus/tests/test_charm.py @@ -20,10 +20,10 @@ # osm-charmers@lists.launchpad.net ## +import sys from typing import NoReturn import unittest - -from ops.model import BlockedStatus +from ops.model import ActiveStatus from ops.testing import Harness from charm import PrometheusCharm @@ -34,445 +34,63 @@ class TestCharm(unittest.TestCase): def setUp(self) -> NoReturn: """Test setup""" + self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() self.harness = Harness(PrometheusCharm) self.harness.set_leader(is_leader=True) self.harness.begin() - - def test_ingress_resources_without_http(self) -> NoReturn: - """Test ingress resources without HTTP.""" - expected_result = { - "version": 3, - "containers": [ - { - "name": "prometheus", - "imageDetails": self.harness.charm.image.fetch(), - "imagePullPolicy": "Always", - "ports": [ - { - "name": "prometheus", - "containerPort": 9090, - "protocol": "TCP", - } - ], - "envConfig": {}, - "volumeConfig": [ - { - "name": "config", - "mountPath": "/etc/prometheus", - "files": [ - { - "path": "prometheus.yml", - "content": ( - "global:\n" - " scrape_interval: 15s\n" - " evaluation_interval: 15s\n" - "alerting:\n" - " alertmanagers:\n" - " - static_configs:\n" - " - targets:\n" - "rule_files:\n" - "scrape_configs:\n" - " - job_name: 'prometheus'\n" - " static_configs:\n" - " - targets: [{}]\n".format("") - ), - } - ], - } - ], - "command": [ - "/bin/prometheus", - "--config.file=/etc/prometheus/prometheus.yml", - "--storage.tsdb.path=/prometheus", - "--web.console.libraries=/usr/share/prometheus/console_libraries", - "--web.console.templates=/usr/share/prometheus/consoles", - "--web.route-prefix={}".format("/"), - "--web.external-url=http://localhost:{}{}".format(9090, "/"), - ], - "kubernetes": { - "readinessProbe": { - "httpGet": { - "path": "/-/ready", - "port": 9090, - }, - "initialDelaySeconds": 10, - "timeoutSeconds": 30, - }, - "livenessProbe": { - "httpGet": { - "path": "/-/healthy", - "port": 9090, - }, - "initialDelaySeconds": 30, - "periodSeconds": 30, - }, - }, - } - ], - "kubernetesResources": {"ingressResources": []}, - } - - self.harness.charm.on.start.emit() - - # Verifying status - self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) - - pod_spec, _ = self.harness.get_pod_spec() - - self.assertDictEqual(expected_result, pod_spec) - - def test_ingress_resources_with_http(self) -> NoReturn: - """Test ingress resources with HTTP.""" - expected_result = { - "version": 3, - "containers": [ - { - "name": "prometheus", - "imageDetails": self.harness.charm.image.fetch(), - "imagePullPolicy": "Always", - "ports": [ - { - "name": "prometheus", - "containerPort": 9090, - "protocol": "TCP", - } - ], - "envConfig": {}, - "volumeConfig": [ - { - "name": "config", - "mountPath": "/etc/prometheus", - "files": [ - { - "path": "prometheus.yml", - "content": ( - "global:\n" - " scrape_interval: 15s\n" - " evaluation_interval: 15s\n" - "alerting:\n" - " alertmanagers:\n" - " - static_configs:\n" - " - targets:\n" - "rule_files:\n" - "scrape_configs:\n" - " - job_name: 'prometheus'\n" - " static_configs:\n" - " - targets: [{}]\n".format("") - ), - } - ], - } - ], - "command": [ - "/bin/prometheus", - "--config.file=/etc/prometheus/prometheus.yml", - "--storage.tsdb.path=/prometheus", - "--web.console.libraries=/usr/share/prometheus/console_libraries", - "--web.console.templates=/usr/share/prometheus/consoles", - "--web.route-prefix={}".format("/"), - "--web.external-url=http://localhost:{}{}".format(9090, "/"), - ], - "kubernetes": { - "readinessProbe": { - "httpGet": { - "path": "/-/ready", - "port": 9090, - }, - "initialDelaySeconds": 10, - "timeoutSeconds": 30, - }, - "livenessProbe": { - "httpGet": { - "path": "/-/healthy", - "port": 9090, - }, - "initialDelaySeconds": 30, - "periodSeconds": 30, - }, - }, - } - ], - "kubernetesResources": { - "ingressResources": [ - { - "name": "prometheus-ingress", - "annotations": { - "nginx.ingress.kubernetes.io/proxy-body-size": "0", - "nginx.ingress.kubernetes.io/ssl-redirect": "false", - }, - "spec": { - "rules": [ - { - "host": "prometheus", - "http": { - "paths": [ - { - "path": "/", - "backend": { - "serviceName": "prometheus", - "servicePort": 9090, - }, - } - ] - }, - } - ] - }, - } - ], - }, + self.config = { + "web-subpath": "/", + "default-target": "", + "max_file_size": 0, + "ingress_whitelist_source_range": "", + "tls_secret_name": "", + "site_url": "https://prometheus.192.168.100.100.xip.io", + "enable_web_admin_api": False, } + self.harness.update_config(self.config) - self.harness.charm.on.start.emit() - - self.harness.update_config({"site_url": "http://prometheus"}) - - pod_spec, _ = self.harness.get_pod_spec() - - self.assertDictEqual(expected_result, pod_spec) - - def test_ingress_resources_with_https(self) -> NoReturn: - """Test ingress resources with HTTPS.""" - expected_result = { - "version": 3, - "containers": [ - { - "name": "prometheus", - "imageDetails": self.harness.charm.image.fetch(), - "imagePullPolicy": "Always", - "ports": [ - { - "name": "prometheus", - "containerPort": 9090, - "protocol": "TCP", - } - ], - "envConfig": {}, - "volumeConfig": [ - { - "name": "config", - "mountPath": "/etc/prometheus", - "files": [ - { - "path": "prometheus.yml", - "content": ( - "global:\n" - " scrape_interval: 15s\n" - " evaluation_interval: 15s\n" - "alerting:\n" - " alertmanagers:\n" - " - static_configs:\n" - " - targets:\n" - "rule_files:\n" - "scrape_configs:\n" - " - job_name: 'prometheus'\n" - " static_configs:\n" - " - targets: [{}]\n".format("") - ), - } - ], - } - ], - "command": [ - "/bin/prometheus", - "--config.file=/etc/prometheus/prometheus.yml", - "--storage.tsdb.path=/prometheus", - "--web.console.libraries=/usr/share/prometheus/console_libraries", - "--web.console.templates=/usr/share/prometheus/consoles", - "--web.route-prefix={}".format("/"), - "--web.external-url=http://localhost:{}{}".format(9090, "/"), - ], - "kubernetes": { - "readinessProbe": { - "httpGet": { - "path": "/-/ready", - "port": 9090, - }, - "initialDelaySeconds": 10, - "timeoutSeconds": 30, - }, - "livenessProbe": { - "httpGet": { - "path": "/-/healthy", - "port": 9090, - }, - "initialDelaySeconds": 30, - "periodSeconds": 30, - }, - }, - } - ], - "kubernetesResources": { - "ingressResources": [ - { - "name": "prometheus-ingress", - "annotations": { - "nginx.ingress.kubernetes.io/proxy-body-size": "0", - }, - "spec": { - "rules": [ - { - "host": "prometheus", - "http": { - "paths": [ - { - "path": "/", - "backend": { - "serviceName": "prometheus", - "servicePort": 9090, - }, - } - ] - }, - } - ], - "tls": [ - {"hosts": ["prometheus"], "secretName": "prometheus"} - ], - }, - } - ], - }, - } + def test_config_changed( + self, + ) -> NoReturn: + """Test ingress resources without HTTP.""" - self.harness.charm.on.start.emit() + self.harness.charm.on.config_changed.emit() - self.harness.update_config( - {"site_url": "https://prometheus", "tls_secret_name": "prometheus"} - ) + # Assertions + self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) - pod_spec, _ = self.harness.get_pod_spec() + def test_config_changed_non_leader( + self, + ) -> NoReturn: + """Test ingress resources without HTTP.""" + self.harness.set_leader(is_leader=False) + self.harness.charm.on.config_changed.emit() - self.assertDictEqual(expected_result, pod_spec) + # Assertions + self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) - def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn: - """Test ingress resources with HTTPS and ingress whitelist.""" + def test_publish_prometheus_info( + self, + ) -> NoReturn: + """Test to see if prometheus relation is updated.""" expected_result = { - "version": 3, - "containers": [ - { - "name": "prometheus", - "imageDetails": self.harness.charm.image.fetch(), - "imagePullPolicy": "Always", - "ports": [ - { - "name": "prometheus", - "containerPort": 9090, - "protocol": "TCP", - } - ], - "envConfig": {}, - "volumeConfig": [ - { - "name": "config", - "mountPath": "/etc/prometheus", - "files": [ - { - "path": "prometheus.yml", - "content": ( - "global:\n" - " scrape_interval: 15s\n" - " evaluation_interval: 15s\n" - "alerting:\n" - " alertmanagers:\n" - " - static_configs:\n" - " - targets:\n" - "rule_files:\n" - "scrape_configs:\n" - " - job_name: 'prometheus'\n" - " static_configs:\n" - " - targets: [{}]\n".format("") - ), - } - ], - } - ], - "command": [ - "/bin/prometheus", - "--config.file=/etc/prometheus/prometheus.yml", - "--storage.tsdb.path=/prometheus", - "--web.console.libraries=/usr/share/prometheus/console_libraries", - "--web.console.templates=/usr/share/prometheus/consoles", - "--web.route-prefix={}".format("/"), - "--web.external-url=http://localhost:{}{}".format(9090, "/"), - ], - "kubernetes": { - "readinessProbe": { - "httpGet": { - "path": "/-/ready", - "port": 9090, - }, - "initialDelaySeconds": 10, - "timeoutSeconds": 30, - }, - "livenessProbe": { - "httpGet": { - "path": "/-/healthy", - "port": 9090, - }, - "initialDelaySeconds": 30, - "periodSeconds": 30, - }, - }, - } - ], - "kubernetesResources": { - "ingressResources": [ - { - "name": "prometheus-ingress", - "annotations": { - "nginx.ingress.kubernetes.io/proxy-body-size": "0", - "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0", - }, - "spec": { - "rules": [ - { - "host": "prometheus", - "http": { - "paths": [ - { - "path": "/", - "backend": { - "serviceName": "prometheus", - "servicePort": 9090, - }, - } - ] - }, - } - ], - "tls": [ - {"hosts": ["prometheus"], "secretName": "prometheus"} - ], - }, - } - ], - }, + "hostname": "prometheus", + "port": "9090", } - self.harness.charm.on.start.emit() - - self.harness.update_config( - { - "site_url": "https://prometheus", - "tls_secret_name": "prometheus", - "ingress_whitelist_source_range": "0.0.0.0/0", - } - ) - - pod_spec, _ = self.harness.get_pod_spec() + relation_id = self.harness.add_relation("prometheus", "mon") + self.harness.add_relation_unit(relation_id, "mon/0") + relation_data = self.harness.get_relation_data(relation_id, "prometheus") - self.assertDictEqual(expected_result, pod_spec) + self.assertDictEqual(expected_result, relation_data) - def test_publish_prometheus_info(self) -> NoReturn: + def test_publish_prometheus_info_non_leader( + self, + ) -> NoReturn: """Test to see if prometheus relation is updated.""" - expected_result = { - "host": "prometheus", - "port": "9090", - } - - self.harness.charm.on.start.emit() + expected_result = {} + self.harness.set_leader(is_leader=False) relation_id = self.harness.add_relation("prometheus", "mon") self.harness.add_relation_unit(relation_id, "mon/0") relation_data = self.harness.get_relation_data(relation_id, "prometheus")