From 51bb2563cf26ba7c7797f499d0e5069371efb8e5 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Fri, 29 Jan 2021 13:27:22 +0100 Subject: [PATCH] Add enable_web_admin_api config to prometheus charm Unit tests fixed Change-Id: I8400794918780f929ad7612f72d477d37aee1b74 Signed-off-by: David Garcia --- installers/charm/prometheus/config.yaml | 4 + installers/charm/prometheus/src/pod_spec.py | 8 +- .../charm/prometheus/tests/test_charm.py | 104 ++++++++--------- .../charm/prometheus/tests/test_pod_spec.py | 110 +++++++++++------- 4 files changed, 124 insertions(+), 102 deletions(-) diff --git a/installers/charm/prometheus/config.yaml b/installers/charm/prometheus/config.yaml index 038391dd..baa04cd4 100644 --- a/installers/charm/prometheus/config.yaml +++ b/installers/charm/prometheus/config.yaml @@ -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 diff --git a/installers/charm/prometheus/src/pod_spec.py b/installers/charm/prometheus/src/pod_spec.py index 541cf3e9..202114ee 100644 --- a/installers/charm/prometheus/src/pod_spec.py +++ b/installers/charm/prometheus/src/pod_spec.py @@ -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( diff --git a/installers/charm/prometheus/tests/test_charm.py b/installers/charm/prometheus/tests/test_charm.py index 0f902a55..87d7bc5c 100644 --- a/installers/charm/prometheus/tests/test_charm.py +++ b/installers/charm/prometheus/tests/test_charm.py @@ -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", diff --git a/installers/charm/prometheus/tests/test_pod_spec.py b/installers/charm/prometheus/tests/test_pod_spec.py index 445d4a8e..22f6bf54 100644 --- a/installers/charm/prometheus/tests/test_pod_spec.py +++ b/installers/charm/prometheus/tests/test_pod_spec.py @@ -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" -- 2.25.1