|
10 | 10 |
|
11 | 11 | from charm import CassandraCharm
|
12 | 12 | from core.state import PEER_RELATION
|
13 |
| -from managers.config import ConfigManager |
14 | 13 |
|
15 | 14 | BOOTSTRAP_RELATION = "bootstrap"
|
16 | 15 | PEER_SECRET = "cassandra-peers.cassandra.app"
|
17 | 16 |
|
18 |
| -# TODO: add start change password unit test |
| 17 | + |
| 18 | +def test_start_change_password(): |
| 19 | + """Leader should generate & configure cassandra password.""" |
| 20 | + ctx = testing.Context(CassandraCharm) |
| 21 | + relation = testing.PeerRelation(id=1, endpoint=PEER_RELATION) |
| 22 | + bootstrap_relation = testing.PeerRelation(id=2, endpoint=BOOTSTRAP_RELATION) |
| 23 | + secret = testing.Secret(label=PEER_SECRET, tracked_content={}, owner="app") |
| 24 | + state = testing.State(leader=True, relations={relation, bootstrap_relation}, secrets={secret}) |
| 25 | + |
| 26 | + with ( |
| 27 | + patch("managers.config.ConfigManager.render_env") as render_env, |
| 28 | + patch("managers.config.ConfigManager.render_cassandra_config") as render_cassandra_config, |
| 29 | + patch( |
| 30 | + "managers.database.DatabaseManager.update_system_user_password" |
| 31 | + ) as update_system_user_password, |
| 32 | + patch("charm.CassandraWorkload") as workload, |
| 33 | + patch( |
| 34 | + "managers.cluster.ClusterManager.is_healthy", |
| 35 | + new_callable=PropertyMock(return_value=True), |
| 36 | + ), |
| 37 | + ): |
| 38 | + workload.return_value.generate_password.return_value = "password" |
| 39 | + |
| 40 | + state = ctx.run(ctx.on.start(), state) |
| 41 | + render_env.assert_called() |
| 42 | + render_cassandra_config.assert_called_once() |
| 43 | + assert render_cassandra_config.call_args.kwargs["authentication"] is False |
| 44 | + workload.return_value.start.assert_called() |
| 45 | + |
| 46 | + render_cassandra_config.reset_mock() |
| 47 | + state = ctx.run(ctx.on.start(), state) |
| 48 | + update_system_user_password.assert_called_once_with("cassandra", "password") |
| 49 | + assert render_cassandra_config.call_args.kwargs["authentication"] is True |
| 50 | + workload.return_value.restart.assert_called() |
19 | 51 |
|
20 | 52 |
|
21 | 53 | def test_start_leader():
|
22 | 54 | """Leader should render all required configs and start workload."""
|
23 | 55 | ctx = testing.Context(CassandraCharm)
|
24 | 56 | relation = testing.PeerRelation(id=1, endpoint=PEER_RELATION)
|
25 | 57 | bootstrap_relation = testing.PeerRelation(id=2, endpoint=BOOTSTRAP_RELATION)
|
26 |
| - secret = testing.Secret(label=PEER_SECRET, tracked_content={"cassandra-password": "ua"}) |
| 58 | + secret = testing.Secret( |
| 59 | + label=PEER_SECRET, tracked_content={"cassandra-password": "ua"}, owner="app" |
| 60 | + ) |
27 | 61 | state = testing.State(leader=True, relations={relation, bootstrap_relation}, secrets={secret})
|
28 | 62 |
|
29 | 63 | with (
|
|
0 commit comments