Add enable_web_admin_api config to prometheus charm 37/10237/2
authorDavid Garcia <david.garcia@canonical.com>
Fri, 29 Jan 2021 12:27:22 +0000 (13:27 +0100)
committerDavid Garcia <david.garcia@canonical.com>
Fri, 29 Jan 2021 12:29:12 +0000 (13:29 +0100)
Unit tests fixed

Change-Id: I8400794918780f929ad7612f72d477d37aee1b74
Signed-off-by: David Garcia <david.garcia@canonical.com>
installers/charm/prometheus/config.yaml
installers/charm/prometheus/src/pod_spec.py
installers/charm/prometheus/tests/test_charm.py
installers/charm/prometheus/tests/test_pod_spec.py

index 038391d..baa04cd 100644 (file)
@@ -52,3 +52,7 @@ options:
     type: string
     description: Ingress URL
     default: ""
+  enable_web_admin_api:
+    type: boolean
+    description: Boolean to enable the web admin api
+    default: False
index 541cf3e..202114e 100644 (file)
@@ -95,6 +95,7 @@ def _validate_data(config_data: Dict[str, Any], relation_data: Dict[str, Any]) -
         "tls_secret_name": lambda value, _: isinstance(value, str)
         if value is not None
         else True,
+        "enable_web_admin_api": lambda value, _: isinstance(value, bool),
     }
     relation_validators = {}
     problems = []
@@ -307,9 +308,7 @@ def _make_pod_command(config: Dict[str, Any], port: int) -> List[str]:
     Returns:
         List[str]: command to startup the process.
     """
-    return [
-        "sh",
-        "-c",
+    command = [
         "/bin/prometheus",
         "--config.file=/etc/prometheus/prometheus.yml",
         "--storage.tsdb.path=/prometheus",
@@ -320,6 +319,9 @@ def _make_pod_command(config: Dict[str, Any], port: int) -> List[str]:
             port, config.get("web_subpath")
         ),
     ]
+    if config.get("enable_web_admin_api"):
+        command.append("--web.enable-admin-api")
+    return command
 
 
 def make_pod_spec(
index 0f902a5..87d7bc5 100644 (file)
@@ -63,26 +63,24 @@ class TestCharm(unittest.TestCase):
                                 {
                                     "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("")
+                                        "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": [
-                        "sh",
-                        "-c",
                         "/bin/prometheus",
                         "--config.file=/etc/prometheus/prometheus.yml",
                         "--storage.tsdb.path=/prometheus",
@@ -148,26 +146,24 @@ class TestCharm(unittest.TestCase):
                                 {
                                     "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("")
+                                        "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": [
-                        "sh",
-                        "-c",
                         "/bin/prometheus",
                         "--config.file=/etc/prometheus/prometheus.yml",
                         "--storage.tsdb.path=/prometheus",
@@ -260,26 +256,24 @@ class TestCharm(unittest.TestCase):
                                 {
                                     "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("")
+                                        "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": [
-                        "sh",
-                        "-c",
                         "/bin/prometheus",
                         "--config.file=/etc/prometheus/prometheus.yml",
                         "--storage.tsdb.path=/prometheus",
@@ -376,26 +370,24 @@ class TestCharm(unittest.TestCase):
                                 {
                                     "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("")
+                                        "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": [
-                        "sh",
-                        "-c",
                         "/bin/prometheus",
                         "--config.file=/etc/prometheus/prometheus.yml",
                         "--storage.tsdb.path=/prometheus",
index 445d4a8..22f6bf5 100644 (file)
@@ -267,18 +267,18 @@ class TestPodSpec(unittest.TestCase):
                     {
                         "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(config["default_target"])
+                            "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(config["default_target"])
                         ),
                     }
                 ],
@@ -286,7 +286,7 @@ class TestPodSpec(unittest.TestCase):
         ]
 
         pod_envconfig = pod_spec._make_pod_files(config)
-
+        print(expected_result, pod_envconfig)
         self.assertListEqual(expected_result, pod_envconfig)
 
     def test_make_readiness_probe(self) -> NoReturn:
@@ -333,8 +333,6 @@ class TestPodSpec(unittest.TestCase):
         }
 
         expected_result = [
-            "sh",
-            "-c",
             "/bin/prometheus",
             "--config.file=/etc/prometheus/prometheus.yml",
             "--storage.tsdb.path=/prometheus",
@@ -350,6 +348,33 @@ class TestPodSpec(unittest.TestCase):
 
         self.assertListEqual(expected_result, pod_envconfig)
 
+    def test_make_pod_command_with_web_admin_api_enabled(self) -> NoReturn:
+        """Testing make pod command."""
+        port = 9090
+        config = {
+            "web_subpath": "/",
+            "default_target": "",
+            "site_url": "",
+            "enable_web_admin_api": True,
+        }
+
+        expected_result = [
+            "/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(config.get("web_subpath")),
+            "--web.external-url=http://localhost:{}{}".format(
+                port, config.get("web_subpath")
+            ),
+            "--web.enable-admin-api",
+        ]
+
+        pod_envconfig = pod_spec._make_pod_command(config, port)
+
+        self.assertListEqual(expected_result, pod_envconfig)
+
     def test_make_pod_spec(self) -> NoReturn:
         """Testing make pod spec."""
         image_info = {"upstream-source": "ubuntu/prometheus:latest"}
@@ -357,6 +382,7 @@ class TestPodSpec(unittest.TestCase):
             "web_subpath": "/",
             "default_target": "",
             "site_url": "",
+            "enable_web_admin_api": False,
         }
         relation_state = {}
         app_name = "prometheus"
@@ -385,18 +411,18 @@ class TestPodSpec(unittest.TestCase):
                                 {
                                     "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(
+                                        "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(
                                             config.get("default_target")
                                         )
                                     ),
@@ -405,8 +431,6 @@ class TestPodSpec(unittest.TestCase):
                         }
                     ],
                     "command": [
-                        "sh",
-                        "-c",
                         "/bin/prometheus",
                         "--config.file=/etc/prometheus/prometheus.yml",
                         "--storage.tsdb.path=/prometheus",
@@ -456,6 +480,7 @@ class TestPodSpec(unittest.TestCase):
             "tls_secret_name": "prometheus",
             "max_file_size": 0,
             "ingress_whitelist_source_range": "0.0.0.0/0",
+            "enable_web_admin_api": False,
         }
         relation_state = {}
         app_name = "prometheus"
@@ -484,18 +509,18 @@ class TestPodSpec(unittest.TestCase):
                                 {
                                     "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(
+                                        "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(
                                             config.get("default_target")
                                         )
                                     ),
@@ -504,8 +529,6 @@ class TestPodSpec(unittest.TestCase):
                         }
                     ],
                     "command": [
-                        "sh",
-                        "-c",
                         "/bin/prometheus",
                         "--config.file=/etc/prometheus/prometheus.yml",
                         "--storage.tsdb.path=/prometheus",
@@ -590,6 +613,7 @@ class TestPodSpec(unittest.TestCase):
             "web_subpath": "/",
             "default_target": "",
             "site_url": "",
+            "enable_web_admin_api": False,
         }
         relation_state = {}
         app_name = "prometheus"