12
12
from core .state import PEER_RELATION
13
13
14
14
15
- def test_start_maintenance_status_when_starting ():
15
+ def test_start_maintenance_status ():
16
16
"""Charm enters MaintenanceStatus when Cassandra is not yet healthy."""
17
17
ctx = testing .Context (CassandraCharm )
18
18
relation = testing .PeerRelation (id = 1 , endpoint = PEER_RELATION )
19
- state_in = testing .State (leader = True , relations = {relation })
19
+ state = testing .State (leader = True , relations = {relation })
20
20
21
21
with (
22
22
patch ("managers.config.ConfigManager.render_env" ),
23
- patch ("workload.CassandraWorkload.restart " ),
24
- patch ("workload .CassandraWorkload.is_alive " ),
23
+ patch ("managers.config.ConfigManager.render_cassandra_config " ),
24
+ patch ("charm .CassandraWorkload" ),
25
25
patch (
26
26
"managers.cluster.ClusterManager.is_healthy" ,
27
27
new_callable = PropertyMock (return_value = False ),
28
28
),
29
29
):
30
- state_out = ctx .run (ctx .on .start (), state_in )
31
- assert state_out .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
32
- assert state_out .get_relation (1 ).local_unit_data .get ("workload_state" ) == "active "
33
- assert state_out .get_relation (1 ).local_app_data .get ("cluster_state" ) == "active"
30
+ state = ctx .run (ctx .on .start (), state )
31
+ assert state .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
32
+ assert state .get_relation (1 ).local_unit_data .get ("workload_state" ) == "starting "
33
+ assert not state .get_relation (1 ).local_app_data .get ("cluster_state" )
34
34
35
+ state = ctx .run (ctx .on .update_status (), state )
36
+ assert state .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
37
+ assert state .get_relation (1 ).local_unit_data .get ("workload_state" ) == "starting"
38
+ assert not state .get_relation (1 ).local_app_data .get ("cluster_state" )
35
39
36
- def test_start_sets_active_status_when_healthy ():
40
+
41
+ def test_start_active_status_when_healthy ():
37
42
"""Charm enters ActiveStatus when Cassandra is healthy after start."""
38
43
ctx = testing .Context (CassandraCharm )
39
44
relation = testing .PeerRelation (id = 1 , endpoint = PEER_RELATION )
40
- state_in = testing .State (leader = True , relations = {relation })
45
+ state = testing .State (leader = True , relations = {relation })
41
46
42
47
with (
43
48
patch ("managers.config.ConfigManager.render_env" ),
44
- patch ("workload.CassandraWorkload.restart " ),
45
- patch ("workload .CassandraWorkload.is_alive " ),
49
+ patch ("managers.config.ConfigManager.render_cassandra_config " ),
50
+ patch ("charm .CassandraWorkload" ),
46
51
patch (
47
52
"managers.cluster.ClusterManager.is_healthy" ,
48
53
new_callable = PropertyMock (return_value = True ),
49
54
),
50
55
):
51
- state_out = ctx .run (ctx .on .start (), state_in )
52
- assert state_out .unit_status == ops .ActiveStatus ()
53
- assert state_out .get_relation (1 ).local_unit_data .get ("workload_state" ) == "active"
54
- assert state_out .get_relation (1 ).local_app_data .get ("cluster_state" ) == "active"
56
+ state = ctx .run (ctx .on .start (), state )
57
+ assert state .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
58
+ assert state .get_relation (1 ).local_unit_data .get ("workload_state" ) == "starting"
59
+ assert not state .get_relation (1 ).local_app_data .get ("cluster_state" )
60
+
61
+ state = ctx .run (ctx .on .update_status (), state )
62
+ assert state .unit_status == ops .ActiveStatus ()
63
+ assert state .get_relation (1 ).local_unit_data .get ("workload_state" ) == "active"
64
+ assert state .get_relation (1 ).local_app_data .get ("cluster_state" ) == "active"
55
65
56
66
57
67
def test_start_only_after_leader_active ():
58
68
"""Ensure Cassandra node starts only after leader is active and cluster_state is 'active'."""
59
69
ctx = testing .Context (CassandraCharm )
60
70
relation = testing .PeerRelation (id = 1 , endpoint = PEER_RELATION )
61
- state_in = testing .State (leader = False , relations = {relation })
71
+ state = testing .State (leader = False , relations = {relation })
62
72
63
73
with (
64
74
patch ("managers.config.ConfigManager.render_env" ),
65
- patch ("workload.CassandraWorkload.restart" ) as restart ,
66
- patch ("workload .CassandraWorkload.is_alive" ) ,
75
+ patch ("managers.config.ConfigManager.render_cassandra_config" ) ,
76
+ patch ("charm .CassandraWorkload" ) as workload ,
67
77
patch (
68
78
"managers.cluster.ClusterManager.is_healthy" ,
69
79
new_callable = PropertyMock (return_value = False ),
70
80
),
71
81
):
72
- state_out = ctx .run (ctx .on .start (), state_in )
73
- assert state_out .unit_status == ops .MaintenanceStatus ("installing Cassandra" )
74
- restart .assert_not_called ()
82
+ state = ctx .run (ctx .on .start (), state )
83
+ assert state .unit_status == ops .WaitingStatus ("waiting for cluster to start" )
84
+ assert state .get_relation (1 ).local_unit_data .get ("workload_state" ) == "waiting_for_start"
85
+ workload .return_value .start .assert_not_called ()
75
86
76
87
relation = testing .PeerRelation (
77
88
id = 1 , endpoint = PEER_RELATION , local_app_data = {"cluster_state" : "active" }
78
89
)
79
- state_in = testing .State (leader = False , relations = {relation })
90
+ state = testing .State (leader = False , relations = {relation })
80
91
81
92
with (
82
93
patch ("managers.config.ConfigManager.render_env" ),
83
- patch ("workload.CassandraWorkload.restart" ) as restart ,
84
- patch ("workload .CassandraWorkload.is_alive" ) ,
94
+ patch ("managers.config.ConfigManager.render_cassandra_config" ) ,
95
+ patch ("charm .CassandraWorkload" ) as workload ,
85
96
patch (
86
97
"managers.cluster.ClusterManager.is_healthy" ,
87
98
new_callable = PropertyMock (return_value = False ),
88
99
),
89
100
):
90
- state_out = ctx .run (ctx .on .start (), state_in )
91
- assert state_out .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
92
- assert state_out .get_relation (1 ).local_unit_data .get ("workload_state" ) == "active "
93
- restart .assert_called ()
101
+ state = ctx .run (ctx .on .start (), state )
102
+ assert state .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
103
+ assert state .get_relation (1 ).local_unit_data .get ("workload_state" ) == "starting "
104
+ workload . return_value . start .assert_called ()
94
105
95
106
96
107
def test_config_changed_invalid_config ():
97
108
"""Ensure charm enters BlockedStatus if config is invalid during config_changed event."""
98
109
ctx = testing .Context (CassandraCharm )
99
110
relation = testing .PeerRelation (id = 1 , endpoint = PEER_RELATION )
100
- state_in = testing .State (leader = True , relations = {relation }, config = {"profile" : "invalid" })
111
+ state = testing .State (leader = True , relations = {relation }, config = {"profile" : "invalid" })
112
+
101
113
with (
102
114
patch ("managers.config.ConfigManager.render_env" ),
103
- patch ("workload.CassandraWorkload.restart" ),
104
- patch ("workload.CassandraWorkload.is_alive" ),
105
- patch (
106
- "managers.cluster.ClusterManager.is_healthy" ,
107
- new_callable = PropertyMock (return_value = False ),
108
- ),
115
+ patch ("managers.config.ConfigManager.render_cassandra_config" ),
116
+ patch ("charm.CassandraWorkload" ),
109
117
):
110
- state_out = ctx .run (ctx .on .config_changed (), state_in )
111
- assert state_out .unit_status == ops .BlockedStatus ("invalid config" )
118
+ state = ctx .run (ctx .on .config_changed (), state )
119
+ assert state .unit_status == ops .BlockedStatus ("invalid config" )
112
120
113
121
114
122
def test_config_changed_no_restart ():
@@ -117,59 +125,12 @@ def test_config_changed_no_restart():
117
125
relation = testing .PeerRelation (
118
126
id = 1 , endpoint = PEER_RELATION , local_unit_data = {"workload_state" : "starting" }
119
127
)
120
- state_in = testing .State (leader = True , relations = {relation })
128
+ state = testing .State (leader = True , relations = {relation })
121
129
with (
122
130
patch ("managers.config.ConfigManager.render_env" ),
123
- patch ("workload.CassandraWorkload.restart" ) as restart ,
124
- patch ("workload.CassandraWorkload.is_alive" ),
125
- patch (
126
- "managers.cluster.ClusterManager.is_healthy" ,
127
- new_callable = PropertyMock (return_value = False ),
128
- ),
129
- ):
130
- state_out = ctx .run (ctx .on .config_changed (), state_in )
131
- assert state_out .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
132
- restart .assert_not_called ()
133
-
134
-
135
- def test_collect_unit_status_active_but_not_healthy ():
136
- """Ensure unit is MaintenanceStatus if workload_state is 'active' but node is not healthy."""
137
- ctx = testing .Context (CassandraCharm )
138
- relation = testing .PeerRelation (
139
- id = 1 ,
140
- endpoint = PEER_RELATION ,
141
- local_unit_data = {"workload_state" : "active" },
142
- )
143
- state_in = testing .State (leader = True , relations = {relation })
144
- with (
145
- patch ("managers.config.ConfigManager.render_env" ),
146
- patch ("workload.CassandraWorkload.restart" ),
147
- patch ("workload.CassandraWorkload.is_alive" ),
148
- patch (
149
- "managers.cluster.ClusterManager.is_healthy" ,
150
- new_callable = PropertyMock (return_value = False ),
151
- ),
152
- ):
153
- state_out = ctx .run (ctx .on .collect_unit_status (), state_in )
154
- assert state_out .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start" )
155
-
156
-
157
- def test_start_not_leader_and_cluster_state_not_active ():
158
- """Ensure charm does not start Cassandra if not leader and cluster_state is not 'active'."""
159
- ctx = testing .Context (CassandraCharm )
160
- relation = testing .PeerRelation (
161
- id = 1 , endpoint = PEER_RELATION , local_app_data = {"cluster_state" : "pending" }
162
- )
163
- state_in = testing .State (leader = False , relations = {relation })
164
- with (
165
- patch ("managers.config.ConfigManager.render_env" ),
166
- patch ("workload.CassandraWorkload.restart" ) as restart ,
167
- patch ("workload.CassandraWorkload.is_alive" ),
168
- patch (
169
- "managers.cluster.ClusterManager.is_healthy" ,
170
- new_callable = PropertyMock (return_value = False ),
171
- ),
131
+ patch ("managers.config.ConfigManager.render_cassandra_config" ),
132
+ patch ("charm.CassandraWorkload" ) as workload ,
172
133
):
173
- state_out = ctx .run (ctx .on .start (), state_in )
174
- assert state_out .unit_status == ops .MaintenanceStatus ("installing Cassandra" )
175
- restart .assert_not_called ()
134
+ state = ctx .run (ctx .on .config_changed (), state )
135
+ assert state .unit_status == ops .MaintenanceStatus ("waiting for Cassandra to start " )
136
+ workload . return_value . restart .assert_not_called ()
0 commit comments