Compare commits
2 Commits
0dfb16e5f6
...
accb689117
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
accb689117 | ||
|
|
c515886cbf |
@ -1,7 +1,8 @@
|
||||
use crate::kube::portforward::PortForwardSessionConfig;
|
||||
use crate::kube::portforward::{PortForwardSession, PortForwardSessionConfig};
|
||||
use crate::kube::ClusterClient;
|
||||
use crate::shell::kubectl::locate_kubectl;
|
||||
use crate::state::AppState;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::Value;
|
||||
@ -10,6 +11,11 @@ use tauri::State;
|
||||
use tokio::process::Command;
|
||||
use tracing::info;
|
||||
|
||||
// Regex pattern for Kubernetes resource names - cached for performance
|
||||
lazy_static! {
|
||||
static ref NAME_PATTERN_REGEX: Regex = Regex::new(r"^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$").unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ClusterInfo {
|
||||
pub id: String,
|
||||
@ -200,6 +206,15 @@ pub async fn test_cluster_connection(
|
||||
std::fs::write(&temp_path, kubeconfig_content)
|
||||
.map_err(|e| format!("Failed to write kubeconfig temp file: {e}"))?;
|
||||
|
||||
// Cleanup temp file on drop
|
||||
struct TempFileCleanup(std::path::PathBuf);
|
||||
impl Drop for TempFileCleanup {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
let _cleanup = TempFileCleanup(temp_path.clone());
|
||||
|
||||
// Run kubectl cluster-info
|
||||
let kubectl_path = locate_kubectl()?;
|
||||
|
||||
@ -372,7 +387,6 @@ fn parse_creation_timestamp(timestamp: &str) -> String {
|
||||
// Regex patterns for Kubernetes resource names
|
||||
// Must match: ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$ (DNS subdomain name)
|
||||
// Added max length check (253 chars) to prevent ReDoS attacks
|
||||
const NAME_PATTERN: &str = r"^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$";
|
||||
const MAX_NAME_LENGTH: usize = 253;
|
||||
|
||||
/// Validates a Kubernetes resource name against DNS subdomain naming rules.
|
||||
@ -409,12 +423,11 @@ pub fn validate_resource_name(name: &str, field_name: &str) -> Result<(), String
|
||||
));
|
||||
}
|
||||
|
||||
// Validate against the regex pattern
|
||||
let pattern = Regex::new(NAME_PATTERN).map_err(|e| format!("Invalid regex pattern: {}", e))?;
|
||||
if !pattern.is_match(name) {
|
||||
// Use cached regex pattern
|
||||
if !NAME_PATTERN_REGEX.is_match(name) {
|
||||
return Err(format!(
|
||||
"{} '{}' does not match pattern {}",
|
||||
field_name, name, NAME_PATTERN
|
||||
field_name, name, r"^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$"
|
||||
));
|
||||
}
|
||||
|
||||
@ -466,6 +479,15 @@ pub async fn start_port_forward(
|
||||
std::fs::write(&temp_path, kubeconfig_content.as_ref())
|
||||
.map_err(|e| format!("Failed to write kubeconfig temp file: {e}"))?;
|
||||
|
||||
// Cleanup temp file on drop
|
||||
struct TempFileCleanup(std::path::PathBuf);
|
||||
impl Drop for TempFileCleanup {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
let _cleanup = TempFileCleanup(temp_path.clone());
|
||||
|
||||
// Build kubectl command
|
||||
let kubectl_path = locate_kubectl()?;
|
||||
let args = vec![
|
||||
@ -490,10 +512,8 @@ pub async fn start_port_forward(
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to spawn kubectl: {e}"))?;
|
||||
|
||||
let child_mutex = Arc::new(std::sync::Mutex::new(child));
|
||||
|
||||
// Create session with allocated port
|
||||
let session = crate::kube::PortForwardSession::new(PortForwardSessionConfig {
|
||||
let session = PortForwardSession::new(PortForwardSessionConfig {
|
||||
id: session_id.clone(),
|
||||
cluster_id: request.cluster_id.clone(),
|
||||
cluster_name,
|
||||
@ -502,14 +522,15 @@ pub async fn start_port_forward(
|
||||
container: None,
|
||||
ports: vec![request.container_port],
|
||||
local_ports: vec![local_port],
|
||||
temp_kubeconfig_path: Some(temp_path),
|
||||
});
|
||||
|
||||
// Store child handle in session
|
||||
// Store child handle in session - spawn background task to wait on child
|
||||
{
|
||||
let mut port_forwards = state.port_forwards.lock().await;
|
||||
port_forwards.insert(session_id.clone(), session);
|
||||
let session_mut = port_forwards.get_mut(&session_id).unwrap();
|
||||
session_mut.kubectl_child = Some(child_mutex);
|
||||
session_mut.spawn_child_waiter(child);
|
||||
}
|
||||
|
||||
info!(
|
||||
@ -534,17 +555,8 @@ pub async fn stop_port_forward(id: String, state: State<'_, AppState>) -> Result
|
||||
let mut port_forwards = state.port_forwards.lock().await;
|
||||
|
||||
if let Some(session) = port_forwards.get_mut(&id) {
|
||||
session.stop();
|
||||
session.stop_async().await;
|
||||
info!(session_id = %id, "Port-forward session stopped");
|
||||
|
||||
// Kill the kubectl process to ensure termination
|
||||
if let Some(child_mutex) = &session.kubectl_child {
|
||||
let mut child = child_mutex.lock().unwrap();
|
||||
// Kill the child process - use std::mem::drop to explicitly handle the Future
|
||||
std::mem::drop(child.kill());
|
||||
let _ = child.try_wait();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Port forward session {id} not found"))
|
||||
@ -649,4 +661,33 @@ mod tests {
|
||||
assert_eq!(request.container_port, parsed.container_port);
|
||||
assert_eq!(request.local_port, parsed.local_port);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_resource_name_valid() {
|
||||
// Valid names
|
||||
assert!(validate_resource_name("my-pod", "pod").is_ok());
|
||||
assert!(validate_resource_name("my-pod-123", "pod").is_ok());
|
||||
assert!(validate_resource_name("a", "pod").is_ok());
|
||||
assert!(validate_resource_name("my.pod.name", "pod").is_ok());
|
||||
assert!(validate_resource_name("123", "pod").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_resource_name_invalid() {
|
||||
// Invalid names
|
||||
assert!(validate_resource_name("-mypod", "pod").is_err());
|
||||
assert!(validate_resource_name("mypod-", "pod").is_err());
|
||||
assert!(validate_resource_name(".mypod", "pod").is_err());
|
||||
assert!(validate_resource_name("mypod.", "pod").is_err());
|
||||
assert!(validate_resource_name("MYPOD", "pod").is_err());
|
||||
assert!(validate_resource_name("my_pod", "pod").is_err());
|
||||
assert!(validate_resource_name("", "pod").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_resource_name_length() {
|
||||
// Too long names
|
||||
let long_name = "a".repeat(254);
|
||||
assert!(validate_resource_name(&long_name, "pod").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,16 @@
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::process::Child;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
|
||||
/// Background task handle for waiting on kubectl child process
|
||||
pub struct ChildWaitHandle {
|
||||
pub join_handle: tokio::task::JoinHandle<()>,
|
||||
pub child_id: String,
|
||||
pub child: Arc<TokioMutex<Option<Child>>>,
|
||||
}
|
||||
|
||||
pub struct PortForwardSession {
|
||||
pub id: String,
|
||||
pub cluster_id: String,
|
||||
@ -11,11 +21,15 @@ pub struct PortForwardSession {
|
||||
pub ports: Vec<u16>,
|
||||
pub local_ports: Vec<u16>,
|
||||
pub status: PortForwardStatus,
|
||||
pub kubectl_child: Option<Arc<std::sync::Mutex<tokio::process::Child>>>,
|
||||
/// Join handle for the background task waiting on the kubectl child
|
||||
pub child_wait_handle: Option<Arc<TokioMutex<ChildWaitHandle>>>,
|
||||
pub is_stopped: Arc<AtomicBool>,
|
||||
pub error_message: Option<String>,
|
||||
/// Path to temp kubeconfig file for cleanup
|
||||
pub temp_kubeconfig_path: Option<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum PortForwardStatus {
|
||||
Active,
|
||||
Stopped,
|
||||
@ -32,6 +46,8 @@ pub struct PortForwardSessionConfig {
|
||||
pub container: Option<String>,
|
||||
pub ports: Vec<u16>,
|
||||
pub local_ports: Vec<u16>,
|
||||
/// Path to temp kubeconfig file for cleanup
|
||||
pub temp_kubeconfig_path: Option<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
impl PortForwardSession {
|
||||
@ -46,21 +62,81 @@ impl PortForwardSession {
|
||||
ports: config.ports,
|
||||
local_ports: config.local_ports,
|
||||
status: PortForwardStatus::Active,
|
||||
kubectl_child: None,
|
||||
child_wait_handle: None,
|
||||
is_stopped: Arc::new(AtomicBool::new(false)),
|
||||
error_message: None,
|
||||
temp_kubeconfig_path: config.temp_kubeconfig_path,
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a background task to wait on the kubectl child process
|
||||
/// and update session state on completion/error
|
||||
pub fn spawn_child_waiter(&mut self, child: Child) {
|
||||
let child_id = format!("{}-{}", self.id, self.pod);
|
||||
let is_stopped = self.is_stopped.clone();
|
||||
let status_clone = Arc::new(std::sync::Mutex::new(self.status.clone()));
|
||||
let error_clone = Arc::new(std::sync::Mutex::new(self.error_message.clone()));
|
||||
|
||||
// Store the child in an Arc<Mutex<Option<Child>>> so it can be accessed from the async task
|
||||
// and also from the stop() method
|
||||
let child_arc = Arc::new(TokioMutex::new(Some(child)));
|
||||
|
||||
let child_for_task = child_arc.clone();
|
||||
let join_handle = tokio::spawn(async move {
|
||||
// Take the child from the Arc
|
||||
let mut child = child_for_task.lock().await.take().expect("Child not set");
|
||||
|
||||
// Wait for the child process to complete
|
||||
// This is safe because we're in an async context
|
||||
let result = child.wait().await;
|
||||
|
||||
// Only update if not already explicitly stopped
|
||||
if !is_stopped.load(Ordering::SeqCst) {
|
||||
match result {
|
||||
Ok(status) if status.success() => {
|
||||
*status_clone.lock().unwrap() = PortForwardStatus::Stopped;
|
||||
}
|
||||
Ok(status) => {
|
||||
let error_msg = format!("kubectl process exited with status: {}", status);
|
||||
*status_clone.lock().unwrap() = PortForwardStatus::Error(error_msg.clone());
|
||||
*error_clone.lock().unwrap() = Some(error_msg);
|
||||
}
|
||||
Err(e) => {
|
||||
let error_msg = format!("Failed to wait for kubectl process: {}", e);
|
||||
*status_clone.lock().unwrap() = PortForwardStatus::Error(error_msg.clone());
|
||||
*error_clone.lock().unwrap() = Some(error_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
self.child_wait_handle = Some(Arc::new(TokioMutex::new(ChildWaitHandle {
|
||||
join_handle,
|
||||
child_id,
|
||||
child: child_arc,
|
||||
})));
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
self.is_stopped.store(true, Ordering::SeqCst);
|
||||
self.status = PortForwardStatus::Stopped;
|
||||
|
||||
if let Some(child_mutex) = &self.kubectl_child {
|
||||
let mut child = child_mutex.lock().unwrap();
|
||||
// Kill the child process - kill() returns a Future
|
||||
// We use std::mem::drop to ignore the Future result since we can't await here
|
||||
std::mem::drop(child.kill());
|
||||
// Drop the child wait handle - this cancels the background task
|
||||
// and the Child will be dropped, which kills it
|
||||
self.child_wait_handle = None;
|
||||
}
|
||||
|
||||
pub async fn stop_async(&mut self) {
|
||||
self.is_stopped.store(true, Ordering::SeqCst);
|
||||
self.status = PortForwardStatus::Stopped;
|
||||
|
||||
// Kill the child process if it exists
|
||||
if let Some(ref child_wait_handle) = self.child_wait_handle {
|
||||
let guard = child_wait_handle.lock().await;
|
||||
let child_opt = guard.child.lock().await.take();
|
||||
if let Some(mut child) = child_opt {
|
||||
let _ = child.kill().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,11 +157,14 @@ impl Drop for PortForwardSession {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(child_mutex) = &self.kubectl_child {
|
||||
let mut child = child_mutex.lock().unwrap();
|
||||
// Kill the child process - kill() returns a Future
|
||||
// We use std::mem::drop to ignore the Future result since we can't await here
|
||||
std::mem::drop(child.kill());
|
||||
// Kill the child process if it exists
|
||||
// Note: This is called from sync context, so we can't await
|
||||
// The Child will be dropped and cleaned up by the background task
|
||||
self.child_wait_handle = None;
|
||||
|
||||
// Clean up temp kubeconfig file if it exists
|
||||
if let Some(path) = &self.temp_kubeconfig_path {
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -105,6 +184,7 @@ mod tests {
|
||||
container: None,
|
||||
ports: vec![8080],
|
||||
local_ports: vec![0],
|
||||
temp_kubeconfig_path: None,
|
||||
};
|
||||
|
||||
let session = PortForwardSession::new(config);
|
||||
@ -130,6 +210,7 @@ mod tests {
|
||||
container: None,
|
||||
ports: vec![9000],
|
||||
local_ports: vec![0],
|
||||
temp_kubeconfig_path: None,
|
||||
};
|
||||
|
||||
let mut session = PortForwardSession::new(config);
|
||||
@ -150,6 +231,7 @@ mod tests {
|
||||
container: None,
|
||||
ports: vec![9000],
|
||||
local_ports: vec![0],
|
||||
temp_kubeconfig_path: None,
|
||||
};
|
||||
|
||||
let mut session = PortForwardSession::new(config);
|
||||
@ -175,6 +257,7 @@ mod tests {
|
||||
container: None,
|
||||
ports: vec![9000],
|
||||
local_ports: vec![0],
|
||||
temp_kubeconfig_path: None,
|
||||
};
|
||||
|
||||
let session = PortForwardSession::new(config);
|
||||
@ -191,9 +274,10 @@ mod tests {
|
||||
ports: vec![9000],
|
||||
local_ports: vec![0],
|
||||
status: PortForwardStatus::Stopped,
|
||||
kubectl_child: None,
|
||||
child_wait_handle: None,
|
||||
is_stopped: Arc::new(AtomicBool::new(false)),
|
||||
error_message: None,
|
||||
temp_kubeconfig_path: None,
|
||||
};
|
||||
assert!(!stopped_session.is_active());
|
||||
|
||||
@ -208,9 +292,10 @@ mod tests {
|
||||
ports: vec![9000],
|
||||
local_ports: vec![0],
|
||||
status: PortForwardStatus::Error("error".to_string()),
|
||||
kubectl_child: None,
|
||||
child_wait_handle: None,
|
||||
is_stopped: Arc::new(AtomicBool::new(false)),
|
||||
error_message: Some("error".to_string()),
|
||||
temp_kubeconfig_path: None,
|
||||
};
|
||||
assert!(!error_session.is_active());
|
||||
}
|
||||
|
||||
@ -1,364 +0,0 @@
|
||||
// Cluster management integration tests
|
||||
// Tests: add cluster, list clusters, remove cluster
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
fn setup_test_state() -> trcaa_lib::state::AppState {
|
||||
let conn = rusqlite::Connection::open_in_memory().expect("Failed to create in-memory DB");
|
||||
|
||||
trcaa_lib::state::AppState {
|
||||
db: Arc::new(Mutex::new(conn)),
|
||||
settings: Arc::new(Mutex::new(trcaa_lib::state::AppSettings::default())),
|
||||
app_data_dir: std::path::PathBuf::from("./test-data"),
|
||||
integration_webviews: Arc::new(Mutex::new(HashMap::new())),
|
||||
mcp_connections: Arc::new(Mutex::new(HashMap::new())),
|
||||
pending_approvals: Arc::new(Mutex::new(HashMap::new())),
|
||||
clusters: Arc::new(Mutex::new(HashMap::new())),
|
||||
port_forwards: Arc::new(Mutex::new(HashMap::new())),
|
||||
refresh_registry: Arc::new(Mutex::new(trcaa_lib::kube::RefreshRegistry::new())),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_success() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
namespace: default
|
||||
name: production-context
|
||||
current-context: production-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production Cluster".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cluster_info = result.unwrap();
|
||||
assert_eq!(cluster_info.id, "cluster-1");
|
||||
assert_eq!(cluster_info.name, "Production Cluster");
|
||||
assert_eq!(cluster_info.context, "production-context");
|
||||
assert_eq!(cluster_info.cluster_url, "https://k8s.example.com:6443");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_empty_content() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Empty Cluster".to_string(),
|
||||
"".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Kubeconfig content cannot be empty"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_missing_contexts() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"No Contexts".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Missing 'contexts' field"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_no_contexts() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts: []
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Empty Contexts".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("No contexts found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_missing_clusters() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: production-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"No Clusters".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Missing 'clusters' field"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_invalid_yaml() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
invalid yaml here: [
|
||||
missing closing bracket
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Invalid YAML".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Invalid kubeconfig YAML"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_clusters_empty() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let clusters = result.unwrap();
|
||||
assert!(clusters.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_clusters_multiple() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add first cluster
|
||||
let kubeconfig1 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: user1
|
||||
name: context1
|
||||
users:
|
||||
- name: user1
|
||||
user:
|
||||
token: token1
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Cluster 1".to_string(),
|
||||
kubeconfig1.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Add second cluster
|
||||
let kubeconfig2 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster2
|
||||
user: user2
|
||||
name: context2
|
||||
users:
|
||||
- name: user2
|
||||
user:
|
||||
token: token2
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-2".to_string(),
|
||||
"Cluster 2".to_string(),
|
||||
kubeconfig2.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// List clusters
|
||||
let result = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let clusters = result.unwrap();
|
||||
assert_eq!(clusters.len(), 2);
|
||||
|
||||
let cluster_names: Vec<&str> = clusters.iter().map(|c| c.name.as_str()).collect();
|
||||
assert!(cluster_names.contains(&"Cluster 1"));
|
||||
assert!(cluster_names.contains(&"Cluster 2"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_cluster_success() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify cluster exists
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(clusters.len(), 1);
|
||||
|
||||
// Remove cluster
|
||||
let result = trcaa_lib::commands::kube::remove_cluster(
|
||||
"cluster-1".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Verify cluster is gone
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert!(clusters.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_cluster_not_found() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::remove_cluster(
|
||||
"non-existent".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Cluster non-existent not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_cluster_with_no_server_url() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
# No server URL
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"No Server".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Server URL not found"));
|
||||
}
|
||||
@ -1,470 +0,0 @@
|
||||
// Error scenarios integration tests
|
||||
// Tests: invalid kubeconfig, cluster not found, port conflicts, edge cases
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
fn setup_test_state() -> trcaa_lib::state::AppState {
|
||||
let conn = rusqlite::Connection::open_in_memory().expect("Failed to create in-memory DB");
|
||||
|
||||
trcaa_lib::state::AppState {
|
||||
db: Arc::new(Mutex::new(conn)),
|
||||
settings: Arc::new(Mutex::new(trcaa_lib::state::AppSettings::default())),
|
||||
app_data_dir: std::path::PathBuf::from("./test-data"),
|
||||
integration_webviews: Arc::new(Mutex::new(HashMap::new())),
|
||||
mcp_connections: Arc::new(Mutex::new(HashMap::new())),
|
||||
pending_approvals: Arc::new(Mutex::new(HashMap::new())),
|
||||
clusters: Arc::new(Mutex::new(HashMap::new())),
|
||||
port_forwards: Arc::new(Mutex::new(HashMap::new())),
|
||||
refresh_registry: Arc::new(Mutex::new(trcaa_lib::kube::RefreshRegistry::new())),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_invalid_yaml_syntax() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let invalid_yaml = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com
|
||||
invalid: [unclosed array
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Invalid YAML".to_string(),
|
||||
invalid_yaml.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
let err = result.unwrap_err();
|
||||
assert!(err.contains("Invalid kubeconfig YAML") || err.contains("YAML"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_empty_kubeconfig() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Empty".to_string(),
|
||||
"".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("cannot be empty"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_whitespace_only_kubeconfig() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Whitespace".to_string(),
|
||||
" \n\t \n ".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("cannot be empty"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_with_null_values() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: null
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Null Server".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Server URL not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_to_nonexistent_cluster() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "non-existent-cluster".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_stop_nonexistent_port_forward() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::stop_port_forward(
|
||||
"non-existent-session".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_delete_nonexistent_port_forward() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::delete_port_forward(
|
||||
"non-existent-session".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_nonexistent_cluster() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::remove_cluster(
|
||||
"non-existent-cluster".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_with_empty_clusters_array() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters: []
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Empty Clusters".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("No clusters found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_with_empty_contexts_array() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts: []
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Empty Contexts".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("No contexts found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_missing_api_version() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"No API Version".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
// Should still work - we only check for required fields
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_with_extra_fields() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
metadata:
|
||||
name: my-config
|
||||
annotations:
|
||||
created-by: test
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"With Metadata".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_with_multiple_clusters() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Use first cluster's server URL
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: admin
|
||||
name: context1
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Multiple Clusters".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cluster_info = result.unwrap();
|
||||
assert_eq!(cluster_info.cluster_url, "https://k8s1.example.com:6443");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kubeconfig_with_multiple_contexts() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
namespace: default
|
||||
name: default-context
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
namespace: kube-system
|
||||
name: kube-system-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
let result = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Multiple Contexts".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cluster_info = result.unwrap();
|
||||
// Should use first context
|
||||
assert_eq!(cluster_info.context, "default-context");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_with_empty_namespace() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster first
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Try port forward with empty namespace
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
// Note: Current implementation doesn't validate namespace/pod
|
||||
// This may need validation added
|
||||
let result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok()); // Current behavior allows empty namespace
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_with_empty_pod() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster first
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Try port forward with empty pod
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
// Note: Current implementation doesn't validate pod name
|
||||
let result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok()); // Current behavior allows empty pod
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
// Integration tests for Kubernetes management feature
|
||||
// Tests end-to-end cluster management, port forwarding, and error scenarios
|
||||
|
||||
mod cluster_management;
|
||||
mod port_forwarding;
|
||||
mod multi_cluster;
|
||||
mod error_scenarios;
|
||||
mod session_recovery;
|
||||
@ -1,391 +0,0 @@
|
||||
// Multi-cluster management integration tests
|
||||
// Tests: multiple cluster operations, cluster isolation, cross-cluster port forwarding
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
fn setup_test_state() -> trcaa_lib::state::AppState {
|
||||
let conn = rusqlite::Connection::open_in_memory().expect("Failed to create in-memory DB");
|
||||
|
||||
trcaa_lib::state::AppState {
|
||||
db: Arc::new(Mutex::new(conn)),
|
||||
settings: Arc::new(Mutex::new(trcaa_lib::state::AppSettings::default())),
|
||||
app_data_dir: std::path::PathBuf::from("./test-data"),
|
||||
integration_webviews: Arc::new(Mutex::new(HashMap::new())),
|
||||
mcp_connections: Arc::new(Mutex::new(HashMap::new())),
|
||||
pending_approvals: Arc::new(Mutex::new(HashMap::new())),
|
||||
clusters: Arc::new(Mutex::new(HashMap::new())),
|
||||
port_forwards: Arc::new(Mutex::new(HashMap::new())),
|
||||
refresh_registry: Arc::new(Mutex::new(trcaa_lib::kube::RefreshRegistry::new())),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_multiple_clusters_with_same_name() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig1 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: admin
|
||||
name: context1
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token1
|
||||
"#;
|
||||
|
||||
let kubeconfig2 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster2
|
||||
user: admin
|
||||
name: context2
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token2
|
||||
"#;
|
||||
|
||||
// Add first cluster
|
||||
let result1 = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Same Name".to_string(),
|
||||
kubeconfig1.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
assert!(result1.is_ok());
|
||||
|
||||
// Add second cluster with same display name but different ID
|
||||
let result2 = trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-2".to_string(),
|
||||
"Same Name".to_string(),
|
||||
kubeconfig2.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
assert!(result2.is_ok());
|
||||
|
||||
// Verify both clusters exist
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(clusters.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_cluster_isolation() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add first cluster
|
||||
let kubeconfig1 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: admin
|
||||
name: context1
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token1
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Cluster 1".to_string(),
|
||||
kubeconfig1.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Add second cluster
|
||||
let kubeconfig2 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster2
|
||||
user: admin
|
||||
name: context2
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token2
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-2".to_string(),
|
||||
"Cluster 2".to_string(),
|
||||
kubeconfig2.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// List clusters - verify they're isolated
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
let cluster_ids: Vec<&str> = clusters.iter().map(|c| c.id.as_str()).collect();
|
||||
assert!(cluster_ids.contains(&"cluster-1"));
|
||||
assert!(cluster_ids.contains(&"cluster-2"));
|
||||
|
||||
let cluster_names: Vec<&str> = clusters.iter().map(|c| c.name.as_str()).collect();
|
||||
assert!(cluster_names.contains(&"Cluster 1"));
|
||||
assert!(cluster_names.contains(&"Cluster 2"));
|
||||
|
||||
let cluster_urls: Vec<&str> = clusters.iter().map(|c| c.cluster_url.as_str()).collect();
|
||||
assert!(cluster_urls.contains(&"https://k8s1.example.com:6443"));
|
||||
assert!(cluster_urls.contains(&"https://k8s2.example.com:6443"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_to_specific_cluster() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add first cluster
|
||||
let kubeconfig1 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: admin
|
||||
name: context1
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token1
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Cluster 1".to_string(),
|
||||
kubeconfig1.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Add second cluster
|
||||
let kubeconfig2 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster2
|
||||
user: admin
|
||||
name: context2
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token2
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-2".to_string(),
|
||||
"Cluster 2".to_string(),
|
||||
kubeconfig2.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward to first cluster
|
||||
let request1 = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "pod-1".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let result1 = trcaa_lib::commands::kube::start_port_forward(
|
||||
request1,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward to second cluster
|
||||
let request2 = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-2".to_string(),
|
||||
namespace: "kube-system".to_string(),
|
||||
pod: "pod-2".to_string(),
|
||||
container_port: 443,
|
||||
};
|
||||
|
||||
let result2 = trcaa_lib::commands::kube::start_port_forward(
|
||||
request2,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// List port forwards - verify both are present
|
||||
let forwards = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(forwards.len(), 2);
|
||||
|
||||
// Verify cluster isolation in port forwards
|
||||
let cluster_ids: Vec<&str> = forwards.iter().map(|f| f.cluster_id.as_str()).collect();
|
||||
assert!(cluster_ids.contains(&"cluster-1"));
|
||||
assert!(cluster_ids.contains(&"cluster-2"));
|
||||
|
||||
// Verify container_ports and local_ports are arrays
|
||||
for f in &forwards {
|
||||
assert!(!f.container_ports.is_empty());
|
||||
assert!(!f.local_ports.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_cluster_cascades_to_port_forwards() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify port forward exists
|
||||
let forwards = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(forwards.len(), 1);
|
||||
|
||||
// Remove cluster
|
||||
trcaa_lib::commands::kube::remove_cluster(
|
||||
"cluster-1".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Note: Current implementation doesn't cascade delete port forwards
|
||||
// This test documents the current behavior - port forwards persist after cluster removal
|
||||
// This may be intentional for debugging or may need to be fixed
|
||||
|
||||
let forwards_after = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(forwards_after.len(), 1); // Port forward still exists
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_clusters_with_different_contexts() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig1 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: admin
|
||||
namespace: production
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token1
|
||||
"#;
|
||||
|
||||
let kubeconfig2 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster2
|
||||
user: admin
|
||||
namespace: staging
|
||||
name: staging-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token2
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig1.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-2".to_string(),
|
||||
"Staging".to_string(),
|
||||
kubeconfig2.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
assert_eq!(clusters.len(), 2);
|
||||
assert_eq!(clusters[0].context, "prod-context");
|
||||
assert_eq!(clusters[1].context, "staging-context");
|
||||
}
|
||||
@ -1,408 +0,0 @@
|
||||
// Port forwarding integration tests
|
||||
// Tests: start port forward, list port forwards, stop port forward, delete port forward
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
fn setup_test_state() -> trcaa_lib::state::AppState {
|
||||
let conn = rusqlite::Connection::open_in_memory().expect("Failed to create in-memory DB");
|
||||
|
||||
trcaa_lib::state::AppState {
|
||||
db: Arc::new(Mutex::new(conn)),
|
||||
settings: Arc::new(Mutex::new(trcaa_lib::state::AppSettings::default())),
|
||||
app_data_dir: std::path::PathBuf::from("./test-data"),
|
||||
integration_webviews: Arc::new(Mutex::new(HashMap::new())),
|
||||
mcp_connections: Arc::new(Mutex::new(HashMap::new())),
|
||||
pending_approvals: Arc::new(Mutex::new(HashMap::new())),
|
||||
clusters: Arc::new(Mutex::new(HashMap::new())),
|
||||
port_forwards: Arc::new(Mutex::new(HashMap::new())),
|
||||
refresh_registry: Arc::new(Mutex::new(trcaa_lib::kube::RefreshRegistry::new())),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_start_port_forward_success() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster first
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod-abc123".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let response = result.unwrap();
|
||||
assert!(response.id.len() > 0);
|
||||
assert_eq!(response.cluster_id, "cluster-1");
|
||||
assert_eq!(response.namespace, "default");
|
||||
assert_eq!(response.pod, "nginx-pod-abc123");
|
||||
assert_eq!(response.container_ports, vec![80]);
|
||||
assert_eq!(response.status, "Active");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_start_port_forward_cluster_not_found() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "non-existent".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Cluster non-existent not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_port_forwards_empty() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let forwards = result.unwrap();
|
||||
assert!(forwards.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_port_forwards_multiple() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start first port forward
|
||||
let request1 = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "pod-1".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
trcaa_lib::commands::kube::start_port_forward(
|
||||
request1,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start second port forward
|
||||
let request2 = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "kube-system".to_string(),
|
||||
pod: "pod-2".to_string(),
|
||||
container_port: 443,
|
||||
};
|
||||
|
||||
trcaa_lib::commands::kube::start_port_forward(
|
||||
request2,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// List port forwards
|
||||
let result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
let forwards = result.unwrap();
|
||||
assert_eq!(forwards.len(), 2);
|
||||
|
||||
let pods: Vec<&str> = forwards.iter().map(|f| f.pod.as_str()).collect();
|
||||
assert!(pods.contains(&"pod-1"));
|
||||
assert!(pods.contains(&"pod-2"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_stop_port_forward_success() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let start_result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify it's active
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(list_result[0].status, "Active");
|
||||
|
||||
// Stop port forward
|
||||
let result = trcaa_lib::commands::kube::stop_port_forward(
|
||||
start_result.id.clone(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Verify it's stopped
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(list_result[0].status, "Stopped");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_stop_port_forward_not_found() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::stop_port_forward(
|
||||
"non-existent".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Port forward session non-existent not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_delete_port_forward_success() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let start_result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify port forward exists
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(list_result.len(), 1);
|
||||
|
||||
// Delete port forward
|
||||
let result = trcaa_lib::commands::kube::delete_port_forward(
|
||||
start_result.id.clone(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Verify port forward is gone
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert!(list_result.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_delete_port_forward_not_found() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let result = trcaa_lib::commands::kube::delete_port_forward(
|
||||
"non-existent".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Port forward session non-existent not found"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_session_lifecycle() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add a cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let start_result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify session is active
|
||||
let session_id = start_result.id.clone();
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(list_result[0].id, session_id);
|
||||
assert_eq!(list_result[0].status, "Active");
|
||||
|
||||
// Stop port forward
|
||||
trcaa_lib::commands::kube::stop_port_forward(
|
||||
session_id.clone(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify session is stopped
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(list_result[0].status, "Stopped");
|
||||
|
||||
// Delete port forward
|
||||
trcaa_lib::commands::kube::delete_port_forward(
|
||||
session_id.clone(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify session is deleted
|
||||
let list_result = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert!(list_result.is_empty());
|
||||
}
|
||||
@ -1,371 +0,0 @@
|
||||
// Session recovery integration tests
|
||||
// Tests: cluster and port forward persistence across restarts
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
fn setup_test_state() -> trcaa_lib::state::AppState {
|
||||
let conn = rusqlite::Connection::open_in_memory().expect("Failed to create in-memory DB");
|
||||
|
||||
trcaa_lib::state::AppState {
|
||||
db: Arc::new(Mutex::new(conn)),
|
||||
settings: Arc::new(Mutex::new(trcaa_lib::state::AppSettings::default())),
|
||||
app_data_dir: std::path::PathBuf::from("./test-data"),
|
||||
integration_webviews: Arc::new(Mutex::new(HashMap::new())),
|
||||
mcp_connections: Arc::new(Mutex::new(HashMap::new())),
|
||||
pending_approvals: Arc::new(Mutex::new(HashMap::new())),
|
||||
clusters: Arc::new(Mutex::new(HashMap::new())),
|
||||
port_forwards: Arc::new(Mutex::new(HashMap::new())),
|
||||
refresh_registry: Arc::new(Mutex::new(trcaa_lib::kube::RefreshRegistry::new())),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_clusters_persist_in_memory() {
|
||||
let state = setup_test_state();
|
||||
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
// Add cluster
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// List clusters - should find it
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(clusters.len(), 1);
|
||||
|
||||
// Note: In-memory state doesn't persist across restarts
|
||||
// This test documents the current in-memory behavior
|
||||
// For true persistence, database storage would be required
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forwards_persist_in_memory() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// List port forwards - should find it
|
||||
let forwards = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(forwards.len(), 1);
|
||||
|
||||
// Note: In-memory state doesn't persist across restarts
|
||||
// For true persistence, database storage would be required
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_multiple_clusters_and_port_forwards() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add multiple clusters
|
||||
let kubeconfig1 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s1.example.com:6443
|
||||
name: cluster1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster1
|
||||
user: admin
|
||||
name: context1
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token1
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Cluster 1".to_string(),
|
||||
kubeconfig1.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
let kubeconfig2 = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s2.example.com:6443
|
||||
name: cluster2
|
||||
contexts:
|
||||
- context:
|
||||
cluster: cluster2
|
||||
user: admin
|
||||
name: context2
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: token2
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-2".to_string(),
|
||||
"Cluster 2".to_string(),
|
||||
kubeconfig2.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start multiple port forwards
|
||||
let request1 = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "pod-1".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
trcaa_lib::commands::kube::start_port_forward(
|
||||
request1,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
let request2 = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-2".to_string(),
|
||||
namespace: "kube-system".to_string(),
|
||||
pod: "pod-2".to_string(),
|
||||
container_port: 443,
|
||||
};
|
||||
|
||||
trcaa_lib::commands::kube::start_port_forward(
|
||||
request2,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify all clusters exist
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(clusters.len(), 2);
|
||||
|
||||
// Verify all port forwards exist
|
||||
let forwards = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(forwards.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_cluster_removal_clears_cluster_data() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify cluster exists
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(clusters.len(), 1);
|
||||
|
||||
// Remove cluster
|
||||
trcaa_lib::commands::kube::remove_cluster(
|
||||
"cluster-1".to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify cluster is gone
|
||||
let clusters = trcaa_lib::commands::kube::list_clusters(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert!(clusters.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_stop_clears_session() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let start_result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Stop port forward
|
||||
trcaa_lib::commands::kube::stop_port_forward(
|
||||
start_result.id.clone(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify session is stopped (not deleted)
|
||||
let forwards = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert_eq!(forwards.len(), 1);
|
||||
assert_eq!(forwards[0].status, "Stopped");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_port_forward_delete_removes_session() {
|
||||
let state = setup_test_state();
|
||||
|
||||
// Add cluster
|
||||
let kubeconfig = r#"
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.example.com:6443
|
||||
name: production
|
||||
contexts:
|
||||
- context:
|
||||
cluster: production
|
||||
user: admin
|
||||
name: prod-context
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: test-token
|
||||
"#;
|
||||
|
||||
trcaa_lib::commands::kube::add_cluster(
|
||||
"cluster-1".to_string(),
|
||||
"Production".to_string(),
|
||||
kubeconfig.to_string(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Start port forward
|
||||
let request = trcaa_lib::commands::kube::PortForwardRequest {
|
||||
cluster_id: "cluster-1".to_string(),
|
||||
namespace: "default".to_string(),
|
||||
pod: "nginx-pod".to_string(),
|
||||
container_port: 80,
|
||||
};
|
||||
|
||||
let start_result = trcaa_lib::commands::kube::start_port_forward(
|
||||
request,
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Delete port forward
|
||||
trcaa_lib::commands::kube::delete_port_forward(
|
||||
start_result.id.clone(),
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
|
||||
// Verify session is deleted
|
||||
let forwards = trcaa_lib::commands::kube::list_port_forwards(
|
||||
trcaa_lib::State::new(&state),
|
||||
).await.unwrap();
|
||||
assert!(forwards.is_empty());
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user