- Add Kubernetes cluster management and operations - Include pod, service, and deployment management - Implement pattern-based resource deletion - Support namespace creation and management - Provide Rhai scripting wrappers for all functions - Include production safety features (timeouts, retries, rate limiting)
138 lines
4.2 KiB
Plaintext
138 lines
4.2 KiB
Plaintext
//! Resource management test
|
|
//!
|
|
//! This script tests resource listing and management operations.
|
|
|
|
print("=== Resource Management Test ===");
|
|
|
|
// Test 1: Create manager
|
|
print("Test 1: Creating KubernetesManager...");
|
|
let km = kubernetes_manager_new("default");
|
|
print("✓ Manager created for namespace: " + namespace(km));
|
|
|
|
// Test 2: Resource listing
|
|
print("\nTest 2: Testing resource listing...");
|
|
try {
|
|
// Test pods listing
|
|
let pods = pods_list(km);
|
|
print("✓ Pods list: " + pods.len() + " pods found");
|
|
|
|
// Test services listing
|
|
let services = services_list(km);
|
|
print("✓ Services list: " + services.len() + " services found");
|
|
|
|
// Test deployments listing
|
|
let deployments = deployments_list(km);
|
|
print("✓ Deployments list: " + deployments.len() + " deployments found");
|
|
|
|
// Show some pod names if available
|
|
if pods.len() > 0 {
|
|
print("Sample pods:");
|
|
let count = 0;
|
|
for pod in pods {
|
|
if count < 3 {
|
|
print(" - " + pod);
|
|
count = count + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch(e) {
|
|
print("Note: Resource listing failed (likely no cluster): " + e);
|
|
}
|
|
|
|
// Test 3: Resource counts
|
|
print("\nTest 3: Testing resource counts...");
|
|
try {
|
|
let counts = resource_counts(km);
|
|
print("✓ Resource counts retrieved for " + counts.len() + " resource types");
|
|
|
|
// Display counts
|
|
for resource_type in counts.keys() {
|
|
let count = counts[resource_type];
|
|
print(" " + resource_type + ": " + count);
|
|
}
|
|
|
|
// Verify expected resource types are present
|
|
let expected_types = ["pods", "services", "deployments", "configmaps", "secrets"];
|
|
for expected_type in expected_types {
|
|
if expected_type in counts {
|
|
print("✓ Found expected resource type: " + expected_type);
|
|
} else {
|
|
print("⚠ Missing expected resource type: " + expected_type);
|
|
}
|
|
}
|
|
|
|
} catch(e) {
|
|
print("Note: Resource counts failed (likely no cluster): " + e);
|
|
}
|
|
|
|
// Test 4: Multi-namespace comparison
|
|
print("\nTest 4: Multi-namespace resource comparison...");
|
|
let test_namespaces = ["default", "kube-system"];
|
|
let total_resources = #{};
|
|
|
|
for ns in test_namespaces {
|
|
try {
|
|
let ns_km = kubernetes_manager_new(ns);
|
|
let counts = resource_counts(ns_km);
|
|
|
|
print("Namespace '" + ns + "':");
|
|
let ns_total = 0;
|
|
for resource_type in counts.keys() {
|
|
let count = counts[resource_type];
|
|
print(" " + resource_type + ": " + count);
|
|
ns_total = ns_total + count;
|
|
|
|
// Accumulate totals
|
|
if resource_type in total_resources {
|
|
total_resources[resource_type] = total_resources[resource_type] + count;
|
|
} else {
|
|
total_resources[resource_type] = count;
|
|
}
|
|
}
|
|
print(" Total: " + ns_total + " resources");
|
|
|
|
} catch(e) {
|
|
print("Note: Failed to analyze namespace '" + ns + "': " + e);
|
|
}
|
|
}
|
|
|
|
// Show totals
|
|
print("\nTotal resources across all namespaces:");
|
|
let grand_total = 0;
|
|
for resource_type in total_resources.keys() {
|
|
let count = total_resources[resource_type];
|
|
print(" " + resource_type + ": " + count);
|
|
grand_total = grand_total + count;
|
|
}
|
|
print("Grand total: " + grand_total + " resources");
|
|
|
|
// Test 5: Pattern matching simulation
|
|
print("\nTest 5: Pattern matching simulation...");
|
|
try {
|
|
let pods = pods_list(km);
|
|
print("Testing pattern matching on " + pods.len() + " pods:");
|
|
|
|
// Simulate pattern matching (since Rhai doesn't have regex)
|
|
let test_patterns = ["test", "kube", "system", "app"];
|
|
for pattern in test_patterns {
|
|
let matches = [];
|
|
for pod in pods {
|
|
if pod.contains(pattern) {
|
|
matches.push(pod);
|
|
}
|
|
}
|
|
print(" Pattern '" + pattern + "' would match " + matches.len() + " pods");
|
|
if matches.len() > 0 && matches.len() <= 3 {
|
|
for match in matches {
|
|
print(" - " + match);
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch(e) {
|
|
print("Note: Pattern matching test failed (likely no cluster): " + e);
|
|
}
|
|
|
|
print("\n=== Resource management test completed! ===");
|