Skip to content

Commit

Permalink
Remove env::set_var usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Jan 6, 2025
1 parent ea0bf41 commit 7e41123
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ clap = { version = "4.0.4", features = ["cargo", "derive"] }
async-trait = "0.1.30"
typetag = "0.2.5"
aws-throwaway = { version = "0.6.0", default-features = false }
tokio-bin-process = "0.6.0"
#tokio-bin-process = "0.6.0"
tokio-bin-process = { git = "https://github.com/rukai/tokio-bin-process", branch = "set_env_vars"}
ordered-float = { version = "4.0.0", features = ["serde"] }
shell-quote = { default-features = false, features = ["bash"], version = "0.7.0" }
pretty_assertions = "1.4.0"
20 changes: 13 additions & 7 deletions custom-transforms-example/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use redis::Cmd;
use std::time::Duration;
use test_helpers::connection::valkey_connection;
use test_helpers::docker_compose::docker_compose;
use test_helpers::shotover_process::{bin_path, BinProcess, EventMatcher, Level};
use test_helpers::shotover_process::{
bin_path, BinProcess, BinProcessBuilder, EventMatcher, Level,
};

#[tokio::test(flavor = "multi_thread")]
async fn test_custom_transform() {
Expand Down Expand Up @@ -37,12 +39,16 @@ async fn test_custom_transform() {
}

async fn shotover_proxy(topology_path: &str) -> BinProcess {
let mut shotover = BinProcess::start_binary(
bin_path!("custom-transforms-example"),
"shotover",
&["-t", topology_path, "--log-format", "json"],
)
.await;
let mut shotover = BinProcessBuilder::from_path(bin_path!("custom-transforms-example"))
.with_log_name(Some("shotover".to_owned()))
.with_args(vec![
"-t".to_owned(),
topology_path.to_owned(),
"--log-format".to_owned(),
"json".to_owned(),
])
.start()
.await;

tokio::time::timeout(
Duration::from_secs(30),
Expand Down
2 changes: 1 addition & 1 deletion shotover-proxy/benches/windsock/cloud/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ pub async fn upload_shotover(instance: &Ec2Instance) {

instance
.ssh()
.push_file(local_shotover_path, Path::new("shotover-bin"))
.push_file(&local_shotover_path, Path::new("shotover-bin"))
.await;
instance
.ssh()
Expand Down
7 changes: 1 addition & 6 deletions test-helpers/src/docker_compose.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use docker_compose_runner::*;
use std::{env, time::Duration};
use std::time::Duration;

pub use docker_compose_runner::DockerCompose;

Expand All @@ -12,11 +12,6 @@ pub fn docker_compose(file_path: &str) -> DockerCompose {

/// Creates a new DockerCompose running an instance of moto the AWS mocking server
pub fn new_moto() -> DockerCompose {
// Overwrite any existing AWS credential env vars belonging to the user with dummy values to be sure that
// we wont hit their real AWS account in the case of a bug in shotover or the test
env::set_var("AWS_ACCESS_KEY_ID", "dummy-access-key");
env::set_var("AWS_SECRET_ACCESS_KEY", "dummy-access-key-secret");

docker_compose("tests/transforms/docker-compose-moto.yaml")
}

Expand Down
68 changes: 47 additions & 21 deletions test-helpers/src/shotover_process.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::time::Duration;

pub use tokio_bin_process::bin_path;
pub use tokio_bin_process::event::{Event, Level};
pub use tokio_bin_process::event_matcher::{Count, EventMatcher, Events};
pub use tokio_bin_process::BinProcess;
pub use tokio_bin_process::BinProcessBuilder;

pub struct ShotoverProcessBuilder {
topology_path: String,
Expand Down Expand Up @@ -40,8 +41,8 @@ impl ShotoverProcessBuilder {

/// Hint that there is a precompiled shotover binary available.
/// This binary will be used unless a profile is specified.
pub fn with_bin(mut self, bin_path: &Path) -> Self {
self.bin_path = Some(bin_path.to_owned());
pub fn with_bin(mut self, bin_path: PathBuf) -> Self {
self.bin_path = Some(bin_path);
self
}

Expand Down Expand Up @@ -71,16 +72,16 @@ impl ShotoverProcessBuilder {
self
}

pub async fn start(&self) -> BinProcess {
let mut shotover = self.start_inner().await;
pub async fn start(self) -> BinProcess {
let (mut shotover, event_matchers) = self.start_inner().await;

tokio::time::timeout(
Duration::from_secs(30),
shotover.wait_for(
&EventMatcher::new()
.with_level(Level::Info)
.with_message("Shotover is now accepting inbound connections"),
&self.event_matchers,
&event_matchers,
),
)
.await
Expand All @@ -90,37 +91,62 @@ impl ShotoverProcessBuilder {
}

pub async fn assert_fails_to_start(
&self,
self,
expected_errors_and_warnings: &[EventMatcher],
) -> Events {
self.start_inner()
.await
.0
.consume_remaining_events_expect_failure(expected_errors_and_warnings)
.await
}

async fn start_inner(&self) -> BinProcess {
let mut args = vec!["-t", &self.topology_path, "--log-format", "json"];
if let Some(cores) = &self.cores {
args.extend(["--core-threads", cores]);
async fn start_inner(self) -> (BinProcess, Vec<EventMatcher>) {
let mut args = vec![
"-t".to_owned(),
self.topology_path,
"--log-format".to_owned(),
"json".to_owned(),
];
if let Some(cores) = self.cores {
args.extend(["--core-threads".to_owned(), cores]);
}
let config_path = self
.config_path
.clone()
.unwrap_or_else(|| "config/config.yaml".to_owned());
args.extend(["-c", &config_path]);
args.extend(["-c".to_owned(), config_path]);

let log_name = self.log_name.as_deref().unwrap_or("shotover");
let log_name = self.log_name.unwrap_or_else(|| "shotover".to_owned());

match (&self.profile, &self.bin_path) {
let builder = match (self.profile, self.bin_path) {
(Some(profile), _) => {
BinProcess::start_binary_name("shotover-proxy", log_name, &args, Some(profile))
.await
BinProcessBuilder::from_cargo_name("shotover-proxy".to_owned(), Some(profile))
}
(None, Some(bin_path)) => BinProcess::start_binary(bin_path, log_name, &args).await,
(None, None) => {
BinProcess::start_binary_name("shotover-proxy", log_name, &args, None).await
}
}
(None, Some(bin_path)) => BinProcessBuilder::from_path(bin_path),
(None, None) => BinProcessBuilder::from_cargo_name("shotover-proxy".to_owned(), None),
};
let process = builder
.with_log_name(Some(log_name))
.with_args(args)
// Overwrite any existing AWS credential env vars belonging to the user with dummy values to be sure that
// shotover wont run with their real AWS account
//
// This also enables tests to run against moto mock AWS service as shotover's AWS client will give up
// if it cant find a key even though moto will accept any key.
.with_env_vars(vec![
(
"AWS_ACCESS_KEY_ID".to_owned(),
"dummy-access-key".to_owned(),
),
(
"AWS_SECRET_ACCESS_KEY".to_owned(),
"dummy-access-key-secret".to_owned(),
),
])
.start()
.await;

(process, self.event_matchers)
}
}

0 comments on commit 7e41123

Please sign in to comment.