@@ -50,6 +50,33 @@ def test_stanza_name(harness):
50
50
)
51
51
52
52
53
+ def test_tls_ca_chain_filename (harness ):
54
+ # Test when the TLS CA chain is not available.
55
+ tc .assertEqual (
56
+ harness .charm .backup ._tls_ca_chain_filename ,
57
+ "" ,
58
+ )
59
+
60
+ # Test when the TLS CA chain is available.
61
+ with harness .hooks_disabled ():
62
+ remote_application = "s3-integrator"
63
+ s3_rel_id = harness .add_relation (S3_PARAMETERS_RELATION , remote_application )
64
+ harness .update_relation_data (
65
+ s3_rel_id ,
66
+ remote_application ,
67
+ {
68
+ "bucket" : "fake-bucket" ,
69
+ "access-key" : "fake-access-key" ,
70
+ "secret-key" : "fake-secret-key" ,
71
+ "tls-ca-chain" : '["fake-tls-ca-chain"]' ,
72
+ },
73
+ )
74
+ tc .assertEqual (
75
+ harness .charm .backup ._tls_ca_chain_filename ,
76
+ "/var/snap/charmed-postgresql/common/pgbackrest-tls-ca-chain.crt" ,
77
+ )
78
+
79
+
53
80
def test_are_backup_settings_ok (harness ):
54
81
# Test without S3 relation.
55
82
tc .assertEqual (
@@ -401,9 +428,17 @@ def test_construct_endpoint(harness):
401
428
)
402
429
403
430
404
- def test_create_bucket_if_not_exists (harness ):
431
+ @pytest .mark .parametrize (
432
+ "tls_ca_chain_filename" ,
433
+ ["" , "/var/snap/charmed-postgresql/common/pgbackrest-tls-ca-chain.crt" ],
434
+ )
435
+ def test_create_bucket_if_not_exists (harness , tls_ca_chain_filename ):
405
436
with (
406
437
patch ("boto3.session.Session.resource" ) as _resource ,
438
+ patch (
439
+ "charm.PostgreSQLBackups._tls_ca_chain_filename" ,
440
+ new_callable = PropertyMock (return_value = tls_ca_chain_filename ),
441
+ ) as _tls_ca_chain_filename ,
407
442
patch ("charm.PostgreSQLBackups._retrieve_s3_parameters" ) as _retrieve_s3_parameters ,
408
443
):
409
444
# Test when there are missing S3 parameters.
@@ -427,11 +462,15 @@ def test_create_bucket_if_not_exists(harness):
427
462
harness .charm .backup ._create_bucket_if_not_exists ()
428
463
429
464
# Test when the bucket already exists.
465
+ _resource .reset_mock ()
430
466
_resource .side_effect = None
431
467
head_bucket = _resource .return_value .Bucket .return_value .meta .client .head_bucket
432
468
create = _resource .return_value .Bucket .return_value .create
433
469
wait_until_exists = _resource .return_value .Bucket .return_value .wait_until_exists
434
470
harness .charm .backup ._create_bucket_if_not_exists ()
471
+ _resource .assert_called_once_with (
472
+ "s3" , endpoint_url = "test-endpoint" , verify = (tls_ca_chain_filename or None )
473
+ )
435
474
head_bucket .assert_called_once ()
436
475
create .assert_not_called ()
437
476
wait_until_exists .assert_not_called ()
@@ -1482,9 +1521,17 @@ def test_pre_restore_checks(harness):
1482
1521
1483
1522
1484
1523
@patch_network_get (private_address = "1.1.1.1" )
1485
- def test_render_pgbackrest_conf_file (harness ):
1524
+ @pytest .mark .parametrize (
1525
+ "tls_ca_chain_filename" ,
1526
+ ["" , "/var/snap/charmed-postgresql/common/pgbackrest-tls-ca-chain.crt" ],
1527
+ )
1528
+ def test_render_pgbackrest_conf_file (harness , tls_ca_chain_filename ):
1486
1529
with (
1487
1530
patch ("charm.Patroni.render_file" ) as _render_file ,
1531
+ patch (
1532
+ "charm.PostgreSQLBackups._tls_ca_chain_filename" ,
1533
+ new_callable = PropertyMock (return_value = tls_ca_chain_filename ),
1534
+ ) as _tls_ca_chain_filename ,
1488
1535
patch ("charm.PostgreSQLBackups._retrieve_s3_parameters" ) as _retrieve_s3_parameters ,
1489
1536
):
1490
1537
# Set up a mock for the `open` method, set returned data to postgresql.conf template.
@@ -1513,6 +1560,7 @@ def test_render_pgbackrest_conf_file(harness):
1513
1560
"region" : "us-east-1" ,
1514
1561
"s3-uri-style" : "path" ,
1515
1562
"delete-older-than-days" : "30" ,
1563
+ "tls-ca-chain" : (["fake-tls-ca-chain" ] if tls_ca_chain_filename != "" else "" ),
1516
1564
},
1517
1565
[],
1518
1566
)
@@ -1531,6 +1579,7 @@ def test_render_pgbackrest_conf_file(harness):
1531
1579
endpoint = "https://storage.googleapis.com" ,
1532
1580
bucket = "test-bucket" ,
1533
1581
s3_uri_style = "path" ,
1582
+ tls_ca_chain = (tls_ca_chain_filename or "" ),
1534
1583
access_key = "test-access-key" ,
1535
1584
secret_key = "test-secret-key" ,
1536
1585
stanza = harness .charm .backup .stanza_name ,
@@ -1548,11 +1597,16 @@ def test_render_pgbackrest_conf_file(harness):
1548
1597
tc .assertEqual (mock .call_args_list [0 ][0 ], ("templates/pgbackrest.conf.j2" , "r" ))
1549
1598
1550
1599
# Ensure the correct rendered template is sent to _render_file method.
1551
- _render_file .assert_called_once_with (
1552
- "/var/snap/charmed-postgresql/current/etc/pgbackrest/pgbackrest.conf" ,
1553
- expected_content ,
1554
- 0o644 ,
1555
- )
1600
+ calls = [
1601
+ call (
1602
+ "/var/snap/charmed-postgresql/current/etc/pgbackrest/pgbackrest.conf" ,
1603
+ expected_content ,
1604
+ 0o644 ,
1605
+ )
1606
+ ]
1607
+ if tls_ca_chain_filename != "" :
1608
+ calls .insert (0 , call (tls_ca_chain_filename , "fake-tls-ca-chain" , 0o644 ))
1609
+ _render_file .assert_has_calls (calls )
1556
1610
1557
1611
1558
1612
@patch_network_get (private_address = "1.1.1.1" )
@@ -1737,11 +1791,19 @@ def test_start_stop_pgbackrest_service(harness):
1737
1791
restart .assert_called_once ()
1738
1792
1739
1793
1740
- def test_upload_content_to_s3 (harness ):
1794
+ @pytest .mark .parametrize (
1795
+ "tls_ca_chain_filename" ,
1796
+ ["" , "/var/snap/charmed-postgresql/common/pgbackrest-tls-ca-chain.crt" ],
1797
+ )
1798
+ def test_upload_content_to_s3 (harness , tls_ca_chain_filename ):
1741
1799
with (
1742
1800
patch ("tempfile.NamedTemporaryFile" ) as _named_temporary_file ,
1743
1801
patch ("charm.PostgreSQLBackups._construct_endpoint" ) as _construct_endpoint ,
1744
1802
patch ("boto3.session.Session.resource" ) as _resource ,
1803
+ patch (
1804
+ "charm.PostgreSQLBackups._tls_ca_chain_filename" ,
1805
+ new_callable = PropertyMock (return_value = tls_ca_chain_filename ),
1806
+ ) as _tls_ca_chain_filename ,
1745
1807
):
1746
1808
# Set some parameters.
1747
1809
content = "test-content"
@@ -1764,7 +1826,11 @@ def test_upload_content_to_s3(harness):
1764
1826
harness .charm .backup ._upload_content_to_s3 (content , s3_path , s3_parameters ),
1765
1827
False ,
1766
1828
)
1767
- _resource .assert_called_once_with ("s3" , endpoint_url = "https://s3.us-east-1.amazonaws.com" )
1829
+ _resource .assert_called_once_with (
1830
+ "s3" ,
1831
+ endpoint_url = "https://s3.us-east-1.amazonaws.com" ,
1832
+ verify = (tls_ca_chain_filename or None ),
1833
+ )
1768
1834
_named_temporary_file .assert_not_called ()
1769
1835
upload_file .assert_not_called ()
1770
1836
@@ -1775,7 +1841,11 @@ def test_upload_content_to_s3(harness):
1775
1841
harness .charm .backup ._upload_content_to_s3 (content , s3_path , s3_parameters ),
1776
1842
False ,
1777
1843
)
1778
- _resource .assert_called_once_with ("s3" , endpoint_url = "https://s3.us-east-1.amazonaws.com" )
1844
+ _resource .assert_called_once_with (
1845
+ "s3" ,
1846
+ endpoint_url = "https://s3.us-east-1.amazonaws.com" ,
1847
+ verify = (tls_ca_chain_filename or None ),
1848
+ )
1779
1849
_named_temporary_file .assert_called_once ()
1780
1850
upload_file .assert_called_once_with ("/tmp/test-file" , "test-path/test-file." )
1781
1851
@@ -1788,6 +1858,10 @@ def test_upload_content_to_s3(harness):
1788
1858
harness .charm .backup ._upload_content_to_s3 (content , s3_path , s3_parameters ),
1789
1859
True ,
1790
1860
)
1791
- _resource .assert_called_once_with ("s3" , endpoint_url = "https://s3.us-east-1.amazonaws.com" )
1861
+ _resource .assert_called_once_with (
1862
+ "s3" ,
1863
+ endpoint_url = "https://s3.us-east-1.amazonaws.com" ,
1864
+ verify = (tls_ca_chain_filename or None ),
1865
+ )
1792
1866
_named_temporary_file .assert_called_once ()
1793
1867
upload_file .assert_called_once_with ("/tmp/test-file" , "test-path/test-file." )
0 commit comments