|
| 1 | +/* |
| 2 | +Copyright 2025 The OADP CLI Contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package restore |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/migtools/oadp-cli/internal/testutil" |
| 23 | +) |
| 24 | + |
| 25 | +// TestNonAdminRestoreCommands tests the non-admin restore command functionality |
| 26 | +func TestNonAdminRestoreCommands(t *testing.T) { |
| 27 | + binaryPath := testutil.BuildCLIBinary(t) |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + args []string |
| 32 | + expectContains []string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "nonadmin restore help", |
| 36 | + args: []string{"nonadmin", "restore", "--help"}, |
| 37 | + expectContains: []string{ |
| 38 | + "Work with non-admin restores", |
| 39 | + "create", |
| 40 | + "describe", |
| 41 | + "delete", |
| 42 | + "get", |
| 43 | + "logs", |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "nonadmin restore create help", |
| 48 | + args: []string{"nonadmin", "restore", "create", "--help"}, |
| 49 | + expectContains: []string{ |
| 50 | + "Create a non-admin restore", |
| 51 | + "--from-backup", |
| 52 | + "--include-resources", |
| 53 | + "--exclude-resources", |
| 54 | + "--wait", |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "nonadmin restore describe help", |
| 59 | + args: []string{"nonadmin", "restore", "describe", "--help"}, |
| 60 | + expectContains: []string{ |
| 61 | + "Describe a non-admin restore", |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "nonadmin restore delete help", |
| 66 | + args: []string{"nonadmin", "restore", "delete", "--help"}, |
| 67 | + expectContains: []string{ |
| 68 | + "Delete one or more non-admin restores", |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "nonadmin restore get help", |
| 73 | + args: []string{"nonadmin", "restore", "get", "--help"}, |
| 74 | + expectContains: []string{ |
| 75 | + "Get non-admin restores in the current namespace", |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "nonadmin restore logs help", |
| 80 | + args: []string{"nonadmin", "restore", "logs", "--help"}, |
| 81 | + expectContains: []string{ |
| 82 | + "Display logs for a specified non-admin restore operation", |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "na restore shorthand help", |
| 87 | + args: []string{"na", "restore", "--help"}, |
| 88 | + expectContains: []string{ |
| 89 | + "Work with non-admin restores", |
| 90 | + "create", |
| 91 | + "describe", |
| 92 | + "delete", |
| 93 | + "get", |
| 94 | + "logs", |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + testutil.TestHelpCommand(t, binaryPath, tt.args, tt.expectContains) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// TestNonAdminRestoreHelpFlags tests that both --help and -h work for restore commands |
| 107 | +func TestNonAdminRestoreHelpFlags(t *testing.T) { |
| 108 | + binaryPath := testutil.BuildCLIBinary(t) |
| 109 | + |
| 110 | + commands := [][]string{ |
| 111 | + {"nonadmin", "restore", "--help"}, |
| 112 | + {"nonadmin", "restore", "-h"}, |
| 113 | + {"nonadmin", "restore", "create", "--help"}, |
| 114 | + {"nonadmin", "restore", "create", "-h"}, |
| 115 | + {"nonadmin", "restore", "describe", "--help"}, |
| 116 | + {"nonadmin", "restore", "describe", "-h"}, |
| 117 | + {"nonadmin", "restore", "delete", "--help"}, |
| 118 | + {"nonadmin", "restore", "delete", "-h"}, |
| 119 | + {"nonadmin", "restore", "get", "--help"}, |
| 120 | + {"nonadmin", "restore", "get", "-h"}, |
| 121 | + {"nonadmin", "restore", "logs", "--help"}, |
| 122 | + {"nonadmin", "restore", "logs", "-h"}, |
| 123 | + {"na", "restore", "--help"}, |
| 124 | + {"na", "restore", "-h"}, |
| 125 | + } |
| 126 | + |
| 127 | + for _, cmd := range commands { |
| 128 | + t.Run("help_flags_"+cmd[len(cmd)-1], func(t *testing.T) { |
| 129 | + testutil.TestHelpCommand(t, binaryPath, cmd, []string{"Usage:"}) |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// TestNonAdminRestoreCreateFlags tests create command specific flags |
| 135 | +func TestNonAdminRestoreCreateFlags(t *testing.T) { |
| 136 | + binaryPath := testutil.BuildCLIBinary(t) |
| 137 | + |
| 138 | + t.Run("create command has all expected flags", func(t *testing.T) { |
| 139 | + expectedFlags := []string{ |
| 140 | + "--from-backup", |
| 141 | + "--include-resources", |
| 142 | + "--exclude-resources", |
| 143 | + "--labels", |
| 144 | + "--annotations", |
| 145 | + "--wait", |
| 146 | + "--selector", |
| 147 | + "--or-selector", |
| 148 | + "--include-cluster-resources", |
| 149 | + "--restore-volumes", |
| 150 | + "--preserve-nodeports", |
| 151 | + "--item-operation-timeout", |
| 152 | + "--existing-resource-policy", |
| 153 | + } |
| 154 | + |
| 155 | + testutil.TestHelpCommand(t, binaryPath, |
| 156 | + []string{"nonadmin", "restore", "create", "--help"}, |
| 157 | + expectedFlags) |
| 158 | + }) |
| 159 | +} |
| 160 | + |
| 161 | +// TestNonAdminRestoreExamples tests that help text contains proper examples |
| 162 | +func TestNonAdminRestoreExamples(t *testing.T) { |
| 163 | + binaryPath := testutil.BuildCLIBinary(t) |
| 164 | + |
| 165 | + t.Run("create examples use correct command format", func(t *testing.T) { |
| 166 | + expectedExamples := []string{ |
| 167 | + "kubectl oadp nonadmin restore create", |
| 168 | + "--from-backup", |
| 169 | + "--include-resources", |
| 170 | + "--exclude-resources", |
| 171 | + "--wait", |
| 172 | + } |
| 173 | + |
| 174 | + testutil.TestHelpCommand(t, binaryPath, |
| 175 | + []string{"nonadmin", "restore", "create", "--help"}, |
| 176 | + expectedExamples) |
| 177 | + }) |
| 178 | + |
| 179 | + t.Run("main restore help shows subcommands", func(t *testing.T) { |
| 180 | + expectedSubcommands := []string{ |
| 181 | + "create", |
| 182 | + "delete", |
| 183 | + "describe", |
| 184 | + "get", |
| 185 | + "logs", |
| 186 | + } |
| 187 | + |
| 188 | + testutil.TestHelpCommand(t, binaryPath, |
| 189 | + []string{"nonadmin", "restore", "--help"}, |
| 190 | + expectedSubcommands) |
| 191 | + }) |
| 192 | +} |
| 193 | + |
| 194 | +// TestNonAdminRestoreClientConfigIntegration tests that restore commands respect client config |
| 195 | +func TestNonAdminRestoreClientConfigIntegration(t *testing.T) { |
| 196 | + binaryPath := testutil.BuildCLIBinary(t) |
| 197 | + _, cleanup := testutil.SetupTempHome(t) |
| 198 | + defer cleanup() |
| 199 | + |
| 200 | + t.Run("restore commands work with client config", func(t *testing.T) { |
| 201 | + // Set a known namespace |
| 202 | + _, err := testutil.RunCommand(t, binaryPath, "client", "config", "set", "namespace=user-namespace") |
| 203 | + if err != nil { |
| 204 | + t.Fatalf("Failed to set client config: %v", err) |
| 205 | + } |
| 206 | + |
| 207 | + // Test that restore commands can be invoked (they should respect the namespace) |
| 208 | + // We test help commands since they don't require actual K8s resources |
| 209 | + commands := [][]string{ |
| 210 | + {"nonadmin", "restore", "get", "--help"}, |
| 211 | + {"nonadmin", "restore", "create", "--help"}, |
| 212 | + {"nonadmin", "restore", "describe", "--help"}, |
| 213 | + {"nonadmin", "restore", "delete", "--help"}, |
| 214 | + {"nonadmin", "restore", "logs", "--help"}, |
| 215 | + {"na", "restore", "get", "--help"}, |
| 216 | + } |
| 217 | + |
| 218 | + for _, cmd := range commands { |
| 219 | + t.Run("config_test_"+cmd[len(cmd)-2], func(t *testing.T) { |
| 220 | + output, err := testutil.RunCommand(t, binaryPath, cmd...) |
| 221 | + if err != nil { |
| 222 | + t.Fatalf("Non-admin restore command should work with client config: %v", err) |
| 223 | + } |
| 224 | + if output == "" { |
| 225 | + t.Errorf("Expected help output for %v", cmd) |
| 226 | + } |
| 227 | + }) |
| 228 | + } |
| 229 | + }) |
| 230 | +} |
| 231 | + |
| 232 | +// TestNonAdminRestoreCommandStructure tests the overall command structure |
| 233 | +func TestNonAdminRestoreCommandStructure(t *testing.T) { |
| 234 | + binaryPath := testutil.BuildCLIBinary(t) |
| 235 | + |
| 236 | + t.Run("restore commands available under nonadmin", func(t *testing.T) { |
| 237 | + _, err := testutil.RunCommand(t, binaryPath, "nonadmin", "--help") |
| 238 | + if err != nil { |
| 239 | + t.Fatalf("nonadmin command should exist: %v", err) |
| 240 | + } |
| 241 | + |
| 242 | + expectedCommands := []string{"restore"} |
| 243 | + for _, cmd := range expectedCommands { |
| 244 | + testutil.TestHelpCommand(t, binaryPath, []string{"nonadmin", "--help"}, []string{cmd}) |
| 245 | + } |
| 246 | + }) |
| 247 | + |
| 248 | + t.Run("restore commands available under na shorthand", func(t *testing.T) { |
| 249 | + _, err := testutil.RunCommand(t, binaryPath, "na", "--help") |
| 250 | + if err != nil { |
| 251 | + t.Fatalf("na command should exist: %v", err) |
| 252 | + } |
| 253 | + |
| 254 | + expectedCommands := []string{"restore"} |
| 255 | + for _, cmd := range expectedCommands { |
| 256 | + testutil.TestHelpCommand(t, binaryPath, []string{"na", "--help"}, []string{cmd}) |
| 257 | + } |
| 258 | + }) |
| 259 | +} |
0 commit comments