Fix snapshot-location get --selector to filter VSLs by label

Signed-off-by: PratikMane0112 <prmane@redhat.com>
This commit is contained in:
PratikMane0112
2026-08-30 11:00:07 +05:30
parent 836689f6cc
commit 226fefbc29
3 changed files with 68 additions and 2 deletions
+5 -2
View File
@@ -1,5 +1,5 @@
/*
Copyright 2018 the Velero contributors.
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -50,7 +50,10 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
locations.Items = append(locations.Items, *location)
}
} else {
err = client.List(context.TODO(), locations, &kbclient.ListOptions{Namespace: f.Namespace()})
err = client.List(context.TODO(), locations, &kbclient.ListOptions{
Namespace: f.Namespace(),
Raw: &listOptions,
})
cmd.CheckError(err)
}
_, err = output.PrintWithFormat(c, locations)
+62
View File
@@ -0,0 +1,62 @@
/*
Copyright The Velero Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package snapshotlocation
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
)
func TestNewGetCommand(t *testing.T) {
vslList := []string{"vsl1", "vsl2"}
f := &factorymocks.Factory{}
kbclient := velerotest.NewFakeControllerRuntimeClient(t)
f.On("Namespace").Return(mock.Anything)
f.On("KubebuilderClient").Return(kbclient, nil)
// get command
c := NewGetCommand(f, "velero snapshot-location get")
assert.Equal(t, "Get snapshot locations", c.Short)
c.Execute()
if os.Getenv(cmdtest.CaptureFlag) == "1" {
c.SetArgs([]string{"vsl1", "vsl2"})
c.Execute()
return
}
cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand"}...)
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
_, stderr, err := veleroexec.RunCommand(cmd)
if err != nil {
assert.Contains(t, stderr, fmt.Sprintf("volumesnapshotlocations.velero.io \"%s\" not found", vslList[0]))
return
}
t.Fatalf("process ran with err %v, want snapshot location get to fail for non-existent VSL", err)
}