# osm-charmers@lists.launchpad.net
##
+import sys
from typing import NoReturn
import unittest
-from ops.model import BlockedStatus
-from ops.testing import Harness
-
from charm import PrometheusCharm
+from ops.model import ActiveStatus
+from ops.testing import Harness
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:"
- " scrape_interval: 15s"
- " evaluation_interval: 15s"
- "alerting:"
- " alertmanagers:"
- " - static_configs:"
- " - targets:"
- "rule_files:"
- "scrape_configs:"
- " - job_name: 'prometheus'"
- " static_configs:"
- " - targets: [{}]".format("")
- ),
- }
- ],
- }
- ],
- "command": [
- "sh",
- "-c",
- "/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.config = {
+ "web-subpath": "/",
+ "default-target": "",
+ "max_file_size": 0,
+ "ingress_whitelist_source_range": "",
+ "tls_secret_name": "",
+ "site_url": "https://prometheus.192.168.100.100.nip.io",
+ "cluster_issuer": "vault-issuer",
+ "enable_web_admin_api": False,
+ "web_config_username": "admin",
+ "web_config_password": "1234",
}
+ self.harness.update_config(self.config)
- 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:"
- " scrape_interval: 15s"
- " evaluation_interval: 15s"
- "alerting:"
- " alertmanagers:"
- " - static_configs:"
- " - targets:"
- "rule_files:"
- "scrape_configs:"
- " - job_name: 'prometheus'"
- " static_configs:"
- " - targets: [{}]".format("")
- ),
- }
- ],
- }
- ],
- "command": [
- "sh",
- "-c",
- "/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.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:"
- " scrape_interval: 15s"
- " evaluation_interval: 15s"
- "alerting:"
- " alertmanagers:"
- " - static_configs:"
- " - targets:"
- "rule_files:"
- "scrape_configs:"
- " - job_name: 'prometheus'"
- " static_configs:"
- " - targets: [{}]".format("")
- ),
- }
- ],
- }
- ],
- "command": [
- "sh",
- "-c",
- "/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:"
- " scrape_interval: 15s"
- " evaluation_interval: 15s"
- "alerting:"
- " alertmanagers:"
- " - static_configs:"
- " - targets:"
- "rule_files:"
- "scrape_configs:"
- " - job_name: 'prometheus'"
- " static_configs:"
- " - targets: [{}]".format("")
- ),
- }
- ],
- }
- ],
- "command": [
- "sh",
- "-c",
- "/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": self.harness.charm.app.name,
+ "port": "9090",
+ "user": "admin",
+ "password": "1234",
}
- 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",
- }
+ 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, self.harness.charm.app.name
)
- pod_spec, _ = self.harness.get_pod_spec()
-
- 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")
+ relation_data = self.harness.get_relation_data(
+ relation_id, self.harness.charm.app.name
+ )
self.assertDictEqual(expected_result, relation_data)