mdevctl 1.4.0

A mediated device management utility for Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use anyhow::{anyhow, Result};
use log::info;
use nix::sys::wait::waitpid;
use nix::unistd::{fork, ForkResult};
use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Mutex;
use tempfile::Builder;
use tempfile::TempDir;
use uuid::Uuid;

use crate::callouts::*;
use crate::environment::Environment;
use crate::logger::logger;
use crate::mdev::MDev;

// additional tests
mod callouts;
mod define;
mod list;
mod modify;
mod startstop;
mod types;

const TEST_DATA_DIR: &str = "testdata";

fn init() {
    let _ = logger().is_test(true).try_init();
}

#[derive(PartialEq, Clone, Copy)]
enum Expect<'a> {
    Pass,
    Fail(Option<&'a str>),
}

#[derive(Debug)]
struct TestEnvironment {
    datapath: PathBuf,
    scratch: TempDir,
    name: String,
    case: String,
    callout_scripts: Mutex<CalloutScriptCache>,
}

impl Environment for TestEnvironment {
    fn root(&self) -> &Path {
        self.scratch.path()
    }

    fn find_script(&self, dev: &MDev) -> Option<CalloutScriptInfo> {
        return self
            .callout_scripts
            .lock()
            .unwrap()
            .find_versioned_script(dev);
    }

    fn as_env(self: Rc<Self>) -> Rc<dyn Environment> {
        self.clone()
    }
}

impl TestEnvironment {
    pub fn new(testname: &str, testcase: &str) -> Rc<TestEnvironment> {
        let path: PathBuf = [TEST_DATA_DIR, testname].iter().collect();
        let scratchdir = Builder::new().prefix("mdevctl-test").tempdir().unwrap();
        let test = TestEnvironment {
            datapath: path,
            scratch: scratchdir,
            name: testname.to_owned(),
            case: testcase.to_owned(),
            callout_scripts: Mutex::new(CalloutScriptCache::new()),
        };
        // populate the basic directories in the environment
        fs::create_dir_all(test.mdev_base()).expect("Unable to create mdev_base");
        fs::create_dir_all(test.config_base()).expect("Unable to create config_base");
        fs::create_dir_all(test.parent_base()).expect("Unable to create parent_base");
        for dir in test.callout_dirs() {
            fs::create_dir_all(&dir)
                .unwrap_or_else(|_| panic!("Unable to create callout_dir {:?}", &dir))
        }
        for dir in test.notification_dirs() {
            fs::create_dir_all(&dir)
                .unwrap_or_else(|_| panic!("Unable to create notification_dir '{:?}'", &dir))
        }
        info!("---- Running test '{}/{}' ----", testname, testcase);
        Rc::new(test)
    }

    // set up a few files in the test environment to simulate an defined mediated device
    fn populate_defined_device(&self, uuid: &str, parent: &str, filename: &str) {
        let jsonfile = self.datapath.join(filename);
        let parentdir = self.config_base().join(parent);
        fs::create_dir_all(&parentdir).expect("Unable to setup parent dir");
        let deffile = parentdir.join(uuid);
        assert!(jsonfile.exists());
        assert!(!deffile.exists());
        fs::copy(jsonfile, deffile).expect("Unable to copy device def");
    }

    // set up a few files in the test environment to simulate an active mediated device
    fn populate_active_device(&self, uuid: &str, parent: &str, mdev_type: &str) {
        self.general_populate_active_device(uuid, parent, mdev_type, false, false, false, false);
    }

    // set up a few files in the test environment to simulate an active mediated device with error
    fn populate_broken_active_device_links(
        &self,
        uuid: &str,
        parent: &str,
        mdev_type: &str,
        break_parent: bool,
        break_type: bool,
    ) {
        self.general_populate_active_device(
            uuid,
            parent,
            mdev_type,
            break_parent,
            false,
            break_type,
            false,
        );
    }

    fn populate_removed_active_device_attributes(
        &self,
        uuid: &str,
        parent: &str,
        mdev_type: &str,
        break_parent: bool,
        break_type: bool,
    ) {
        self.general_populate_active_device(
            uuid,
            parent,
            mdev_type,
            break_parent,
            true,
            break_type,
            true,
        );
    }

