2015-06-25 22:56:30 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-06-25 22:56:30 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-02-21 02:12:13 +00:00
|
|
|
"strings"
|
2015-06-25 22:56:30 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-24 15:00:24 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2016-10-18 22:53:26 +00:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-04-26 13:30:21 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2018-02-21 17:10:38 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2015-06-25 22:56:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPatchObject(t *testing.T) {
|
|
|
|
_, svc, _ := testData()
|
|
|
|
|
2018-05-24 13:33:36 +00:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2018-08-02 17:29:16 +00:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 17:10:38 +00:00
|
|
|
|
2017-01-22 16:38:05 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2015-06-25 22:56:30 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"):
|
2017-09-13 22:08:15 +00:00
|
|
|
obj := svc.Items[0]
|
|
|
|
|
|
|
|
// ensure patched object reflects successful
|
|
|
|
// patch edits from the client
|
|
|
|
if m == "PATCH" {
|
|
|
|
obj.Spec.Type = "NodePort"
|
|
|
|
}
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &obj)}, nil
|
2015-06-25 22:56:30 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2018-04-26 13:30:21 +00:00
|
|
|
stream, _, buf, _ := genericclioptions.NewTestIOStreams()
|
2015-06-25 22:56:30 +00:00
|
|
|
|
2018-04-26 13:30:21 +00:00
|
|
|
cmd := NewCmdPatch(tf, stream)
|
2015-06-25 22:56:30 +00:00
|
|
|
cmd.Flags().Set("namespace", "test")
|
|
|
|
cmd.Flags().Set("patch", `{"spec":{"type":"NodePort"}}`)
|
2015-07-01 22:47:43 +00:00
|
|
|
cmd.Flags().Set("output", "name")
|
2015-06-25 22:56:30 +00:00
|
|
|
cmd.Run(cmd, []string{"services/frontend"})
|
|
|
|
|
2017-02-21 02:12:13 +00:00
|
|
|
// uses the name from the response
|
2018-02-21 01:14:21 +00:00
|
|
|
if buf.String() != "service/baz\n" {
|
2015-06-25 22:56:30 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 09:24:02 +00:00
|
|
|
|
|
|
|
func TestPatchObjectFromFile(t *testing.T) {
|
|
|
|
_, svc, _ := testData()
|
|
|
|
|
2018-05-24 13:33:36 +00:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2018-08-02 17:29:16 +00:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 17:10:38 +00:00
|
|
|
|
2017-01-22 16:38:05 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2015-08-06 09:24:02 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"):
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
|
2015-08-06 09:24:02 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2018-04-26 13:30:21 +00:00
|
|
|
stream, _, buf, _ := genericclioptions.NewTestIOStreams()
|
2015-08-06 09:24:02 +00:00
|
|
|
|
2018-04-26 13:30:21 +00:00
|
|
|
cmd := NewCmdPatch(tf, stream)
|
2015-08-06 09:24:02 +00:00
|
|
|
cmd.Flags().Set("namespace", "test")
|
|
|
|
cmd.Flags().Set("patch", `{"spec":{"type":"NodePort"}}`)
|
|
|
|
cmd.Flags().Set("output", "name")
|
2018-03-15 22:32:24 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../test/e2e/testing-manifests/guestbook/frontend-service.yaml")
|
2015-08-06 09:24:02 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
2017-02-21 02:12:13 +00:00
|
|
|
// uses the name from the response
|
2018-02-21 01:14:21 +00:00
|
|
|
if buf.String() != "service/baz\n" {
|
2017-02-21 02:12:13 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 06:00:48 +00:00
|
|
|
func TestPatchNoop(t *testing.T) {
|
|
|
|
_, svc, _ := testData()
|
|
|
|
getObject := &svc.Items[0]
|
|
|
|
patchObject := &svc.Items[0]
|
|
|
|
|
2018-05-24 13:33:36 +00:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2018-08-02 17:29:16 +00:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 17:10:38 +00:00
|
|
|
|
2017-03-02 06:00:48 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces/test/services/frontend" && m == "PATCH":
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, patchObject)}, nil
|
|
|
|
case p == "/namespaces/test/services/frontend" && m == "GET":
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, getObject)}, nil
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patched
|
|
|
|
{
|
2017-08-15 12:13:20 +00:00
|
|
|
patchObject = patchObject.DeepCopy()
|
2017-03-02 06:00:48 +00:00
|
|
|
if patchObject.Annotations == nil {
|
|
|
|
patchObject.Annotations = map[string]string{}
|
|
|
|
}
|
|
|
|
patchObject.Annotations["foo"] = "bar"
|
2018-04-26 13:30:21 +00:00
|
|
|
stream, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdPatch(tf, stream)
|
2017-03-02 06:00:48 +00:00
|
|
|
cmd.Flags().Set("namespace", "test")
|
|
|
|
cmd.Flags().Set("patch", `{"metadata":{"annotations":{"foo":"bar"}}}`)
|
|
|
|
cmd.Run(cmd, []string{"services", "frontend"})
|
2018-04-19 00:02:37 +00:00
|
|
|
if buf.String() != "service/baz patched\n" {
|
2017-03-02 06:00:48 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 02:12:13 +00:00
|
|
|
func TestPatchObjectFromFileOutput(t *testing.T) {
|
|
|
|
_, svc, _ := testData()
|
|
|
|
|
2017-08-15 12:13:20 +00:00
|
|
|
svcCopy := svc.Items[0].DeepCopy()
|
2017-02-21 02:12:13 +00:00
|
|
|
if svcCopy.Labels == nil {
|
|
|
|
svcCopy.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
svcCopy.Labels["post-patch"] = "post-patch-value"
|
|
|
|
|
2018-05-24 13:33:36 +00:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2018-08-02 17:29:16 +00:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 17:10:38 +00:00
|
|
|
|
2017-02-21 02:12:13 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces/test/services/frontend" && m == "GET":
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
|
|
|
|
case p == "/namespaces/test/services/frontend" && m == "PATCH":
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, svcCopy)}, nil
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2018-04-26 13:30:21 +00:00
|
|
|
stream, _, buf, _ := genericclioptions.NewTestIOStreams()
|
2017-02-21 02:12:13 +00:00
|
|
|
|
2018-04-26 13:30:21 +00:00
|
|
|
cmd := NewCmdPatch(tf, stream)
|
2017-02-21 02:12:13 +00:00
|
|
|
cmd.Flags().Set("namespace", "test")
|
|
|
|
cmd.Flags().Set("patch", `{"spec":{"type":"NodePort"}}`)
|
|
|
|
cmd.Flags().Set("output", "yaml")
|
2018-03-15 22:32:24 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../test/e2e/testing-manifests/guestbook/frontend-service.yaml")
|
2017-02-21 02:12:13 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
|
|
t.Log(buf.String())
|
|
|
|
// make sure the value returned by the server is used
|
|
|
|
if !strings.Contains(buf.String(), "post-patch: post-patch-value") {
|
2015-08-06 09:24:02 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|