| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2020 Canonical Ltd. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | # |
| 16 | # For those usages not covered by the Apache License, Version 2.0 please |
| 17 | # contact: legal@canonical.com |
| 18 | # |
| 19 | # To get in touch with the maintainers, please contact: |
| 20 | # osm-charmers@lists.launchpad.net |
| 21 | ## |
| 22 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 23 | import sys |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 24 | from typing import NoReturn |
| 25 | import unittest |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 26 | |
| 27 | from charm import PrometheusCharm |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 28 | from ops.model import ActiveStatus |
| 29 | from ops.testing import Harness |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| 33 | """Prometheus Charm unit tests.""" |
| 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 37 | self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 38 | self.harness = Harness(PrometheusCharm) |
| 39 | self.harness.set_leader(is_leader=True) |
| 40 | self.harness.begin() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 41 | self.config = { |
| 42 | "web-subpath": "/", |
| 43 | "default-target": "", |
| 44 | "max_file_size": 0, |
| 45 | "ingress_whitelist_source_range": "", |
| 46 | "tls_secret_name": "", |
| David Garcia | c395a45 | 2021-05-05 14:22:26 +0200 | [diff] [blame] | 47 | "site_url": "https://prometheus.192.168.100.100.nip.io", |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 48 | "cluster_issuer": "vault-issuer", |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 49 | "enable_web_admin_api": False, |
| David Garcia | de440ed | 2021-10-11 19:56:53 +0200 | [diff] [blame] | 50 | "web_config_username": "admin", |
| 51 | "web_config_password": "1234", |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 52 | } |
| 53 | self.harness.update_config(self.config) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 54 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 55 | def test_config_changed( |
| 56 | self, |
| 57 | ) -> NoReturn: |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 58 | """Test ingress resources without HTTP.""" |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 59 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 60 | self.harness.charm.on.config_changed.emit() |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 61 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 62 | # Assertions |
| 63 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 64 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 65 | def test_config_changed_non_leader( |
| 66 | self, |
| 67 | ) -> NoReturn: |
| 68 | """Test ingress resources without HTTP.""" |
| 69 | self.harness.set_leader(is_leader=False) |
| 70 | self.harness.charm.on.config_changed.emit() |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 71 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 72 | # Assertions |
| 73 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 74 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 75 | def test_publish_prometheus_info( |
| 76 | self, |
| 77 | ) -> NoReturn: |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 78 | """Test to see if prometheus relation is updated.""" |
| 79 | expected_result = { |
| David Garcia | 53a09d8 | 2022-02-01 16:39:44 +0100 | [diff] [blame] | 80 | "hostname": self.harness.charm.app.name, |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 81 | "port": "9090", |
| David Garcia | de440ed | 2021-10-11 19:56:53 +0200 | [diff] [blame] | 82 | "user": "admin", |
| 83 | "password": "1234", |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 86 | relation_id = self.harness.add_relation("prometheus", "mon") |
| 87 | self.harness.add_relation_unit(relation_id, "mon/0") |
| David Garcia | 53a09d8 | 2022-02-01 16:39:44 +0100 | [diff] [blame] | 88 | relation_data = self.harness.get_relation_data( |
| 89 | relation_id, self.harness.charm.app.name |
| 90 | ) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 91 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 92 | self.assertDictEqual(expected_result, relation_data) |
| 93 | |
| 94 | def test_publish_prometheus_info_non_leader( |
| 95 | self, |
| 96 | ) -> NoReturn: |
| 97 | """Test to see if prometheus relation is updated.""" |
| 98 | expected_result = {} |
| 99 | |
| 100 | self.harness.set_leader(is_leader=False) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 101 | relation_id = self.harness.add_relation("prometheus", "mon") |
| 102 | self.harness.add_relation_unit(relation_id, "mon/0") |
| David Garcia | 53a09d8 | 2022-02-01 16:39:44 +0100 | [diff] [blame] | 103 | relation_data = self.harness.get_relation_data( |
| 104 | relation_id, self.harness.charm.app.name |
| 105 | ) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 106 | |
| 107 | self.assertDictEqual(expected_result, relation_data) |
| 108 | |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | unittest.main() |