    #[allow(clippy::too_many_arguments)]
    fn general_populate_active_device(
        &self,
        uuid: &str,
        parent: &str,
        mdev_type: &str,
        break_parent: bool,
        remove_parent_link: bool,
        break_type: bool,
        remove_type_link: bool,
    ) {
        use std::os::unix::fs::symlink;

        let (parentdir, parenttypedir) =
            self.populate_parent_device(parent, mdev_type, 1, "", "", None);

        let parentdevdir = parentdir.join(uuid);
        fs::create_dir_all(&parentdevdir).expect("Unable to setup parent device dir");

        let devdir = self.mdev_base().join(uuid);
        fs::create_dir_all(devdir.parent().unwrap()).expect("Unable to setup mdev dir");
        symlink(&parentdevdir, &devdir).expect("Unable to setup parent dir");

        let typefile = devdir.join("mdev_type");
        symlink(&parenttypedir, &typefile).expect("Unable to setup mdev type");

        if break_type {
            if remove_type_link {
                // removes directory link is pointing to
                fs::remove_dir_all(parenttypedir)
                    .expect("Unable to remove setup for supported mdev type");
            } else {
                fs::remove_file(typefile)
                    .expect("Unable to remove setup for link to supported mdev type");
            }
        }
        if break_parent {
            if remove_parent_link {
                // removes directory link is pointing to
                fs::remove_dir_all(parentdevdir).expect("Unable to remove setup parent dir");
            } else {
                // remove link itself
                fs::remove_file(devdir).expect("Unable to remove setup for dev link to parent dir");
            }
        }
    }

    // set up a script in the test environment to simulate a callout
    fn populate_callout_script(&self, filename: &str) {
        self.populate_callout_script_full(filename, None, true)
    }

    // set up a script in the test environment to simulate a callout
    fn populate_callout_script_full(
        &self,
        filename: &str,
        destname: Option<&str>,
        default_dir: bool,
    ) {
        let calloutscriptdir: PathBuf = [TEST_DATA_DIR, "callouts"].iter().collect();
        let calloutscript = calloutscriptdir.join(filename);
        let dest = match default_dir {
            true => self.callout_dir(),
            false => self.old_callout_dir(),
        }
        .join(destname.unwrap_or(filename));
        assert!(calloutscript.exists());

        /* Because the test suite is multi-threaded, we end up having the same flaky failures
         * described in this bug: https://github.com/golang/go/issues/22315. When we copy the
         * callout script into the test environment, another thread might be in the middle of
         * forking. This fork would then inherit the open writable file descriptor from the parent.
         * If that child process file descriptor stays open until we try to execute this callout
         * script, the script will fail to run and we'll get an ETXTBSY error from the OS. In order
         * to avoid this, we need to avoid the possibility of having any open writable file
         * descriptors to executable files in the parent process that could be inherited by forks
         * in other threads. Copying executable files in a child process avoid this. */
        match unsafe { fork() }.expect("failed to fork") {
            ForkResult::Parent { child } => {
                waitpid(child, None).expect("Failed to wait for child");
            }
            ForkResult::Child => {
                fs::copy(calloutscript, dest).expect("Unable to copy callout script");
                unsafe {
                    libc::_exit(0);
                }
            }
        }
    }

    // set up a few files in the test environment to simulate a parent device that supports
    // mediated devices
    fn populate_parent_device(
        &self,
        parent: &str,
        supported_type: &str,
        instances: i32,
        device_api: &str,
        name: &str,
        description: Option<&str>,
    ) -> (PathBuf, PathBuf) {
        let parentdir = self.parent_base().join(parent);
        let parenttypedir = parentdir.join("mdev_supported_types").join(supported_type);
        fs::create_dir_all(&parenttypedir).expect("Unable to setup mdev parent type");

        let instancefile = parenttypedir.join("available_instances");
        fs::write(instancefile, format!("{}", instances))
            .expect("Unable to write available_instances");

        let apifile = parenttypedir.join("device_api");
        fs::write(apifile, device_api).expect("Unable to write device_api");

        let namefile = parenttypedir.join("name");
        fs::write(namefile, name).expect("Unable to write name");

        if let Some(desc) = description {
            let descfile = parenttypedir.join("description");
            fs::write(descfile, desc).expect("Unable to write description");
        }

        (parentdir, parenttypedir)
    }

