Skip to content

Commit

Permalink
update unit tests for swap allocator
Browse files Browse the repository at this point in the history
Signed-off-by: Xichen Lin <lukelin0907@gmail.com>
  • Loading branch information
Xichen96 authored and preetham-singh committed Nov 18, 2022
1 parent e04708f commit d6064c4
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/swap_allocator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ def test_read_from_meminfo(self):
proc_meminfo_lines = [
"MemTotal: 32859496 kB",
"MemFree: 16275512 kB",
"SwapTotal: 2000000 kB",
"SwapFree: 1000000 kB",
"HugePages_Total: 0",
"HugePages_Free: 0",
]

read_meminfo_expected_return = {
"MemTotal": 32859496,
"MemFree": 16275512,
"SwapTotal": 2000000,
"SwapFree": 1000000,
"HugePages_Total": 0,
"HugePages_Free": 0
}
Expand Down Expand Up @@ -113,6 +117,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory(se
mock_meminfo.return_value = {
"MemTotal": 2000000,
"MemAvailable": 1900000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

Expand All @@ -135,6 +141,56 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_available_memor
mock_meminfo.return_value = {
"MemTotal": 3000000,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

swap_allocator = SWAPAllocator(allocate=True)
try:
swap_allocator.__enter__()
except Exception as detail:
pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail))
mock_setup.assert_called_once()
mock_remove.assert_not_called()
assert swap_allocator.is_allocated is True

def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory_plus_swap(self):
with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \
mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \
mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \
mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \
mock.patch("os.path.exists") as mock_exists:
mock_disk_free.return_value = 10 * 1024 * 1024 * 1024
mock_meminfo.return_value = {
"MemTotal": 1000000,
"MemAvailable": 900000,
"SwapTotal": 1000000,
"SwapFree": 1000000,
}
mock_exists.return_value = False

swap_allocator = SWAPAllocator(allocate=True)
try:
swap_allocator.__enter__()
except Exception as detail:
pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail))
mock_setup.assert_called_once()
mock_remove.assert_not_called()
assert swap_allocator.is_allocated is True

def test_swap_allocator_context_enter_allocate_true_insufficient_available_memory_plus_swap(self):
with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \
mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \
mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \
mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \
mock.patch("os.path.exists") as mock_exists:
mock_disk_free.return_value = 10 * 1024 * 1024 * 1024
mock_meminfo.return_value = {
"MemTotal": 2000000,
"MemAvailable": 500000,
"SwapTotal": 1000000,
"SwapFree": 500000,
}
mock_exists.return_value = False

Expand All @@ -157,6 +213,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_disk_space(self
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 16275512,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

Expand All @@ -179,6 +237,8 @@ def test_swap_allocator_context_enter_allocate_true_swapfile_present(self):
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = True

Expand All @@ -201,6 +261,8 @@ def test_swap_allocator_context_enter_setup_error(self):
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False
expected_err_str = "Pseudo Error"
Expand All @@ -225,6 +287,8 @@ def test_swap_allocator_context_enter_allocate_false(self):
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

Expand Down

0 comments on commit d6064c4

Please sign in to comment.