|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cluster |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "hpc-toolkit/pkg/orchestrator/gke" |
| 20 | + "hpc-toolkit/pkg/shell" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | +) |
| 26 | + |
| 27 | +func executeCommand(root *cobra.Command, args ...string) (string, error) { |
| 28 | + buf := new(bytes.Buffer) |
| 29 | + root.SetOut(buf) |
| 30 | + root.SetErr(buf) |
| 31 | + root.SetArgs(args) |
| 32 | + |
| 33 | + err := root.Execute() |
| 34 | + |
| 35 | + return buf.String(), err |
| 36 | +} |
| 37 | + |
| 38 | +func TestDescribeCmd_MissingFlags(t *testing.T) { |
| 39 | + resetClusterCmdFlags() |
| 40 | + |
| 41 | + _, err := executeCommand(ClusterCmd, "describe", "--project", "test-project") |
| 42 | + if err == nil { |
| 43 | + t.Fatalf("expected error for missing flags, got nil") |
| 44 | + } |
| 45 | + |
| 46 | + if !strings.Contains(err.Error(), `required flag(s) "cluster", "location" not set`) { |
| 47 | + t.Errorf("unexpected error output: %v", err) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestDescribeCmd_Success(t *testing.T) { |
| 52 | + resetClusterCmdFlags() |
| 53 | + |
| 54 | + oldFactory := gkeOrchestratorFactory |
| 55 | + defer func() { gkeOrchestratorFactory = oldFactory }() |
| 56 | + |
| 57 | + gkeOrchestratorFactory = func() *gke.GKEOrchestrator { |
| 58 | + g := gke.NewGKEOrchestrator() |
| 59 | + g.SetExecutor(&mockClusterExecutor{}) |
| 60 | + return g |
| 61 | + } |
| 62 | + |
| 63 | + output, err := executeCommand(ClusterCmd, "describe", "--cluster", "test-cluster", "--location", "us-central1-a", "--project", "test-project") |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("unexpected error: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + if !strings.Contains(output, "status: RUNNING") { |
| 69 | + t.Errorf("expected output to contain status: RUNNING, got %s", output) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func resetClusterCmdFlags() { |
| 74 | + clusterName = "" |
| 75 | + location = "" |
| 76 | + projectID = "" |
| 77 | +} |
| 78 | + |
| 79 | +type mockClusterExecutor struct{} |
| 80 | + |
| 81 | +func (m *mockClusterExecutor) ExecuteCommand(name string, args ...string) shell.CommandResult { |
| 82 | + if name == "gcloud" { |
| 83 | + if len(args) > 2 && args[0] == "container" && args[1] == "clusters" { |
| 84 | + if args[2] == "describe" { |
| 85 | + if strings.Contains(strings.Join(args, " "), "--format=yaml") { |
| 86 | + return shell.CommandResult{ |
| 87 | + ExitCode: 0, |
| 88 | + Stdout: "status: RUNNING\nname: test-cluster\n", |
| 89 | + } |
| 90 | + } |
| 91 | + return shell.CommandResult{ |
| 92 | + ExitCode: 0, |
| 93 | + Stdout: `{"status": "RUNNING", "name": "test-cluster"}`, |
| 94 | + } |
| 95 | + } |
| 96 | + if args[2] == "list" { |
| 97 | + return shell.CommandResult{ |
| 98 | + ExitCode: 0, |
| 99 | + Stdout: `[]`, |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + if name == "kubectl" { |
| 105 | + if len(args) > 1 && args[0] == "get" { |
| 106 | + if args[1] == "pvc" { |
| 107 | + return shell.CommandResult{ |
| 108 | + ExitCode: 0, |
| 109 | + Stdout: `{"items": []}`, |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return shell.CommandResult{ExitCode: 0, Stdout: "{}"} // Default to empty object JSON |
| 115 | +} |
| 116 | + |
| 117 | +func (m *mockClusterExecutor) ExecuteCommandStream(name string, args ...string) error { |
| 118 | + return nil |
| 119 | +} |
0 commit comments