Skip to content

Commit 264667a

Browse files
ShadowCursebchalios
authored andcommitted
feat: add test for vsock h2g connection reset
The test starts `socat` server on the host and `socat` client in the guest. The test then validates that `socat` client stops after snapshot is taken. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 14ce070 commit 264667a

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

tests/integration_tests/functional/test_vsock.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"""
1515

1616
import os.path
17+
import subprocess
18+
import time
19+
from pathlib import Path
1720
from socket import timeout as SocketTimeout
1821

1922
from framework.utils_vsock import (
@@ -126,7 +129,7 @@ def test_vsock_epipe(uvm_plain, bin_vsock_path, test_fc_session_root_path):
126129
validate_fc_metrics(metrics)
127130

128131

129-
def test_vsock_transport_reset(
132+
def test_vsock_transport_reset_h2g(
130133
uvm_nano, microvm_factory, bin_vsock_path, test_fc_session_root_path
131134
):
132135
"""
@@ -215,3 +218,67 @@ def test_vsock_transport_reset(
215218
check_host_connections(path, blob_path, blob_hash)
216219
metrics = vm2.flush_metrics()
217220
validate_fc_metrics(metrics)
221+
222+
223+
def test_vsock_transport_reset_g2h(uvm_nano, microvm_factory):
224+
"""
225+
Vsock transport reset test.
226+
"""
227+
test_vm = uvm_nano
228+
test_vm.add_net_iface()
229+
test_vm.api.vsock.put(vsock_id="vsock0", guest_cid=3, uds_path=f"/{VSOCK_UDS_PATH}")
230+
test_vm.start()
231+
test_vm.wait_for_up()
232+
233+
host_socket_path = os.path.join(
234+
test_vm.path, f"{VSOCK_UDS_PATH}_{ECHO_SERVER_PORT}"
235+
)
236+
host_socat_commmand = [
237+
"socat",
238+
"-dddd",
239+
f"UNIX-LISTEN:{host_socket_path},fork",
240+
"STDOUT",
241+
]
242+
host_socat = subprocess.Popen(
243+
host_socat_commmand, stdout=subprocess.PIPE, stderr=subprocess.PIPE
244+
)
245+
246+
# Give some time for host socat to create socket
247+
time.sleep(0.5)
248+
assert Path(host_socket_path).exists()
249+
test_vm.create_jailed_resource(host_socket_path)
250+
251+
# Create a socat process in the guest which will connect to the host socat
252+
guest_socat_commmand = f"tmux new -d 'socat - vsock-connect:2:{ECHO_SERVER_PORT}'"
253+
test_vm.ssh.run(guest_socat_commmand)
254+
255+
# socat should be running in the guest now
256+
code, _, _ = test_vm.ssh.run("pidof socat")
257+
assert code == 0
258+
259+
# Create snapshot.
260+
snapshot = test_vm.snapshot_full()
261+
test_vm.resume()
262+
263+
# After `create_snapshot` + 'restore' calls, connection should be dropped
264+
code, _, _ = test_vm.ssh.run("pidof socat")
265+
assert code == 1
266+
267+
# Kill host socat as it is not useful anymore
268+
host_socat.kill()
269+
host_socat.communicate()
270+
271+
# Terminate VM.
272+
test_vm.kill()
273+
274+
# Load snapshot.
275+
vm2 = microvm_factory.build()
276+
vm2.spawn()
277+
vm2.restore_from_snapshot(snapshot, resume=True)
278+
vm2.wait_for_up()
279+
280+
# After snap restore all vsock connections should be
281+
# dropped. This means guest socat should exit same way
282+
# as it did after snapshot was taken.
283+
code, _, _ = vm2.ssh.run("pidof socat")
284+
assert code == 1

0 commit comments

Comments
 (0)