From 9bb12fa037690257cfc0836505e5bf4a892717e2 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 18 Aug 2023 22:37:59 +0000 Subject: [PATCH] tests: fix the integration tests for lunatic this commits fix the integration tests for lunatic shim by adding hello to the end of the url for curling. It also removes unused code in mod.rs Signed-off-by: jiaxiao zhou --- tests/common/mod.rs | 248 ++++---------------------------------- tests/integration_test.rs | 4 +- 2 files changed, 26 insertions(+), 226 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index c01e05de..a2dc6395 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,204 +1,10 @@ -use std::{io::Read, net::TcpListener, time::Duration}; +use std::{io::Read, time::Duration}; use anyhow::Result; use curl::easy::Easy; use k8s_openapi::api::core::v1::Pod; use kube::{api::ListParams, config::KubeConfigOptions, Api, Client, Config, ResourceExt}; use rand::{distributions::Alphanumeric, Rng}; -use tokio::process::Command; - -async fn which_binary(bianry_name: &str) -> Result<()> { - println!(" >>> which {}", bianry_name); - let mut cmd = Command::new("which"); - cmd.arg(bianry_name); - let output = cmd.output().await; - if output.is_err() { - anyhow::bail!(format!("{} not found in PATH", bianry_name)); - } - let output = output.unwrap(); - if !output.status.success() { - anyhow::bail!(format!("{} not found in PATH", bianry_name)); - } - Ok(()) -} - -pub async fn setup_test(test_ns: &str) -> Result { - let res = setup_test_helper(test_ns).await; - if res.is_err() { - println!(" >>> setup test failed"); - teardown_test(test_ns).await?; - return res; - } - res -} - -async fn setup_test_helper(test_ns: &str) -> Result { - which_binary("k3d").await?; - which_binary("cross").await?; - which_binary("docker").await?; - which_binary("kubectl").await?; - - let dockerfile_path = "deployments/k3d"; - let bin_path = "deployments/k3d/.tmp/"; - let slight_shim_path = "deployments/k3d/.tmp/containerd-shim-slight-v1"; - let spin_shim_path = "deployments/k3d/.tmp/containerd-shim-spin-v1"; - let wws_shim_path = "deployments/k3d/.tmp/containerd-shim-wws-v1"; - - if which_binary(slight_shim_path).await.is_err() { - println!(" >>> install containerd-shim-slight-v1"); - let mut cmd = Command::new("cross"); - cmd.arg("build") - .arg("--target") - .arg("x86_64-unknown-linux-musl") - .arg("--release") - .arg("--manifest-path") - .arg("containerd-shim-slight-v1/Cargo.toml"); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!("failed to build containerd-shim-slight-v1"); - } - let mut cmd = Command::new("sudo"); - cmd.arg("install") - .arg("containerd-shim-slight-v1/target/x86_64-unknown-linux-musl/release/containerd-shim-slight-v1") - .arg(bin_path); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!("failed to install containerd-shim-slight-v1"); - } - } - - if which_binary(spin_shim_path).await.is_err() { - println!(" >>> install containerd-shim-spin-v1"); - let mut cmd = Command::new("cross"); - cmd.arg("build") - .arg("--target") - .arg("x86_64-unknown-linux-musl") - .arg("--release") - .arg("--manifest-path") - .arg("containerd-shim-spin-v1/Cargo.toml"); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!("failed to build containerd-shim-spin-v1"); - } - let mut cmd = Command::new("sudo"); - cmd.arg("install") - .arg("containerd-shim-spin-v1/target/x86_64-unknown-linux-musl/release/containerd-shim-spin-v1") - .arg(bin_path); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!("failed to install containerd-shim-spin-v1"); - } - } - - if which_binary(wws_shim_path).await.is_err() { - println!(" >>> install containerd-shim-wws-v1"); - let mut cmd = Command::new("cross"); - cmd.arg("build") - .arg("--target") - .arg("x86_64-unknown-linux-musl") - .arg("--release") - .arg("--manifest-path") - .arg("containerd-shim-wws-v1/Cargo.toml"); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!("failed to build containerd-shim-wws-v1"); - } - let mut cmd = Command::new("sudo"); - cmd.arg("install") - .arg("containerd-shim-wws-v1/target/x86_64-unknown-linux-musl/release/containerd-shim-wws-v1") - .arg(bin_path); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!("failed to install containerd-shim-wws-v1"); - } - } - - // build docker image - let mut cmd = Command::new("docker"); - cmd.arg("build").arg("-t").arg(test_ns).arg(dockerfile_path); - let output = cmd.output().await?; - if !output.status.success() { - // print out the error message to stderr - eprintln!("{}", String::from_utf8_lossy(&output.stderr)); - // print out the stdout - println!("{}", String::from_utf8_lossy(&output.stdout)); - anyhow::bail!(format!("failed to build docker image {}", test_ns)); - } - - // create k3d cluster - let cluster_name = format!("{}-cluster", test_ns); - let image_name = test_ns; - let _context_name = format!("k3d-{}", cluster_name); - - let host_port = get_available_port().expect("failed to get available port"); - // k3d cluster create $(CLUSTER_NAME) --image $(IMAGE_NAME) --api-port 6550 -p "8081:80@loadbalancer" --agents 1 - let mut cmd = Command::new("k3d"); - cmd.arg("cluster") - .arg("create") - .arg(&cluster_name) - .arg("--image") - .arg(&image_name) - .arg("-p") - .arg(format!("{}:80@loadbalancer", host_port)) - .arg("--agents") - .arg("1"); - let output = cmd.output().await?; - if !output.status.success() { - // print out the error message to stderr - eprintln!("{}", String::from_utf8_lossy(&output.stderr)); - anyhow::bail!(format!("failed to create k3d cluster {}", cluster_name)); - } - - Ok(host_port) -} - -pub async fn teardown_test(test_ns: &str) -> Result<()> { - let cluster_name = format!("{}-cluster", test_ns); - - // delete k3d cluster - let mut cmd = Command::new("k3d"); - cmd.arg("cluster").arg("delete").arg(cluster_name); - let output = cmd.output().await?; - if !output.status.success() { - anyhow::bail!(format!("failed to delete k3d cluster {}", test_ns)); - } - - // delete docker image - let mut cmd = Command::new("docker"); - cmd.arg("rmi").arg(test_ns); - let output = cmd.output().await?; - if !output.status.success() { - // print out the error message to stderr - eprintln!("{}", String::from_utf8_lossy(&output.stderr)); - // print out the stdout - println!("{}", String::from_utf8_lossy(&output.stdout)); - anyhow::bail!(format!("failed to delete docker image {}", test_ns)); - } - - // check docker image is deleted - let mut cmd = Command::new("docker"); - cmd.arg("image").arg("inspect").arg(test_ns); - let output = cmd.output().await?; - if output.status.success() { - anyhow::bail!(format!("failed to delete docker image {}", test_ns)); - } - - Ok(()) -} - -fn port_is_available(port: u16) -> bool { - TcpListener::bind(("127.0.0.1", port)).is_ok() -} - -fn get_available_port() -> Option { - let mut rng = rand::thread_rng(); - loop { - let port: u16 = rng.gen_range(1025..65535); - if port_is_available(port) { - return Some(port); - } - } -} pub async fn retry_get( url: &str, @@ -209,15 +15,18 @@ pub async fn retry_get( let mut i = 0; let mut handle = Easy::new(); handle.url(url)?; - let mut transfer = handle.transfer(); - transfer.write_function(|data| { - buf.extend_from_slice(data); - Ok(data.len()) - })?; - loop { - let res = transfer.perform(); - if res.is_ok() { + let res = { + let mut transfer = handle.transfer(); + transfer.write_function(|data| { + buf.extend_from_slice(data); + Ok(data.len()) + })?; + transfer.perform() + }; + let response_code = handle.response_code()?; + // verify res is ok and not 404 + if res.is_ok() && response_code != 404 { break; } i += 1; @@ -240,16 +49,19 @@ pub async fn retry_put( handle.url(url)?; handle.put(true)?; handle.post_field_size(data.len() as u64)?; - let mut transfer = handle.transfer(); - transfer.read_function(|into| Ok(data.as_bytes().read(into).unwrap_or(0)))?; - transfer.write_function(|data| { - println!("{}", String::from_utf8_lossy(data)); - Ok(data.len()) - })?; - loop { - let res = transfer.perform(); - if res.is_ok() { + let res = { + let mut transfer = handle.transfer(); + transfer.read_function(|into| Ok(data.as_bytes().read(into).unwrap_or(0)))?; + transfer.write_function(|data| { + println!("{}", String::from_utf8_lossy(data)); + Ok(data.len()) + })?; + transfer.perform() + }; + + let response_code = handle.response_code()?; + if res.is_ok() && response_code != 404 { break; } i += 1; @@ -277,18 +89,6 @@ pub async fn list_pods(cluster_name: &str) -> Result<()> { Ok(()) } -pub async fn k_apply(path: &str) -> Result<()> { - let mut cmd = Command::new("kubectl"); - cmd.arg("apply").arg("-f").arg(path); - let output = cmd.output().await?; - if !output.status.success() { - // print out the error message to stderr - eprintln!("{}", String::from_utf8_lossy(&output.stderr)); - anyhow::bail!(format!("failed to apply {}", path)); - } - Ok(()) -} - pub async fn random_payload() -> String { let rng = rand::thread_rng(); let payload: String = rng diff --git a/tests/integration_test.rs b/tests/integration_test.rs index e9fd201a..0d1129a3 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -105,10 +105,10 @@ async fn lunatic_test() -> Result<()> { list_pods(&cluster_name).await?; // curl for hello - println!(" >>> curl http://localhost:{}/lunatic", host_port); + println!(" >>> curl http://localhost:{}/lunatic/hello", host_port); let mut res = Vec::new(); retry_get( - &format!("http://localhost:{}/lunatic", host_port), + &format!("http://localhost:{}/lunatic/hello", host_port), &mut res, RETRY_TIMES, INTERVAL_IN_SECS,