mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-07 01:50:46 +00:00
client-go/testing: properly handle Patch actions for missing Objects
Currently the fake client will return a default empty Object when a Patch action is submitted on a missing Object. The correct behavior is to instead propagate the NotFound error. Kubernetes-commit: 96d0588440a96c5eba8b3ba0810563ad1e1a08b6
This commit is contained in:
committed by
Kubernetes Publisher
parent
7a94d612ad
commit
213a1e8b13
@@ -131,8 +131,7 @@ func ObjectReaction(tracker ObjectTracker) ReactionFunc {
|
||||
case PatchActionImpl:
|
||||
obj, err := tracker.Get(gvr, ns, action.GetName())
|
||||
if err != nil {
|
||||
// object is not registered
|
||||
return false, nil, err
|
||||
return true, nil, err
|
||||
}
|
||||
|
||||
old, err := json.Marshal(obj)
|
||||
|
@@ -30,6 +30,7 @@ import (
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
)
|
||||
|
||||
@@ -258,3 +259,17 @@ func TestWatchAddAfterStop(t *testing.T) {
|
||||
t.Errorf("test resource creation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchWithMissingObject(t *testing.T) {
|
||||
nodesResource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "nodes"}
|
||||
|
||||
scheme := runtime.NewScheme()
|
||||
codecs := serializer.NewCodecFactory(scheme)
|
||||
o := NewObjectTracker(scheme, codecs.UniversalDecoder())
|
||||
reaction := ObjectReaction(o)
|
||||
action := NewRootPatchSubresourceAction(nodesResource, "node-1", types.StrategicMergePatchType, []byte(`{}`))
|
||||
handled, node, err := reaction(action)
|
||||
assert.True(t, handled)
|
||||
assert.Nil(t, node)
|
||||
assert.EqualError(t, err, `nodes "node-1" not found`)
|
||||
}
|
||||
|
Reference in New Issue
Block a user