|
| 1 | +import click |
| 2 | +import mock |
| 3 | +import pytest |
| 4 | +import pdb |
| 5 | +import subprocess |
| 6 | + |
| 7 | +from sonic_installer.main import SWAPAllocator |
| 8 | + |
| 9 | + |
| 10 | +class TestSWAPAllocator(object): |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def setup(cls): |
| 14 | + print("SETUP") |
| 15 | + |
| 16 | + def test_read_from_meminfo(self): |
| 17 | + proc_meminfo_lines = [ |
| 18 | + "MemTotal: 32859496 kB", |
| 19 | + "MemFree: 16275512 kB", |
| 20 | + "HugePages_Total: 0", |
| 21 | + "HugePages_Free: 0", |
| 22 | + ] |
| 23 | + |
| 24 | + read_meminfo_expected_return = { |
| 25 | + "MemTotal": 32859496, |
| 26 | + "MemFree": 16275512, |
| 27 | + "HugePages_Total": 0, |
| 28 | + "HugePages_Free": 0 |
| 29 | + } |
| 30 | + |
| 31 | + with mock.patch("builtins.open") as mock_open: |
| 32 | + pseudo_fd = mock.MagicMock() |
| 33 | + pseudo_fd.readlines = mock.MagicMock(return_value=proc_meminfo_lines) |
| 34 | + mock_open.return_value.__enter__.return_value = pseudo_fd |
| 35 | + read_meminfo_actual_return = SWAPAllocator.read_from_meminfo() |
| 36 | + assert read_meminfo_actual_return == read_meminfo_expected_return |
| 37 | + |
| 38 | + def test_setup_swapmem(self): |
| 39 | + with mock.patch("builtins.open") as mock_open, \ |
| 40 | + mock.patch("os.posix_fallocate") as mock_fallocate, \ |
| 41 | + mock.patch("os.chmod") as mock_chmod, \ |
| 42 | + mock.patch("sonic_installer.main.run_command") as mock_run: |
| 43 | + pseudo_fd = mock.MagicMock() |
| 44 | + pseudo_fd_fileno = 10 |
| 45 | + pseudo_fd.fileno.return_value = pseudo_fd_fileno |
| 46 | + mock_open.return_value.__enter__.return_value = pseudo_fd |
| 47 | + |
| 48 | + swap_mem_size_in_mib = 2048 * 1024 |
| 49 | + expected_swap_mem_size_in_bytes = swap_mem_size_in_mib * 1024 * 1024 |
| 50 | + expected_swapfile_location = SWAPAllocator.SWAP_FILE_PATH |
| 51 | + expected_swapfile_permission = 0o600 |
| 52 | + swap_allocator = SWAPAllocator(allocate=True, swap_mem_size=swap_mem_size_in_mib) |
| 53 | + swap_allocator.setup_swapmem() |
| 54 | + |
| 55 | + mock_fallocate.assert_called_once_with(pseudo_fd_fileno, 0, expected_swap_mem_size_in_bytes) |
| 56 | + mock_chmod.assert_called_once_with(expected_swapfile_location, expected_swapfile_permission) |
| 57 | + mock_run.assert_called_once_with(f'mkswap {expected_swapfile_location}; swapon {expected_swapfile_location}') |
| 58 | + |
| 59 | + def test_remove_swapmem(self): |
| 60 | + with mock.patch("subprocess.Popen") as mock_popen, \ |
| 61 | + mock.patch("os.unlink") as mock_unlink: |
| 62 | + pseudo_subproc = mock.MagicMock() |
| 63 | + mock_popen.return_value = pseudo_subproc |
| 64 | + pseudo_subproc.communicate.return_value = ("swapoff: /home/swapfile: swapoff failed: No such file or directory", None) |
| 65 | + pseudo_subproc.returncode = 255 |
| 66 | + |
| 67 | + swap_allocator = SWAPAllocator(allocate=True) |
| 68 | + try: |
| 69 | + swap_allocator.remove_swapmem() |
| 70 | + except Exception as detail: |
| 71 | + pytest.fail("SWAPAllocator.remove_swapmem should not raise exception %s" % repr(detail)) |
| 72 | + |
| 73 | + expected_swapfile_location = SWAPAllocator.SWAP_FILE_PATH |
| 74 | + mock_popen.assert_called_once_with(['swapoff', expected_swapfile_location], stdout=subprocess.PIPE, text=True) |
| 75 | + mock_unlink.assert_called_once_with(SWAPAllocator.SWAP_FILE_PATH) |
| 76 | + |
| 77 | + def test_swap_allocator_initialization_default_args(self): |
| 78 | + expected_allocate = False |
| 79 | + expected_swap_mem_size = SWAPAllocator.SWAP_MEM_SIZE |
| 80 | + expected_total_mem_threshold = SWAPAllocator.TOTAL_MEM_THRESHOLD |
| 81 | + expected_available_mem_threshold = SWAPAllocator.AVAILABLE_MEM_THRESHOLD |
| 82 | + swap_allocator = SWAPAllocator(allocate=expected_allocate) |
| 83 | + assert swap_allocator.allocate is expected_allocate |
| 84 | + assert swap_allocator.swap_mem_size == expected_swap_mem_size |
| 85 | + assert swap_allocator.total_mem_threshold == expected_total_mem_threshold |
| 86 | + assert swap_allocator.available_mem_threshold == expected_available_mem_threshold |
| 87 | + assert swap_allocator.is_allocated is False |
| 88 | + |
| 89 | + def test_swap_allocator_initialization_custom_args(self): |
| 90 | + expected_allocate = True |
| 91 | + expected_swap_mem_size = 2048 |
| 92 | + expected_total_mem_threshold = 4096 |
| 93 | + expected_available_mem_threshold = 1024 |
| 94 | + swap_allocator = SWAPAllocator( |
| 95 | + allocate=expected_allocate, |
| 96 | + swap_mem_size=expected_swap_mem_size, |
| 97 | + total_mem_threshold=expected_total_mem_threshold, |
| 98 | + available_mem_threshold=expected_available_mem_threshold |
| 99 | + ) |
| 100 | + assert swap_allocator.allocate is expected_allocate |
| 101 | + assert swap_allocator.swap_mem_size == expected_swap_mem_size |
| 102 | + assert swap_allocator.total_mem_threshold == expected_total_mem_threshold |
| 103 | + assert swap_allocator.available_mem_threshold == expected_available_mem_threshold |
| 104 | + assert swap_allocator.is_allocated is False |
| 105 | + |
| 106 | + def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory(self): |
| 107 | + with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \ |
| 108 | + mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \ |
| 109 | + mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \ |
| 110 | + mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \ |
| 111 | + mock.patch("os.path.exists") as mock_exists: |
| 112 | + mock_disk_free.return_value = 10 * 1024 * 1024 * 1024 |
| 113 | + mock_meminfo.return_value = { |
| 114 | + "MemTotal": 2000000, |
| 115 | + "MemAvailable": 1900000, |
| 116 | + } |
| 117 | + mock_exists.return_value = False |
| 118 | + |
| 119 | + swap_allocator = SWAPAllocator(allocate=True) |
| 120 | + try: |
| 121 | + swap_allocator.__enter__() |
| 122 | + except Exception as detail: |
| 123 | + pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail)) |
| 124 | + mock_setup.assert_called_once() |
| 125 | + mock_remove.assert_not_called() |
| 126 | + assert swap_allocator.is_allocated is True |
| 127 | + |
| 128 | + def test_swap_allocator_context_enter_allocate_true_insufficient_available_memory(self): |
| 129 | + with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \ |
| 130 | + mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \ |
| 131 | + mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \ |
| 132 | + mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \ |
| 133 | + mock.patch("os.path.exists") as mock_exists: |
| 134 | + mock_disk_free.return_value = 10 * 1024 * 1024 * 1024 |
| 135 | + mock_meminfo.return_value = { |
| 136 | + "MemTotal": 3000000, |
| 137 | + "MemAvailable": 1000000, |
| 138 | + } |
| 139 | + mock_exists.return_value = False |
| 140 | + |
| 141 | + swap_allocator = SWAPAllocator(allocate=True) |
| 142 | + try: |
| 143 | + swap_allocator.__enter__() |
| 144 | + except Exception as detail: |
| 145 | + pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail)) |
| 146 | + mock_setup.assert_called_once() |
| 147 | + mock_remove.assert_not_called() |
| 148 | + assert swap_allocator.is_allocated is True |
| 149 | + |
| 150 | + def test_swap_allocator_context_enter_allocate_true_insufficient_disk_space(self): |
| 151 | + with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \ |
| 152 | + mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \ |
| 153 | + mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \ |
| 154 | + mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \ |
| 155 | + mock.patch("os.path.exists") as mock_exists: |
| 156 | + mock_disk_free.return_value = 1 * 1024 * 1024 * 1024 |
| 157 | + mock_meminfo.return_value = { |
| 158 | + "MemTotal": 32859496, |
| 159 | + "MemAvailable": 16275512, |
| 160 | + } |
| 161 | + mock_exists.return_value = False |
| 162 | + |
| 163 | + swap_allocator = SWAPAllocator(allocate=True) |
| 164 | + try: |
| 165 | + swap_allocator.__enter__() |
| 166 | + except Exception as detail: |
| 167 | + pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail)) |
| 168 | + mock_setup.assert_not_called() |
| 169 | + mock_remove.assert_not_called() |
| 170 | + assert swap_allocator.is_allocated is False |
| 171 | + |
| 172 | + def test_swap_allocator_context_enter_allocate_true_swapfile_present(self): |
| 173 | + with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \ |
| 174 | + mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \ |
| 175 | + mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \ |
| 176 | + mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \ |
| 177 | + mock.patch("os.path.exists") as mock_exists: |
| 178 | + mock_disk_free.return_value = 10 * 1024 * 1024 * 1024 |
| 179 | + mock_meminfo.return_value = { |
| 180 | + "MemTotal": 32859496, |
| 181 | + "MemAvailable": 1000000, |
| 182 | + } |
| 183 | + mock_exists.return_value = True |
| 184 | + |
| 185 | + swap_allocator = SWAPAllocator(allocate=True) |
| 186 | + try: |
| 187 | + swap_allocator.__enter__() |
| 188 | + except Exception as detail: |
| 189 | + pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail)) |
| 190 | + mock_setup.assert_called_once() |
| 191 | + mock_remove.assert_called_once() |
| 192 | + assert swap_allocator.is_allocated is True |
| 193 | + |
| 194 | + def test_swap_allocator_context_enter_setup_error(self): |
| 195 | + with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \ |
| 196 | + mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \ |
| 197 | + mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \ |
| 198 | + mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \ |
| 199 | + mock.patch("os.path.exists") as mock_exists: |
| 200 | + mock_disk_free.return_value = 10 * 1024 * 1024 * 1024 |
| 201 | + mock_meminfo.return_value = { |
| 202 | + "MemTotal": 32859496, |
| 203 | + "MemAvailable": 1000000, |
| 204 | + } |
| 205 | + mock_exists.return_value = False |
| 206 | + expected_err_str = "Pseudo Error" |
| 207 | + mock_setup.side_effect = Exception(expected_err_str) |
| 208 | + |
| 209 | + swap_allocator = SWAPAllocator(allocate=True) |
| 210 | + try: |
| 211 | + swap_allocator.__enter__() |
| 212 | + except Exception as detail: |
| 213 | + assert expected_err_str in str(detail) |
| 214 | + mock_setup.assert_called_once() |
| 215 | + mock_remove.assert_called_once() |
| 216 | + assert swap_allocator.is_allocated is False |
| 217 | + |
| 218 | + def test_swap_allocator_context_enter_allocate_false(self): |
| 219 | + with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \ |
| 220 | + mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \ |
| 221 | + mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \ |
| 222 | + mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \ |
| 223 | + mock.patch("os.path.exists") as mock_exists: |
| 224 | + mock_disk_free.return_value = 10 * 1024 * 1024 * 1024 |
| 225 | + mock_meminfo.return_value = { |
| 226 | + "MemTotal": 32859496, |
| 227 | + "MemAvailable": 1000000, |
| 228 | + } |
| 229 | + mock_exists.return_value = False |
| 230 | + |
| 231 | + swap_allocator = SWAPAllocator(allocate=False) |
| 232 | + try: |
| 233 | + swap_allocator.__enter__() |
| 234 | + except Exception as detail: |
| 235 | + pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail)) |
| 236 | + mock_setup.assert_not_called() |
| 237 | + mock_remove.assert_not_called() |
| 238 | + assert swap_allocator.is_allocated is False |
| 239 | + |
| 240 | + def test_swap_allocator_context_exit_is_allocated_true(self): |
| 241 | + with mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove: |
| 242 | + swap_allocator = SWAPAllocator(allocate=True) |
| 243 | + swap_allocator.is_allocated = True |
| 244 | + swap_allocator.__exit__(None, None, None) |
| 245 | + mock_remove.assert_called_once() |
| 246 | + |
| 247 | + def test_swap_allocator_context_exit_is_allocated_false(self): |
| 248 | + with mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove: |
| 249 | + swap_allocator = SWAPAllocator(allocate=True) |
| 250 | + swap_allocator.is_allocated = False |
| 251 | + swap_allocator.__exit__(None, None, None) |
| 252 | + mock_remove.assert_not_called() |
0 commit comments