blob: dd8b732cb90a6af4cfa2fbc3d261545eb2276742 [file] [log] [blame]
sousaedu2459af62021-01-15 16:50:26 +00001#!/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 Garcia49379ce2021-02-24 13:48:22 +010023import sys
sousaedu2459af62021-01-15 16:50:26 +000024from typing import NoReturn
25import unittest
sousaedu2459af62021-01-15 16:50:26 +000026
27from charm import PrometheusCharm
David Garciac753dc52021-03-17 15:28:47 +010028from ops.model import ActiveStatus
29from ops.testing import Harness
sousaedu2459af62021-01-15 16:50:26 +000030
31
32class TestCharm(unittest.TestCase):
33 """Prometheus Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
David Garcia49379ce2021-02-24 13:48:22 +010037 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
sousaedu2459af62021-01-15 16:50:26 +000038 self.harness = Harness(PrometheusCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010041 self.config = {
42 "web-subpath": "/",
43 "default-target": "",
44 "max_file_size": 0,
45 "ingress_whitelist_source_range": "",
46 "tls_secret_name": "",
47 "site_url": "https://prometheus.192.168.100.100.xip.io",
48 "enable_web_admin_api": False,
49 }
50 self.harness.update_config(self.config)
sousaedu2459af62021-01-15 16:50:26 +000051
David Garcia49379ce2021-02-24 13:48:22 +010052 def test_config_changed(
53 self,
54 ) -> NoReturn:
sousaedu2459af62021-01-15 16:50:26 +000055 """Test ingress resources without HTTP."""
sousaedu2459af62021-01-15 16:50:26 +000056
David Garcia49379ce2021-02-24 13:48:22 +010057 self.harness.charm.on.config_changed.emit()
sousaedu2459af62021-01-15 16:50:26 +000058
David Garcia49379ce2021-02-24 13:48:22 +010059 # Assertions
60 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
sousaedu2459af62021-01-15 16:50:26 +000061
David Garcia49379ce2021-02-24 13:48:22 +010062 def test_config_changed_non_leader(
63 self,
64 ) -> NoReturn:
65 """Test ingress resources without HTTP."""
66 self.harness.set_leader(is_leader=False)
67 self.harness.charm.on.config_changed.emit()
sousaedu2459af62021-01-15 16:50:26 +000068
David Garcia49379ce2021-02-24 13:48:22 +010069 # Assertions
70 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
sousaedu2459af62021-01-15 16:50:26 +000071
David Garcia49379ce2021-02-24 13:48:22 +010072 def test_publish_prometheus_info(
73 self,
74 ) -> NoReturn:
sousaedu2459af62021-01-15 16:50:26 +000075 """Test to see if prometheus relation is updated."""
76 expected_result = {
David Garcia49379ce2021-02-24 13:48:22 +010077 "hostname": "prometheus",
sousaedu2459af62021-01-15 16:50:26 +000078 "port": "9090",
79 }
80
David Garcia49379ce2021-02-24 13:48:22 +010081 relation_id = self.harness.add_relation("prometheus", "mon")
82 self.harness.add_relation_unit(relation_id, "mon/0")
83 relation_data = self.harness.get_relation_data(relation_id, "prometheus")
sousaedu2459af62021-01-15 16:50:26 +000084
David Garcia49379ce2021-02-24 13:48:22 +010085 self.assertDictEqual(expected_result, relation_data)
86
87 def test_publish_prometheus_info_non_leader(
88 self,
89 ) -> NoReturn:
90 """Test to see if prometheus relation is updated."""
91 expected_result = {}
92
93 self.harness.set_leader(is_leader=False)
sousaedu2459af62021-01-15 16:50:26 +000094 relation_id = self.harness.add_relation("prometheus", "mon")
95 self.harness.add_relation_unit(relation_id, "mon/0")
96 relation_data = self.harness.get_relation_data(relation_id, "prometheus")
97
98 self.assertDictEqual(expected_result, relation_data)
99
100
101if __name__ == "__main__":
102 unittest.main()