in rust/azure_iot_operations_mqtt/src/connection_settings.rs [564:643]
fn from_environment_full_configuration(auth_env_var: &str, auth_env_value: Option<&str>) {
temp_env::with_vars(
[
("AIO_MQTT_CLIENT_ID", Some("test-client-id")),
("AIO_BROKER_HOSTNAME", Some("test.hostname.com")),
("AIO_BROKER_TCP_PORT", Some("1883")),
("AIO_MQTT_KEEP_ALIVE", Some("60")),
("AIO_MQTT_SESSION_EXPIRY", Some("3600")),
("AIO_MQTT_CLEAN_START", Some("true")),
("AIO_MQTT_USERNAME", Some("test-username")),
("AIO_MQTT_USE_TLS", Some("true")),
("AIO_TLS_CA_FILE", Some("/path/to/ca/file")),
("AIO_TLS_CERT_FILE", Some("/path/to/cert/file")),
("AIO_TLS_KEY_FILE", Some("/path/to/key/file")),
(
"AIO_TLS_KEY_PASSWORD_FILE",
Some("/path/to/key/password/file"),
),
// Set default None values for mutually exclusive auth vars, then override
("AIO_MQTT_PASSWORD_FILE", None),
("AIO_SAT_FILE", None),
(auth_env_var, auth_env_value), // This will override one of the above two vars
],
|| {
let builder = MqttConnectionSettingsBuilder::from_environment().unwrap();
// Validate that all values from env variables were set on the builder
assert_eq!(builder.client_id, Some("test-client-id".to_string()));
assert_eq!(builder.hostname, Some("test.hostname.com".to_string()));
assert_eq!(builder.tcp_port, Some(1883));
assert_eq!(builder.keep_alive, Some(Duration::from_secs(60)));
assert_eq!(builder.session_expiry, Some(Duration::from_secs(3600)));
assert_eq!(builder.clean_start, Some(true));
assert_eq!(builder.username, Some(Some("test-username".to_string())));
assert_eq!(builder.use_tls, Some(true));
assert_eq!(builder.ca_file, Some(Some("/path/to/ca/file".to_string())));
assert_eq!(
builder.cert_file,
Some(Some("/path/to/cert/file".to_string()))
);
assert_eq!(
builder.key_file,
Some(Some("/path/to/key/file".to_string()))
);
assert_eq!(
builder.key_password_file,
Some(Some("/path/to/key/password/file".to_string()))
);
if auth_env_var == "AIO_MQTT_PASSWORD_FILE" {
assert_eq!(
builder.password_file,
Some(Some("/path/to/password/file".to_string()))
);
} else if auth_env_var == "AIO_SAT_FILE" {
assert_eq!(
builder.sat_file,
Some(Some("/path/to/sat/file".to_string()))
);
} else {
panic!("Unexpected auth_env_var: {auth_env_var}");
}
// Validate that the default values were set correctly for values that were not
// provided
let default_builder = MqttConnectionSettingsBuilder::default();
assert_eq!(builder.receive_max, default_builder.receive_max);
assert_eq!(
builder.receive_packet_size_max,
default_builder.receive_packet_size_max
);
assert_eq!(
builder.connection_timeout,
default_builder.connection_timeout
);
assert_eq!(builder.password, default_builder.password);
// Validate that the settings struct can be built using only the values provided
// from the environment
assert!(builder.build().is_ok());
},
);
}