-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[antlir2][image_test] test execution subcommand
Summary: Create a subcommand that actually does the test execution. This does not change any net behavior of `image_test`, but it will soon make it possible for this to be invoked by a user in an interactive shell. Test Plan: ``` ❯ buck2 test fbcode//antlir/antlir2/testing/tests:test-rust-centos9 fbcode//antlir/antlir2/testing/tests:test-rust-nobody-centos9 fbcode//antlir/antlir2/testing/tests:test-rust-boot-centos9 fbcode//antlir/antlir2/testing/tests:test-rust-boot-nobody-centos9 Buck UI: https://www.internalfb.com/buck2/7b40707e-700d-43a5-a43b-6aa790f29c1b Test UI: https://www.internalfb.com/intern/testinfra/testrun/9288674289047734 Network: Up: 6.2KiB Down: 2.0KiB (reSessionID-07bd93dd-40c3-4a2d-afad-25ac6a24c372) Jobs completed: 60. Time elapsed: 7.4s. Cache hits: 0%. Commands: 6 (cached: 0, remote: 0, local: 6) Tests finished: Pass 20. Fail 0. Fatal 0. Skip 0. Build failure 0 ``` Reviewed By: naveedgol Differential Revision: D65825538 fbshipit-source-id: dff6f2307a6740a80a963277289de33fc568d522
- Loading branch information
1 parent
5b69325
commit 48ef781
Showing
8 changed files
with
119 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use std::collections::BTreeMap; | ||
use std::ffi::OsString; | ||
use std::os::unix::process::CommandExt; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
use anyhow::Context; | ||
use anyhow::Result; | ||
use bon::Builder; | ||
use clap::Parser; | ||
use json_arg::JsonFile; | ||
use nix::unistd::User; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, Builder, Serialize, Deserialize)] | ||
/// Specification of how to execute the test. | ||
/// This specification is just how to invoke the inner test binary, the | ||
/// containerization should already have been set up by 'spawn'. | ||
pub(crate) struct Spec { | ||
/// The test command | ||
cmd: Vec<OsString>, | ||
/// CWD of the test | ||
working_directory: PathBuf, | ||
/// Run the test as this user | ||
user: String, | ||
/// Set these env vars in the test environment | ||
#[serde(default)] | ||
env: BTreeMap<String, String>, | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
/// Execute the inner test | ||
pub(crate) struct Args { | ||
#[clap(default_value = "/__antlir2_image_test__/exec_spec.json")] | ||
spec: JsonFile<Spec>, | ||
} | ||
|
||
impl Args { | ||
pub(crate) fn run(self) -> Result<()> { | ||
let spec = self.spec.into_inner(); | ||
std::env::set_current_dir(&spec.working_directory) | ||
.with_context(|| format!("while changing to '{}'", spec.working_directory.display()))?; | ||
let mut env = spec.env; | ||
env.insert("USER".into(), spec.user.clone()); | ||
env.insert( | ||
"PWD".into(), | ||
spec.working_directory | ||
.to_str() | ||
.with_context(|| { | ||
format!("pwd '{}' was not utf8", spec.working_directory.display()) | ||
})? | ||
.into(), | ||
); | ||
|
||
let user = User::from_name(&spec.user) | ||
.context("failed to lookup user")? | ||
.with_context(|| format!("no such user '{}'", spec.user))?; | ||
|
||
let mut cmd = spec.cmd.into_iter(); | ||
let err = Command::new(cmd.next().context("test command was empty")?) | ||
.args(cmd) | ||
.envs(env) | ||
.uid(user.uid.into()) | ||
.gid(user.gid.into()) | ||
.exec(); | ||
Err(err.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters