|
| 1 | +/* |
| 2 | +Copyright The Velero 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 cli |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + kbclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + |
| 30 | + velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" |
| 31 | + factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks" |
| 32 | + velerotest "github.com/vmware-tanzu/velero/pkg/test" |
| 33 | +) |
| 34 | + |
| 35 | +// TestCompleteNames exercises the core completeNames helper with various list |
| 36 | +// types, prefix filters, and edge cases (empty cluster, no match). |
| 37 | +func TestCompleteNames(t *testing.T) { |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + objects []runtime.Object |
| 41 | + list kbclient.ObjectList |
| 42 | + toComplete string |
| 43 | + want []string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "no resources returns nil", |
| 47 | + objects: nil, |
| 48 | + list: &velerov1api.BackupList{}, |
| 49 | + toComplete: "", |
| 50 | + want: nil, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "returns all matching names", |
| 54 | + objects: []runtime.Object{ |
| 55 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "daily", Namespace: "velero"}}, |
| 56 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "weekly", Namespace: "velero"}}, |
| 57 | + }, |
| 58 | + list: &velerov1api.BackupList{}, |
| 59 | + toComplete: "", |
| 60 | + want: []string{"daily", "weekly"}, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "filters by prefix", |
| 64 | + objects: []runtime.Object{ |
| 65 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "daily", Namespace: "velero"}}, |
| 66 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "weekly", Namespace: "velero"}}, |
| 67 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "daily-full", Namespace: "velero"}}, |
| 68 | + }, |
| 69 | + list: &velerov1api.BackupList{}, |
| 70 | + toComplete: "dai", |
| 71 | + want: []string{"daily", "daily-full"}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "no prefix match returns nil", |
| 75 | + objects: []runtime.Object{ |
| 76 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "daily", Namespace: "velero"}}, |
| 77 | + }, |
| 78 | + list: &velerov1api.BackupList{}, |
| 79 | + toComplete: "xyz", |
| 80 | + want: nil, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "works with RestoreList", |
| 84 | + objects: []runtime.Object{ |
| 85 | + &velerov1api.Restore{ObjectMeta: metav1.ObjectMeta{Name: "restore-1", Namespace: "velero"}}, |
| 86 | + &velerov1api.Restore{ObjectMeta: metav1.ObjectMeta{Name: "restore-2", Namespace: "velero"}}, |
| 87 | + }, |
| 88 | + list: &velerov1api.RestoreList{}, |
| 89 | + toComplete: "restore-", |
| 90 | + want: []string{"restore-1", "restore-2"}, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "works with ScheduleList", |
| 94 | + objects: []runtime.Object{ |
| 95 | + &velerov1api.Schedule{ObjectMeta: metav1.ObjectMeta{Name: "nightly", Namespace: "velero"}}, |
| 96 | + }, |
| 97 | + list: &velerov1api.ScheduleList{}, |
| 98 | + toComplete: "", |
| 99 | + want: []string{"nightly"}, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "works with BackupStorageLocationList", |
| 103 | + objects: []runtime.Object{ |
| 104 | + &velerov1api.BackupStorageLocation{ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: "velero"}}, |
| 105 | + &velerov1api.BackupStorageLocation{ObjectMeta: metav1.ObjectMeta{Name: "secondary", Namespace: "velero"}}, |
| 106 | + }, |
| 107 | + list: &velerov1api.BackupStorageLocationList{}, |
| 108 | + toComplete: "s", |
| 109 | + want: []string{"secondary"}, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "works with VolumeSnapshotLocationList", |
| 113 | + objects: []runtime.Object{ |
| 114 | + &velerov1api.VolumeSnapshotLocation{ObjectMeta: metav1.ObjectMeta{Name: "aws-snap", Namespace: "velero"}}, |
| 115 | + }, |
| 116 | + list: &velerov1api.VolumeSnapshotLocationList{}, |
| 117 | + toComplete: "", |
| 118 | + want: []string{"aws-snap"}, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "works with BackupRepositoryList", |
| 122 | + objects: []runtime.Object{ |
| 123 | + &velerov1api.BackupRepository{ObjectMeta: metav1.ObjectMeta{Name: "repo-1", Namespace: "velero"}}, |
| 124 | + }, |
| 125 | + list: &velerov1api.BackupRepositoryList{}, |
| 126 | + toComplete: "", |
| 127 | + want: []string{"repo-1"}, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tc := range tests { |
| 132 | + t.Run(tc.name, func(t *testing.T) { |
| 133 | + kbClient := velerotest.NewFakeControllerRuntimeClient(t, tc.objects...) |
| 134 | + |
| 135 | + f := new(factorymocks.Factory) |
| 136 | + f.On("KubebuilderClient").Return(kbClient, nil) |
| 137 | + f.On("Namespace").Return("velero") |
| 138 | + |
| 139 | + completionFn := completeNames(f, tc.list) |
| 140 | + got, directive := completionFn(&cobra.Command{}, nil, tc.toComplete) |
| 141 | + |
| 142 | + assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive) |
| 143 | + assert.Equal(t, tc.want, got) |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// TestCompleteNames_KubebuilderClientError verifies that a factory error |
| 149 | +// (e.g. no kubeconfig) returns nil completions instead of panicking. |
| 150 | +func TestCompleteNames_KubebuilderClientError(t *testing.T) { |
| 151 | + f := new(factorymocks.Factory) |
| 152 | + f.On("KubebuilderClient").Return(nil, fmt.Errorf("connection refused")) |
| 153 | + |
| 154 | + completionFn := completeNames(f, &velerov1api.BackupList{}) |
| 155 | + got, directive := completionFn(&cobra.Command{}, nil, "") |
| 156 | + |
| 157 | + assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive) |
| 158 | + assert.Nil(t, got) |
| 159 | +} |
| 160 | + |
| 161 | +// TestCompleteWrappers verifies each exported Complete*Names wrapper returns |
| 162 | +// only its own resource type. A single fake client holds one object of every |
| 163 | +// type, so each wrapper must filter correctly and not leak other kinds. |
| 164 | +func TestCompleteWrappers(t *testing.T) { |
| 165 | + objects := []runtime.Object{ |
| 166 | + &velerov1api.Backup{ObjectMeta: metav1.ObjectMeta{Name: "b1", Namespace: "velero"}}, |
| 167 | + &velerov1api.Restore{ObjectMeta: metav1.ObjectMeta{Name: "r1", Namespace: "velero"}}, |
| 168 | + &velerov1api.Schedule{ObjectMeta: metav1.ObjectMeta{Name: "s1", Namespace: "velero"}}, |
| 169 | + &velerov1api.BackupStorageLocation{ObjectMeta: metav1.ObjectMeta{Name: "bsl1", Namespace: "velero"}}, |
| 170 | + &velerov1api.VolumeSnapshotLocation{ObjectMeta: metav1.ObjectMeta{Name: "vsl1", Namespace: "velero"}}, |
| 171 | + &velerov1api.BackupRepository{ObjectMeta: metav1.ObjectMeta{Name: "br1", Namespace: "velero"}}, |
| 172 | + } |
| 173 | + kbClient := velerotest.NewFakeControllerRuntimeClient(t, objects...) |
| 174 | + |
| 175 | + f := new(factorymocks.Factory) |
| 176 | + f.On("KubebuilderClient").Return(kbClient, nil) |
| 177 | + f.On("Namespace").Return("velero") |
| 178 | + |
| 179 | + tests := []struct { |
| 180 | + name string |
| 181 | + fn completionFunc |
| 182 | + expected []string |
| 183 | + }{ |
| 184 | + {"CompleteBackupNames", CompleteBackupNames(f), []string{"b1"}}, |
| 185 | + {"CompleteRestoreNames", CompleteRestoreNames(f), []string{"r1"}}, |
| 186 | + {"CompleteScheduleNames", CompleteScheduleNames(f), []string{"s1"}}, |
| 187 | + {"CompleteBackupStorageLocationNames", CompleteBackupStorageLocationNames(f), []string{"bsl1"}}, |
| 188 | + {"CompleteVolumeSnapshotLocationNames", CompleteVolumeSnapshotLocationNames(f), []string{"vsl1"}}, |
| 189 | + {"CompleteBackupRepositoryNames", CompleteBackupRepositoryNames(f), []string{"br1"}}, |
| 190 | + } |
| 191 | + |
| 192 | + for _, tc := range tests { |
| 193 | + t.Run(tc.name, func(t *testing.T) { |
| 194 | + got, directive := tc.fn(&cobra.Command{}, nil, "") |
| 195 | + require.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive) |
| 196 | + assert.Equal(t, tc.expected, got) |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
0 commit comments