Skip to content

Commit 6ebaf4d

Browse files
refactor(testkit): make the provider helpers Context methods
testkit creates things through Context methods, such as cx.sandbox(..) and cx.on_cleanup(..), so import_profile, create_provider and install_provider become cx methods instead of free functions. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent aeec397 commit 6ebaf4d

4 files changed

Lines changed: 79 additions & 76 deletions

File tree

‎tests/suites/conformance/src/credential_gating.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ credentials:
285285
{endpoints}binaries:
286286
{binaries}"
287287
);
288-
testkit::install_provider(cx, &name, &profile, &[(TOKEN_ENV, TEST_SECRET)]).await?;
288+
cx.install_provider(&name, &profile, &[(TOKEN_ENV, TEST_SECRET)])
289+
.await?;
289290
Ok(name)
290291
}
291292

‎tests/testkit/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ fails. A killed test process skips cleanup, so names are unique to the run.
9393
Pass the result to `cx.sandbox(..).policy(policy)`, or `policy::to_yaml`
9494
for the CLI.
9595
`testkit::PYTHON_BINARIES` names `python3` in `python_image()` images.
96-
- `testkit::import_profile(cx, profile)` to import a provider profile and
97-
return its `id`, and `testkit::create_provider(cx, name, type, credentials)`
98-
to create a provider, each with cleanup. `testkit::install_provider(cx, name,
99-
profile, credentials)` does both.
96+
- `cx.import_profile(profile)` to import a provider profile and return its
97+
`id`, and `cx.create_provider(name, type, credentials)` to create a
98+
provider, each with cleanup. `cx.install_provider(name, profile,
99+
credentials)` does both.
100100
- `testkit::YamlFile` for YAML a CLI command reads from a file.
101101
- `cx.client()` for SDK calls testkit does not wrap.
102102
- `error.grpc_code()` for the gRPC status code (`testkit::GrpcCode`) of a

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ pub use image::{PYTHON_BINARIES, base_image, python_image};
5353
pub use openshell_sdk as sdk;
5454
pub use poll::{Poll, poll_until};
5555
pub use program::main;
56-
pub use provider::{create_provider, import_profile, install_provider};
5756
pub use ready::check as check_ready;
5857
pub use registry::Test;
5958
pub use run::{TestInfo, run};

‎tests/testkit/testkit/src/provider.rs‎

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,82 @@ use crate::context::Context;
77
use crate::error::{Error, Result, ResultExt as _};
88
use crate::yaml_file::YamlFile;
99

