diff options
author | Alex Solomes <alex.solomes@softwareag.com> | 2022-06-09 17:09:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 17:09:06 +0100 |
commit | c95fe8e202f470e79123bf4c992f779df249ec42 (patch) | |
tree | 848540c0275a70b143440edfdacea0533e28d8ac /plugins/c8y_configuration_plugin | |
parent | b91cc0205399fef3b73e4a41e56b02fd8cfc7d87 (diff) |
Testing utility: TempTedgeDir POC (#1148)
* temp tedge dir poc
Signed-off-by: initard <solo@softwareag.com>
* renaming crate and removing TedgeChildTempDir
- renamed the crate to tedge_test_utils
- removed TedgeChildTempDir as TempTedgeDir already had the same
functions
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing agent.rs tests to use TempTedgeDir
- changing agent.rs to use TempTedgeDir
- un-ignoring a test to check if agent restart creates a file in the
right place
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing tedge_mapper tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing tedge_config tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing c8y_configuration_plugin tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing tedge_apama_plugin tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing logged_command tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* adding another method to TempTedgeDir
Signed-off-by: initard <solo@softwareag.com>
Co-authored-by: initard <solo@softwareag.com>
Diffstat (limited to 'plugins/c8y_configuration_plugin')
-rw-r--r-- | plugins/c8y_configuration_plugin/Cargo.toml | 2 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/config.rs | 19 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/main.rs | 3 |
3 files changed, 10 insertions, 14 deletions
diff --git a/plugins/c8y_configuration_plugin/Cargo.toml b/plugins/c8y_configuration_plugin/Cargo.toml index 20a27927..171a73e3 100644 --- a/plugins/c8y_configuration_plugin/Cargo.toml +++ b/plugins/c8y_configuration_plugin/Cargo.toml @@ -37,6 +37,6 @@ mockall = "0.11" mockito = "0.31" mqtt_tests = { path = "../../crates/tests/mqtt_tests" } serial_test = "0.6" -tempfile = "3.3" +tedge_test_utils = { path = "../../crates/tests/tedge_test_utils" } test-case = "2.0" toml = "0.5" diff --git a/plugins/c8y_configuration_plugin/src/config.rs b/plugins/c8y_configuration_plugin/src/config.rs index 892d157a..d738bda2 100644 --- a/plugins/c8y_configuration_plugin/src/config.rs +++ b/plugins/c8y_configuration_plugin/src/config.rs @@ -182,9 +182,7 @@ impl PluginConfig { #[cfg(test)] mod tests { use super::*; - use std::io::Write; - use std::path::PathBuf; - use tempfile::TempDir; + use tedge_test_utils::fs::TempTedgeDir; use test_case::test_case; const PLUGIN_CONFIG_FILE: &str = "c8y-configuration-plugin.toml"; @@ -397,8 +395,8 @@ mod tests { file_content: &str, expected_config: PluginConfig, ) -> anyhow::Result<()> { - let (_dir, config_root_path) = create_temp_plugin_config(file_content)?; - let tmp_path_to_plugin_config = config_root_path.join(PLUGIN_CONFIG_FILE); + let dir = create_temp_plugin_config(file_content)?; + let tmp_path_to_plugin_config = dir.path().join(PLUGIN_CONFIG_FILE); let tmp_path_to_plugin_config_str = tmp_path_to_plugin_config.as_path().display().to_string(); @@ -470,12 +468,9 @@ mod tests { } // Need to return TempDir, otherwise the dir will be deleted when this function ends. - fn create_temp_plugin_config(content: &str) -> std::io::Result<(TempDir, PathBuf)> { - let temp_dir = TempDir::new()?; - let config_root = temp_dir.path().to_path_buf(); - let config_file_path = config_root.join(PLUGIN_CONFIG_FILE); - let mut file = fs::File::create(config_file_path.as_path())?; - file.write_all(content.as_bytes())?; - Ok((temp_dir, config_root)) + fn create_temp_plugin_config(content: &str) -> std::io::Result<TempTedgeDir> { + let temp_dir = TempTedgeDir::new(); + temp_dir.file(PLUGIN_CONFIG_FILE).with_raw_content(content); + Ok(temp_dir) } } diff --git a/plugins/c8y_configuration_plugin/src/main.rs b/plugins/c8y_configuration_plugin/src/main.rs index 6f1d7592..eb4b8272 100644 --- a/plugins/c8y_configuration_plugin/src/main.rs +++ b/plugins/c8y_configuration_plugin/src/main.rs @@ -250,6 +250,7 @@ mod tests { use c8y_api::http_proxy::MockC8YHttpProxy; use mockall::predicate; use std::{path::Path, time::Duration}; + use tedge_test_utils::fs::TempTedgeDir; const TEST_TIMEOUT_MS: Duration = Duration::from_millis(5000); @@ -272,7 +273,7 @@ mod tests { ) .return_once(|_path, _type| Ok("http://server/some/test/config/url".to_string())); - let tmp_dir = tempfile::tempdir()?; + let tmp_dir = TempTedgeDir::new(); // Run the plugin's runtime logic in an async task tokio::spawn(async move { |