summaryrefslogtreecommitdiffstats
path: root/crates/tests/tedge_test_utils/src/fs.rs
blob: 45ed9ff028fda34288a7525b5c543ba0cddd592b (plain)
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
use std::{
    fs::{self, OpenOptions},
    io::Write,
    path::{Path, PathBuf},
    sync::Arc,
};
use tempfile::TempDir;

pub struct TempTedgeDir {
    pub temp_dir: Arc<TempDir>,
    current_file_path: PathBuf,
}

pub struct TempTedgeFile {
    file_path: PathBuf,
}

impl TempTedgeDir {
    pub fn new() -> Self {
        let temp_dir = TempDir::new().unwrap();
        let current_file_path = temp_dir.path().to_path_buf();
        TempTedgeDir {
            temp_dir: Arc::new(temp_dir),
            current_file_path,
        }
    }

    pub fn dir(&self, directory_name: &str) -> TempTedgeDir {
        let root = self.temp_dir.path().to_path_buf();
        let path = root
            .join(self.current_file_path.to_path_buf())
            .join(directory_name);

        if !path.exists() {
            let () = fs::create_dir(&path).unwrap();
        };

        TempTedgeDir {
            temp_dir: self.temp_dir.clone(),
            current_file_path: path,
        }
    }

    pub fn file(&self, file_name: &str) -> TempTedgeFile {
        let root = self.temp_dir.path().to_path_buf();
        let path = root
            .join(self.current_file_path.to_path_buf())
            .join(file_name);

        if !path.exists() {
            let _file = fs::File::create(&path).unwrap();
        };
        TempTedgeFile { file_path: path }
    }

    pub fn path(&self) -> &Path {
        Path::new(self.temp_dir.path())
    }

    pub fn to_path_buf(&self) -> PathBuf {
        PathBuf::from(self.path())
    }
}

impl TempTedgeFile {
    pub fn with_raw_content(self, content: &str) {
        let mut file = OpenOptions::new()
            .write(true)
            .create(false)
            .open(self.file_path)
            .unwrap();
        file.write_all(content.as_bytes()).unwrap();
    }

    pub fn with_toml_content(self, content: toml::Value) {
        let mut file = OpenOptions::new()
            .write(true)
            .create(false)
            .open(self.file_path)
            .unwrap();
        let file_content = content.to_string();
        file.write_all(file_content.as_bytes()).unwrap();
    }

    pub fn path(&self) -> &Path {
        Path::new(&self.file_path)
    }

    pub fn to_path_buf(&self) -> PathBuf {
        PathBuf::from(self.path())
    }
}

pub fn create_full_tedge_dir_structure() {
    let ttd = TempTedgeDir::new();
    ttd.file("tedge.toml");
    ttd.dir(".agent").file("current-operation");
    ttd.dir("c8y")
        .file("c8y-log-plugin.toml")
        .with_toml_content(toml::toml! {
            files = [
                {type = "software-management", path = "/var/log/tedge/agent/software-*" }
            ]
        });
    ttd.dir("contrib").dir("collectd").file("collectd.conf");
    ttd.dir("device").file("inventory.json");
    ttd.dir("device-certs");
    ttd.dir("mosquitto-conf").file("c8y-bridge.conf");
    ttd.dir("mosquitto-conf").file("tedge-mosquitto.conf");
    ttd.dir("operations")
        .dir("c8y")
        .file("c8y_LogfileRequest")
        .with_raw_content("");
    ttd.dir("operations").dir("c8y").file("c8y_Restart");
    ttd.dir("operations").dir("c8y").file("c8y_SoftwareUpdate");
    ttd.dir("sm-plugins").file("apt");
}

#[cfg(test)]
mod tests {
    use super::TempTedgeDir;
    use std::{io::Read, path::Path};

    #[test]
    fn assert_dir_file_and_content() -> Result<(), anyhow::Error> {
        let tedge_dir = TempTedgeDir::new();
        tedge_dir.dir("c8y").file("c8y-log-plugin.toml");
        tedge_dir
            .dir("operations")
            .dir("c8y")
            .file("c8y_Restart")
            .with_toml_content(toml::toml! {
                files = []
            });

        assert!(Path::new(&format!(
            "{}/c8y/c8y-log-plugin.toml",
            &tedge_dir.temp_dir.path().to_str().unwrap()
        ))
        .exists());

        assert!(Path::new(&format!(
            "{}/operations/c8y/c8y_Restart",
            &tedge_dir.temp_dir.path().to_str().unwrap()
        ))
        .exists());
        Ok(())
    }

    #[test]
    fn test_with_toml() -> Result<(), anyhow::Error> {
        let tedge_dir = TempTedgeDir::new();
        tedge_dir
            .dir("c8y")
            .file("c8y_log_plugin.toml")
            .with_toml_content(toml::toml! {
                files = [
                    { type = "apt", path = "/var/log/apt/history.log"}
                ]
            });
        let file_path = &format!(
            "{}/c8y/c8y_log_plugin.toml",
            &tedge_dir.temp_dir.path().to_str().unwrap()
        );
        assert!(Path::new(&file_path).exists());

        let mut file_content = String::new();
        let mut file = std::fs::File::open(&file_path).unwrap();
        file.read_to_string(&mut file_content).unwrap();

        let as_toml: toml::Value = toml::from_str(&file_content).unwrap();
        assert_eq!(
            as_toml,
            toml::toml! {
                files = [
                    { type = "apt", path = "/var/log/apt/history.log"}
                ]
            }
        );

        Ok(())
    }

    #[test]
    fn test_multiple_files_in_same_dir() -> Result<(), anyhow::Error> {
        let ttd = TempTedgeDir::new();
        let operations_dir = ttd.dir("operations");
        operations_dir.dir("c8y").file("c8y_Restart");
        operations_dir.dir("c8y").file("c8y_SoftwareUpdate");

        assert!(Path::new(&format!(
            "{}/operations/c8y/c8y_Restart",
            &ttd.temp_dir.path().to_str().unwrap()
        ))
        .exists());

        assert!(Path::new(&format!(
            "{}/operations/c8y/c8y_SoftwareUpdate",
            &ttd.temp_dir.path().to_str().unwrap()
        ))
        .exists());
        Ok(())
    }
}