|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
4 | 7 | "fmt" |
5 | 8 | "io" |
| 9 | + "log" |
6 | 10 | "net/http" |
7 | 11 | "os" |
| 12 | + "os/exec" |
| 13 | + "strconv" |
8 | 14 | "strings" |
9 | 15 | "time" |
10 | 16 | ) |
11 | 17 |
|
12 | 18 | func main() { |
13 | | - metricsURL := getenvDefault("SCHEDULER_METRICS_URL", "http://localhost:8084") |
| 19 | + cfg := loadConfig() |
14 | 20 | client := &http.Client{Timeout: 10 * time.Second} |
15 | 21 |
|
16 | | - if err := waitFor(metricsURL+"/health", client, 30, 2*time.Second); err != nil { |
17 | | - panic(fmt.Sprintf("health check failed: %v", err)) |
18 | | - } |
19 | | - if err := waitFor(metricsURL+"/metrics", client, 30, 2*time.Second); err != nil { |
20 | | - panic(fmt.Sprintf("metrics endpoint failed: %v", err)) |
| 22 | + must(waitForHTTP(cfg.schedulerMetricsURL+"/health", client, cfg.maxRetries, cfg.retryInterval)) |
| 23 | + must(waitForHTTP(cfg.schedulerMetricsURL+"/metrics", client, cfg.maxRetries, cfg.retryInterval)) |
| 24 | + must(waitForHTTP(cfg.agentMetricsURL+"/health", client, cfg.maxRetries, cfg.retryInterval)) |
| 25 | + |
| 26 | + logStep("apply workload") |
| 27 | + must(applyWorkloadWithRetry(cfg)) |
| 28 | + |
| 29 | + var workload getWorkloadResponse |
| 30 | + must(poll(cfg.maxRetries, cfg.retryInterval, func() (bool, error) { |
| 31 | + out, err := runSmokeCapture(cfg.schedulerGRPCAddr, "-op", "get-workload", "-workload-id", cfg.testWorkloadID) |
| 32 | + if err != nil { |
| 33 | + return false, nil |
| 34 | + } |
| 35 | + if err := unmarshalSmokeJSON(out, &workload); err != nil { |
| 36 | + return false, nil |
| 37 | + } |
| 38 | + s := strings.ToLower(strings.TrimSpace(workload.Workload.Status)) |
| 39 | + return s == "running" || s == "pending" || s == "unknown", nil |
| 40 | + })) |
| 41 | + |
| 42 | + listOut, err := runSmokeCapture(cfg.schedulerGRPCAddr, "-op", "list-workloads") |
| 43 | + must(err) |
| 44 | + var listResp listWorkloadsResponse |
| 45 | + must(unmarshalSmokeJSON(listOut, &listResp)) |
| 46 | + if !containsWorkload(listResp.Workloads, cfg.testWorkloadID) { |
| 47 | + failf("workload %s missing from list-workloads", cfg.testWorkloadID) |
21 | 48 | } |
22 | 49 |
|
23 | | - body, err := getBody(metricsURL+"/metrics", client) |
| 50 | + summaryOut, err := runSmokeCapture(cfg.schedulerGRPCAddr, "-op", "cluster-summary") |
24 | 51 | if err != nil { |
25 | | - panic(fmt.Sprintf("metrics fetch failed: %v", err)) |
| 52 | + failf("cluster-summary failed: %v", err) |
| 53 | + } |
| 54 | + var summary clusterSummaryResponse |
| 55 | + must(unmarshalSmokeJSON(summaryOut, &summary)) |
| 56 | + if summary.TotalWorkloads < 1 { |
| 57 | + failf("cluster summary reported total_workloads=%d, want >=1", summary.TotalWorkloads) |
26 | 58 | } |
27 | 59 |
|
| 60 | + logStep("delete workload") |
| 61 | + must(runSmoke(cfg.schedulerGRPCAddr, "-op", "delete-workload", "-workload-id", cfg.testWorkloadID)) |
| 62 | + must(poll(cfg.maxRetries, cfg.retryInterval, func() (bool, error) { |
| 63 | + out, err := runSmokeCapture(cfg.schedulerGRPCAddr, "-op", "get-workload", "-workload-id", cfg.testWorkloadID) |
| 64 | + if err != nil { |
| 65 | + return true, nil // NotFound is acceptable terminal state. |
| 66 | + } |
| 67 | + var r getWorkloadResponse |
| 68 | + if err := unmarshalSmokeJSON(out, &r); err != nil { |
| 69 | + return false, nil |
| 70 | + } |
| 71 | + s := strings.ToLower(strings.TrimSpace(r.Workload.Status)) |
| 72 | + return s == "deleting" || s == "deleted", nil |
| 73 | + })) |
| 74 | + |
| 75 | + body, err := getBody(cfg.schedulerMetricsURL+"/metrics", client) |
| 76 | + must(err) |
28 | 77 | required := []string{ |
29 | | - "go_gc_duration_seconds", |
30 | | - "process_cpu_seconds_total", |
| 78 | + "persys_scheduler_grpc_server_requests_total", |
| 79 | + "persys_scheduler_grpc_server_request_duration_seconds", |
| 80 | + "persys_scheduler_agent_rpc_requests_total", |
| 81 | + "persys_scheduler_agent_rpc_duration_seconds", |
| 82 | + "persys_scheduler_nodes_status", |
| 83 | + "persys_scheduler_workloads_status", |
| 84 | + "persys_scheduler_reconciliation_results_total", |
31 | 85 | } |
32 | 86 | for _, metric := range required { |
33 | 87 | if !strings.Contains(body, metric) { |
34 | | - panic(fmt.Sprintf("expected metric %q not found", metric)) |
| 88 | + failf("expected metric %q not found", metric) |
35 | 89 | } |
36 | 90 | } |
37 | 91 |
|
38 | | - fmt.Println("✅ Go-based E2E probe passed") |
| 92 | + fmt.Println("E2E scheduler + compute-agent suite passed") |
| 93 | +} |
| 94 | + |
| 95 | +type config struct { |
| 96 | + schedulerMetricsURL string |
| 97 | + schedulerGRPCAddr string |
| 98 | + agentMetricsURL string |
| 99 | + testWorkloadID string |
| 100 | + retryInterval time.Duration |
| 101 | + maxRetries int |
| 102 | +} |
| 103 | + |
| 104 | +type getWorkloadResponse struct { |
| 105 | + Workload workloadView `json:"workload"` |
| 106 | +} |
| 107 | + |
| 108 | +type listWorkloadsResponse struct { |
| 109 | + Workloads []workloadView `json:"workloads"` |
| 110 | +} |
| 111 | + |
| 112 | +type workloadView struct { |
| 113 | + WorkloadID string `json:"workloadId"` |
| 114 | + Status string `json:"status"` |
| 115 | +} |
| 116 | + |
| 117 | +type clusterSummaryResponse struct { |
| 118 | + TotalWorkloads int32 `json:"totalWorkloads"` |
| 119 | +} |
| 120 | + |
| 121 | +func loadConfig() config { |
| 122 | + return config{ |
| 123 | + schedulerMetricsURL: getenvDefault("SCHEDULER_METRICS_URL", "http://localhost:8084"), |
| 124 | + schedulerGRPCAddr: getenvDefault("SCHEDULER_GRPC_ADDR", "localhost:8085"), |
| 125 | + agentMetricsURL: getenvDefault("AGENT_METRICS_URL", "http://compute-agent:8080"), |
| 126 | + testWorkloadID: getenvDefault("TEST_WORKLOAD_ID", "e2e-workload-1"), |
| 127 | + retryInterval: getenvDurationDefault("RETRY_INTERVAL", 2*time.Second), |
| 128 | + maxRetries: getenvIntDefault("MAX_RETRIES", 40), |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func applyWorkloadWithRetry(cfg config) error { |
| 133 | + return poll(cfg.maxRetries, cfg.retryInterval, func() (bool, error) { |
| 134 | + err := runSmoke(cfg.schedulerGRPCAddr, |
| 135 | + "-op", "apply-container", |
| 136 | + "-workload-id", cfg.testWorkloadID, |
| 137 | + "-container-image", "busybox:latest", |
| 138 | + "-container-cmd", "sh,-c,sleep 60", |
| 139 | + "-w-cpu", "100", |
| 140 | + "-w-mem", "128", |
| 141 | + "-w-disk", "1", |
| 142 | + ) |
| 143 | + if err != nil { |
| 144 | + msg := strings.ToLower(err.Error()) |
| 145 | + if strings.Contains(msg, "no suitable node") || strings.Contains(msg, "cannot place workload") { |
| 146 | + return false, nil |
| 147 | + } |
| 148 | + return false, err |
| 149 | + } |
| 150 | + return true, nil |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +func runSmoke(schedulerAddr string, args ...string) error { |
| 155 | + _, err := runSmokeCapture(schedulerAddr, args...) |
| 156 | + return err |
39 | 157 | } |
40 | 158 |
|
41 | | -func waitFor(url string, client *http.Client, retries int, interval time.Duration) error { |
| 159 | +func runSmokeCapture(schedulerAddr string, args ...string) (string, error) { |
| 160 | + cmdArgs := append([]string{"-scheduler", schedulerAddr}, args...) |
| 161 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 162 | + defer cancel() |
| 163 | + |
| 164 | + cmd := exec.CommandContext(ctx, "./smoke-client", cmdArgs...) |
| 165 | + var stdout, stderr bytes.Buffer |
| 166 | + cmd.Stdout = &stdout |
| 167 | + cmd.Stderr = &stderr |
| 168 | + err := cmd.Run() |
| 169 | + out := strings.TrimSpace(stdout.String() + "\n" + stderr.String()) |
| 170 | + if err != nil { |
| 171 | + return out, fmt.Errorf("smoke-client %v failed: %w\n%s", args, err, out) |
| 172 | + } |
| 173 | + return out, nil |
| 174 | +} |
| 175 | + |
| 176 | +func waitForHTTP(url string, client *http.Client, retries int, interval time.Duration) error { |
42 | 177 | for i := 0; i < retries; i++ { |
43 | 178 | resp, err := client.Get(url) |
44 | 179 | if err == nil && resp.StatusCode == http.StatusOK { |
@@ -69,9 +204,89 @@ func getBody(url string, client *http.Client) (string, error) { |
69 | 204 | return string(b), nil |
70 | 205 | } |
71 | 206 |
|
| 207 | +func poll(retries int, interval time.Duration, f func() (bool, error)) error { |
| 208 | + for i := 0; i < retries; i++ { |
| 209 | + ok, err := f() |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + if ok { |
| 214 | + return nil |
| 215 | + } |
| 216 | + time.Sleep(interval) |
| 217 | + } |
| 218 | + return fmt.Errorf("timeout while polling condition") |
| 219 | +} |
| 220 | + |
| 221 | +func unmarshalSmokeJSON(out string, target interface{}) error { |
| 222 | + raw, err := extractJSONObject(out) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + return json.Unmarshal([]byte(raw), target) |
| 227 | +} |
| 228 | + |
| 229 | +func extractJSONObject(s string) (string, error) { |
| 230 | + start := strings.Index(s, "{") |
| 231 | + end := strings.LastIndex(s, "}") |
| 232 | + if start == -1 || end == -1 || end <= start { |
| 233 | + return "", fmt.Errorf("no JSON object found in output: %s", s) |
| 234 | + } |
| 235 | + return s[start : end+1], nil |
| 236 | +} |
| 237 | + |
| 238 | +func containsWorkload(workloads []workloadView, workloadID string) bool { |
| 239 | + for _, w := range workloads { |
| 240 | + if strings.TrimSpace(w.WorkloadID) == workloadID { |
| 241 | + return true |
| 242 | + } |
| 243 | + } |
| 244 | + return false |
| 245 | +} |
| 246 | + |
| 247 | +func getenvIntDefault(key string, fallback int) int { |
| 248 | + v := strings.TrimSpace(os.Getenv(key)) |
| 249 | + if v == "" { |
| 250 | + return fallback |
| 251 | + } |
| 252 | + n, err := strconv.Atoi(v) |
| 253 | + if err != nil { |
| 254 | + return fallback |
| 255 | + } |
| 256 | + return n |
| 257 | +} |
| 258 | + |
| 259 | +func getenvDurationDefault(key string, fallback time.Duration) time.Duration { |
| 260 | + v := strings.TrimSpace(os.Getenv(key)) |
| 261 | + if v == "" { |
| 262 | + return fallback |
| 263 | + } |
| 264 | + if d, err := time.ParseDuration(v); err == nil { |
| 265 | + return d |
| 266 | + } |
| 267 | + if s, err := strconv.Atoi(v); err == nil { |
| 268 | + return time.Duration(s) * time.Second |
| 269 | + } |
| 270 | + return fallback |
| 271 | +} |
| 272 | + |
72 | 273 | func getenvDefault(key, fallback string) string { |
73 | 274 | if v := strings.TrimSpace(os.Getenv(key)); v != "" { |
74 | 275 | return v |
75 | 276 | } |
76 | 277 | return fallback |
77 | 278 | } |
| 279 | + |
| 280 | +func logStep(msg string) { |
| 281 | + log.Printf("==> %s", msg) |
| 282 | +} |
| 283 | + |
| 284 | +func must(err error) { |
| 285 | + if err != nil { |
| 286 | + failf("%v", err) |
| 287 | + } |
| 288 | +} |
| 289 | + |
| 290 | +func failf(format string, args ...interface{}) { |
| 291 | + log.Fatalf(format, args...) |
| 292 | +} |
0 commit comments