Skip to content

Commit fc23b71

Browse files
committed
fix(test): ensure socat gets killed in test_5_snapshots
Use a try-finally block inside check_guest_connection to make sure that socat gets killed even if something inside check_guest_connection fails and raises an exception. Signed-off-by: Patrick Roy <[email protected]>
1 parent e7ccf54 commit fc23b71

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

tests/framework/utils_vsock.py

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -142,53 +142,55 @@ def check_guest_connections(vm, server_port_path, blob_path, blob_hash):
142142
["socat", f"UNIX-LISTEN:{server_port_path},fork,backlog=5", "exec:'/bin/cat'"]
143143
)
144144

145-
# Link the listening Unix socket into the VM's jail, so that
146-
# Firecracker can connect to it.
147-
attempt = 0
148-
# But 1st, give socat a bit of time to create the socket
149-
while not Path(server_port_path).exists() and attempt < 3:
150-
time.sleep(0.2)
151-
attempt += 1
152-
vm.create_jailed_resource(server_port_path)
153-
154-
# Increase maximum process count for the ssh service.
155-
# Avoids: "bash: fork: retry: Resource temporarily unavailable"
156-
# Needed to execute the bash script that tests for concurrent
157-
# vsock guest initiated connections.
158-
pids_max_file = "/sys/fs/cgroup/system.slice/ssh.service/pids.max"
159-
ecode, _, _ = vm.ssh.run(f"echo 1024 > {pids_max_file}")
160-
assert ecode == 0, "Unable to set max process count for guest ssh service."
161-
162-
# Build the guest worker sub-command.
163-
# `vsock_helper` will read the blob file from STDIN and send the echo
164-
# server response to STDOUT. This response is then hashed, and the
165-
# hash is compared against `blob_hash` (computed on the host). This
166-
# comparison sets the exit status of the worker command.
167-
worker_cmd = "hash=$("
168-
worker_cmd += "cat {}".format(blob_path)
169-
worker_cmd += " | /tmp/vsock_helper echo 2 {}".format(ECHO_SERVER_PORT)
170-
worker_cmd += " | md5sum | cut -f1 -d\\ "
171-
worker_cmd += ")"
172-
worker_cmd += ' && [[ "$hash" = "{}" ]]'.format(blob_hash)
173-
174-
# Run `TEST_CONNECTION_COUNT` concurrent workers, using the above
175-
# worker sub-command.
176-
# If any worker fails, this command will fail. If all worker sub-commands
177-
# succeed, this will also succeed.
178-
cmd = 'workers="";'
179-
cmd += "for i in $(seq 1 {}); do".format(TEST_CONNECTION_COUNT)
180-
cmd += " ({})& ".format(worker_cmd)
181-
cmd += ' workers="$workers $!";'
182-
cmd += "done;"
183-
cmd += "for w in $workers; do wait $w || (wait; exit 1); done"
184-
185-
ecode, _, stderr = vm.ssh.run(cmd)
186-
echo_server.terminate()
187-
rc = echo_server.wait()
188-
# socat exits with 128 + 15 (SIGTERM)
189-
assert rc == 143
190-
191-
assert ecode == 0, stderr
145+
try:
146+
# Link the listening Unix socket into the VM's jail, so that
147+
# Firecracker can connect to it.
148+
attempt = 0
149+
# But 1st, give socat a bit of time to create the socket
150+
while not Path(server_port_path).exists() and attempt < 3:
151+
time.sleep(0.2)
152+
attempt += 1
153+
vm.create_jailed_resource(server_port_path)
154+
155+
# Increase maximum process count for the ssh service.
156+
# Avoids: "bash: fork: retry: Resource temporarily unavailable"
157+
# Needed to execute the bash script that tests for concurrent
158+
# vsock guest initiated connections.
159+
pids_max_file = "/sys/fs/cgroup/system.slice/ssh.service/pids.max"
160+
ecode, _, _ = vm.ssh.run(f"echo 1024 > {pids_max_file}")
161+
assert ecode == 0, "Unable to set max process count for guest ssh service."
162+
163+
# Build the guest worker sub-command.
164+
# `vsock_helper` will read the blob file from STDIN and send the echo
165+
# server response to STDOUT. This response is then hashed, and the
166+
# hash is compared against `blob_hash` (computed on the host). This
167+
# comparison sets the exit status of the worker command.
168+
worker_cmd = "hash=$("
169+
worker_cmd += "cat {}".format(blob_path)
170+
worker_cmd += " | /tmp/vsock_helper echo 2 {}".format(ECHO_SERVER_PORT)
171+
worker_cmd += " | md5sum | cut -f1 -d\\ "
172+
worker_cmd += ")"
173+
worker_cmd += ' && [[ "$hash" = "{}" ]]'.format(blob_hash)
174+
175+
# Run `TEST_CONNECTION_COUNT` concurrent workers, using the above
176+
# worker sub-command.
177+
# If any worker fails, this command will fail. If all worker sub-commands
178+
# succeed, this will also succeed.
179+
cmd = 'workers="";'
180+
cmd += "for i in $(seq 1 {}); do".format(TEST_CONNECTION_COUNT)
181+
cmd += " ({})& ".format(worker_cmd)
182+
cmd += ' workers="$workers $!";'
183+
cmd += "done;"
184+
cmd += "for w in $workers; do wait $w || (wait; exit 1); done"
185+
186+
ecode, _, stderr = vm.ssh.run(cmd)
187+
188+
assert ecode == 0, stderr
189+
finally:
190+
echo_server.terminate()
191+
rc = echo_server.wait()
192+
# socat exits with 128 + 15 (SIGTERM)
193+
assert rc == 143
192194

193195

194196
def make_host_port_path(uds_path, port):

0 commit comments

Comments
 (0)