use crate::git_helpers::create_local_repo; use crate::harness::{SiteBuilder, TestServer, test_config_with_site}; use crate::runtime::{skip_without_git, skip_without_runtime}; use std::collections::HashMap; use std::time::Duration; // --------------------------------------------------------------------------- // Tier 2 (requires container runtime + git) // --------------------------------------------------------------------------- #[tokio::test] async fn env_vars_passed_to_container_build() { skip_without_git!(); skip_without_runtime!(); let tempdir = tempfile::tempdir().unwrap(); let base_dir = tempdir.path().to_path_buf(); let repo_dir = tempdir.path().join("repos"); tokio::fs::create_dir_all(&repo_dir).await.unwrap(); let repo_url = create_local_repo(&repo_dir, "main").await; let env_vars = HashMap::from([("MY_TEST_VAR".to_owned(), "test_value_123".to_owned())]); let site = SiteBuilder::new("env-test", &repo_url, "test-token") .overrides( "alpine:latest", "sh -c \"mkdir -p out && echo $MY_TEST_VAR > out/env.txt\"", "out", ) .env(env_vars) .build(); let server = TestServer::start(test_config_with_site(base_dir.clone(), site)).await; let resp = TestServer::client() .post(server.url("/env-test")) .header("Authorization", "Bearer test-token") .send() .await .unwrap(); assert_eq!(resp.status().as_u16(), 202); // Wait for build to complete let builds_dir = base_dir.join("builds/env-test"); let max_wait = Duration::from_secs(120); let start = std::time::Instant::now(); loop { assert!(start.elapsed() <= max_wait, "build timed out"); if builds_dir.join("current").is_symlink() { break; } tokio::time::sleep(Duration::from_millis(500)).await; } // Verify the env var was available in the container let current_target = tokio::fs::read_link(builds_dir.join("current")) .await .expect("current symlink should exist"); let content = tokio::fs::read_to_string(current_target.join("env.txt")) .await .expect("env.txt should exist"); assert_eq!( content.trim(), "test_value_123", "env var should be passed to container build" ); } #[tokio::test] async fn env_vars_passed_to_post_deploy_hook() { skip_without_git!(); skip_without_runtime!(); let tempdir = tempfile::tempdir().unwrap(); let base_dir = tempdir.path().to_path_buf(); let repo_dir = tempdir.path().join("repos"); tokio::fs::create_dir_all(&repo_dir).await.unwrap(); let repo_url = create_local_repo(&repo_dir, "main").await; let env_vars = HashMap::from([ ("HOOK_VAR".to_owned(), "hook_value_456".to_owned()), ("DEPLOY_ENV".to_owned(), "production".to_owned()), ]); let site = SiteBuilder::new("hook-env-test", &repo_url, "test-token") .overrides( "alpine:latest", "mkdir -p out && echo test > out/index.html", "out", ) .env(env_vars) .post_deploy(vec![ "sh".to_owned(), "-c".to_owned(), "env > \"$WITRYNA_BUILD_DIR/env-dump.txt\"".to_owned(), ]) .build(); let server = TestServer::start(test_config_with_site(base_dir.clone(), site)).await; let resp = TestServer::client() .post(server.url("/hook-env-test")) .header("Authorization", "Bearer test-token") .send() .await .unwrap(); assert_eq!(resp.status().as_u16(), 202); // Wait for build + hook to complete (poll for env-dump.txt) let builds_dir = base_dir.join("builds/hook-env-test"); let max_wait = Duration::from_secs(120); let start = std::time::Instant::now(); let env_dump_path = loop { assert!(start.elapsed() <= max_wait, "build timed out"); if builds_dir.join("current").is_symlink() { let target = tokio::fs::read_link(builds_dir.join("current")) .await .expect("current symlink should exist"); let dump = target.join("env-dump.txt"); if dump.exists() { break dump; } } tokio::time::sleep(Duration::from_millis(500)).await; }; let content = tokio::fs::read_to_string(&env_dump_path) .await .expect("env-dump.txt should be readable"); // Verify custom env vars assert!( content.contains("HOOK_VAR=hook_value_456"), "HOOK_VAR should be in hook environment" ); assert!( content.contains("DEPLOY_ENV=production"), "DEPLOY_ENV should be in hook environment" ); // Verify standard witryna env vars are also present assert!( content.contains("WITRYNA_SITE=hook-env-test"), "WITRYNA_SITE should be set" ); assert!( content.contains("WITRYNA_BUILD_DIR="), "WITRYNA_BUILD_DIR should be set" ); assert!( content.contains("WITRYNA_PUBLIC_DIR="), "WITRYNA_PUBLIC_DIR should be set" ); assert!( content.contains("WITRYNA_BUILD_TIMESTAMP="), "WITRYNA_BUILD_TIMESTAMP should be set" ); assert!( content.contains("WITRYNA_BUILD_STATUS=success"), "WITRYNA_BUILD_STATUS should be set to success" ); }