helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::path::PathBuf;

use super::*;
use crate::cli::chart_rows::capture;
use crate::cli::testing::{FlagRow, argv, build_error, check_flags, invocation, registry_auths};
use crate::command::{ChartPathOptions, ChartRef, Pull, Push, Verify};

fn pull() -> Pull {
    Pull::new(ChartRef::Repo("repo/web".into()))
}

#[test]
fn pull_maps_every_flag() {
    let mut table: Vec<FlagRow<Pull>> = download_rows!(Pull).into();

    table.extend([
        (
            "untar",
            (|c| c.untar = true) as fn(&mut Pull),
            &["--untar"] as &[&str],
        ),
        (
            "untardir",
            |c| c.untardir = Some(PathBuf::from("src")),
            &["--untardir", "src"],
        ),
        (
            "destination",
            |c| c.destination = Some(PathBuf::from("/out")),
            &["--destination", "/out"],
        ),
        ("prov", |c| c.prov = true, &["--prov"]),
    ]);

    check_flags(&pull(), &["pull", "repo/web"], &[], &table);
}

#[test]
fn pull_combines_flags_after_the_chart_path_flags() {
    let command = Pull {
        untar: true,
        prov: true,
        destination: Some(PathBuf::from("/out")),
        chart_path: ChartPathOptions {
            version: Some("1".into()),
            verify: true,
            ..ChartPathOptions::default()
        },
        ..Pull::new(ChartRef::Oci("oci://r.example/web".into()))
    };

    assert_eq!(
        argv(&command),
        [
            "pull",
            "oci://r.example/web",
            "--version",
            "1",
            "--verify",
            "--untar",
            "--destination",
            "/out",
            "--prov"
        ]
    );
}

#[test]
fn an_oci_pull_password_goes_to_the_registry_config_and_never_to_argv() {
    let command = Pull {
        chart_path: ChartPathOptions {
            username: Some("alice".into()),
            password: Some("s3cret".into()),
            ..ChartPathOptions::default()
        },
        ..Pull::new(ChartRef::Oci("oci://r.example:5000/web".into()))
    };
    let args = argv(&command);

    assert!(!args.join(" ").contains("s3cret"));
    assert!(!args.contains(&"--password".to_owned()));
    assert!(args.contains(&"--registry-config".to_owned()));
    assert!(matches!(
        build_error(&Pull {
            chart_path: command.chart_path.clone(),
            ..pull()
        }),
        Error::InvalidCommand(_)
    ));
}

#[test]
fn pull_output_is_parsed_from_real_captures() {
    let plain = pull().parse(capture("pull-oci.txt")).expect("plain");
    let verified = pull()
        .parse(capture("pull-oci-verify.txt"))
        .expect("verified");
    let digest = "sha256:07fb5546e98b5dac606508a8de01c305c7d364e41734f37e75fb75db803dabb8";

    assert_eq!(plain.artifacts.len(), 1);
    assert_eq!(
        plain.artifacts[0].reference,
        "127.0.0.1:24616/fixture/demo:0.1.0"
    );
    assert_eq!(plain.artifacts[0].digest, digest);
    assert_eq!(plain.verification, None);
    assert_eq!(verified.artifacts.len(), 2);
    assert_eq!(verified.artifacts[1], plain.artifacts[0]);

    let signature = verified.verification.expect("verification");

    assert_eq!(
        signature.signed_by,
        ["helm-rs test plain <plain@example.invalid>"]
    );
    assert_eq!(
        signature.fingerprint,
        "8AE6B7E9A038BB3BC854770CEE9433C39DCCC623"
    );
    assert_eq!(
        signature.hash,
        "sha256:49436395949f449ddf0865932fdeb6ad1bf8fe8c698456b7cb3e74096b08f6e9"
    );
}

