|
12 | 12 | microservice_available_and_healthy, microservice_is_not_available, \ |
13 | 13 | service_endpoint_is_initialized, deployment_is_not_fully_available, \ |
14 | 14 | deployment_is_fully_available, read_microservices_logs |
| 15 | +from chaosk8s.statefulset.probes import statefulset_fully_available, \ |
| 16 | + statefulset_not_fully_available |
15 | 17 |
|
16 | 18 |
|
17 | 19 | @patch('chaosk8s.has_local_config_file', autospec=True) |
@@ -223,6 +225,88 @@ def test_deployment_is_not_fully_available_when_it_should(cl, client, |
223 | 225 | str(excinfo.value) |
224 | 226 |
|
225 | 227 |
|
| 228 | +@patch('chaosk8s.has_local_config_file', autospec=True) |
| 229 | +@patch('chaosk8s.statefulset.probes.watch', autospec=True) |
| 230 | +@patch('chaosk8s.statefulset.probes.client', autospec=True) |
| 231 | +@patch('chaosk8s.client') |
| 232 | +def test_statefulset_not_fully_available(cl, client, watch, has_conf): |
| 233 | + has_conf.return_value = False |
| 234 | + statefulset = MagicMock() |
| 235 | + statefulset.spec.replicas = 2 |
| 236 | + statefulset.status.ready_replicas = 1 |
| 237 | + |
| 238 | + watcher = MagicMock() |
| 239 | + watcher.stream = MagicMock() |
| 240 | + watcher.stream.side_effect = [[{"object": statefulset, "type": "ADDED"}]] |
| 241 | + watch.Watch.return_value = watcher |
| 242 | + |
| 243 | + assert statefulset_not_fully_available("mysvc") is True |
| 244 | + |
| 245 | + |
| 246 | +@patch('chaosk8s.has_local_config_file', autospec=True) |
| 247 | +@patch('chaosk8s.statefulset.probes.watch', autospec=True) |
| 248 | +@patch('chaosk8s.statefulset.probes.client', autospec=True) |
| 249 | +@patch('chaosk8s.client') |
| 250 | +def test_statefulset_fully_available_when_it_should_not(cl, client, |
| 251 | + watch, has_conf): |
| 252 | + has_conf.return_value = False |
| 253 | + statefulset = MagicMock() |
| 254 | + statefulset.spec.replicas = 2 |
| 255 | + statefulset.status.ready_replicas = 2 |
| 256 | + |
| 257 | + watcher = MagicMock() |
| 258 | + watcher.stream = MagicMock() |
| 259 | + watcher.stream.side_effect = urllib3.exceptions.ReadTimeoutError( |
| 260 | + None, None, None) |
| 261 | + watch.Watch.return_value = watcher |
| 262 | + |
| 263 | + with pytest.raises(ActivityFailed) as excinfo: |
| 264 | + statefulset_not_fully_available("mysvc") |
| 265 | + assert "microservice 'mysvc' failed to stop running within" in \ |
| 266 | + str(excinfo.value) |
| 267 | + |
| 268 | + |
| 269 | +@patch('chaosk8s.has_local_config_file', autospec=True) |
| 270 | +@patch('chaosk8s.statefulset.probes.watch', autospec=True) |
| 271 | +@patch('chaosk8s.statefulset.probes.client', autospec=True) |
| 272 | +@patch('chaosk8s.client') |
| 273 | +def test_statefulset_fully_available(cl, client, watch, has_conf): |
| 274 | + has_conf.return_value = False |
| 275 | + statefulset = MagicMock() |
| 276 | + statefulset.spec.replicas = 2 |
| 277 | + statefulset.status.ready_replicas = 2 |
| 278 | + |
| 279 | + watcher = MagicMock() |
| 280 | + watcher.stream = MagicMock() |
| 281 | + watcher.stream.side_effect = [[{"object": statefulset, "type": "ADDED"}]] |
| 282 | + watch.Watch.return_value = watcher |
| 283 | + |
| 284 | + assert statefulset_fully_available("mysvc") is True |
| 285 | + |
| 286 | + |
| 287 | +@patch('chaosk8s.has_local_config_file', autospec=True) |
| 288 | +@patch('chaosk8s.statefulset.probes.watch', autospec=True) |
| 289 | +@patch('chaosk8s.statefulset.probes.client', autospec=True) |
| 290 | +@patch('chaosk8s.client') |
| 291 | +def test_statefulset_not_fully_available_when_it_should(cl, client, |
| 292 | + watch, has_conf): |
| 293 | + has_conf.return_value = False |
| 294 | + statefulset = MagicMock() |
| 295 | + statefulset.spec.replicas = 2 |
| 296 | + statefulset.status.ready_replicas = 1 |
| 297 | + |
| 298 | + watcher = MagicMock() |
| 299 | + watcher.stream = MagicMock() |
| 300 | + watcher.stream.side_effect = urllib3.exceptions.ReadTimeoutError( |
| 301 | + None, None, None) |
| 302 | + watch.Watch.return_value = watcher |
| 303 | + |
| 304 | + with pytest.raises(ActivityFailed) as excinfo: |
| 305 | + statefulset_fully_available("mysvc") |
| 306 | + assert "microservice 'mysvc' failed to recover within" in \ |
| 307 | + str(excinfo.value) |
| 308 | + |
| 309 | + |
226 | 310 | @patch('chaosk8s.has_local_config_file', autospec=True) |
227 | 311 | @patch('chaosk8s.pod.probes.client', autospec=True) |
228 | 312 | @patch('chaosk8s.client') |
|
0 commit comments