Update label selector pattern and add a test case

Signed-off-by: PratikMane0112 <prmane@redhat.com>
This commit is contained in:
PratikMane0112
2026-08-31 15:00:48 +05:30
parent 3083b38e2d
commit 3c47a2c326
2 changed files with 48 additions and 3 deletions
+6 -3
View File
@@ -1,5 +1,5 @@
/*
Copyright the Velero contributors.
Copyright 2018 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.
@@ -21,6 +21,7 @@ import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
@@ -50,9 +51,11 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
locations.Items = append(locations.Items, *location)
}
} else {
parsedSelector, err := labels.Parse(listOptions.LabelSelector)
cmd.CheckError(err)
err = client.List(context.TODO(), locations, &kbclient.ListOptions{
Namespace: f.Namespace(),
Raw: &listOptions,
LabelSelector: parsedSelector,
Namespace: f.Namespace(),
})
cmd.CheckError(err)
}
+42
View File
@@ -24,7 +24,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/vmware-tanzu/velero/pkg/builder"
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"
@@ -60,3 +63,42 @@ func TestNewGetCommand(t *testing.T) {
}
t.Fatalf("process ran with err %v, want snapshot location get to fail for non-existent VSL", err)
}
func TestNewGetCommand_SelectorFiltersVSLs(t *testing.T) {
f := &factorymocks.Factory{}
client := velerotest.NewFakeControllerRuntimeClient(t)
vslLabeled := builder.ForVolumeSnapshotLocation(cmdtest.VeleroNameSpace, "vsl-labeled").
ObjectMeta(builder.WithLabels("env", "test")).
Result()
err := client.Create(t.Context(), vslLabeled, &kbclient.CreateOptions{})
require.NoError(t, err)
vslUnlabeled := builder.ForVolumeSnapshotLocation(cmdtest.VeleroNameSpace, "vsl-unlabeled").
Result()
err = client.Create(t.Context(), vslUnlabeled, &kbclient.CreateOptions{})
require.NoError(t, err)
f.On("KubebuilderClient").Return(client, nil)
f.On("Namespace").Return(cmdtest.VeleroNameSpace)
// get command with selector
c := NewGetCommand(f, "velero snapshot-location get")
c.SetArgs([]string{"--selector", "env=test"})
err = c.Execute()
require.NoError(t, err)
if os.Getenv(cmdtest.CaptureFlag) == "1" {
return
}
cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand_SelectorFiltersVSLs"}...)
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
stdout, _, err := veleroexec.RunCommand(cmd)
require.NoError(t, err)
// assert that the labeled VSL is returned
assert.Contains(t, stdout, "vsl-labeled")
// assert that the unlabeled VSL is not returned
assert.NotContains(t, stdout, "vsl-unlabeled")
}