helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! `plugin` commands against the real `helm`, with plugins installed into an isolated home.

use std::path::{Path, PathBuf};

use helm_rs::Error;
use helm_rs::command::{
    PluginInstall, PluginList, PluginPackage, PluginUninstall, PluginUpdate, PluginVerify,
};

use helm_rs::command::Passphrase;

use super::home::{Home, copy, fixtures};

const PASSPHRASE: &str = "test-passphrase";

fn plugin_dir(home: &Home, name: &str) -> PathBuf {
    let dir = home.dir.path().join("src").join(name);

    copy(
        &fixtures().join("plugin").join(name).join("plugin.yaml"),
        &dir.join("plugin.yaml"),
    );

    dir
}

async fn installed(home: &Home) -> Vec<String> {
    home.helm()
        .await
        .run(PluginList::default())
        .await
        .expect("list")
        .into_iter()
        .map(|p| p.name)
        .collect()
}

#[tokio::test]
#[ignore = "needs helm 4"]
async fn plugins_are_installed_listed_and_uninstalled() {
    let home = Home::new();
    let helm = home.helm().await;

    assert_eq!(installed(&home).await.len(), 0);

    for (name, installed) in [("hello", "hello"), ("legacy", "oldie")] {
        let output = helm
            .run(PluginInstall::new(
                plugin_dir(&home, name).to_string_lossy(),
            ))
            .await
            .expect("install");

        assert_eq!(output.name, installed);
        assert_eq!(output.verification, None);
    }

    let all = helm.run(PluginList::default()).await.expect("list");
    let hello = all.iter().find(|p| p.name == "hello").expect("hello");
    let oldie = all.iter().find(|p| p.name == "oldie").expect("oldie");

    assert_eq!(all.len(), 2);
    assert_eq!(
        (hello.version.as_str(), hello.kind.as_str()),
        ("0.1.0", "cli/v1")
    );
    assert_eq!(
        (hello.api_version.as_str(), hello.provenance.as_str()),
        ("v1", "local dev")
    );
    assert_eq!(oldie.api_version, "legacy");
    assert_eq!(oldie.source, None);

    let getters = helm
        .run(PluginList {
            kind: Some("getter/v1".into()),
        })
        .await
        .expect("filtered");

    assert_eq!(getters.len(), 0);

    helm.run(PluginUninstall::new(["hello", "oldie"]))
        .await
        .expect("uninstall");

    assert_eq!(installed(&home).await.len(), 0);
    assert!(matches!(
        helm.run(PluginUninstall::new(["hello"])).await,
        Err(Error::Backend { stderr, .. }) if stderr.contains("not found")
    ));
}

#[tokio::test]
#[ignore = "needs helm 4"]
async fn unsigned_packages_cannot_be_verified_and_local_plugins_cannot_update() {
    let home = Home::new();
    let helm = home.helm().await;
    let out = home.dir.path().join("out");
    let package = PluginPackage {
        destination: Some(out.clone()),
        sign: false,
        ..PluginPackage::new(plugin_dir(&home, "hello"))
    };

    helm.run(package).await.expect("package");

    let tarball: &Path = &out.join("hello-0.1.0.tgz");

    assert!(tarball.exists());
    assert!(matches!(
        helm.run(PluginVerify::new(tarball)).await,
        Err(Error::Backend { stderr, .. }) if stderr.contains("provenance file")
    ));

    helm.run(PluginInstall::new(
        plugin_dir(&home, "hello").to_string_lossy(),
    ))
    .await
    .expect("install");

    assert!(helm.run(PluginUpdate::new(["hello"])).await.is_err());
    assert!(matches!(
        helm.run(PluginUpdate::new(["missing"])).await,
        Err(Error::Backend { stderr, .. }) if stderr.contains("not found")
    ));
}

#[tokio::test]
#[ignore = "needs helm 4"]
async fn signing_without_a_keyring_fails_before_writing_anything() {
    let home = Home::new();
    let helm = home.helm().await;
    let out = home.dir.path().join("out");
    let package = PluginPackage {
        destination: Some(out.clone()),
        keyring: Some(home.dir.path().join("missing.gpg")),
        ..PluginPackage::new(plugin_dir(&home, "hello"))
    };

    assert!(matches!(
        helm.run(package).await,
        Err(Error::Backend { stderr, .. }) if stderr.contains("keyring")
    ));
    assert!(!out.join("hello-0.1.0.tgz").exists());
}

