|
1 | 1 | # Copyright 2021 Canonical Ltd.
|
2 | 2 | # See LICENSE file for licensing details.
|
3 | 3 | import itertools
|
| 4 | +import json |
4 | 5 | import logging
|
5 | 6 | import platform
|
6 | 7 | import subprocess
|
@@ -558,6 +559,9 @@ def test_enable_disable_extensions(harness, caplog):
|
558 | 559 | @patch_network_get(private_address="1.1.1.1")
|
559 | 560 | def test_on_start(harness):
|
560 | 561 | with (
|
| 562 | + patch( |
| 563 | + "charm.PostgresqlOperatorCharm._restart_services_after_reboot" |
| 564 | + ) as _restart_services_after_reboot, |
561 | 565 | patch(
|
562 | 566 | "charm.PostgresqlOperatorCharm._set_primary_status_message"
|
563 | 567 | ) as _set_primary_status_message,
|
@@ -617,33 +621,41 @@ def test_on_start(harness):
|
617 | 621 | harness.charm.on.start.emit()
|
618 | 622 | _bootstrap_cluster.assert_called_once()
|
619 | 623 | _oversee_users.assert_not_called()
|
| 624 | + _restart_services_after_reboot.assert_called_once() |
620 | 625 | assert isinstance(harness.model.unit.status, BlockedStatus)
|
621 | 626 | # Set an initial waiting status (like after the install hook was triggered).
|
622 | 627 | harness.model.unit.status = WaitingStatus("fake message")
|
623 | 628 |
|
624 | 629 | # Test the event of an error happening when trying to create the default postgres user.
|
| 630 | + _restart_services_after_reboot.reset_mock() |
625 | 631 | _member_started.return_value = True
|
626 | 632 | harness.charm.on.start.emit()
|
627 | 633 | _postgresql.create_user.assert_called_once()
|
628 | 634 | _oversee_users.assert_not_called()
|
| 635 | + _restart_services_after_reboot.assert_called_once() |
629 | 636 | assert isinstance(harness.model.unit.status, BlockedStatus)
|
630 | 637 |
|
631 | 638 | # Set an initial waiting status again (like after the install hook was triggered).
|
632 | 639 | harness.model.unit.status = WaitingStatus("fake message")
|
633 | 640 |
|
634 | 641 | # Then test the event of a correct cluster bootstrapping.
|
| 642 | + _restart_services_after_reboot.reset_mock() |
635 | 643 | harness.charm.on.start.emit()
|
636 | 644 | assert _postgresql.create_user.call_count == 4 # Considering the previous failed call.
|
637 | 645 | _oversee_users.assert_called_once()
|
638 | 646 | _enable_disable_extensions.assert_called_once()
|
639 | 647 | _set_primary_status_message.assert_called_once()
|
| 648 | + _restart_services_after_reboot.assert_called_once() |
640 | 649 |
|
641 | 650 |
|
642 | 651 | @patch_network_get(private_address="1.1.1.1")
|
643 | 652 | def test_on_start_replica(harness):
|
644 | 653 | with (
|
645 | 654 | patch("charm.snap.SnapCache") as _snap_cache,
|
646 | 655 | patch("charm.Patroni.get_postgresql_version") as _get_postgresql_version,
|
| 656 | + patch( |
| 657 | + "charm.PostgresqlOperatorCharm._restart_services_after_reboot" |
| 658 | + ) as _restart_services_after_reboot, |
647 | 659 | patch("charm.Patroni.configure_patroni_on_unit") as _configure_patroni_on_unit,
|
648 | 660 | patch(
|
649 | 661 | "charm.Patroni.member_started",
|
@@ -674,25 +686,30 @@ def test_on_start_replica(harness):
|
674 | 686 | harness.charm._peers.data[harness.charm.app].update({"cluster_initialised": ""})
|
675 | 687 | harness.charm.on.start.emit()
|
676 | 688 | _defer.assert_called_once()
|
| 689 | + _restart_services_after_reboot.assert_called_once() |
677 | 690 |
|
678 | 691 | # Set an initial waiting status again (like after a machine restart).
|
679 | 692 | harness.model.unit.status = WaitingStatus("fake message")
|
680 | 693 |
|
681 | 694 | # Mark the cluster as initialised and with the workload up and running.
|
| 695 | + _restart_services_after_reboot.reset_mock() |
682 | 696 | harness.charm._peers.data[harness.charm.app].update({"cluster_initialised": "True"})
|
683 | 697 | _member_started.return_value = True
|
684 | 698 | harness.charm.on.start.emit()
|
685 | 699 | _configure_patroni_on_unit.assert_not_called()
|
| 700 | + _restart_services_after_reboot.assert_called_once() |
686 | 701 | assert isinstance(harness.model.unit.status, ActiveStatus)
|
687 | 702 |
|
688 | 703 | # Set an initial waiting status (like after the install hook was triggered).
|
689 | 704 | harness.model.unit.status = WaitingStatus("fake message")
|
690 | 705 |
|
691 | 706 | # Check that the unit status doesn't change when the workload is not running.
|
692 | 707 | # In that situation only Patroni is configured in the unit (but not started).
|
| 708 | + _restart_services_after_reboot.reset_mock() |
693 | 709 | _member_started.return_value = False
|
694 | 710 | harness.charm.on.start.emit()
|
695 | 711 | _configure_patroni_on_unit.assert_called_once()
|
| 712 | + _restart_services_after_reboot.assert_called_once() |
696 | 713 | assert isinstance(harness.model.unit.status, WaitingStatus)
|
697 | 714 |
|
698 | 715 |
|
@@ -2447,70 +2464,104 @@ def test_set_primary_status_message(harness, is_leader):
|
2447 | 2464 | harness.charm._set_primary_status_message()
|
2448 | 2465 | tc.assertIsInstance(harness.charm.unit.status, MaintenanceStatus)
|
2449 | 2466 |
|
2450 |
| - @patch("charm.Patroni.update_patroni_restart_condition") |
2451 |
| - @patch("charm.Patroni.get_patroni_restart_condition") |
2452 |
| - @patch("charm.PostgresqlOperatorCharm._unit_ip") |
2453 |
| - def test_override_patroni_restart_condition( |
2454 |
| - self, _unit_ip, get_restart_condition, update_restart_condition |
| 2467 | + |
| 2468 | +def test_override_patroni_restart_condition(harness): |
| 2469 | + with ( |
| 2470 | + patch("charm.Patroni.update_patroni_restart_condition") as _update_restart_condition, |
| 2471 | + patch("charm.Patroni.get_patroni_restart_condition") as _get_restart_condition, |
| 2472 | + patch("charm.PostgresqlOperatorCharm._unit_ip") as _unit_ip, |
2455 | 2473 | ):
|
2456 |
| - get_restart_condition.return_value = "always" |
| 2474 | + _get_restart_condition.return_value = "always" |
2457 | 2475 |
|
2458 | 2476 | # Do override without repeat_cause
|
2459 |
| - assert self.charm.override_patroni_restart_condition("no") is True |
2460 |
| - get_restart_condition.assert_called_once() |
2461 |
| - update_restart_condition.assert_called_once_with("no") |
2462 |
| - get_restart_condition.reset_mock() |
2463 |
| - update_restart_condition.reset_mock() |
| 2477 | + assert harness.charm.override_patroni_restart_condition("no", None) is True |
| 2478 | + _get_restart_condition.assert_called_once() |
| 2479 | + _update_restart_condition.assert_called_once_with("no") |
| 2480 | + _get_restart_condition.reset_mock() |
| 2481 | + _update_restart_condition.reset_mock() |
2464 | 2482 |
|
2465 |
| - get_restart_condition.return_value = "no" |
| 2483 | + _get_restart_condition.return_value = "no" |
2466 | 2484 |
|
2467 | 2485 | # Must not be overridden twice without repeat_cause
|
2468 |
| - assert self.charm.override_patroni_restart_condition("on-failure") is False |
2469 |
| - get_restart_condition.assert_called_once() |
2470 |
| - update_restart_condition.assert_not_called() |
2471 |
| - get_restart_condition.reset_mock() |
2472 |
| - update_restart_condition.reset_mock() |
| 2486 | + assert harness.charm.override_patroni_restart_condition("on-failure", None) is False |
| 2487 | + _get_restart_condition.assert_called_once() |
| 2488 | + _update_restart_condition.assert_not_called() |
| 2489 | + _get_restart_condition.reset_mock() |
| 2490 | + _update_restart_condition.reset_mock() |
2473 | 2491 |
|
2474 | 2492 | # Reset override
|
2475 |
| - self.charm.restore_patroni_restart_condition() |
2476 |
| - update_restart_condition.assert_called_once_with("always") |
2477 |
| - update_restart_condition.reset_mock() |
| 2493 | + harness.charm.restore_patroni_restart_condition() |
| 2494 | + _update_restart_condition.assert_called_once_with("always") |
| 2495 | + _update_restart_condition.reset_mock() |
2478 | 2496 |
|
2479 | 2497 | # Must not be reset twice
|
2480 |
| - self.charm.restore_patroni_restart_condition() |
2481 |
| - update_restart_condition.assert_not_called() |
2482 |
| - update_restart_condition.reset_mock() |
| 2498 | + harness.charm.restore_patroni_restart_condition() |
| 2499 | + _update_restart_condition.assert_not_called() |
| 2500 | + _update_restart_condition.reset_mock() |
2483 | 2501 |
|
2484 |
| - get_restart_condition.return_value = "always" |
| 2502 | + _get_restart_condition.return_value = "always" |
2485 | 2503 |
|
2486 | 2504 | # Do override with repeat_cause
|
2487 |
| - assert self.charm.override_patroni_restart_condition("no", "test_charm") is True |
2488 |
| - get_restart_condition.assert_called_once() |
2489 |
| - update_restart_condition.assert_called_once_with("no") |
2490 |
| - get_restart_condition.reset_mock() |
2491 |
| - update_restart_condition.reset_mock() |
| 2505 | + assert harness.charm.override_patroni_restart_condition("no", "test_charm") is True |
| 2506 | + _get_restart_condition.assert_called_once() |
| 2507 | + _update_restart_condition.assert_called_once_with("no") |
| 2508 | + _get_restart_condition.reset_mock() |
| 2509 | + _update_restart_condition.reset_mock() |
2492 | 2510 |
|
2493 |
| - get_restart_condition.return_value = "no" |
| 2511 | + _get_restart_condition.return_value = "no" |
2494 | 2512 |
|
2495 | 2513 | # Do re-override with repeat_cause
|
2496 |
| - assert self.charm.override_patroni_restart_condition("on-success", "test_charm") is True |
2497 |
| - get_restart_condition.assert_called_once() |
2498 |
| - update_restart_condition.assert_called_once_with("on-success") |
2499 |
| - get_restart_condition.reset_mock() |
2500 |
| - update_restart_condition.reset_mock() |
| 2514 | + assert harness.charm.override_patroni_restart_condition("on-success", "test_charm") is True |
| 2515 | + _get_restart_condition.assert_called_once() |
| 2516 | + _update_restart_condition.assert_called_once_with("on-success") |
| 2517 | + _get_restart_condition.reset_mock() |
| 2518 | + _update_restart_condition.reset_mock() |
2501 | 2519 |
|
2502 |
| - get_restart_condition.return_value = "on-success" |
| 2520 | + _get_restart_condition.return_value = "on-success" |
2503 | 2521 |
|
2504 | 2522 | # Must not be re-overridden with different repeat_cause
|
2505 | 2523 | assert (
|
2506 |
| - self.charm.override_patroni_restart_condition("on-failure", "test_not_charm") is False |
| 2524 | + harness.charm.override_patroni_restart_condition("on-failure", "test_not_charm") |
| 2525 | + is False |
2507 | 2526 | )
|
2508 |
| - get_restart_condition.assert_called_once() |
2509 |
| - update_restart_condition.assert_not_called() |
2510 |
| - get_restart_condition.reset_mock() |
2511 |
| - update_restart_condition.reset_mock() |
| 2527 | + _get_restart_condition.assert_called_once() |
| 2528 | + _update_restart_condition.assert_not_called() |
| 2529 | + _get_restart_condition.reset_mock() |
| 2530 | + _update_restart_condition.reset_mock() |
2512 | 2531 |
|
2513 | 2532 | # Reset override
|
2514 |
| - self.charm.restore_patroni_restart_condition() |
2515 |
| - update_restart_condition.assert_called_once_with("always") |
2516 |
| - update_restart_condition.reset_mock() |
| 2533 | + harness.charm.restore_patroni_restart_condition() |
| 2534 | + _update_restart_condition.assert_called_once_with("always") |
| 2535 | + _update_restart_condition.reset_mock() |
| 2536 | + |
| 2537 | + |
| 2538 | +def test_restart_services_after_reboot(harness): |
| 2539 | + with ( |
| 2540 | + patch( |
| 2541 | + "backups.PostgreSQLBackups.start_stop_pgbackrest_service" |
| 2542 | + ) as _start_stop_pgbackrest_service, |
| 2543 | + patch("charm.Patroni.start_patroni") as _start_patroni, |
| 2544 | + patch( |
| 2545 | + "charm.PostgresqlOperatorCharm._unit_ip", |
| 2546 | + new_callable=PropertyMock(return_value="1.1.1.1"), |
| 2547 | + ) as _unit_ip, |
| 2548 | + ): |
| 2549 | + with harness.hooks_disabled(): |
| 2550 | + harness.update_relation_data( |
| 2551 | + harness.model.get_relation(PEER).id, |
| 2552 | + harness.charm.app.name, |
| 2553 | + {"members_ips": json.dumps([])}, |
| 2554 | + ) |
| 2555 | + harness.charm._restart_services_after_reboot() |
| 2556 | + _start_patroni.assert_not_called() |
| 2557 | + _start_stop_pgbackrest_service.assert_not_called() |
| 2558 | + |
| 2559 | + with harness.hooks_disabled(): |
| 2560 | + harness.update_relation_data( |
| 2561 | + harness.model.get_relation(PEER).id, |
| 2562 | + harness.charm.app.name, |
| 2563 | + {"members_ips": json.dumps([_unit_ip])}, |
| 2564 | + ) |
| 2565 | + harness.charm._restart_services_after_reboot() |
| 2566 | + _start_patroni.assert_called_once() |
| 2567 | + _start_stop_pgbackrest_service.assert_called_once() |
0 commit comments