@@ -17,13 +17,17 @@ def test_read_from_meminfo(self):
17
17
proc_meminfo_lines = [
18
18
"MemTotal: 32859496 kB" ,
19
19
"MemFree: 16275512 kB" ,
20
+ "SwapTotal: 2000000 kB" ,
21
+ "SwapFree: 1000000 kB" ,
20
22
"HugePages_Total: 0" ,
21
23
"HugePages_Free: 0" ,
22
24
]
23
25
24
26
read_meminfo_expected_return = {
25
27
"MemTotal" : 32859496 ,
26
28
"MemFree" : 16275512 ,
29
+ "SwapTotal" : 2000000 ,
30
+ "SwapFree" : 1000000 ,
27
31
"HugePages_Total" : 0 ,
28
32
"HugePages_Free" : 0
29
33
}
@@ -113,6 +117,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory(se
113
117
mock_meminfo .return_value = {
114
118
"MemTotal" : 2000000 ,
115
119
"MemAvailable" : 1900000 ,
120
+ "SwapTotal" : 0 ,
121
+ "SwapFree" : 0 ,
116
122
}
117
123
mock_exists .return_value = False
118
124
@@ -135,6 +141,56 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_available_memor
135
141
mock_meminfo .return_value = {
136
142
"MemTotal" : 3000000 ,
137
143
"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 ,
138
194
}
139
195
mock_exists .return_value = False
140
196
@@ -157,6 +213,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_disk_space(self
157
213
mock_meminfo .return_value = {
158
214
"MemTotal" : 32859496 ,
159
215
"MemAvailable" : 16275512 ,
216
+ "SwapTotal" : 0 ,
217
+ "SwapFree" : 0 ,
160
218
}
161
219
mock_exists .return_value = False
162
220
@@ -179,6 +237,8 @@ def test_swap_allocator_context_enter_allocate_true_swapfile_present(self):
179
237
mock_meminfo .return_value = {
180
238
"MemTotal" : 32859496 ,
181
239
"MemAvailable" : 1000000 ,
240
+ "SwapTotal" : 0 ,
241
+ "SwapFree" : 0 ,
182
242
}
183
243
mock_exists .return_value = True
184
244
@@ -201,6 +261,8 @@ def test_swap_allocator_context_enter_setup_error(self):
201
261
mock_meminfo .return_value = {
202
262
"MemTotal" : 32859496 ,
203
263
"MemAvailable" : 1000000 ,
264
+ "SwapTotal" : 0 ,
265
+ "SwapFree" : 0 ,
204
266
}
205
267
mock_exists .return_value = False
206
268
expected_err_str = "Pseudo Error"
@@ -225,6 +287,8 @@ def test_swap_allocator_context_enter_allocate_false(self):
225
287
mock_meminfo .return_value = {
226
288
"MemTotal" : 32859496 ,
227
289
"MemAvailable" : 1000000 ,
290
+ "SwapTotal" : 0 ,
291
+ "SwapFree" : 0 ,
228
292
}
229
293
mock_exists .return_value = False
230
294
0 commit comments