1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
import json
import pathlib
import unittest
from typing import Any, Optional
from unittest import mock
from unittest.mock import mock_open, patch
from dataclasses import dataclass
from podman import api
class TestUtilsCase(unittest.TestCase):
def test_format_filters(self):
@dataclass
class TestCase:
name: str
input: Any
expected: Optional[str]
cases = [
TestCase(name="empty str", input="", expected=None),
TestCase(name="str", input="reference=fedora", expected='{"reference": ["fedora"]}'),
TestCase(
name="list[str]", input=["reference=fedora"], expected='{"reference": ["fedora"]}'
),
TestCase(
name="dict[str,str]",
input={"reference": "fedora"},
expected='{"reference": ["fedora"]}',
),
]
for case in cases:
actual = api.prepare_filters(case.input)
self.assertEqual(
case.expected,
actual,
f"failed test {case.name} expected {case.expected}, actual {actual}",
)
if actual is not None:
self.assertIsInstance(actual, str)
def test_containerignore_404(self):
actual = api.prepare_containerignore("/does/not/exists")
self.assertListEqual([], actual)
@patch.object(pathlib.Path, "exists", return_value=True)
def test_containerignore_read(self, patch_exists):
data = r"""# unittest
#Ignore the logs directory
logs/
#Ignoring the password file
passwords.txt
#Ignoring git and cache folders
.git
.cache
#Ignoring all the markdown and class files
*.md
**/*.class
"""
with mock.patch("pathlib.Path.open", mock_open(read_data=data)):
actual = api.prepare_containerignore(".")
self.assertListEqual(
actual, ["logs/", "passwords.txt", ".git", ".cache", "*.md", "**/*.class"]
)
patch_exists.assert_called_once_with()
@patch.object(pathlib.Path, "exists", return_value=True)
def test_containerignore_empty(self, patch_exists):
data = r"""# unittest
"""
patch_exists.return_value = True
with mock.patch("pathlib.Path.open", mock_open(read_data=data)):
actual = api.prepare_containerignore(".")
self.assertListEqual(actual, [])
patch_exists.assert_called_once_with()
@mock.patch("pathlib.Path.parent", autospec=True)
def test_containerfile_1(self, mock_parent):
mock_parent.samefile.return_value = True
actual = api.prepare_containerfile("/work", "/work/Dockerfile")
self.assertEqual(actual, "Dockerfile")
mock_parent.samefile.assert_called()
@mock.patch("pathlib.Path.parent", autospec=True)
def test_containerfile_2(self, mock_parent):
mock_parent.samefile.return_value = True
actual = api.prepare_containerfile(".", "Dockerfile")
self.assertEqual(actual, "Dockerfile")
mock_parent.samefile.assert_called()
@mock.patch("shutil.copy2")
def test_containerfile_copy(self, mock_copy):
mock_copy.return_value = None
with mock.patch.object(pathlib.Path, "parent") as mock_parent:
mock_parent.samefile.return_value = False
actual = api.prepare_containerfile("/work", "/home/Dockerfile")
self.assertRegex(actual, r"\.containerfile\..*")
def test_prepare_body_all_types(self):
payload = {
"String": "string",
"Integer": 42,
"Boolean": True,
"Dictionary": {"key": "value"},
"Tuple": (1, 2),
"List": [1, 2],
}
actual = api.prepare_body(payload)
self.assertEqual(actual, json.dumps(payload, sort_keys=True))
def test_prepare_body_none(self):
payload = {
"String": "",
"Integer": None,
"Boolean": False,
"Dictionary": dict(),
"Tuple": tuple(),
"List": list(),
}
actual = api.prepare_body(payload)
self.assertEqual(actual, '{"Boolean": false}')
def test_prepare_body_embedded(self):
payload = {
"String": "",
"Integer": None,
"Boolean": False,
"Dictionary": {"key": "value"},
"Dictionary2": {"key": {"key2": None}},
"Tuple": tuple(),
"List": [None],
"Set1": {"item1", "item2"},
"Set2": {None},
}
actual = api.prepare_body(payload)
actual_dict = json.loads(actual)
# Because of the sets above we have to do some type dances to test results
self.assertListEqual([*actual_dict], ["Boolean", "Dictionary", "Set1"])
self.assertEqual(actual_dict["Boolean"], payload["Boolean"])
self.assertDictEqual(actual_dict["Dictionary"], payload["Dictionary"])
self.assertEqual(set(actual_dict["Set1"]), {"item1", "item2"})
def test_prepare_body_dict_empty_string(self):
payload = {"Dictionary": {"key1": "", "key2": {"key3": ""}, "key4": [], "key5": {}}}
actual = api.prepare_body(payload)
actual_dict = json.loads(actual)
payload["Dictionary"].pop("key4")
payload["Dictionary"].pop("key5")
self.assertDictEqual(payload, actual_dict)
if __name__ == '__main__':
unittest.main()
|