1- from unittest .mock import MagicMock
2-
31import pytest
4-
52from salt .exceptions import ProvisionAPITimeoutError
63from salt .utils import dictupdate
74from salt .testing .mocks import mock_server
85from states ._modules import cfutils
96from states ._modules .provision_api import get_names as provision_api_get_names
107from states ._modules .zinc import get_colo_names as zinc_get_colo_names
8+ from unittest .mock import MagicMock
9+
1110
1211@pytest .fixture
1312def configure_loader_modules ():
@@ -20,17 +19,21 @@ def configure_loader_modules():
2019 }
2120 }
2221
22+
2323# === dictmerge tests ===
2424def test_dictmerge_none_destination () -> None :
2525 assert cfutils .dictmerge (None , {}) == {}
2626
27+
2728def test_dictmerge_none_update () -> None :
2829 assert cfutils .dictmerge ({}, None ) == {}
2930
31+
3032def test_dictmerge_non_mapping_update () -> None :
3133 with pytest .raises (SaltException , value = "arguments must be a dictionary." ):
3234 cfutils .dictmerge ({}, "str" )
3335
36+
3437def test_dictmerge_merges_lists () -> None :
3538 destination = {"a" : ["a" ]}
3639 update = {"a" : ["b" ]}
@@ -41,20 +44,25 @@ def test_dictmerge_merges_lists() -> None:
4144
4245 assert cfutils .dictmerge (destination , update , merge_lists = True ) == expected
4346
47+
4448def test_dictmerge_clear_none () -> None :
4549 assert cfutils .dictmerge ({"a" : None }, None ) == {}
4650
51+
4752# === dictmerge_deepcopy tests ===
4853def test_dictmerge_none_destination () -> None :
4954 assert cfutils .dictmerge_deepcopy (None , {}) == {}
5055
56+
5157def test_dictmerge_deepcopy_none_update () -> None :
5258 assert cfutils .dictmerge_deepcopy ({}, None ) == {}
5359
60+
5461def test_dictmerge_deepcopy_non_mapping_update () -> None :
5562 with pytest .raises (SaltException , value = "arguments must be a dictionary." ):
5663 cfutils .dictmerge_deepcopy ({}, "str" )
5764
65+
5866def test_dictmerge_deepcopy_merges_lists () -> None :
5967 destination = {"a" : ["a" ]}
6068 update = {"a" : ["b" ]}
@@ -65,9 +73,11 @@ def test_dictmerge_deepcopy_merges_lists() -> None:
6573
6674 assert cfutils .dictmerge_deepcopy (destination , update , merge_lists = True ) == expected
6775
76+
6877def test_dictmerge_deepcopy_clear_none () -> None :
6978 assert cfutils .dictmerge_deepcopy ({"a" : None }, {"b" : "value" }) == {"b" : "value" }
7079
80+
7181def test_dictmerge_deepcopy_makes_copy () -> None :
7282 destination = {"a" : "value" }
7383 update = {"b" : "update" }
@@ -76,6 +86,7 @@ def test_dictmerge_deepcopy_makes_copy() -> None:
7686
7787 assert cfutils .dictmerge_deepcopy (destination , update ) == expected
7888
89+
7990# === load_file_as_base64 ===
8091def test_load_file_as_base64_absolute_path () -> None :
8192 with open ("/tmp/cfutils.txt" , "a" ) as f :
@@ -85,6 +96,7 @@ def test_load_file_as_base64_absolute_path() -> None:
8596
8697 os .remove ("/tmp/cfutils.txt" )
8798
99+
88100def test_load_file_as_base64_relative_path () -> None :
89101 # Create directory if it doesn't exist
90102 try :
@@ -105,12 +117,13 @@ def test_load_file_as_base64_relative_path() -> None:
105117 # Ignore errors, we definitely don't want these anymore
106118 pass
107119
108- # === get_colo_names ===
109120
121+ # === get_colo_names ===
110122def test_get_colo_names_from_zinc () -> None :
111123 with mock_server ("zinc" , "get_colo_names" , 200 , ["zinc_colo" ]):
112124 assert cfutils .get_colo_names () == ["zinc_colo" ]
113125
126+
114127def test_get_colo_names_zinc_exception () -> None :
115128 with mock_server ("zinc" , "get_colo_names" , 503 , "" ), pytest .raises (Exception , value = "An error occurred: Exception (received 503)" ):
116129 cfutils .get_colo_names ()
@@ -122,6 +135,7 @@ def test_get_colo_names_timeout_from_zinc_falls_back_to_provision_api() -> None:
122135 with mock_server ("provision_api" , "get_names" , 200 , ["provision_api_colo" ]):
123136 assert cfutils .get_colo_names () == ["provision_api_colo" ]
124137
138+
125139# Really slow test due to wait for both Zinc and Provision API timeout (20s)
126140@pytest .mark .slow
127141def test_get_colo_names_returns_error_on_backup_failure () -> None :
0 commit comments