fn keys() -> PathBuf {
    fixtures().join("plugin/keys")
}

async fn signed(home: &Home, key: &str, ring: &str, passphrase: Option<Passphrase>) -> PathBuf {
    let out = home.dir.path().join("out");
    let package = PluginPackage {
        destination: Some(out.clone()),
        key: Some(format!("helm-rs test {key}")),
        keyring: Some(keys().join(ring)),
        passphrase,
        ..PluginPackage::new(plugin_dir(home, "hello"))
    };

    home.helm().await.run(package).await.expect("package");

    out.join("hello-0.1.0.tgz")
}

#[tokio::test]
#[ignore = "needs helm 4"]
async fn signed_packages_verify_with_the_right_keyring_only() {
    let table = [
        ("plain", "plain-secring.gpg", "none"),
        ("locked", "locked-secring.gpg", "file"),
        ("locked", "locked-secring.gpg", "stdin"),
    ];

    for (key, ring, how) in table {
        let home = Home::new();
        let file = home.dir.path().join("passphrase");
        let passphrase = match how {
            "file" => {
                std::fs::write(&file, PASSPHRASE).expect("write passphrase");

                Some(Passphrase::File(file))
            }
            "stdin" => Some(Passphrase::Stdin(PASSPHRASE.into())),
            _ => None,
        };
        let tarball = signed(&home, key, ring, passphrase).await;
        let helm = home.helm().await;
        let verified = helm
            .run(PluginVerify {
                keyring: Some(keys().join("pubring.gpg")),
                ..PluginVerify::new(&tarball)
            })
            .await
            .expect("verify");

        assert!(tarball.with_extension("tgz.prov").exists());
        assert_eq!(
            verified.signed_by,
            [format!("helm-rs test {key} <{key}@example.invalid>")]
        );
        assert_eq!(verified.fingerprint.len(), 40);
        assert!(verified.hash.starts_with("sha256:"));
        assert!(matches!(
            helm.run(PluginVerify {
                keyring: Some(keys().join("stranger-pubring.gpg")),
                ..PluginVerify::new(&tarball)
            })
            .await,
            Err(Error::Backend { stderr, .. }) if stderr.contains("unknown entity")
        ));
    }
}

#[tokio::test]
#[ignore = "needs helm 4"]
async fn a_locked_key_needs_its_passphrase() {
    let home = Home::new();
    let package = PluginPackage {
        destination: Some(home.dir.path().join("out")),
        key: Some("helm-rs test locked".into()),
        keyring: Some(keys().join("locked-secring.gpg")),
        passphrase: Some(Passphrase::Stdin("wrong".into())),
        ..PluginPackage::new(plugin_dir(&home, "hello"))
    };

    assert!(matches!(
        home.helm().await.run(package).await,
        Err(Error::Backend { .. })
    ));
}

#[tokio::test]
#[ignore = "needs helm 4"]
async fn install_verifies_a_signed_package_against_the_keyring() {
    let home = Home::new();
    let tarball = signed(&home, "plain", "plain-secring.gpg", None).await;
    let helm = home.helm().await;
    let install = |ring: &str| PluginInstall {
        keyring: Some(keys().join(ring)),
        ..PluginInstall::new(tarball.to_string_lossy())
    };

    assert!(matches!(
        helm.run(install("stranger-pubring.gpg")).await,
        Err(Error::Backend { stderr, .. }) if stderr.contains("unknown entity")
    ));
    assert_eq!(installed(&home).await.len(), 0);

    let installed = helm.run(install("pubring.gpg")).await.expect("install");
    let verification = installed.verification.expect("verification");

    assert_eq!(installed.name, "hello");
    assert_eq!(
        verification.signed_by,
        ["helm-rs test plain <plain@example.invalid>"]
    );
    assert_eq!(verification.fingerprint.len(), 40);
    assert!(verification.hash.starts_with("sha256:"));

    let all = helm.run(PluginList::default()).await.expect("list");

    assert_eq!(all.len(), 1);
    assert_eq!(all[0].provenance, "signed");
}