Skip to content

Commit 4fa8730

Browse files
committed
Fix colo code
1 parent e244233 commit 4fa8730

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

states/_modules/cfutils.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import copy
33
import logging
44
import os
5-
65
from salt.exceptions import SaltException, ZincTimeoutError
76
from salt.utils import dictupdate
87

9-
log = logging.getLogger(__name__)
8+
logger = logging.getLogger(__name__)
109

1110

1211
def dictmerge(destination: dict | None, update: dict | None, clear_none=False: bool, merge_lists=False: bool) -> dict | None:
@@ -53,36 +52,35 @@ def dictmerge_deepcopy(destination: dict | None, update: dict | None, clear_none
5352
return destination_copy
5453

5554

56-
def load_file_as_base64(path: str) -> bytes:
57-
"""
58-
Read arbitrary file, and return its content as base64
59-
This module exists to allow including raw config files in pillar
60-
:param path: path of the file
61-
:return: base64 string
62-
"""
63-
64-
if path[0] != "/":
65-
path = "/etc/salt/data/" + path
66-
67-
with open(path, "rb") as f:
68-
return base64.b64encode(f.read())
69-
70-
7155
def get_colo_names(timeout=10: int, backup=True: bool) -> list[str]:
7256
"""
7357
Gets colo names using systems
74-
75-
:param backup (boolean)
7658
:param timeout (int)
59+
:param backup (boolean)
7760
"""
7861

7962
try:
8063
return __salt__["zinc.get_colo_names"](timeout=(timeout * 1000))
8164
except ZincTimeoutError:
82-
pass
65+
if backup:
66+
return __salt__["provision_api.get_names"](type="colo", timeout=timeout)
67+
else:
68+
[]
8369
except Exception as e:
84-
log.error(f"An error occurred: {e}")
70+
logger.error(f"An error occurred: {e}")
8571
return []
8672

87-
if backup:
88-
return __salt__["provision_api.get_names"](type="colo", timeout=timeout)
73+
74+
def load_file_as_base64(path: str) -> bytes:
75+
"""
76+
Read arbitrary file, and return its content as base64
77+
This module exists to allow including raw config files in pillar
78+
:param path: path of the file
79+
:return: base64 string
80+
"""
81+
82+
if path[0] != "/":
83+
path = "/etc/salt/data/" + path
84+
85+
with open(path, "rb") as f:
86+
return base64.b64encode(f.read())

tests/pytests/unit/_modules/test_cfutils.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from unittest.mock import MagicMock
2-
31
import pytest
4-
52
from salt.exceptions import ProvisionAPITimeoutError
63
from salt.utils import dictupdate
74
from salt.testing.mocks import mock_server
85
from states._modules import cfutils
96
from states._modules.provision_api import get_names as provision_api_get_names
107
from states._modules.zinc import get_colo_names as zinc_get_colo_names
8+
from unittest.mock import MagicMock
9+
1110

1211
@pytest.fixture
1312
def configure_loader_modules():
@@ -20,17 +19,21 @@ def configure_loader_modules():
2019
}
2120
}
2221

22+
2323
# === dictmerge tests ===
2424
def test_dictmerge_none_destination() -> None:
2525
assert cfutils.dictmerge(None, {}) == {}
2626

27+
2728
def test_dictmerge_none_update() -> None:
2829
assert cfutils.dictmerge({}, None) == {}
2930

31+
3032
def test_dictmerge_non_mapping_update() -> None:
3133
with pytest.raises(SaltException, value = "arguments must be a dictionary."):
3234
cfutils.dictmerge({}, "str")
3335

36+
3437
def 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+
4448
def test_dictmerge_clear_none() -> None:
4549
assert cfutils.dictmerge({"a": None}, None) == {}
4650

51+
4752
# === dictmerge_deepcopy tests ===
4853
def test_dictmerge_none_destination() -> None:
4954
assert cfutils.dictmerge_deepcopy(None, {}) == {}
5055

56+
5157
def test_dictmerge_deepcopy_none_update() -> None:
5258
assert cfutils.dictmerge_deepcopy({}, None) == {}
5359

60+
5461
def 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+
5866
def 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+
6877
def test_dictmerge_deepcopy_clear_none() -> None:
6978
assert cfutils.dictmerge_deepcopy({"a": None}, {"b": "value"}) == {"b": "value"}
7079

80+
7181
def 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 ===
8091
def 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+
88100
def 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 ===
110122
def 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+
114127
def 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
127141
def test_get_colo_names_returns_error_on_backup_failure() -> None:

0 commit comments

Comments
 (0)