use assert_matches::assert_matches;
use std::convert::TryFrom;
use std::io::Write;
use std::net::{IpAddr, Ipv4Addr};
use tedge_config::*;
use tempfile::TempDir;
#[test]
fn test_parse_config_with_all_values() -> Result<(), TEdgeConfigError> {
let toml_conf = r#"
[device]
key_path = "/path/to/key"
cert_path = "/path/to/cert"
[c8y]
url = "your-tenant.cumulocity.com"
root_cert_path = "/path/to/c8y/root/cert"
connect = "true"
[az]
url = "MyAzure.azure-devices.net"
root_cert_path = "/path/to/azure/root/cert"
connect = "false"
mapper_timestamp = true
[mqtt]
port = 1234
external_port = 2345
external_bind_address = "0.0.0.0"
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"
"#;
let (_tempdir, config_location) = create_temp_tedge_config(toml_conf)?;
let config_defaults = dummy_tedge_config_defaults();
let config =
TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?;
assert!(config.query_optional(DeviceIdSetting).is_err());
assert_eq!(
config.query(DeviceKeyPathSetting)?,
FilePath::from("/path/to/key")
);
assert_eq!(
config.query(DeviceCertPathSetting)?,
FilePath::from("/path/to/cert")
);
assert_eq!(
config.query(C8yUrlSetting)?.as_str(),
"your-tenant.cumulocity.com"
);
assert_eq!(
config.query(C8yRootCertPathSetting)?,
FilePath::from("/path/to/c8y/root/cert")
);
assert_eq!(
config.query(AzureUrlSetting)?.as_str(),
"MyAzure.azure-devices.net"
);
assert_eq!(
config.query(AzureRootCertPathSetting)?,
FilePath::from("/path/to/azure/root/cert")
);
assert_eq!(config.query(AzureMapperTimestamp)?, Flag(true));
assert_eq!(config.query(MqttPortSetting)?, Port(1234));
assert_eq!(config.query(MqttExternalPortSetting)?, Port(2345));
assert_eq!(
config.query(MqttExternalBindAddressSetting)?,
IpAddress::try_from("0.0.0.0".to_string()).unwrap()
);
assert_eq!(
config.query(MqttExternalBindInterfaceSetting)?.as_str(),
"wlan0"
);
assert_eq!(
config.query(MqttExternalCAPathSetting)?,
FilePath::from("ca.pem")
);
assert_eq!(
config.query(MqttExternalCertfileSetting)?,
FilePath::from("cert.pem")
);
assert_eq!(
config.query(MqttExternalKeyfileSetting)?,
FilePath::from("key.pem")
);
assert_eq!(config.query(TmpPathSetting)?, FilePath::from("/some/value"));
assert_eq!(
config.query(MqttBindAddressSetting)?,
IpAddress::try_from("0.0.0.0".to_string()).unwrap()
);
Ok(())
}
#[test]
fn test_store_config_with_all_values() -> Result<(), TEdgeConfigError> {
let toml_conf = r#"
[device]
key_path = "/path/to/key"
cert_path = "/path/to/cert"
[c8y]
url = "your-tenant.cumulocity.com"
root_cert_path = "/path/to/c8y/root/cert"
[az]
url = "MyAzure.azure-devices.net"
root_cert_path = "/path/to/azure/root/cert"
mapper_timestamp = false
[mqtt]
port = 1883
bind_address = "0.0.0.0"
"#;
let (_tempdir, config_location) = create_temp_tedge_config(toml_conf)?;
let config_defaults = TEdgeConfigDefaults {
default_c8y_root_cert_path: FilePath::from("default_c8y_root_cert_path"),
default_azure_root_cert_path: FilePath::from("default_azure_root_cert_path"),
..dummy_tedge_config_defaults()
};
let config_repo = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults);
let updated_c8y_url = "other-tenant.cumulocity.com";
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 = 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()?