mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 23:17:50 +00:00
test for explicit null value propagation in apply
This commit is contained in:
committed by
Jordan Liggitt
parent
86d561424d
commit
4bdcc03c0b
@@ -20,6 +20,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -35,6 +36,8 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/annotations"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
"k8s.io/kubernetes/pkg/client/restclient/fake"
|
||||
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
)
|
||||
@@ -443,3 +446,87 @@ func testApplyMultipleObjects(t *testing.T, asList bool) {
|
||||
t.Fatalf("unexpected output: %s\nexpected: %s OR %s", buf.String(), expectOne, expectTwo)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
filenameDeployObjServerside = "../../../test/fixtures/pkg/kubectl/cmd/apply/deploy-serverside.yaml"
|
||||
filenameDeployObjClientside = "../../../test/fixtures/pkg/kubectl/cmd/apply/deploy-clientside.yaml"
|
||||
)
|
||||
|
||||
// validateNULLPatchApplication retrieves the StrategicMergePatch created by kubectl apply and checks that this patch
|
||||
// contains the null value defined by the user.
|
||||
func validateNULLPatchApplication(t *testing.T, req *http.Request) {
|
||||
patch, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
patchMap := map[string]interface{}{}
|
||||
if err := json.Unmarshal(patch, &patchMap); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
annotationsMap := walkMapPath(t, patchMap, []string{"metadata", "annotations"})
|
||||
if _, ok := annotationsMap[annotations.LastAppliedConfigAnnotation]; !ok {
|
||||
t.Fatalf("patch does not contain annotation:\n%s\n", patch)
|
||||
}
|
||||
|
||||
labelMap := walkMapPath(t, patchMap, []string{"spec", "strategy"})
|
||||
// Checks if `rollingUpdate = null` exists in the patch
|
||||
if deleteMe, ok := labelMap["rollingUpdate"]; !ok || deleteMe != nil {
|
||||
t.Fatalf("patch did not retain null value in key: rollingUpdate:\n%s\n", patch)
|
||||
}
|
||||
}
|
||||
|
||||
func getServersideObject(t *testing.T) io.ReadCloser {
|
||||
raw := readBytesFromFile(t, filenameDeployObjServerside)
|
||||
obj := &extensions.Deployment{}
|
||||
if err := runtime.DecodeInto(testapi.Extensions.Codec(), raw, obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objJSON, err := runtime.Encode(testapi.Extensions.Codec(), obj)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ioutil.NopCloser(bytes.NewReader(objJSON))
|
||||
}
|
||||
|
||||
func TestApplyNULLPreservation(t *testing.T) {
|
||||
deploymentName := "nginx-deployment"
|
||||
deploymentPath := "/namespaces/test/deployments/" + deploymentName
|
||||
|
||||
f, tf, _, ns := cmdtesting.NewExtensionsAPIFactory()
|
||||
tf.Client = &fake.RESTClient{
|
||||
NegotiatedSerializer: ns,
|
||||
GroupName: "extensions",
|
||||
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
||||
switch p, m := req.URL.Path, req.Method; {
|
||||
case p == deploymentPath && m == "GET":
|
||||
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: getServersideObject(t)}, nil
|
||||
case p == deploymentPath && m == "PATCH":
|
||||
validateNULLPatchApplication(t, req)
|
||||
// The real API server would had returned the patched object but Kubectl
|
||||
// is ignoring the actual return object.
|
||||
// TODO: Make this match actual server behavior by returning the patched object.
|
||||
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: getServersideObject(t)}, nil
|
||||
default:
|
||||
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||
return nil, nil
|
||||
}
|
||||
}),
|
||||
}
|
||||
tf.Namespace = "test"
|
||||
tf.ClientConfig = defaultClientConfig()
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
errBuf := bytes.NewBuffer([]byte{})
|
||||
|
||||
cmd := NewCmdApply(f, buf, errBuf)
|
||||
cmd.Flags().Set("filename", filenameDeployObjClientside)
|
||||
cmd.Flags().Set("output", "name")
|
||||
|
||||
cmd.Run(cmd, []string{})
|
||||
|
||||
expected := "deployment/" + deploymentName + "\n"
|
||||
if buf.String() != expected {
|
||||
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user