|
| 1 | +// Copyright (c) Contributors to the SPK project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// https://github.com/spkenv/spk |
| 4 | + |
| 5 | +use clap::Parser; |
| 6 | +use rstest::rstest; |
| 7 | +use spfs::config::Remote; |
| 8 | +use spfs::RemoteAddress; |
| 9 | +use spk_cli_common::Run; |
| 10 | +use spk_solve::{recipe, spec, Component}; |
| 11 | +use spk_storage::fixtures::{empty_layer_digest, spfs_runtime, spfsrepo}; |
| 12 | + |
| 13 | +use super::Bake; |
| 14 | + |
| 15 | +#[derive(Parser)] |
| 16 | +struct Opt { |
| 17 | + #[clap(flatten)] |
| 18 | + bake: Bake, |
| 19 | +} |
| 20 | + |
| 21 | +#[rstest] |
| 22 | +#[tokio::test] |
| 23 | +async fn test_bake() { |
| 24 | + // Test the bake command runs |
| 25 | + let mut rt = spfs_runtime().await; |
| 26 | + let remote_repo = spfsrepo().await; |
| 27 | + |
| 28 | + // Populate the "origin" repo with one package. |
| 29 | + // The "local" repo is empty. |
| 30 | + rt.add_remote_repo( |
| 31 | + "origin", |
| 32 | + Remote::Address(RemoteAddress { |
| 33 | + address: remote_repo.address().clone(), |
| 34 | + }), |
| 35 | + ) |
| 36 | + .unwrap(); |
| 37 | + |
| 38 | + let recipe = recipe!({"pkg": "my-pkg/1.0.1"}); |
| 39 | + remote_repo.publish_recipe(&recipe).await.unwrap(); |
| 40 | + let spec = spec!({"pkg": "my-pkg/1.0.1/ZPGKGOTY"}); |
| 41 | + remote_repo |
| 42 | + .publish_package( |
| 43 | + &spec, |
| 44 | + &vec![(Component::Run, empty_layer_digest())] |
| 45 | + .into_iter() |
| 46 | + .collect(), |
| 47 | + ) |
| 48 | + .await |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + // Test a basic bake |
| 52 | + let mut opt = Opt::try_parse_from(["bake", "--no-runtime", "my-pkg:run"]).unwrap(); |
| 53 | + let result = opt.bake.run().await.unwrap(); |
| 54 | + assert_eq!(result, 0); |
| 55 | +} |
| 56 | + |
| 57 | +#[rstest] |
| 58 | +#[tokio::test] |
| 59 | +async fn test_bake_incompatible_merged_request() { |
| 60 | + // Test bake with an incompatible set of requests |
| 61 | + let mut rt = spfs_runtime().await; |
| 62 | + let remote_repo = spfsrepo().await; |
| 63 | + |
| 64 | + // Populate the "origin" repo with one package. |
| 65 | + // The "local" repo is empty. |
| 66 | + rt.add_remote_repo( |
| 67 | + "origin", |
| 68 | + Remote::Address(RemoteAddress { |
| 69 | + address: remote_repo.address().clone(), |
| 70 | + }), |
| 71 | + ) |
| 72 | + .unwrap(); |
| 73 | + |
| 74 | + let recipe = recipe!({"pkg": "my-pkg/1.0.33+r.1"}); |
| 75 | + remote_repo.publish_recipe(&recipe).await.unwrap(); |
| 76 | + let spec = spec!({"pkg": "my-pkg/1.0.33+r.1/ZPGKGOTY"}); |
| 77 | + remote_repo |
| 78 | + .publish_package( |
| 79 | + &spec, |
| 80 | + &vec![(Component::Run, empty_layer_digest())] |
| 81 | + .into_iter() |
| 82 | + .collect(), |
| 83 | + ) |
| 84 | + .await |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + // Test bake command with 2 incompatible requests. This should |
| 88 | + // not panic, it should error out |
| 89 | + let mut opt = Opt::try_parse_from([ |
| 90 | + "bake", |
| 91 | + "--no-runtime", |
| 92 | + "my-pkg:run/==1.0.33+r.1/ZPGKGOTY", |
| 93 | + "my-pkg:run/=1.0.99", |
| 94 | + ]) |
| 95 | + .unwrap(); |
| 96 | + let result = opt.bake.run().await; |
| 97 | + println!("bake run result: {result:?}"); |
| 98 | + |
| 99 | + match result { |
| 100 | + Err(err) => { |
| 101 | + println!("Bake errored with: {err}"); |
| 102 | + } |
| 103 | + Ok(_value) => { |
| 104 | + panic!("Incompatible requests for same package should cause bake to error"); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments