10
10
from charms .operator_libs_linux .v2 import snap
11
11
from jinja2 import Template
12
12
from ops .testing import Harness
13
- from tenacity import stop_after_delay
13
+ from tenacity import RetryError , stop_after_delay , wait_fixed
14
14
15
15
from charm import PostgresqlOperatorCharm
16
16
from cluster import Patroni
@@ -429,7 +429,9 @@ def test_switchover(peers_ips, patroni):
429
429
430
430
431
431
def test_update_synchronous_node_count (peers_ips , patroni ):
432
- with patch ("requests.patch" ) as _patch :
432
+ with patch ("cluster.stop_after_delay" , return_value = stop_after_delay (0 )) as _wait_fixed , patch (
433
+ "cluster.wait_fixed" , return_value = wait_fixed (0 )
434
+ ) as _wait_fixed , patch ("requests.patch" ) as _patch :
433
435
response = _patch .return_value
434
436
response .status_code = 200
435
437
@@ -439,6 +441,11 @@ def test_update_synchronous_node_count(peers_ips, patroni):
439
441
"http://1.1.1.1:8008/config" , json = {"synchronous_node_count" : 0 }, verify = True
440
442
)
441
443
444
+ # Test when the request fails.
445
+ response .status_code = 500
446
+ with tc .assertRaises (RetryError ):
447
+ patroni .update_synchronous_node_count ()
448
+
442
449
443
450
def test_configure_patroni_on_unit (peers_ips , patroni ):
444
451
with (
@@ -542,3 +549,69 @@ def test_member_inactive_error(peers_ips, patroni):
542
549
assert patroni .member_inactive
543
550
544
551
_get .assert_called_once_with ("http://1.1.1.1:8008/health" , verify = True , timeout = 5 )
552
+
553
+
554
+ def test_patroni_logs (patroni ):
555
+ with patch ("charm.snap.SnapCache" ) as _snap_cache :
556
+ # Test when the logs are returned successfully.
557
+ logs = _snap_cache .return_value .__getitem__ .return_value .logs
558
+ logs .return_value = "fake-logs"
559
+ assert patroni .patroni_logs () == "fake-logs"
560
+
561
+ # Test the charm fails to get the logs.
562
+ logs .side_effect = snap .SnapError
563
+ assert patroni .patroni_logs () == ""
564
+
565
+
566
+ def test_last_postgresql_logs (patroni ):
567
+ with patch ("glob.glob" ) as _glob , patch (
568
+ "builtins.open" , mock_open (read_data = "fake-logs" )
569
+ ) as _open :
570
+ # Test when there are no files to read.
571
+ assert patroni .last_postgresql_logs () == ""
572
+ _open .assert_not_called ()
573
+
574
+ # Test when there are multiple files in the logs directory.
575
+ _glob .return_value = [
576
+ "/var/snap/charmed-postgresql/common/var/log/postgresql/postgresql.log.1" ,
577
+ "/var/snap/charmed-postgresql/common/var/log/postgresql/postgresql.log.2" ,
578
+ "/var/snap/charmed-postgresql/common/var/log/postgresql/postgresql.log.3" ,
579
+ ]
580
+ assert patroni .last_postgresql_logs () == "fake-logs"
581
+ _open .assert_called_once_with (
582
+ "/var/snap/charmed-postgresql/common/var/log/postgresql/postgresql.log.3" , "r"
583
+ )
584
+
585
+ # Test when the charm fails to read the logs.
586
+ _open .reset_mock ()
587
+ _open .side_effect = OSError
588
+ assert patroni .last_postgresql_logs () == ""
589
+ _open .assert_called_with (
590
+ "/var/snap/charmed-postgresql/common/var/log/postgresql/postgresql.log.3" , "r"
591
+ )
592
+
593
+
594
+ def test_get_patroni_restart_condition (patroni ):
595
+ mock = mock_open ()
596
+ with patch ("builtins.open" , mock ) as _open :
597
+ # Test when there is a restart condition set.
598
+ _open .return_value .__enter__ .return_value .read .return_value = "Restart=always"
599
+ assert patroni .get_patroni_restart_condition () == "always"
600
+
601
+ # Test when there is no restart condition set.
602
+ _open .return_value .__enter__ .return_value .read .return_value = ""
603
+ with tc .assertRaises (RuntimeError ):
604
+ patroni .get_patroni_restart_condition ()
605
+
606
+
607
+ @pytest .mark .parametrize ("new_restart_condition" , ["on-success" , "on-failure" ])
608
+ def test_update_patroni_restart_condition (patroni , new_restart_condition ):
609
+ with patch ("builtins.open" , mock_open (read_data = "Restart=always" )) as _open , patch (
610
+ "subprocess.run"
611
+ ) as _run :
612
+ _open .return_value .__enter__ .return_value .read .return_value = "Restart=always"
613
+ patroni .update_patroni_restart_condition (new_restart_condition )
614
+ _open .return_value .__enter__ .return_value .write .assert_called_once_with (
615
+ f"Restart={ new_restart_condition } "
616
+ )
617
+ _run .assert_called_once_with (["/bin/systemctl" , "daemon-reload" ])
0 commit comments