in metalos/lib/systemd_generator_lib/src/systemd_generator_lib.rs [405:525]
fn test_materialize_boot_info() -> Result<()> {
let log = slog::Logger::root(slog_glog_fmt::default_drain(), o!());
let ts = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let tmpdir = std::env::temp_dir().join(format!("test_environment_{:?}", ts));
let env_dir = tmpdir.join("env_dir");
let deps_dir = tmpdir.join("deps_dir");
let network_unit_dir = tmpdir.join("network_unit_dir");
std::fs::create_dir(&tmpdir).context("failed to make tmp dir")?;
std::fs::create_dir(&env_dir).context("failed to make env_dir")?;
std::fs::create_dir(&deps_dir).context("failed to make deps")?;
std::fs::create_dir(&network_unit_dir).context("failed to make network_unit_dir")?;
let env = TestEnvironment {
req_string: "s1".to_string(),
opt_string: Some("s2".to_string()),
req_path: "s3".into(),
opt_path: Some("s4".into()),
inner: TestInner {
req_inner: "s5".to_string(),
opt_inner: Some("s6".to_string()),
},
};
materialize_boot_info(
log,
&deps_dir,
&env_dir,
&network_unit_dir,
env,
btreemap! {
"extra_1".to_string() => ExtraDependency {
source: "source_1.service".into(),
requires: "required_1.service".into(),
},
"extra_2".to_string() => ExtraDependency {
source: "source_2.service".into(),
requires: "required_2.service".into(),
},
},
MountUnit {
unit_section: UnitSection {
..Default::default()
},
mount_section: MountSection {
what: "/dev/test".into(),
where_: "/test_mount".into(),
options: None,
type_: None,
},
},
Some(Dropin {
target: "eth.network".into(),
unit: NetworkUnit {
match_section: NetworkUnitMatchSection {
name: "eth*".to_string(),
mac_address: "11:22:33:44:55:66".to_string(),
},
},
dropin_filename: Some("match.conf".to_string()),
}),
)
.context("Failed to materialize_boot_info")?;
assert_eq!(
std::fs::read_to_string(env_dir.join(ENVIRONMENT_FILENAME))
.context("Can't read environment file")?,
"\
OPTIONAL_INNER=s6\n\
OPTIONAL_PATH=s4\n\
OPTIONAL_STRING=s2\n\
REQUIRED_INNER=s5\n\
REQUIRED_PATH=s3\n\
REQUIRED_STRING=s1\n\
"
);
assert_eq!(
std::fs::read_to_string(deps_dir.join("source_1.service.d/extra_1.conf"))
.context("Can't read extra_1.conf file")?,
"\
[Unit]\n\
After=required_1.service\n\
Requires=required_1.service\n\
"
);
assert_eq!(
std::fs::read_to_string(deps_dir.join("source_2.service.d/extra_2.conf"))
.context("Can't read extra_2.conf file")?,
"\
[Unit]\n\
After=required_2.service\n\
Requires=required_2.service\n\
"
);
assert_eq!(
std::fs::read_to_string(deps_dir.join("run-fs-control.mount"))
.context("Can't read run-fs-control.mount file")?,
"\
[Unit]\n\
[Mount]\n\
What=/dev/test\n\
Where=/test_mount\n\
Options=\n\
"
);
assert_eq!(
std::fs::read_to_string(network_unit_dir.join("eth.network.d/match.conf"))
.context("Can't read eth.network.d/match.conf file")?,
"\
[Match]\n\
Name=eth*\n\
MACAddress=11:22:33:44:55:66\n\
"
);
Ok(())
}