252 lines
8.6 KiB
Rust
252 lines
8.6 KiB
Rust
//! `leleka up`: idempotent, resumable, never rotating secrets, and the archive checks.
|
|
|
|
use crate::harness::*;
|
|
use leleka_deploy::{
|
|
app::run_up,
|
|
config::RunConfig,
|
|
plan::{FsState, plan_steps},
|
|
steps::gather_fs,
|
|
};
|
|
use std::{fs, os::unix::fs::PermissionsExt};
|
|
|
|
#[tokio::test]
|
|
async fn up_is_idempotent_resumable_and_never_rotates_secrets() {
|
|
let scene = Scene::new("up");
|
|
let opts = scene.options(&[]);
|
|
let env_first = first_run_builds_everything(&scene, &opts).await;
|
|
second_run_does_nothing(&scene, &opts, &env_first).await;
|
|
a_lost_archive_is_rebuilt(&scene, &opts).await;
|
|
reverify_forces_the_verification(&scene).await;
|
|
}
|
|
|
|
type Options = leleka_deploy::cli::UpOptions;
|
|
|
|
/// The directories and fakes of one `up` scenario.
|
|
struct Scene {
|
|
fixture: Fixture,
|
|
bin_src: std::path::PathBuf,
|
|
cublas: std::path::PathBuf,
|
|
mtp: std::path::PathBuf,
|
|
}
|
|
|
|
impl Scene {
|
|
fn new(label: &str) -> Scene {
|
|
let fixture = Fixture::new(label);
|
|
let bin_src = fixture.root.join("release/bin");
|
|
fixture.install_fake_binaries(&bin_src);
|
|
// The fakes find their shared state through the *installed* bin directory.
|
|
let (cublas, mtp) = (fixture.root.join("cublas"), fixture.root.join("mtp-src"));
|
|
fake_libraries(&cublas);
|
|
mtp_source(&mtp);
|
|
Scene {
|
|
fixture,
|
|
bin_src,
|
|
cublas,
|
|
mtp,
|
|
}
|
|
}
|
|
|
|
fn options(&self, extra: &[&str]) -> Options {
|
|
up_options(&self.fixture, &self.bin_src, &self.cublas, &self.mtp, extra)
|
|
}
|
|
}
|
|
|
|
/// First run: everything is a todo, then everything exists. Returns the environment file it wrote.
|
|
async fn first_run_builds_everything(scene: &Scene, opts: &Options) -> String {
|
|
let paths = scene.fixture.dirs.paths();
|
|
let ctx = healthy_context(opts);
|
|
let fs0 = gather_fs(&ctx).await;
|
|
let steps = plan_steps(&ctx, &fs0);
|
|
assert!(
|
|
steps
|
|
.iter()
|
|
.filter(|s| matches!(
|
|
s.id,
|
|
"binaries" | "secrets" | "model" | "kar" | "tokenizer" | "config" | "mtp-pack"
|
|
))
|
|
.all(|s| s.state.tag() == "todo")
|
|
);
|
|
assert_eq!(run_up(&ctx, &steps).await.unwrap(), 0);
|
|
for (what, file) in [
|
|
("binary", paths.bin("leleka-inference")),
|
|
("run config", paths.run_config()),
|
|
("env", paths.env_file()),
|
|
("api key", paths.api_key()),
|
|
("tokenizer", paths.tokenizer_dir()),
|
|
("mtp", paths.mtp_pack().join("experts.bin")),
|
|
("stamp", paths.stamps().join("model.json")),
|
|
] {
|
|
assert!(file.exists(), "{what}: {} missing", file.display());
|
|
}
|
|
check_modes_config_and_calls(scene)
|
|
}
|
|
|
|
/// Private environment file, read-only archive, the profile of the run and the tools called once.
|
|
/// Returns the environment file.
|
|
fn check_modes_config_and_calls(scene: &Scene) -> String {
|
|
let (fixture, paths) = (&scene.fixture, scene.fixture.dirs.paths());
|
|
assert_eq!(
|
|
fs::metadata(paths.env_file()).unwrap().permissions().mode() & 0o777,
|
|
0o600
|
|
);
|
|
assert_eq!(
|
|
fs::metadata(paths.kar()).unwrap().permissions().mode() & 0o222,
|
|
0,
|
|
"the file-mode archive must have no write bits"
|
|
);
|
|
let config = RunConfig::read(&paths.run_config()).unwrap();
|
|
assert_eq!(config.profile.name, "rtx3080-16g");
|
|
let env_first = fs::read_to_string(paths.env_file()).unwrap();
|
|
assert!(
|
|
env_first.contains(&format!("LD_LIBRARY_PATH={}", scene.cublas.display())),
|
|
"{env_first}"
|
|
);
|
|
assert_eq!(
|
|
(
|
|
count_calls(fixture, "model fetch"),
|
|
count_calls(fixture, "ensure-sharded-streamed"),
|
|
count_calls(fixture, "api-key init")
|
|
),
|
|
(0, 1, 1),
|
|
"the archive step fetches and verifies the shards itself"
|
|
);
|
|
env_first
|
|
}
|
|
|
|
/// Second run: the plan has nothing to do and no tool is invoked again.
|
|
async fn second_run_does_nothing(scene: &Scene, opts: &Options, env_first: &str) {
|
|
let (fixture, paths) = (&scene.fixture, scene.fixture.dirs.paths());
|
|
let ctx = healthy_context(opts);
|
|
let fs1 = gather_fs(&ctx).await;
|
|
let steps = plan_steps(&ctx, &fs1);
|
|
for step in &steps {
|
|
assert!(
|
|
matches!(step.state.tag(), "done" | "skip") || step.id == "preflight",
|
|
"{} is {:?} on a re-run",
|
|
step.id,
|
|
step.state
|
|
);
|
|
}
|
|
assert_eq!(run_up(&ctx, &steps).await.unwrap(), 0);
|
|
assert_eq!(
|
|
(
|
|
count_calls(fixture, "model fetch"),
|
|
count_calls(fixture, "model verify"),
|
|
count_calls(fixture, "ensure-sharded-streamed"),
|
|
count_calls(fixture, "extract-tokenizer")
|
|
),
|
|
(0, 0, 1, 1)
|
|
);
|
|
assert_eq!(
|
|
fs::read_to_string(paths.env_file()).unwrap(),
|
|
env_first,
|
|
"tokens must never rotate"
|
|
);
|
|
}
|
|
|
|
/// Resumable: a lost archive is rebuilt, everything else is left alone.
|
|
async fn a_lost_archive_is_rebuilt(scene: &Scene, opts: &leleka_deploy::cli::UpOptions) {
|
|
let (fixture, paths) = (&scene.fixture, scene.fixture.dirs.paths());
|
|
fs::set_permissions(paths.kar(), fs::Permissions::from_mode(0o600)).unwrap();
|
|
fs::remove_file(paths.kar()).unwrap();
|
|
let ctx = healthy_context(opts);
|
|
let steps = plan_steps(&ctx, &gather_fs(&ctx).await);
|
|
assert_eq!(
|
|
steps.iter().find(|s| s.id == "kar").unwrap().state.tag(),
|
|
"todo"
|
|
);
|
|
assert_eq!(
|
|
steps.iter().find(|s| s.id == "model").unwrap().state.tag(),
|
|
"done"
|
|
);
|
|
assert_eq!(run_up(&ctx, &steps).await.unwrap(), 0);
|
|
assert_eq!(
|
|
(
|
|
count_calls(fixture, "model fetch"),
|
|
count_calls(fixture, "ensure-sharded-streamed")
|
|
),
|
|
(0, 2)
|
|
);
|
|
}
|
|
|
|
/// `--reverify` forces the verification again.
|
|
async fn reverify_forces_the_verification(scene: &Scene) {
|
|
let reverify = scene.options(&["--reverify"]);
|
|
let ctx = healthy_context(&reverify);
|
|
let steps = plan_steps(&ctx, &gather_fs(&ctx).await);
|
|
assert_eq!(
|
|
steps.iter().find(|s| s.id == "model").unwrap().state.tag(),
|
|
"todo"
|
|
);
|
|
let _ = FsState::default();
|
|
}
|
|
|
|
/// An archive of the pinned length with its provenance sidecar, as left by an earlier build.
|
|
fn write_archive_with_provenance(fixture: &Fixture) {
|
|
let paths = fixture.dirs.paths();
|
|
fs::create_dir_all(&fixture.dirs.assets).unwrap();
|
|
fs::File::create(paths.kar())
|
|
.unwrap()
|
|
.set_len(55_295_529_472)
|
|
.unwrap();
|
|
fs::write(paths.kar_provenance(), "{}").unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn trust_archive_skips_the_strict_rederivation_but_still_fetches_and_verifies_the_shards() {
|
|
let scene = Scene::new("trust");
|
|
let fixture = &scene.fixture;
|
|
let opts = scene.options(&["--trust-archive"]);
|
|
let paths = fixture.dirs.paths();
|
|
write_archive_with_provenance(fixture);
|
|
let ctx = healthy_context(&opts);
|
|
let steps = plan_steps(&ctx, &gather_fs(&ctx).await);
|
|
let kar = steps.iter().find(|s| s.id == "kar").unwrap();
|
|
assert_eq!(kar.state.tag(), "todo");
|
|
assert!(
|
|
kar.state.why().contains("--trust-archive"),
|
|
"{:?}",
|
|
kar.state
|
|
);
|
|
assert_eq!(run_up(&ctx, &steps).await.unwrap(), 0);
|
|
assert_eq!(
|
|
(
|
|
count_calls(fixture, "model fetch"),
|
|
count_calls(fixture, "model verify"),
|
|
count_calls(fixture, "ensure-sharded-streamed")
|
|
),
|
|
(1, 1, 0)
|
|
);
|
|
assert!(paths.stamps().join("kar.json").exists() && paths.stamps().join("model.json").exists());
|
|
check_strict_plan_reverifies(&scene).await;
|
|
}
|
|
|
|
/// Without the flag the same archive would be re-verified by the real tool.
|
|
async fn check_strict_plan_reverifies(scene: &Scene) {
|
|
let strict = healthy_context(&scene.options(&["--reverify"]));
|
|
let steps = plan_steps(&strict, &gather_fs(&strict).await);
|
|
assert!(
|
|
!steps
|
|
.iter()
|
|
.find(|s| s.id == "kar")
|
|
.unwrap()
|
|
.state
|
|
.why()
|
|
.contains("--trust-archive")
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_wrong_size_archive_is_refused_before_the_engine_could_reject_it() {
|
|
let scene = Scene::new("wrongkar");
|
|
let fixture = &scene.fixture;
|
|
let opts = scene.options(&[]);
|
|
// A pre-existing archive of the wrong length: the fake `ensure` reuses it, the size gate must object.
|
|
fs::create_dir_all(&fixture.dirs.assets).unwrap();
|
|
fs::write(fixture.dirs.paths().kar(), b"short").unwrap();
|
|
let ctx = healthy_context(&opts);
|
|
let steps = plan_steps(&ctx, &gather_fs(&ctx).await);
|
|
let error = run_up(&ctx, &steps).await.unwrap_err();
|
|
assert!(error.contains("engine only admits the pinned"), "{error}");
|
|
}
|