Skip to content

Commit

Permalink
Fixed mirrord ls E2E tests (#3122)
Browse files Browse the repository at this point in the history
* test fixed

* Switch operator use based on features

* Better env
  • Loading branch information
Razz4780 authored Feb 25, 2025
1 parent 592c598 commit ff500a9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 89 deletions.
1 change: 1 addition & 0 deletions changelog.d/+fixed-mirrord-ls-test.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `mirrord ls` E2E tests. Now there's only one test, and it uses a fresh namespace with a random name.
24 changes: 1 addition & 23 deletions tests/src/cli/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
mod cli {
use std::{path::Path, time::Duration};

use regex::Regex;
use rstest::rstest;

use crate::utils::{
config_dir, run_ls, run_verify_config, service_for_mirrord_ls, KubeService,
};
use crate::utils::{config_dir, run_verify_config};

/// Tests `verify-config` with `path` and `--ide` args, which should be:
///
Expand Down Expand Up @@ -95,23 +92,4 @@ mod cli {

assert!(!process.wait().await.success());
}

/// Tests for the `mirrord ls` command
#[rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
pub async fn mirrord_ls(#[future] service_for_mirrord_ls: KubeService) {
let service = service_for_mirrord_ls.await;
let mut process = run_ls::<false>(None, None).await;
let res = process.wait().await;
assert!(res.success());
let stdout = process.get_stdout().await;
let targets: Vec<String> = serde_json::from_str(&stdout).unwrap();
let re = Regex::new(r"^(pod|deployment)/.+(/container/.+)?$").unwrap();
targets
.iter()
.for_each(|output| assert!(re.is_match(output)));
assert!(targets
.iter()
.any(|output| output.starts_with(&format!("pod/{}", service.name))));
}
}
3 changes: 2 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

#[cfg(feature = "cli")]
mod cli;

mod env;
mod file_ops;
mod http;
mod issue1317;
#[cfg(any(feature = "cli", feature = "operator"))]
mod ls;
mod operator;
mod targetless;
mod traffic;
Expand Down
66 changes: 66 additions & 0 deletions tests/src/ls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#![cfg(test)]

use fancy_regex::Regex;
use rstest::rstest;

use crate::utils::{kube_client, run_ls, service_for_mirrord_ls};

/// Test for the `mirrord ls` command.
#[rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
pub async fn mirrord_ls() {
let namespace = format!("test-namespace-{:x}", rand::random::<u32>());
// This function is implemented differently when the `operator` feature is enabled and when it
// is disabled.
let _setup = service_for_mirrord_ls(
&namespace,
"NodePort",
"ghcr.io/metalbear-co/mirrord-pytest:latest",
"ls-service",
false,
kube_client(),
)
.await;

let mut process = run_ls(&namespace).await;
let res = process.wait().await;
assert!(res.success(), "mirrord ls command failed");

let stdout = process.get_stdout().await;
let targets: Vec<String> =
serde_json::from_str(&stdout).expect("mirrord ls output should be a valid JSON");

let types = [
"pod",
"deployment",
#[cfg(feature = "operator")]
"statefulset",
#[cfg(feature = "operator")]
"cronjob",
#[cfg(feature = "operator")]
"job",
#[cfg(feature = "operator")]
"replicaset",
#[cfg(feature = "operator")]
"service",
];

let pattern = format!(r"^({})/.+(/container/.+)?$", types.join("|"));
let re = Regex::new(&pattern).unwrap();

targets.iter().for_each(|output| {
assert!(
re.is_match(output).expect("checking regex match panicked"),
"output line does not match the pattern `{pattern}`: {output}"
)
});

for target_type in types {
assert!(
targets
.iter()
.any(|output| output.starts_with(&format!("{target_type}/ls-service"))),
"target type {target_type} was not found in the output",
)
}
}
1 change: 0 additions & 1 deletion tests/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
mod concurrent_steal;
mod policies;
mod queue_splitting;
mod sanity;
mod setup;
50 changes: 0 additions & 50 deletions tests/src/operator/sanity.rs

This file was deleted.

19 changes: 5 additions & 14 deletions tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,23 +661,14 @@ pub async fn run_exec(
run_mirrord(args, base_env).await
}

/// Runs `mirrord ls` command and asserts if the json matches the expected format
pub async fn run_ls<const USE_OPERATOR: bool>(
args: Option<Vec<&str>>,
namespace: Option<&str>,
) -> TestProcess {
/// Runs `mirrord ls` command.
pub async fn run_ls(namespace: &str) -> TestProcess {
let mut mirrord_args = vec!["ls"];
if let Some(args) = args {
mirrord_args.extend(args);
}
if let Some(namespace) = namespace {
mirrord_args.extend(vec!["--namespace", namespace]);
}
mirrord_args.extend(vec!["--namespace", namespace]);

let mut env = HashMap::new();
if USE_OPERATOR {
env.insert("MIRRORD_OPERATOR_ENABLE", "true");
};
let use_operator = cfg!(feature = "operator").to_string();
env.insert("MIRRORD_OPERATOR_ENABLE", use_operator.as_str());

run_mirrord(mirrord_args, env).await
}
Expand Down

0 comments on commit ff500a9

Please sign in to comment.