2 # Copyright 2020 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 from typing
import NoReturn
25 from unittest
.mock
import patch
, PropertyMock
27 from charm
import ZookeeperCharm
28 from ops
.model
import ActiveStatus
, BlockedStatus
29 from ops
.testing
import Harness
32 class TestCharm(unittest
.TestCase
):
33 """Zookeeper Charm unit tests."""
39 self
.harness
= Harness(ZookeeperCharm
)
40 self
.harness
.set_leader(is_leader
=True)
41 self
.config
= {"log_level": "INFO", "image_pull_pulicy": "always"}
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
)
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
)
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
)
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()
71 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
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
78 "Test with relations (zookeeper)"
79 mock_num_units
.return_value
= 1
80 mock_zookeeper_uri
.return_value
= "zk-uri"
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}
90 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
93 if __name__
== "__main__":