mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Adding a unit test for verifying OrphanDependents in kubectl delete requests
This commit is contained in:
parent
9dc31c7f82
commit
20aa573865
@ -18,6 +18,9 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -88,6 +91,68 @@ func TestDeleteObjectByTuple(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasExpectedOrphanDependents(body io.ReadCloser, expectedOrphanDependents *bool) bool {
|
||||||
|
if body == nil || expectedOrphanDependents == nil {
|
||||||
|
return body == nil && expectedOrphanDependents == nil
|
||||||
|
}
|
||||||
|
var parsedBody metav1.DeleteOptions
|
||||||
|
rawBody, _ := ioutil.ReadAll(body)
|
||||||
|
json.Unmarshal(rawBody, &parsedBody)
|
||||||
|
if parsedBody.OrphanDependents == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return *expectedOrphanDependents == *parsedBody.OrphanDependents
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that DeleteOptions.OrphanDependents is appropriately set while deleting objects.
|
||||||
|
func TestOrphanDependentsInDeleteObject(t *testing.T) {
|
||||||
|
initTestErrorHandler(t)
|
||||||
|
_, _, rc := testData()
|
||||||
|
|
||||||
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
||||||
|
tf.Printer = &testPrinter{}
|
||||||
|
var expectedOrphanDependents *bool
|
||||||
|
tf.UnstructuredClient = &fake.RESTClient{
|
||||||
|
APIRegistry: api.Registry,
|
||||||
|
NegotiatedSerializer: unstructuredSerializer,
|
||||||
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
||||||
|
switch p, m, b := req.URL.Path, req.Method, req.Body; {
|
||||||
|
|
||||||
|
case p == "/namespaces/test/secrets/mysecret" && m == "DELETE" && hasExpectedOrphanDependents(b, expectedOrphanDependents):
|
||||||
|
|
||||||
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
|
||||||
|
default:
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
tf.Namespace = "test"
|
||||||
|
|
||||||
|
// DeleteOptions.OrphanDependents should be false, when cascade is true (default).
|
||||||
|
falseVar := false
|
||||||
|
expectedOrphanDependents = &falseVar
|
||||||
|
buf, errBuf := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
|
||||||
|
cmd := NewCmdDelete(f, buf, errBuf)
|
||||||
|
cmd.Flags().Set("namespace", "test")
|
||||||
|
cmd.Flags().Set("output", "name")
|
||||||
|
cmd.Run(cmd, []string{"secrets/mysecret"})
|
||||||
|
if buf.String() != "secret/mysecret\n" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that delete options should be nil when cascade is false.
|
||||||
|
expectedOrphanDependents = nil
|
||||||
|
buf, errBuf = bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
|
||||||
|
cmd = NewCmdDelete(f, buf, errBuf)
|
||||||
|
cmd.Flags().Set("namespace", "test")
|
||||||
|
cmd.Flags().Set("cascade", "false")
|
||||||
|
cmd.Flags().Set("output", "name")
|
||||||
|
cmd.Run(cmd, []string{"secrets/mysecret"})
|
||||||
|
if buf.String() != "secret/mysecret\n" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDeleteNamedObject(t *testing.T) {
|
func TestDeleteNamedObject(t *testing.T) {
|
||||||
initTestErrorHandler(t)
|
initTestErrorHandler(t)
|
||||||
initTestErrorHandler(t)
|
initTestErrorHandler(t)
|
||||||
|
Loading…
Reference in New Issue
Block a user