fn test_environment()

in metalos/lib/systemd_generator_lib/src/systemd_generator_lib.rs [265:357]


    fn test_environment() -> Result<()> {
        let ts = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
        let tmpdir = std::env::temp_dir().join(format!("test_environment_{:?}", ts));
        std::fs::create_dir(&tmpdir)?;

        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()),
            },
        };

        assert_eq!(
            env.clone()
                .try_into_map()
                .context("failed to convert env to map")?,
            btreemap! {
                "REQUIRED_STRING".to_string() => "s1".to_string(),
                "OPTIONAL_STRING".to_string() => "s2".to_string(),
                "REQUIRED_PATH".to_string() => "s3".to_string(),
                "OPTIONAL_PATH".to_string() => "s4".to_string(),
                "REQUIRED_INNER".to_string() => "s5".to_string(),
                "OPTIONAL_INNER".to_string() => "s6".to_string(),
            }
        );

        let path = env
            .write_systemd_env_file(&tmpdir, Path::new("test_env_file"))
            .context("failed to write env file")?;

        assert_eq!(path, tmpdir.join(Path::new("test_env_file")));

        let content =
            std::fs::read_to_string(path.clone()).context(format!("Can't read file {:?}", path))?;

        assert_eq!(
            content,
            "\
            OPTIONAL_INNER=s6\n\
            OPTIONAL_PATH=s4\n\
            OPTIONAL_STRING=s2\n\
            REQUIRED_INNER=s5\n\
            REQUIRED_PATH=s3\n\
            REQUIRED_STRING=s1\n\
            "
        );

        let env = TestEnvironment {
            req_string: "s1".to_string(),
            opt_string: None,
            req_path: "s3".into(),
            opt_path: None,
            inner: TestInner {
                req_inner: "s5".to_string(),
                opt_inner: None,
            },
        };

        assert_eq!(
            env.clone()
                .try_into_map()
                .context("failed to convert env to map")?,
            btreemap! {
                "REQUIRED_STRING".to_string() => "s1".to_string(),
                "REQUIRED_PATH".to_string() => "s3".to_string(),
                "REQUIRED_INNER".to_string() => "s5".to_string(),
            }
        );

        let path = env
            .write_systemd_env_file(&tmpdir, Path::new("test_env_file"))
            .context("failed to write env file")?;

        assert_eq!(path, tmpdir.join(Path::new("test_env_file")));

        let content =
            std::fs::read_to_string(path.clone()).context(format!("Can't read file {:?}", path))?;

        assert_eq!(
            content,
            "\
            REQUIRED_INNER=s5\n\
            REQUIRED_PATH=s3\n\
            REQUIRED_STRING=s1\n\
            "
        );

        Ok(())
    }