#[test]
fn pull_from_a_repository_prints_nothing_but_warnings() {
    let empty = pull().parse(String::new()).expect("empty");
    let warned = pull()
        .parse("WARNING: Verification not found for repo/web: 404\n".into())
        .expect("warned");

    assert_eq!(empty, Pulled::default());
    assert_eq!(
        warned.warnings,
        ["WARNING: Verification not found for repo/web: 404"]
    );
    assert!(matches!(
        pull().parse("Pulled: r/web:1\n".into()),
        Err(Error::Parse { .. })
    ));
    assert!(matches!(
        pull().parse("Signed by: a\nUsing Key With Fingerprint: AB\n".into()),
        Err(Error::Parse { .. })
    ));
}

#[test]
fn push_maps_every_flag_and_hides_the_password() {
    let base = Push::new("web-1.tgz", "oci://r.example:5000/charts");
    let table: Vec<FlagRow<Push>> = vec![
        (
            "cert-file",
            |c| c.cert_file = Some(PathBuf::from("/c.pem")),
            &["--cert-file", "/c.pem"],
        ),
        (
            "key-file",
            |c| c.key_file = Some(PathBuf::from("/k.pem")),
            &["--key-file", "/k.pem"],
        ),
        (
            "ca-file",
            |c| c.ca_file = Some(PathBuf::from("/ca.pem")),
            &["--ca-file", "/ca.pem"],
        ),
        (
            "insecure-skip-tls-verify",
            |c| c.insecure_skip_tls_verify = true,
            &["--insecure-skip-tls-verify"],
        ),
        ("plain-http", |c| c.plain_http = true, &["--plain-http"]),
        (
            "username",
            |c| c.username = Some("alice".into()),
            &["--username", "alice"],
        ),
    ];

    check_flags(
        &base,
        &["push", "web-1.tgz", "oci://r.example:5000/charts"],
        &[],
        &table,
    );

    let with_password = Push {
        username: Some("alice".into()),
        password: Some("s3cret".into()),
        ..base
    };
    let inv = invocation(&with_password);
    let args = argv(&with_password);

    assert_eq!(
        args[..3],
        ["push", "web-1.tgz", "oci://r.example:5000/charts"]
    );
    assert_eq!(args[3], "--registry-config");
    assert_eq!(args.len(), 5);
    assert_eq!(
        registry_auths(&inv),
        (
            vec![("r.example:5000".to_owned(), "YWxpY2U6czNjcmV0".to_owned())],
            0o600
        )
    );
    assert!(!args.join(" ").contains("s3cret"));
    assert!(inv.stdin.is_none());
    assert!(matches!(
        build_error(&Push {
            password: Some("s3cret".into()),
            ..Push::new("web-1.tgz", "oci://r.example/charts")
        }),
        Error::InvalidCommand(_)
    ));
}

#[test]
fn push_output_is_parsed_from_a_real_capture() {
    let pushed = Push::new("web-1.tgz", "oci://r")
        .parse(capture("push.txt"))
        .expect("pushed");

    assert_eq!(pushed.reference, "127.0.0.1:24616/fixture/demo:0.1.0");
    assert_eq!(
        pushed.digest,
        "sha256:07fb5546e98b5dac606508a8de01c305c7d364e41734f37e75fb75db803dabb8"
    );
    assert!(Push::new("a", "oci://r").parse(String::new()).is_err());
}

#[test]
fn verify_maps_its_flag_and_parses_the_report() {
    let table: Vec<FlagRow<Verify>> = vec![(
        "keyring",
        |c| c.keyring = Some(PathBuf::from("/ring")),
        &["--keyring", "/ring"],
    )];

    check_flags(
        &Verify::new("web-1.tgz"),
        &["verify", "web-1.tgz"],
        &[],
        &table,
    );

    let verified = Verify::new("web-1.tgz")
        .parse(capture("verify.txt"))
        .expect("verified");

    assert_eq!(
        verified.signed_by,
        ["helm-rs test plain <plain@example.invalid>"]
    );
    assert_eq!(verified.fingerprint.len(), 40);
    assert!(verified.hash.starts_with("sha256:"));
    assert!(
        Verify::new("a")
            .parse("Signed by: x\nUsing Key With Fingerprint: AB\n".into())
            .is_err()
    );
    assert!(Verify::new("a").parse(String::new()).is_err());
}