helm-rs 0.1.0

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::os::unix::fs::PermissionsExt;

use tempfile::TempDir;

use super::*;

use crate::options::GlobalOptions;

#[test]
fn values_files_and_sets_follow_the_chart_flags() {
    let mut command = install();

    command.chart_path.version = Some("1".into());
    command.values = Values::new().file("a.yaml").set_string("k", "v");

    assert_eq!(
        argv(&command),
        [
            "install",
            "web",
            "repo/web",
            "--version",
            "1",
            "-f",
            "a.yaml",
            "--set-string",
            "k=v",
            "-o",
            "json"
        ]
    );
}

#[test]
fn in_memory_values_reach_helm_through_a_private_file() {
    let mut command = install();

    command.values = Values::new().json(json!({"password": "s3cret"}));

    let inv = invocation(&command);
    let args = argv(&command);
    let path = inv
        .args
        .iter()
        .skip_while(|arg| *arg != "-f")
        .nth(1)
        .expect("a -f file");

    assert!(!args.join(" ").contains("s3cret"));
    assert_eq!(
        fs::read_to_string(path).expect("values file"),
        r#"{"password":"s3cret"}"#
    );
}

fn oci_install() -> Install {
    let mut command = Install::new(
        ReleaseName::Named("web".into()),
        ChartRef::Oci("oci://registry.example:5000/charts/web".into()),
    );

    command.chart_path = ChartPathOptions {
        username: Some("alice".into()),
        password: Some("s3cret".into()),
        version: Some("1.2.3".into()),
        ..ChartPathOptions::default()
    };

    command
}

#[test]
fn an_oci_password_goes_to_a_private_copy_of_the_registry_config_and_never_to_argv() {
    let dir = TempDir::new().expect("temp dir");
    let other = json!({"auths": {"other.example": {"auth": "b3RoZXI="}}, "credsStore": "pass"});
    let stale = json!({"auths": {"registry.example:5000": {"auth": "c3RhbGU="}}});
    let mine = json!({"auth": "YWxpY2U6czNjcmV0"});
    let file = |name: &str, content: &serde_json::Value| {
        let path = dir.path().join(name);

        fs::write(&path, content.to_string()).expect("write config");

        path
    };
    let home = dir.path().join("home");

    fs::create_dir_all(home.join("registry")).expect("home");
    fs::write(home.join("registry/config.json"), other.to_string()).expect("home config");

    let table: [(&str, GlobalOptions, serde_json::Value); 4] = [
        (
            "missing file starts empty",
            GlobalOptions {
                registry_config: Some(dir.path().join("absent.json")),
                ..GlobalOptions::default()
            },
            json!({"auths": {"registry.example:5000": mine}}),
        ),
        (
            "other registries are kept",
            GlobalOptions {
                registry_config: Some(file("other.json", &other)),
                ..GlobalOptions::default()
            },
            json!({"auths": {"other.example": {"auth": "b3RoZXI="}, "registry.example:5000": mine}, "credsStore": "pass"}),
        ),
        (
            "the chart registry's entry is replaced",
            GlobalOptions {
                registry_config: Some(file("stale.json", &stale)),
                ..GlobalOptions::default()
            },
            json!({"auths": {"registry.example:5000": mine}}),
        ),
        (
            "config home is used without a flag",
            GlobalOptions {
                config_home: Some(home),
                ..GlobalOptions::default()
            },
            json!({"auths": {"other.example": {"auth": "b3RoZXI="}, "registry.example:5000": mine}, "credsStore": "pass"}),
        ),
    ];

    for (label, options, expected) in table {
        let command = oci_install();
        let mut inv = Invocation::default();

        inv.globals(&options).expect("globals");
        command.build(&mut inv).expect("build");

        let args: Vec<String> = inv
            .args
            .iter()
            .map(|arg| arg.to_string_lossy().into_owned())
            .collect();
        let at = args
            .iter()
            .rposition(|arg| arg == "--registry-config")
            .expect("registry config");
        let temp = PathBuf::from(&args[at + 1]);
        let config: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&temp).expect("config")).expect("json");
        let mode = fs::metadata(&temp).expect("metadata").permissions().mode() & 0o777;

        assert_eq!(config, expected, "{label}");
        assert_eq!(mode, 0o600, "{label}");
        assert!(
            !args
                .iter()
                .any(|arg| arg.contains("s3cret") || arg == "--password" || arg == "--username"),
            "{label}"
        );
        assert!(args.contains(&"1.2.3".to_owned()), "{label}");
    }

    assert_eq!(
        fs::read_to_string(dir.path().join("other.json")).expect("untouched"),
        other.to_string()
    );
}

#[test]
fn passwords_helm_only_takes_on_argv_are_refused() {
    let with = |chart: ChartRef, username: Option<&str>| {
        let mut command = install();
        let mut options = ChartPathOptions::default();

        command.chart = chart;
        options.password = Some("s3cret".into());
        options.username = username.map(Into::into);
        command.chart_path = options;

        let mut inv = Invocation::default();

        command.build(&mut inv)
    };

    for (chart, username) in [
        (ChartRef::Repo("repo/web".into()), Some("alice")),
        (
            ChartRef::Url("https://x.example/web.tgz".into()),
            Some("alice"),
        ),
        (ChartRef::Oci("oci://r.example/web".into()), None),
    ] {
        let error = with(chart, username).expect_err("refused");

        assert!(matches!(error, Error::InvalidCommand(_)), "{error:?}");
        assert!(!error.to_string().contains("s3cret"));
    }
}