Skip to content

Commit 0a006e8

Browse files
zulinx86pb8o
andcommitted
test: simplify test_deflate_on_oom
Parametrize test_deflate_on_oom and reduce LOC. Signed-off-by: Takahiro Itazuri <itazur@amazon.com> Co-authored-by: Pablo Barbáchano <pablob@amazon.com>
1 parent 62d4db0 commit 0a006e8

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

tests/integration_tests/functional/test_balloon.py

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import time
77

8+
import pytest
89
from retry import retry
910

1011
from framework.builder import MicrovmBuilder, SnapshotBuilder, SnapshotType
@@ -152,65 +153,34 @@ def test_inflate_reduces_free(test_microvm_with_api):
152153

153154

154155
# pylint: disable=C0103
155-
def test_deflate_on_oom_true(test_microvm_with_api):
156+
@pytest.mark.parametrize("deflate_on_oom", [True, False])
157+
def test_deflate_on_oom(test_microvm_with_api, deflate_on_oom):
156158
"""
157-
Verify that setting the `deflate_on_oom` to True works correctly.
158-
"""
159-
test_microvm = test_microvm_with_api
160-
test_microvm.spawn()
161-
test_microvm.basic_config()
162-
test_microvm.add_net_iface()
159+
Verify that setting the `deflate_on_oom` option works correctly.
163160
164-
# Add a deflated memory balloon.
165-
response = test_microvm.balloon.put(
166-
amount_mib=0, deflate_on_oom=True, stats_polling_interval_s=0
167-
)
168-
assert test_microvm.api_session.is_status_no_content(response.status_code)
161+
https://github.com/firecracker-microvm/firecracker/blob/main/docs/ballooning.md
169162
170-
# Start the microvm.
171-
test_microvm.start()
172-
firecracker_pid = test_microvm.jailer_clone_pid
163+
deflate_on_oom=True
173164
174-
# We get an initial reading of the RSS, then calculate the amount
175-
# we need to inflate the balloon with by subtracting it from the
176-
# VM size and adding an offset of 10 MiB in order to make sure we
177-
# get a lower reading than the initial one.
178-
initial_rss = get_stable_rss_mem_by_pid(firecracker_pid)
179-
inflate_size = 256 - int(initial_rss / 1024) + 10
180-
181-
# Inflate the balloon
182-
response = test_microvm.balloon.patch(amount_mib=inflate_size)
183-
assert test_microvm.api_session.is_status_no_content(response.status_code)
184-
# This call will internally wait for rss to become stable.
185-
_ = get_stable_rss_mem_by_pid(firecracker_pid)
186-
187-
# Check that using memory doesn't lead to an out of memory error.
188-
# Note that due to `test_deflate_on_oom_false`, we know that
189-
# if `deflate_on_oom` were set to False, then such an error
190-
# would have happened.
191-
make_guest_dirty_memory(test_microvm.ssh)
165+
should not result in an OOM kill
192166
167+
deflate_on_oom=False
193168
194-
# pylint: disable=C0103
195-
def test_deflate_on_oom_false(test_microvm_with_api):
196-
"""
197-
Verify that setting the `deflate_on_oom` to False works correctly.
169+
should result in an OOM kill
198170
"""
199171
test_microvm = test_microvm_with_api
200172
test_microvm.spawn()
201173
test_microvm.basic_config()
202174
test_microvm.add_net_iface()
203175

204-
# Add a memory balloon.
176+
# Add a deflated memory balloon.
205177
response = test_microvm.balloon.put(
206-
amount_mib=0, deflate_on_oom=False, stats_polling_interval_s=0
178+
amount_mib=0, deflate_on_oom=deflate_on_oom, stats_polling_interval_s=0
207179
)
208180
assert test_microvm.api_session.is_status_no_content(response.status_code)
209181

210182
# Start the microvm.
211183
test_microvm.start()
212-
213-
# Get an ssh connection to the microvm.
214184
firecracker_pid = test_microvm.jailer_clone_pid
215185

216186
# We get an initial reading of the RSS, then calculate the amount
@@ -220,14 +190,14 @@ def test_deflate_on_oom_false(test_microvm_with_api):
220190
initial_rss = get_stable_rss_mem_by_pid(firecracker_pid)
221191
inflate_size = 256 - int(initial_rss / 1024) + 10
222192

223-
# Inflate the balloon.
193+
# Inflate the balloon
224194
response = test_microvm.balloon.patch(amount_mib=inflate_size)
225195
assert test_microvm.api_session.is_status_no_content(response.status_code)
226196
# This call will internally wait for rss to become stable.
227197
_ = get_stable_rss_mem_by_pid(firecracker_pid)
228198

229-
# Check that using memory does lead to an out of memory error.
230-
make_guest_dirty_memory(test_microvm.ssh, should_oom=True)
199+
# Check that using memory leads an out of memory error (or not).
200+
make_guest_dirty_memory(test_microvm.ssh, should_oom=not deflate_on_oom)
231201

232202

233203
# pylint: disable=C0103

0 commit comments

Comments
 (0)