10-
/// Import `profile`, a provider profile in YAML, and return its `id`, the
11-
/// provider type it defines. Cleanup deletes the profile after the test's
12-
/// sandboxes and the providers created after it.
13-
pub async fn import_profile(cx: &Context, profile: &str) -> Result<String> {
14-
#[derive(Deserialize)]
15-
struct Profile {
16-
id: String,
10+
impl Context {
11+
/// Import `profile`, a provider profile in YAML, and return its `id`, the
12+
/// provider type it defines. Cleanup deletes the profile after the test's
13+
/// sandboxes and the providers created after it.
14+
pub async fn import_profile(&self, profile: &str) -> Result<String> {
15+
#[derive(Deserialize)]
16+
struct Profile {
17+
id: String,
18+
}
19+
let id = serde_yml::from_str::<Profile>(profile)
20+
.map_err(|error| Error::from_error(&error))
21+
.context("read the provider profile's id")?
22+
.id;
23+
// Deleting a missing profile succeeds, so register the delete before
24+
// the import.
25+
self.on_cleanup_delete("profile", &id);
26+
let file = YamlFile::new(profile)?;
27+
self.cli(["profile", "import", "--file", file.path()?])
28+
.await?
29+
.success()
30+
.context(format!("import profile {id}"))?;
31+
Ok(id)
1732
}
18-
let id = serde_yml::from_str::<Profile>(profile)
19-
.map_err(|error| Error::from_error(&error))
20-
.context("read the provider profile's id")?
21-
.id;
22-
// Deleting a missing profile succeeds, so register the delete before the
23-
// import.
24-
on_cleanup_delete(cx, "profile", &id);
25-
let file = YamlFile::new(profile)?;
26-
cx.cli(["profile", "import", "--file", file.path()?])
27-
.await?
28-
.success()
29-
.context(format!("import profile {id}"))?;
30-
Ok(id)
31-
}
3233

33-
/// Create a provider called `name` of `provider_type` with `credentials`, as
34-
/// pairs of environment variable and value. Cleanup deletes the provider
35-
/// after the test's sandboxes.
36-
///
37-
/// The CLI reads the credential values from its environment, so they aren't
38-
/// in the reported command.
39-
pub async fn create_provider(
40-
cx: &Context,
41-
name: &str,
42-
provider_type: &str,
43-
credentials: &[(&str, &str)],
44-
) -> Result {
45-
// Deleting a missing provider succeeds, so register the delete before the
46-
// create.
47-
on_cleanup_delete(cx, "provider", name);
48-
let mut args = vec![
49-
"provider",
50-
"create",
51-
"--name",
52-
name,
53-
"--type",
54-
provider_type,
55-
];
56-
for (key, _) in credentials {
57-
args.extend(["--credential", key]);
34+
/// Create a provider called `name` of `provider_type` with `credentials`,
35+
/// as pairs of environment variable and value. Cleanup deletes the
36+
/// provider after the test's sandboxes.
37+
///
38+
/// The CLI reads the credential values from its environment, so they
39+
/// aren't in the reported command.
40+
pub async fn create_provider(
41+
&self,
42+
name: &str,
43+
provider_type: &str,
44+
credentials: &[(&str, &str)],
45+
) -> Result {
46+
// Deleting a missing provider succeeds, so register the delete before
47+
// the create.
48+
self.on_cleanup_delete("provider", name);
49+
let mut args = vec![
50+
"provider",
51+
"create",
52+
"--name",
53+
name,
54+
"--type",
55+
provider_type,
56+
];
57+
for (key, _) in credentials {
58+
args.extend(["--credential", key]);
59+
}
60+
self.cli_with_env(credentials, args)
61+
.await?
62+
.success()
63+
.context(format!("create provider {name}"))?;
64+
Ok(())
5865
}
59-
cx.cli_with_env(credentials, args)
60-
.await?
61-
.success()
62-
.context(format!("create provider {name}"))?;
63-
Ok(())
64-
}
6566

66-
/// Import `profile` with [`import_profile`] and create a provider called
67-
/// `name` of its type with [`create_provider`].
68-
pub async fn install_provider(
69-
cx: &Context,
70-
name: &str,
71-
profile: &str,
72-
credentials: &[(&str, &str)],
73-
) -> Result {
74-
let provider_type = import_profile(cx, profile).await?;
75-
create_provider(cx, name, &provider_type, credentials).await
76-
}
67+
/// Import `profile` with [`Context::import_profile`] and create a provider
68+
/// called `name` of its type with [`Context::create_provider`].
69+
pub async fn install_provider(
70+
&self,
71+
name: &str,
72+
profile: &str,
73+
credentials: &[(&str, &str)],
74+
) -> Result {
75+
let provider_type = self.import_profile(profile).await?;
76+
self.create_provider(name, &provider_type, credentials)
77+
.await
78+
}
7779

78-
fn on_cleanup_delete(cx: &Context, kind: &'static str, name: &str) {
79-
let cli = cx.cli_handle();
80-
let owned = name.to_string();
81-
cx.on_cleanup(format!("delete {kind} {name}"), async move || {
82-
cli.run([kind, "delete", &owned]).await?.success()?;
83-
Ok(())
84-
});
80+
fn on_cleanup_delete(&self, kind: &'static str, name: &str) {
81+
let cli = self.cli_handle();
82+
let owned = name.to_string();
83+
self.on_cleanup(format!("delete {kind} {name}"), async move || {
84+
cli.run([kind, "delete", &owned]).await?.success()?;
85+
Ok(())
86+
});
87+
}
8588
}

0 commit comments

Comments
 (0)