diff options
Diffstat (limited to 'crates/common/tedge_config/tests/test_tedge_config.rs')
-rw-r--r-- | crates/common/tedge_config/tests/test_tedge_config.rs | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/crates/common/tedge_config/tests/test_tedge_config.rs b/crates/common/tedge_config/tests/test_tedge_config.rs index 26118adf..1dc3aa4f 100644 --- a/crates/common/tedge_config/tests/test_tedge_config.rs +++ b/crates/common/tedge_config/tests/test_tedge_config.rs @@ -1,8 +1,9 @@ use assert_matches::assert_matches; use std::convert::TryFrom; use std::io::Write; +use std::net::{IpAddr, Ipv4Addr}; use tedge_config::*; -use tempfile::TempDir; +use tedge_test_utils::fs::TempTedgeDir; #[test] fn test_parse_config_with_all_values() -> Result<(), TEdgeConfigError> { @@ -30,6 +31,7 @@ external_bind_interface = "wlan0" external_capath = "ca.pem" external_certfile = "cert.pem" external_keyfile = "key.pem" +bind_address = "0.0.0.0" [tmp] path = "/some/value" @@ -75,8 +77,8 @@ path = "/some/value" assert_eq!(config.query(MqttExternalPortSetting)?, Port(2345)); assert_eq!( - config.query(MqttExternalBindAddressSetting)?.as_str(), - "0.0.0.0" + config.query(MqttExternalBindAddressSetting)?, + IpAddress::try_from("0.0.0.0".to_string()).unwrap() ); assert_eq!( @@ -99,10 +101,13 @@ path = "/some/value" FilePath::from("key.pem") ); + assert_eq!(config.query(TmpPathSetting)?, FilePath::from("/some/value")); + assert_eq!( - config.query(TmpPathDefaultSetting)?, - FilePath::from("/some/value") + config.query(MqttBindAddressSetting)?, + IpAddress::try_from("0.0.0.0".to_string()).unwrap() ); + Ok(()) } @@ -124,6 +129,7 @@ mapper_timestamp = false [mqtt] port = 1883 +bind_address = "0.0.0.0" "#; let (_tempdir, config_location) = create_temp_tedge_config(toml_conf)?; @@ -139,11 +145,12 @@ port = 1883 let updated_azure_url = "OtherAzure.azure-devices.net"; let updated_mqtt_port = Port(2345); let updated_mqtt_external_port = Port(3456); - let updated_mqtt_external_bind_address = "localhost"; + let updated_mqtt_external_bind_address = IpAddress::default(); let updated_mqtt_external_bind_interface = "eth0"; let updated_mqtt_external_capath = "/some/path"; let updated_mqtt_external_certfile = "cert.pem"; let updated_mqtt_external_keyfile = "key.pem"; + let updated_mqtt_bind_address = IpAddress(std::net::IpAddr::V4(Ipv4Addr::LOCALHOST)); { let mut config = config_repo.load()?; @@ -187,8 +194,9 @@ port = 1883 config.update(MqttExternalPortSetting, updated_mqtt_external_port)?; config.update( MqttExternalBindAddressSetting, - updated_mqtt_external_bind_address.to_string(), + updated_mqtt_external_bind_address.clone(), )?; + config.update( MqttExternalBindInterfaceSetting, updated_mqtt_external_bind_interface.to_string(), @@ -205,6 +213,7 @@ port = 1883 MqttExternalKeyfileSetting, FilePath::from(updated_mqtt_external_keyfile), )?; + config.update(MqttBindAddressSetting, updated_mqtt_bind_address.clone())?; config_repo.store(&config)?; } @@ -240,7 +249,7 @@ port = 1883 updated_mqtt_external_port ); assert_eq!( - config.query(MqttExternalBindAddressSetting)?.as_str(), + config.query(MqttExternalBindAddressSetting)?, updated_mqtt_external_bind_address ); assert_eq!( @@ -259,6 +268,10 @@ port = 1883 config.query(MqttExternalKeyfileSetting)?, FilePath::from(updated_mqtt_external_keyfile) ); + assert_eq!( + config.query(MqttBindAddressSetting)?, + updated_mqtt_bind_address + ); } Ok(()) @@ -306,6 +319,10 @@ fn test_parse_config_with_only_device_configuration() -> Result<(), TEdgeConfigE assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true)); assert_eq!(config.query(MqttPortSetting)?, Port(1883)); + assert_eq!( + config.query(MqttBindAddressSetting)?, + IpAddress::try_from("127.0.0.1".to_string()).unwrap() + ); Ok(()) } @@ -354,6 +371,10 @@ url = "your-tenant.cumulocity.com" assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true)); assert_eq!(config.query(MqttPortSetting)?, Port(1883)); + assert_eq!( + config.query(MqttBindAddressSetting)?, + IpAddress(IpAddr::V4(Ipv4Addr::LOCALHOST)) + ); Ok(()) } @@ -402,6 +423,10 @@ url = "MyAzure.azure-devices.net" assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true)); assert_eq!(config.query(MqttPortSetting)?, Port(1883)); + assert_eq!( + config.query(MqttBindAddressSetting)?, + IpAddress(IpAddr::V4(Ipv4Addr::LOCALHOST)) + ); Ok(()) } @@ -410,6 +435,7 @@ fn test_parse_config_with_only_mqtt_configuration() -> Result<(), TEdgeConfigErr let toml_conf = r#" [mqtt] port = 2222 +bind_address = "1.2.3.4" "#; let (_tempdir, config_location) = create_temp_tedge_config(toml_conf)?; @@ -447,6 +473,10 @@ port = 2222 assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true)); assert_eq!(config.query(MqttPortSetting)?, Port(2222)); + assert_eq!( + config.query(MqttBindAddressSetting)?, + IpAddress::try_from("1.2.3.4".to_string()).unwrap() + ); Ok(()) } @@ -564,6 +594,10 @@ fn test_parse_config_empty_file() -> Result<(), TEdgeConfigError> { assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true)); assert_eq!(config.query(MqttPortSetting)?, Port(1883)); + assert_eq!( + config.query(MqttBindAddressSetting)?, + IpAddress(IpAddr::V4(Ipv4Addr::LOCALHOST)) + ); Ok(()) } @@ -596,6 +630,10 @@ fn test_parse_config_no_config_file() -> Result<(), TEdgeConfigError> { assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true)); assert_eq!(config.query(MqttPortSetting)?, Port(1883)); + assert_eq!( + config.query(MqttBindAddressSetting)?, + IpAddress(IpAddr::V4(Ipv4Addr::LOCALHOST)) + ); Ok(()) } @@ -840,11 +878,10 @@ cert_path = "/path/to/cert" Ok(()) } -fn create_temp_tedge_config(content: &str) -> std::io::Result<(TempDir, TEdgeConfigLocation)> { - let dir = TempDir::new()?; +fn create_temp_tedge_config(content: &str) -> std::io::Result<(TempTedgeDir, TEdgeConfigLocation)> { + let dir = TempTedgeDir::new(); + dir.file("tedge.toml").with_raw_content(content); let config_location = TEdgeConfigLocation::from_custom_root(dir.path()); - let mut file = std::fs::File::create(config_location.tedge_config_file_path())?; - file.write_all(content.as_bytes())?; Ok((dir, config_location)) } @@ -857,7 +894,11 @@ fn dummy_tedge_config_defaults() -> TEdgeConfigDefaults { default_mapper_timestamp: Flag(true), default_mqtt_port: Port(1883), default_tmp_path: FilePath::from("/tmp"), + default_logs_path: FilePath::from("/var/log"), + default_run_path: FilePath::from("/run"), default_device_type: String::from("test"), + default_mqtt_bind_address: IpAddress(IpAddr::V4(Ipv4Addr::LOCALHOST)), + default_c8y_smartrest_templates: TemplatesSet::default(), } } |