explain the reason why metaclient special processing metav1.DeleteOptions encoding

metaclient explicitly specifies the Content-Type when executing Delete and DeleteCollection, and add test for that

Kubernetes-commit: 8976f6f6d9af22ad40df891565c19e4dfd67f591
This commit is contained in:
scott
2021-08-25 17:55:25 +08:00
committed by Kubernetes Publisher
parent ce70fecbf8
commit 0c4682e956
2 changed files with 69 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ package metadata
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
@@ -35,6 +36,10 @@ import (
func TestClient(t *testing.T) {
gvr := schema.GroupVersionResource{Group: "group", Version: "v1", Resource: "resource"}
statusOK := &metav1.Status{
Status: metav1.StatusSuccess,
Code: http.StatusOK,
}
writeJSON := func(t *testing.T, w http.ResponseWriter, obj runtime.Object) {
data, err := json.Marshal(obj)
@@ -229,6 +234,61 @@ func TestClient(t *testing.T) {
}
},
},
{
name: "Delete fails if DeleteOptions cannot be serialized to JSON",
handler: func(t *testing.T, w http.ResponseWriter, req *http.Request) {
if req.Header.Get("Content-Type") != runtime.ContentTypeJSON {
t.Fatal(req.Header.Get("Content-Type"))
}
if req.Method != "DELETE" && req.URL.String() != "/apis/group/v1/namespaces/ns/resource/name" {
t.Fatal(req.URL.String())
}
defer req.Body.Close()
buf, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if !json.Valid(buf) {
t.Fatalf("request body is not a valid JSON: %s", buf)
}
writeJSON(t, w, statusOK)
},
want: func(t *testing.T, client *Client) {
err := client.Resource(gvr).Namespace("ns").Delete(context.TODO(), "name", metav1.DeleteOptions{})
if err != nil {
t.Fatal(err)
}
},
},
{
name: "DeleteCollection fails if DeleteOptions cannot be serialized to JSON",
handler: func(t *testing.T, w http.ResponseWriter, req *http.Request) {
if req.Header.Get("Content-Type") != runtime.ContentTypeJSON {
t.Fatal(req.Header.Get("Content-Type"))
}
if req.Method != "DELETE" && req.URL.String() != "/apis/group/v1/namespaces/ns/resource/name" {
t.Fatal(req.URL.String())
}
defer req.Body.Close()
buf, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if !json.Valid(buf) {
t.Fatalf("request body is not a valid JSON: %s", buf)
}
writeJSON(t, w, statusOK)
},
want: func(t *testing.T, client *Client) {
err := client.Resource(gvr).Namespace("ns").DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
if err != nil {
t.Fatal(err)
}
},
},
}
for _, tt := range testCases {