1111import host_tools .drive as drive_tools
1212import host_tools .network as net_tools # pylint: disable=import-error
1313
14+ MB = 1024 * 1024
15+
1416
1517def test_rescan_file (test_microvm_with_ssh , network_config ):
1618 """Verify that rescan works with a file-backed virtio device."""
@@ -24,9 +26,11 @@ def test_rescan_file(test_microvm_with_ssh, network_config):
2426
2527 _tap , _ , _ = test_microvm_with_ssh .ssh_network_config (network_config , '1' )
2628
29+ block_size = 2
2730 # Add a scratch block device.
2831 fs = drive_tools .FilesystemFile (
29- os .path .join (test_microvm .fsfiles , 'scratch' )
32+ os .path .join (test_microvm .fsfiles , 'scratch' ),
33+ size = block_size
3034 )
3135 test_microvm .add_drive (
3236 'scratch' ,
@@ -36,11 +40,18 @@ def test_rescan_file(test_microvm_with_ssh, network_config):
3640 test_microvm .start ()
3741
3842 ssh_connection = net_tools .SSHConnection (test_microvm .ssh_config )
39-
4043 _check_block_size (ssh_connection , '/dev/vdb' , fs .size ())
4144
42- # Resize the filesystem from 256 MiB (default) to 512 MiB.
43- fs .resize (512 )
45+ # Check if reading from the entire disk results in a file of the same size
46+ # or errors out, after a truncate on the host.
47+ truncated_size = block_size // 2
48+ utils .run_cmd (f"truncate --size { truncated_size } M { fs .path } " )
49+ block_copy_name = "dev_vdb_copy"
50+ _ , _ , stderr = ssh_connection .execute_command (
51+ f"dd if=/dev/vdb of={ block_copy_name } bs=1M count={ block_size } " )
52+ assert "dd: error reading '/dev/vdb': Input/output error" in stderr .read ()
53+ _check_file_size (ssh_connection , f'{ block_copy_name } ' ,
54+ truncated_size * MB )
4455
4556 response = test_microvm .drive .patch (
4657 drive_id = 'scratch' ,
@@ -338,6 +349,15 @@ def _check_block_size(ssh_connection, dev_path, size):
338349 assert stdout .readline ().strip () == str (size )
339350
340351
352+ def _check_file_size (ssh_connection , dev_path , size ):
353+ _ , stdout , stderr = ssh_connection .execute_command (
354+ 'stat --format=%s {}' .format (dev_path )
355+ )
356+
357+ assert stderr .read () == ''
358+ assert stdout .readline ().strip () == str (size )
359+
360+
341361def _process_blockdev_output (blockdev_out , assert_dict , keys_array ):
342362 blockdev_out_lines = blockdev_out .splitlines ()
343363
0 commit comments