feat(kube): Implement complete kubectl port-forward runtime #72

Merged
sarman merged 18 commits from feature/kubernetes-management into master 2026-06-07 01:59:39 +00:00
6 changed files with 0 additions and 2012 deletions
Showing only changes of commit accb689117 - Show all commits

View File

@ -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"));
}

View File

@ -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
}

View File

@ -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;

View File

@ -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");
}

View File

@ -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());
}

View File

@ -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());
}