Skip to content

Commit 8cf7e3e

Browse files
test(testkit): let create_provider set provider config
create_provider and install_provider return a builder that creates the provider when awaited, so tests can chain .config(key, value) for provider create --config. Existing callers are unchanged. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent d411d94 commit 8cf7e3e

3 files changed

Lines changed: 109 additions & 35 deletions

File tree

‎tests/testkit/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ fails. A killed test process skips cleanup, so names are unique to the run.
135135
- `cx.import_profile(profile)` to import a provider profile and return its
136136
`id`, and `cx.create_provider(name, type, credentials)` to create a
137137
provider, each with cleanup. `cx.install_provider(name, profile,
138-
credentials)` does both.
138+
credentials)` does both. Chain `.config(key, value)` before the `.await`
139+
to set provider config.
139140
- `testkit::YamlFile` for YAML a CLI command reads from a file.
140141
- `cx.client()` for SDK calls testkit does not wrap.
141142
- In `tests/support`: `host-server` starts servers on the machine that runs

‎tests/testkit/testkit/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub use logs::{LOGS_TIMEOUT, Logs};
6060
pub use openshell_sdk as sdk;
6161
pub use poll::{Poll, poll_until, with_deadline};
6262
pub use program::main;
63+
pub use provider::ProviderBuilder;
6364
pub use ready::check as check_ready;
6465
pub use registry::Test;
6566
pub use run::{TestInfo, run};
Lines changed: 106 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use std::future::{Future, IntoFuture};
5+
use std::pin::Pin;
6+
47
use serde::Deserialize;
58

69
use crate::context::Context;
@@ -31,57 +34,126 @@ impl Context {
3134
}
3235

3336
/// Create a provider called `name` of `provider_type` with `credentials`,
34-
/// as pairs of environment variable and value. Cleanup deletes the
35-
/// provider after the test's sandboxes.
37+
/// as pairs of environment variable and value, when awaited. Cleanup
38+
/// deletes the provider after the test's sandboxes.
3639
///
3740
/// The CLI reads the credential values from its environment, so they
38-
/// aren't in the reported command.
39-
pub async fn create_provider(
40-
&self,
41-
name: &str,
42-
provider_type: &str,
43-
credentials: &[(&str, &str)],
44-
) -> Result {
41+
/// aren't in the reported command. [`ProviderBuilder`] sets other
42+
/// `provider create` options before the await.
43+
pub fn create_provider<'a>(
44+
&'a self,
45+
name: &'a str,
46+
provider_type: &'a str,
47+
credentials: &'a [(&'a str, &'a str)],
48+
) -> ProviderBuilder<'a> {
49+
ProviderBuilder::new(self, name, ProviderType::Named(provider_type), credentials)
50+
}
51+
52+
/// Import `profile` with [`Context::import_profile`] and create a provider
53+
/// called `name` of its type with [`Context::create_provider`], when
54+
/// awaited.
55+
pub fn install_provider<'a>(
56+
&'a self,
57+
name: &'a str,
58+
profile: &'a str,
59+
credentials: &'a [(&'a str, &'a str)],
60+
) -> ProviderBuilder<'a> {
61+
ProviderBuilder::new(self, name, ProviderType::Profile(profile), credentials)
62+
}
63+
64+
fn on_cleanup_delete(&self, kind: &'static str, name: &str) {
65+
let cli = self.cli_handle();
66+
let owned = name.to_string();
67+
self.on_cleanup(format!("delete {kind} {name}"), async move || {
68+
cli.run([kind, "delete", &owned]).await?.success()?;
69+
Ok(())
70+
});
71+
}
72+
}
73+
74+
/// Where a [`ProviderBuilder`] gets its provider type.
75+
enum ProviderType<'a> {
76+
/// A profile already on the gateway.
77+
Named(&'a str),
78+
/// A profile, in YAML, to import first.
79+
Profile(&'a str),
80+
}
81+
82+
/// A provider to create, from [`Context::create_provider`] or
83+
/// [`Context::install_provider`]. Awaiting it creates the provider.
84+
///
85+
/// ```ignore
86+
/// cx.create_provider(&name, &provider_type, &[])
87+
/// .config("project", "production")
88+
/// .await?;
89+
/// ```
90+
#[must_use = "a ProviderBuilder does nothing until awaited"]
91+
pub struct ProviderBuilder<'a> {
92+
cx: &'a Context,
93+
name: &'a str,
94+
provider_type: ProviderType<'a>,
95+
credentials: &'a [(&'a str, &'a str)],
96+
config: Vec<String>,
97+
}
98+
99+
impl<'a> ProviderBuilder<'a> {
100+
fn new(
101+
cx: &'a Context,
102+
name: &'a str,
103+
provider_type: ProviderType<'a>,
104+
credentials: &'a [(&'a str, &'a str)],
105+
) -> Self {
106+
Self {
107+
cx,
108+
name,
109+
provider_type,
110+
credentials,
111+
config: Vec::new(),
112+
}
113+
}
114+
115+
/// Set the provider config `key` to `value` (`--config key=value`).
116+
pub fn config(mut self, key: &str, value: &str) -> Self {
117+
self.config.push(format!("{key}={value}"));
118+
self
119+
}
120+
121+
async fn create(self) -> Result {
122+
let provider_type = match self.provider_type {
123+
ProviderType::Named(provider_type) => provider_type.to_string(),
124+
ProviderType::Profile(profile) => self.cx.import_profile(profile).await?,
125+
};
45126
// Deleting a missing provider succeeds, so register the delete before
46127
// the create.
47-
self.on_cleanup_delete("provider", name);
128+
self.cx.on_cleanup_delete("provider", self.name);
48129
let mut args = vec![
49130
"provider",
50131
"create",
51132
"--name",
52-
name,
133+
self.name,
53134
"--type",
54-
provider_type,
135+
&provider_type,
55136
];
56-
for (key, _) in credentials {
137+
for (key, _) in self.credentials {
57138
args.extend(["--credential", key]);
58139
}
59-
self.cli_with_env(credentials, args)
140+
for setting in &self.config {
141+
args.extend(["--config", setting]);
142+
}
143+
self.cx
144+
.cli_with_env(self.credentials, args)
60145
.await?
61146
.success()
62-
.context(format!("create provider {name}"))?;
147+
.context(format!("create provider {}", self.name))?;
63148
Ok(())
64149
}
150+
}
65151

66-
/// Import `profile` with [`Context::import_profile`] and create a provider
67-
/// called `name` of its type with [`Context::create_provider`].
68-
pub async fn install_provider(
69-
&self,
70-
name: &str,
71-
profile: &str,
72-
credentials: &[(&str, &str)],
73-
) -> Result {
74-
let provider_type = self.import_profile(profile).await?;
75-
self.create_provider(name, &provider_type, credentials)
76-
.await
77-
}
152+
impl<'a> IntoFuture for ProviderBuilder<'a> {
153+
type Output = Result;
154+
type IntoFuture = Pin<Box<dyn Future<Output = Result> + 'a>>;
78155

79-
fn on_cleanup_delete(&self, kind: &'static str, name: &str) {
80-
let cli = self.cli_handle();
81-
let owned = name.to_string();
82-
self.on_cleanup(format!("delete {kind} {name}"), async move || {
83-
cli.run([kind, "delete", &owned]).await?.success()?;
84-
Ok(())
85-
});
156+
fn into_future(self) -> Self::IntoFuture {
157+
Box::pin(self.create())
86158
}
87159
}

0 commit comments

Comments
 (0)