    fn compare_to_file(&self, filename: &str, actual: &str) {
        let path = self.datapath.join(filename);
        if get_flag(REGEN_FLAG) {
            regen(&path, actual).expect("Failed to regenerate expected output");
        }
        let expected = fs::read_to_string(path).unwrap_or_else(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                println!(
                    "File {:?} not found, run tests with {}=1 to automatically \
                         generate expected output",
                    filename, REGEN_FLAG
                );
            }
            Default::default()
        });

        assert_eq!(expected, actual);
    }

    fn unused_file(&self, filename: &str) {
        let path = self.datapath.join(filename);
        assert!(
            !path.exists(),
            "Unused file {:?} found! Please remove it.",
            path
        );
    }

    fn load_from_json(self: &Rc<Self>, uuid: &str, parent: &str, filename: &str) -> Result<MDev> {
        let path = self.datapath.join(filename);
        let uuid = Uuid::parse_str(uuid);
        assert!(uuid.is_ok());
        let uuid = uuid.unwrap();
        let mut dev = MDev::new(self.clone(), uuid);

        let jsonstr = fs::read_to_string(path)?;
        let jsonval: serde_json::Value = serde_json::from_str(&jsonstr)?;
        dev.load_from_json(parent.to_string(), &jsonval)?;

        Ok(dev)
    }

    fn assert_result<T: std::fmt::Debug>(
        &self,
        res: Result<T>,
        expect: Expect,
        msg: Option<&str>,
    ) -> Result<T> {
        let mut testname = format!("{}/{}", self.name, self.case);
        if let Some(msg) = msg {
            testname = format!("{}/{}", testname, msg);
        }
        match expect {
            Expect::Fail(msg) => {
                let e = res.expect_err(format!("Expected {} to fail", testname).as_str());
                if let Some(msg) = msg {
                    assert_eq!(msg, e.to_string());
                }
                Err(anyhow!(e))
            }
            Expect::Pass => Ok(res.unwrap_or_else(|e| {
                panic!("Expected {} to pass but result was {:?}", testname, e)
            })),
        }
    }
}

fn get_flag(varname: &str) -> bool {
    env::var(varname).map_or(false, |s| matches!(s.trim().parse::<i32>(), Ok(n) if n > 0))
}

fn regen(filename: &PathBuf, data: &str) -> Result<()> {
    let parentdir = filename.parent().unwrap();
    fs::create_dir_all(parentdir)?;

    fs::write(filename, data.as_bytes())?;
    println!("Regenerated expected data file {:?}", filename);
    Ok(())
}

const REGEN_FLAG: &str = "MDEVCTL_TEST_REGENERATE_OUTPUT";

fn test_load_json_helper(uuid: &str, parent: &str, expect: Expect) {
    let test: Rc<TestEnvironment> = TestEnvironment::new("load-json", uuid);

    let res = test.load_from_json(uuid, parent, &format!("{}.in", uuid));
    if let Ok(dev) = test.assert_result(res, expect, None) {
        let jsonval = dev.to_json(false).unwrap();
        let jsonstr = serde_json::to_string_pretty(&jsonval).unwrap();

        test.compare_to_file(&format!("{}.out", uuid), &jsonstr);
        assert_eq!(uuid, dev.uuid.hyphenated().to_string());
        assert_eq!(Some(parent.to_string()), dev.parent);
    }
}

#[test]
fn test_load_json() {
    init();

    test_load_json_helper(
        "c07ab7b2-8aa2-427a-91c6-ffc949bb77f9",
        "0000:00:02.0",
        Expect::Pass,
    );
    test_load_json_helper(
        "783e6dbb-ea0e-411f-94e2-717eaad438bf",
        "0001:00:03.1",
        Expect::Pass,
    );
    test_load_json_helper(
        "5269fe7a-18d1-48ad-88e1-3fda4176f536",
        "0000:00:03.0",
        Expect::Pass,
    );
    test_load_json_helper(
        "5269fe7a-18d1-48ad-88e1-3fda4176f536",
        "0000:00:03.0",
        Expect::Pass,
    );
    // json file has malformed attributes - an array of one object with multiple fields
    test_load_json_helper(
        "b6f7e33f-ea28-4f9d-8c42-797ff0ec2888",
        "0000:00:03.0",
        Expect::Fail(None),
    );
    // json file has malformed attributes - an array of strings
    test_load_json_helper(
        "fe7a39db-973b-47b4-9b77-1d7b97267d59",
        "0000:00:03.0",
        Expect::Fail(None),
    );
    // json file has malformed attributes - no array
    test_load_json_helper(
        "37ccb149-a0ce-49e3-8391-a952ef07bdc2",
        "0000:00:03.0",
        Expect::Fail(None),
    );
}