diff --git a/changelogs/unreleased/XXXX-PratikMane0112 b/changelogs/unreleased/XXXX-PratikMane0112 new file mode 100644 index 000000000..8174c2b65 --- /dev/null +++ b/changelogs/unreleased/XXXX-PratikMane0112 @@ -0,0 +1 @@ +Fix snapshot-location get --selector flag to actually filter VolumeSnapshotLocations by label diff --git a/pkg/cmd/cli/snapshotlocation/get.go b/pkg/cmd/cli/snapshotlocation/get.go index 79da478bf..ff5b2af9e 100644 --- a/pkg/cmd/cli/snapshotlocation/get.go +++ b/pkg/cmd/cli/snapshotlocation/get.go @@ -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) diff --git a/pkg/cmd/cli/snapshotlocation/get_test.go b/pkg/cmd/cli/snapshotlocation/get_test.go new file mode 100644 index 000000000..9a5ce6ef7 --- /dev/null +++ b/pkg/cmd/cli/snapshotlocation/get_test.go @@ -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) +}