mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-17 15:50:10 +00:00
ResourceNames() should prohibit unnamed arguments
resource.Builder should prohibit empty resource names (the error is from the wrong place) so that commands that work on multiple resources but not resource types can properly limit errors.
This commit is contained in:
parent
1b0bc9421f
commit
994e9e3c62
@ -212,6 +212,10 @@ func (b *Builder) ResourceNames(resource string, names ...string) *Builder {
|
||||
b.resourceTuples = append(b.resourceTuples, tuple)
|
||||
continue
|
||||
}
|
||||
if len(resource) == 0 {
|
||||
b.errs = append(b.errs, fmt.Errorf("the argument %q must be RESOURCE/NAME", name))
|
||||
continue
|
||||
}
|
||||
|
||||
// Use the given default type to create a resource tuple
|
||||
b.resourceTuples = append(b.resourceTuples, resourceTuple{Resource: resource, Name: name})
|
||||
|
@ -626,6 +626,28 @@ func TestResourceNames(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceNamesWithoutResource(t *testing.T) {
|
||||
pods, svc := testData()
|
||||
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
|
||||
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[0]),
|
||||
"/namespaces/test/services/baz": runtime.EncodeOrDie(testapi.Default.Codec(), &svc.Items[0]),
|
||||
}), testapi.Default.Codec()).
|
||||
NamespaceParam("test")
|
||||
|
||||
test := &testVisitor{}
|
||||
|
||||
if b.Do().Err() == nil {
|
||||
t.Errorf("unexpected non-error")
|
||||
}
|
||||
|
||||
b.ResourceNames("", "foo", "services/baz")
|
||||
|
||||
err := b.Do().Visit(test.Handle)
|
||||
if err == nil || !strings.Contains(err.Error(), "must be RESOURCE/NAME") {
|
||||
t.Fatalf("unexpected response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceByNameWithoutRequireObject(t *testing.T) {
|
||||
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{}), testapi.Default.Codec()).
|
||||
NamespaceParam("test")
|
||||
|
Loading…
Reference in New Issue
Block a user