Major improvement in OSM charms
[osm/devops.git] / installers / charm / prometheus / tests / test_charm.py
index 87d7bc5..b1848bd 100644 (file)
 # 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")