@@ -17,13 +17,17 @@ def test_read_from_meminfo(self):
1717 proc_meminfo_lines = [
1818 "MemTotal: 32859496 kB" ,
1919 "MemFree: 16275512 kB" ,
20+ "SwapTotal: 2000000 kB" ,
21+ "SwapFree: 1000000 kB" ,
2022 "HugePages_Total: 0" ,
2123 "HugePages_Free: 0" ,
2224 ]
2325
2426 read_meminfo_expected_return = {
2527 "MemTotal" : 32859496 ,
2628 "MemFree" : 16275512 ,
29+ "SwapTotal" : 2000000 ,
30+ "SwapFree" : 1000000 ,
2731 "HugePages_Total" : 0 ,
2832 "HugePages_Free" : 0
2933 }
@@ -113,6 +117,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory(se
113117 mock_meminfo .return_value = {
114118 "MemTotal" : 2000000 ,
115119 "MemAvailable" : 1900000 ,
120+ "SwapTotal" : 0 ,
121+ "SwapFree" : 0 ,
116122 }
117123 mock_exists .return_value = False
118124
@@ -135,6 +141,56 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_available_memor
135141 mock_meminfo .return_value = {
136142 "MemTotal" : 3000000 ,
137143 "MemAvailable" : 1000000 ,
144+ "SwapTotal" : 0 ,
145+ "SwapFree" : 0 ,
146+ }
147+ mock_exists .return_value = False
148+
149+ swap_allocator = SWAPAllocator (allocate = True )
150+ try :
151+ swap_allocator .__enter__ ()
152+ except Exception as detail :
153+ pytest .fail ("SWAPAllocator context manager should not raise exception %s" % repr (detail ))
154+ mock_setup .assert_called_once ()
155+ mock_remove .assert_not_called ()
156+ assert swap_allocator .is_allocated is True
157+
158+ def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory_plus_swap (self ):
159+ with mock .patch ("sonic_installer.main.SWAPAllocator.get_disk_freespace" ) as mock_disk_free , \
160+ mock .patch ("sonic_installer.main.SWAPAllocator.read_from_meminfo" ) as mock_meminfo , \
161+ mock .patch ("sonic_installer.main.SWAPAllocator.setup_swapmem" ) as mock_setup , \
162+ mock .patch ("sonic_installer.main.SWAPAllocator.remove_swapmem" ) as mock_remove , \
163+ mock .patch ("os.path.exists" ) as mock_exists :
164+ mock_disk_free .return_value = 10 * 1024 * 1024 * 1024
165+ mock_meminfo .return_value = {
166+ "MemTotal" : 1000000 ,
167+ "MemAvailable" : 900000 ,
168+ "SwapTotal" : 1000000 ,
169+ "SwapFree" : 1000000 ,
170+ }
171+ mock_exists .return_value = False
172+
173+ swap_allocator = SWAPAllocator (allocate = True )
174+ try :
175+ swap_allocator .__enter__ ()
176+ except Exception as detail :
177+ pytest .fail ("SWAPAllocator context manager should not raise exception %s" % repr (detail ))
178+ mock_setup .assert_called_once ()
179+ mock_remove .assert_not_called ()
180+ assert swap_allocator .is_allocated is True
181+
182+ def test_swap_allocator_context_enter_allocate_true_insufficient_available_memory_plus_swap (self ):
183+ with mock .patch ("sonic_installer.main.SWAPAllocator.get_disk_freespace" ) as mock_disk_free , \
184+ mock .patch ("sonic_installer.main.SWAPAllocator.read_from_meminfo" ) as mock_meminfo , \
185+ mock .patch ("sonic_installer.main.SWAPAllocator.setup_swapmem" ) as mock_setup , \
186+ mock .patch ("sonic_installer.main.SWAPAllocator.remove_swapmem" ) as mock_remove , \
187+ mock .patch ("os.path.exists" ) as mock_exists :
188+ mock_disk_free .return_value = 10 * 1024 * 1024 * 1024
189+ mock_meminfo .return_value = {
190+ "MemTotal" : 2000000 ,
191+ "MemAvailable" : 500000 ,
192+ "SwapTotal" : 1000000 ,
193+ "SwapFree" : 500000 ,
138194 }
139195 mock_exists .return_value = False
140196
@@ -157,6 +213,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_disk_space(self
157213 mock_meminfo .return_value = {
158214 "MemTotal" : 32859496 ,
159215 "MemAvailable" : 16275512 ,
216+ "SwapTotal" : 0 ,
217+ "SwapFree" : 0 ,
160218 }
161219 mock_exists .return_value = False
162220
@@ -179,6 +237,8 @@ def test_swap_allocator_context_enter_allocate_true_swapfile_present(self):
179237 mock_meminfo .return_value = {
180238 "MemTotal" : 32859496 ,
181239 "MemAvailable" : 1000000 ,
240+ "SwapTotal" : 0 ,
241+ "SwapFree" : 0 ,
182242 }
183243 mock_exists .return_value = True
184244
@@ -201,6 +261,8 @@ def test_swap_allocator_context_enter_setup_error(self):
201261 mock_meminfo .return_value = {
202262 "MemTotal" : 32859496 ,
203263 "MemAvailable" : 1000000 ,
264+ "SwapTotal" : 0 ,
265+ "SwapFree" : 0 ,
204266 }
205267 mock_exists .return_value = False
206268 expected_err_str = "Pseudo Error"
@@ -225,6 +287,8 @@ def test_swap_allocator_context_enter_allocate_false(self):
225287 mock_meminfo .return_value = {
226288 "MemTotal" : 32859496 ,
227289 "MemAvailable" : 1000000 ,
290+ "SwapTotal" : 0 ,
291+ "SwapFree" : 0 ,
228292 }
229293 mock_exists .return_value = False
230294
0 commit comments