Skip to content

Commit 2f6e30f

Browse files
committed
scsi: Refactor tests to use a tmp image file.
This'll be necessary for tests that involve writing later, but it should also fix the CI issue, because we're no longer trying to find the test image file at runtime - instead it's embedded in the test binary then written to a temp file at runtime. Signed-off-by: Gaelan Steele <[email protected]>
1 parent f5a2f19 commit 2f6e30f

File tree

8 files changed

+162
-56
lines changed

8 files changed

+162
-56
lines changed

Cargo.lock

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scsi/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ vmm-sys-util = "0.8.0"
2020
structopt = "0.3"
2121
log = "0.4.14"
2222

23+
[dev-dependencies]
24+
tempfile = "3.2.0"
25+

src/scsi/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod virtio;
99

1010
use std::{
1111
convert::TryInto,
12+
fs::File,
1213
io::{self, ErrorKind, Read},
1314
path::PathBuf,
1415
process::exit,
@@ -343,7 +344,7 @@ fn main() {
343344
}
344345

345346
for image in opt.images {
346-
let mut dev = BlockDevice::new(&image).expect("Opening image");
347+
let mut dev = BlockDevice::new(File::open(image).expect("Opening image"));
347348
dev.set_write_protected(opt.read_only);
348349
dev.set_solid_state(opt.solid_state);
349350
target.add_lun(Box::new(dev));

src/scsi/src/scsi/emulation/block_device.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{
33
fs::File,
44
io::{self, Read, Write},
55
os::unix::prelude::*,
6-
path::Path,
76
};
87

98
use log::{debug, error, warn};
@@ -29,15 +28,15 @@ pub struct BlockDevice {
2928
}
3029

3130
impl BlockDevice {
32-
pub fn new(path: &Path) -> io::Result<Self> {
31+
pub const fn new(file: File) -> Self {
3332
// TODO: trying 4096 logical/physical for now. May need to fall
3433
// back to 512 logical/4096 physical for back compat.
35-
Ok(Self {
36-
file: File::open(path)?,
34+
Self {
35+
file,
3736
block_size: 512,
3837
write_protected: false,
3938
solid_state: false,
40-
})
39+
}
4140
}
4241

4342
fn read_blocks(&self, lba: u64, blocks: u64) -> io::Result<Vec<u8>> {

src/scsi/src/scsi/tests/bad_lun.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::path::Path;
2-
3-
use super::{do_command_fail_lun, do_command_in_lun};
1+
use super::{do_command_fail_lun, do_command_in_lun, null_image};
42
use crate::scsi::{
53
emulation::{block_device::BlockDevice, EmulatedTarget},
64
sense,
@@ -10,7 +8,7 @@ use crate::scsi::{
108
fn test_report_luns() {
119
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
1210
for _ in 0..5 {
13-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
11+
let dev = BlockDevice::new(null_image());
1412
target.add_lun(Box::new(dev));
1513
}
1614

@@ -45,7 +43,7 @@ fn test_report_luns() {
4543
fn test_report_luns_empty() {
4644
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
4745
for _ in 0..5 {
48-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
46+
let dev = BlockDevice::new(null_image());
4947
target.add_lun(Box::new(dev));
5048
}
5149

@@ -76,7 +74,7 @@ fn test_report_luns_empty() {
7674
#[test]
7775
fn test_request_sense() {
7876
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
79-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
77+
let dev = BlockDevice::new(null_image());
8078
target.add_lun(Box::new(dev));
8179

8280
do_command_in_lun(
@@ -96,7 +94,7 @@ fn test_request_sense() {
9694
#[test]
9795
fn test_request_sense_descriptor_format() {
9896
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
99-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
97+
let dev = BlockDevice::new(null_image());
10098
target.add_lun(Box::new(dev));
10199

102100
do_command_fail_lun(
@@ -116,7 +114,7 @@ fn test_request_sense_descriptor_format() {
116114
#[test]
117115
fn test_inquiry() {
118116
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
119-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
117+
let dev = BlockDevice::new(null_image());
120118
target.add_lun(Box::new(dev));
121119

122120
do_command_in_lun(
@@ -160,7 +158,7 @@ fn test_inquiry() {
160158
#[test]
161159
fn test_other_command() {
162160
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
163-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
161+
let dev = BlockDevice::new(null_image());
164162
target.add_lun(Box::new(dev));
165163

166164
do_command_fail_lun(
@@ -179,7 +177,7 @@ fn test_other_command() {
179177
#[test]
180178
fn test_invalid_command() {
181179
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
182-
let dev = BlockDevice::new(Path::new("/dev/null")).unwrap();
180+
let dev = BlockDevice::new(null_image());
183181
target.add_lun(Box::new(dev));
184182

185183
do_command_fail_lun(

src/scsi/src/scsi/tests/generic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Tests for stuff shared between commands.
22
3-
use std::{io::ErrorKind, path::Path};
3+
use std::io::ErrorKind;
44

5-
use super::do_command_fail;
5+
use super::{do_command_fail, test_image};
66
use crate::scsi::{
77
emulation::{block_device::BlockDevice, EmulatedTarget},
88
sense, CmdError, Request, Target, TaskAttr,
@@ -11,7 +11,7 @@ use crate::scsi::{
1111
#[test]
1212
fn test_invalid_opcode() {
1313
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
14-
let dev = BlockDevice::new(Path::new("src/scsi/tests/test.img")).unwrap();
14+
let dev = BlockDevice::new(test_image());
1515
target.add_lun(Box::new(dev));
1616

1717
do_command_fail(
@@ -27,7 +27,7 @@ fn test_invalid_opcode() {
2727
#[test]
2828
fn test_invalid_service_action() {
2929
let mut target: EmulatedTarget<Vec<u8>, &[u8]> = EmulatedTarget::new();
30-
let dev = BlockDevice::new(Path::new("src/scsi/tests/test.img")).unwrap();
30+
let dev = BlockDevice::new(test_image());
3131
target.add_lun(Box::new(dev));
3232

3333
do_command_fail(
@@ -44,7 +44,7 @@ fn test_invalid_service_action() {
4444
#[test]
4545
fn test_short_data_out_buffer() {
4646
let mut target: EmulatedTarget<&mut [u8], &[u8]> = EmulatedTarget::new();
47-
let dev = BlockDevice::new(Path::new("src/scsi/tests/test.img")).unwrap();
47+
let dev = BlockDevice::new(test_image());
4848
target.add_lun(Box::new(dev));
4949

5050
let mut data_in: &mut [u8] = &mut [];
@@ -80,7 +80,7 @@ fn test_short_data_out_buffer() {
8080
#[test]
8181
fn test_short_cdb() {
8282
let mut target: EmulatedTarget<&mut [u8], &[u8]> = EmulatedTarget::new();
83-
let dev = BlockDevice::new(Path::new("src/scsi/tests/test.img")).unwrap();
83+
let dev = BlockDevice::new(test_image());
8484
target.add_lun(Box::new(dev));
8585

8686
let mut data_in: &mut [u8] = &mut [];

0 commit comments

Comments
 (0)