container_device_interface/specs/
oci.rs

1use std::path::PathBuf;
2
3use anyhow::Result;
4use oci_spec::runtime::{
5    Hook as OCIHook, LinuxDevice, LinuxDeviceType, LinuxIntelRdt, Mount as OCIMount,
6};
7
8use crate::specs::config::{DeviceNode, Hook as CDIHook, IntelRdt, Mount as CDIMount};
9
10impl CDIHook {
11    pub fn to_oci(&self) -> Result<OCIHook> {
12        let mut oci_hook: OCIHook = Default::default();
13        oci_hook.set_path(PathBuf::from(&self.path));
14        oci_hook.set_args(self.args.clone());
15        oci_hook.set_env(self.env.clone());
16        oci_hook.set_timeout(self.timeout);
17
18        Ok(oci_hook)
19    }
20}
21
22impl CDIMount {
23    pub fn to_oci(&self) -> Result<OCIMount> {
24        let mut oci_mount: OCIMount = Default::default();
25        oci_mount.set_source(Some(PathBuf::from(&self.host_path)));
26        oci_mount.set_destination(PathBuf::from(&self.container_path));
27        oci_mount.set_typ(self.r#type.clone());
28        oci_mount.set_options(self.options.clone());
29
30        Ok(oci_mount)
31    }
32}
33
34impl DeviceNode {
35    pub fn to_oci(&self) -> Result<LinuxDevice> {
36        let mut linux_device: LinuxDevice = Default::default();
37        linux_device.set_path(PathBuf::from(&self.path));
38        if let Some(_typ) = &self.r#type {
39            linux_device.set_typ(LinuxDeviceType::C);
40        }
41        if let Some(maj) = self.major {
42            linux_device.set_major(maj);
43        };
44        if let Some(min) = self.minor {
45            linux_device.set_minor(min);
46        }
47        linux_device.set_file_mode(self.file_mode);
48        linux_device.set_uid(self.uid);
49        linux_device.set_gid(self.gid);
50
51        Ok(linux_device)
52    }
53}
54
55impl IntelRdt {
56    pub fn to_oci(&self) -> Result<LinuxIntelRdt> {
57        let mut intel_rdt: LinuxIntelRdt = Default::default();
58        intel_rdt.set_clos_id(self.clos_id.clone());
59        intel_rdt.set_l3_cache_schema(self.l3_cache_schema.clone());
60        intel_rdt.set_mem_bw_schema(self.mem_bw_schema.clone());
61        intel_rdt.set_enable_cmt(Some(self.enable_cmt));
62        intel_rdt.set_enable_mbm(Some(self.enable_mbm));
63
64        Ok(intel_rdt)
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use oci_spec::runtime::LinuxDevice;
71    use std::path::PathBuf;
72
73    use crate::specs::{
74        config::DeviceNode,
75        oci::{CDIHook, CDIMount, OCIHook, OCIMount},
76    };
77
78    #[test]
79    fn test_hooks_to_oci() {
80        let cdi_hooks = CDIHook {
81            hook_name: "x".to_owned(),
82            path: "y".to_owned(),
83            args: None,
84            env: Some(vec!["n".to_owned(), "v".to_owned()]),
85            timeout: Some(100_i64),
86        };
87
88        let oci_hook: OCIHook = cdi_hooks.to_oci().unwrap();
89        assert_eq!(PathBuf::from(cdi_hooks.path), oci_hook.path().clone());
90        assert_eq!(None, oci_hook.args().clone());
91        assert_eq!(cdi_hooks.env, oci_hook.env().clone());
92        assert_eq!(cdi_hooks.timeout, oci_hook.timeout());
93    }
94
95    #[test]
96    fn test_mount_to_oci() {
97        let cdi_mount = CDIMount {
98            host_path: "x".to_owned(),
99            container_path: "c".to_owned(),
100            r#type: Some("t".to_owned()),
101            options: None,
102        };
103
104        let oci_mount: OCIMount = cdi_mount.to_oci().unwrap();
105        assert_eq!(
106            Some(PathBuf::from(cdi_mount.host_path)),
107            oci_mount.source().clone()
108        );
109        assert_eq!(
110            PathBuf::from(cdi_mount.container_path),
111            oci_mount.destination().clone()
112        );
113        assert_eq!(cdi_mount.r#type, oci_mount.typ().clone());
114    }
115
116    #[test]
117    fn test_device_node_to_oci() {
118        let dev_node = DeviceNode {
119            path: "p".to_owned(),
120            host_path: Some("hostp".to_owned()),
121            major: Some(251),
122            minor: Some(0),
123            ..Default::default()
124        };
125
126        let linux_dev: LinuxDevice = dev_node.to_oci().unwrap();
127        assert_eq!(PathBuf::from(dev_node.path), linux_dev.path().clone());
128        assert_eq!(dev_node.major, Some(linux_dev.major()));
129        assert_eq!(dev_node.minor, Some(linux_dev.minor()));
130    }
131}