|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/grycap/oscar-cli/pkg/cluster" |
| 12 | +) |
| 13 | + |
| 14 | +func TestClusterStatusCommandPrintsStatus(t *testing.T) { |
| 15 | + expected := cluster.StatusInfo{ |
| 16 | + Cluster: cluster.ClusterStatus{ |
| 17 | + NodesCount: 1, |
| 18 | + Metrics: cluster.ClusterMetrics{ |
| 19 | + CPU: cluster.CPUMetrics{ |
| 20 | + TotalFreeCores: 2, |
| 21 | + MaxFreeOnNodeCores: 2, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + Oscar: cluster.OscarStatus{ |
| 26 | + DeploymentName: "oscar-manager", |
| 27 | + Deployment: cluster.OscarDeployment{ |
| 28 | + CreationTimestamp: time.Unix(1700000000, 0).UTC(), |
| 29 | + }, |
| 30 | + }, |
| 31 | + MinIO: cluster.MinioStatus{ |
| 32 | + BucketsCount: 4, |
| 33 | + TotalObjects: 10, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + const ( |
| 38 | + username = "user" |
| 39 | + password = "pass" |
| 40 | + ) |
| 41 | + |
| 42 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 43 | + if r.URL.Path != "/system/status" { |
| 44 | + http.NotFound(w, r) |
| 45 | + return |
| 46 | + } |
| 47 | + gotUser, gotPass, ok := r.BasicAuth() |
| 48 | + if !ok || gotUser != username || gotPass != password { |
| 49 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 50 | + return |
| 51 | + } |
| 52 | + if err := json.NewEncoder(w).Encode(expected); err != nil { |
| 53 | + t.Fatalf("encoding status: %v", err) |
| 54 | + } |
| 55 | + })) |
| 56 | + defer server.Close() |
| 57 | + |
| 58 | + configFile := writeConfigFile(t, "alpha", server.URL) |
| 59 | + |
| 60 | + stdout, stderr, err := runCommand(t, |
| 61 | + "cluster", "--config", configFile, |
| 62 | + "status", |
| 63 | + "--cluster", "alpha", |
| 64 | + ) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("cluster status command returned error: %v", err) |
| 67 | + } |
| 68 | + if stderr != "" { |
| 69 | + t.Fatalf("expected empty stderr, got %q", stderr) |
| 70 | + } |
| 71 | + if !strings.Contains(stdout, "nodes_count: 1") { |
| 72 | + t.Fatalf("expected nodes_count in output, got %q", stdout) |
| 73 | + } |
| 74 | + if !strings.Contains(stdout, "deployment_name: oscar-manager") { |
| 75 | + t.Fatalf("expected deployment_name in output, got %q", stdout) |
| 76 | + } |
| 77 | + if !strings.Contains(stdout, "buckets_count: 4") { |
| 78 | + t.Fatalf("expected buckets_count in output, got %q", stdout) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestClusterStatusCommandError(t *testing.T) { |
| 83 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 84 | + http.Error(w, "boom", http.StatusInternalServerError) |
| 85 | + })) |
| 86 | + defer server.Close() |
| 87 | + |
| 88 | + configFile := writeConfigFile(t, "alpha", server.URL) |
| 89 | + |
| 90 | + _, _, err := runCommand(t, |
| 91 | + "cluster", "--config", configFile, |
| 92 | + "status", |
| 93 | + "--cluster", "alpha", |
| 94 | + ) |
| 95 | + if err == nil { |
| 96 | + t.Fatalf("expected error, got nil") |
| 97 | + } |
| 98 | + if err.Error() != "boom\n" { |
| 99 | + t.Fatalf("expected boom error, got %v", err) |
| 100 | + } |
| 101 | +} |
0 commit comments