def test_swap_allocator_context_enter_allocate_false(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": 32859496, "MemAvailable": 1000000, } mock_exists.return_value = False swap_allocator = SWAPAllocator(allocate=False) try: swap_allocator.__enter__() except Exception as detail: pytest.fail( "SWAPAllocator context manager should not raise exception %s" % repr(detail)) mock_setup.assert_not_called() mock_remove.assert_not_called() assert swap_allocator.is_allocated is False
def test_swap_allocator_context_enter_setup_error(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": 32859496, "MemAvailable": 1000000, } mock_exists.return_value = False expected_err_str = "Pseudo Error" mock_setup.side_effect = Exception(expected_err_str) swap_allocator = SWAPAllocator(allocate=True) try: swap_allocator.__enter__() except Exception as detail: assert expected_err_str in str(detail) mock_setup.assert_called_once() mock_remove.assert_called_once() assert swap_allocator.is_allocated is False