Prepare installer and pods for Rel TWELVE
[osm/devops.git] / installers / charm / zookeeper / tests / test_charm.py
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
23 from typing import NoReturn
24 import unittest
25 from unittest.mock import patch, PropertyMock
26
27 from charm import ZookeeperCharm
28 from ops.model import ActiveStatus, BlockedStatus
29 from ops.testing import Harness
30
31
32 class TestCharm(unittest.TestCase):
33 """Zookeeper Charm unit tests."""
34
35 def setUp(
36 self,
37 ) -> NoReturn:
38 """Test setup"""
39 self.harness = Harness(ZookeeperCharm)
40 self.harness.set_leader(is_leader=True)
41 self.config = {"log_level": "INFO", "image_pull_pulicy": "always"}
42 self.harness.begin()
43
44 def test_config_invalid_log_level(self) -> NoReturn:
45 """Test invalid log_level config."""
46 self.config.update({"log_level": "invalid log level"})
47 self.harness.update_config(self.config)
48 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
49
50 def test_config_invalid_image_pull_pulicy(self) -> NoReturn:
51 """Test invalid image_pull_pulicy config."""
52 self.config.update({"image_pull_policy": "invalid image_pull_policy"})
53 self.harness.update_config(self.config)
54 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
55
56 @patch("charm.ZookeeperCharm.num_units", new_callable=PropertyMock)
57 def test_config_changed_no_relations(self, mock_num_units) -> NoReturn:
58 """Test config changed without relations."""
59 mock_num_units.return_value = 1
60 self.harness.charm.on.config_changed.emit()
61 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
62
63 @patch("charm.ZookeeperCharm.num_units", new_callable=PropertyMock)
64 def test_config_changed_non_leader(self, mock_num_units) -> NoReturn:
65 """Test config changed without relations (non-leader)."""
66 mock_num_units.return_value = 1
67 self.harness.set_leader(is_leader=False)
68 self.harness.charm.on.config_changed.emit()
69
70 # Assertions
71 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
72
73 @patch("charm.ZookeeperCharm.num_units", new_callable=PropertyMock)
74 @patch("charm.ZookeeperCharm.zookeeper_uri", new_callable=PropertyMock)
75 def test_with_relations_zookeeper(
76 self, mock_zookeeper_uri, mock_num_units
77 ) -> NoReturn:
78 "Test with relations (zookeeper)"
79 mock_num_units.return_value = 1
80 mock_zookeeper_uri.return_value = "zk-uri"
81
82 # Initializing the zookeeper relation
83 zookeeper_relation_id = self.harness.add_relation("zookeeper", "kafka")
84 self.harness.add_relation_unit(zookeeper_relation_id, "kafka/0")
85 # self.harness.update_relation_data(
86 # zookeeper_relation_id, "kafka/0", {"host": "zookeeper", "port": 9092}
87 # )
88
89 # Verifying status
90 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
91
92
93 if __name__ == "__main__":
94 unittest.main()