12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415 |
- /*
- Copyright 2014 The Kubernetes Authors.
- 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 apply
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "github.com/googleapis/gnostic/OpenAPIv2"
- "github.com/spf13/cobra"
- appsv1 "k8s.io/api/apps/v1"
- corev1 "k8s.io/api/core/v1"
- kubeerr "k8s.io/apimachinery/pkg/api/errors"
- "k8s.io/apimachinery/pkg/api/meta"
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/runtime/schema"
- sptest "k8s.io/apimachinery/pkg/util/strategicpatch/testing"
- "k8s.io/cli-runtime/pkg/genericclioptions"
- "k8s.io/cli-runtime/pkg/resource"
- dynamicfakeclient "k8s.io/client-go/dynamic/fake"
- restclient "k8s.io/client-go/rest"
- "k8s.io/client-go/rest/fake"
- clienttesting "k8s.io/client-go/testing"
- cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
- cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
- "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
- "k8s.io/kubernetes/pkg/kubectl/scheme"
- utilpointer "k8s.io/utils/pointer"
- )
- var (
- fakeSchema = sptest.Fake{Path: filepath.Join("..", "..", "..", "..", "api", "openapi-spec", "swagger.json")}
- testingOpenAPISchemaFns = []func() (openapi.Resources, error){nil, AlwaysErrorOpenAPISchemaFn, openAPISchemaFn}
- AlwaysErrorOpenAPISchemaFn = func() (openapi.Resources, error) {
- return nil, errors.New("cannot get openapi spec")
- }
- openAPISchemaFn = func() (openapi.Resources, error) {
- s, err := fakeSchema.OpenAPISchema()
- if err != nil {
- return nil, err
- }
- return openapi.NewOpenAPIData(s)
- }
- codec = scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
- )
- func TestApplyExtraArgsFail(t *testing.T) {
- f := cmdtesting.NewTestFactory()
- defer f.Cleanup()
- c := NewCmdApply("kubectl", f, genericclioptions.NewTestIOStreamsDiscard())
- if validateApplyArgs(c, []string{"rc"}) == nil {
- t.Fatalf("unexpected non-error")
- }
- }
- func validateApplyArgs(cmd *cobra.Command, args []string) error {
- if len(args) != 0 {
- return cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
- }
- return nil
- }
- const (
- filenameCM = "../../../../test/fixtures/pkg/kubectl/cmd/apply/cm.yaml"
- filenameRC = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc.yaml"
- filenameRCArgs = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc-args.yaml"
- filenameRCLastAppliedArgs = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc-lastapplied-args.yaml"
- filenameRCNoAnnotation = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc-no-annotation.yaml"
- filenameRCLASTAPPLIED = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc-lastapplied.yaml"
- filenameSVC = "../../../../test/fixtures/pkg/kubectl/cmd/apply/service.yaml"
- filenameRCSVC = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc-service.yaml"
- filenameNoExistRC = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc-noexist.yaml"
- filenameRCPatchTest = "../../../../test/fixtures/pkg/kubectl/cmd/apply/patch.json"
- dirName = "../../../../test/fixtures/pkg/kubectl/cmd/apply/testdir"
- filenameRCJSON = "../../../../test/fixtures/pkg/kubectl/cmd/apply/rc.json"
- filenameWidgetClientside = "../../../../test/fixtures/pkg/kubectl/cmd/apply/widget-clientside.yaml"
- filenameWidgetServerside = "../../../../test/fixtures/pkg/kubectl/cmd/apply/widget-serverside.yaml"
- filenameDeployObjServerside = "../../../../test/fixtures/pkg/kubectl/cmd/apply/deploy-serverside.yaml"
- filenameDeployObjClientside = "../../../../test/fixtures/pkg/kubectl/cmd/apply/deploy-clientside.yaml"
- )
- func readConfigMapList(t *testing.T, filename string) [][]byte {
- data := readBytesFromFile(t, filename)
- cmList := corev1.ConfigMapList{}
- if err := runtime.DecodeInto(codec, data, &cmList); err != nil {
- t.Fatal(err)
- }
- var listCmBytes [][]byte
- for _, cm := range cmList.Items {
- cmBytes, err := runtime.Encode(codec, &cm)
- if err != nil {
- t.Fatal(err)
- }
- listCmBytes = append(listCmBytes, cmBytes)
- }
- return listCmBytes
- }
- func readBytesFromFile(t *testing.T, filename string) []byte {
- file, err := os.Open(filename)
- if err != nil {
- t.Fatal(err)
- }
- defer file.Close()
- data, err := ioutil.ReadAll(file)
- if err != nil {
- t.Fatal(err)
- }
- return data
- }
- func readReplicationController(t *testing.T, filenameRC string) (string, []byte) {
- rcObj := readReplicationControllerFromFile(t, filenameRC)
- metaAccessor, err := meta.Accessor(rcObj)
- if err != nil {
- t.Fatal(err)
- }
- rcBytes, err := runtime.Encode(codec, rcObj)
- if err != nil {
- t.Fatal(err)
- }
- return metaAccessor.GetName(), rcBytes
- }
- func readReplicationControllerFromFile(t *testing.T, filename string) *corev1.ReplicationController {
- data := readBytesFromFile(t, filename)
- rc := corev1.ReplicationController{}
- if err := runtime.DecodeInto(codec, data, &rc); err != nil {
- t.Fatal(err)
- }
- return &rc
- }
- func readUnstructuredFromFile(t *testing.T, filename string) *unstructured.Unstructured {
- data := readBytesFromFile(t, filename)
- unst := unstructured.Unstructured{}
- if err := runtime.DecodeInto(codec, data, &unst); err != nil {
- t.Fatal(err)
- }
- return &unst
- }
- func readServiceFromFile(t *testing.T, filename string) *corev1.Service {
- data := readBytesFromFile(t, filename)
- svc := corev1.Service{}
- if err := runtime.DecodeInto(codec, data, &svc); err != nil {
- t.Fatal(err)
- }
- return &svc
- }
- func annotateRuntimeObject(t *testing.T, originalObj, currentObj runtime.Object, kind string) (string, []byte) {
- originalAccessor, err := meta.Accessor(originalObj)
- if err != nil {
- t.Fatal(err)
- }
- // The return value of this function is used in the body of the GET
- // request in the unit tests. Here we are adding a misc label to the object.
- // In tests, the validatePatchApplication() gets called in PATCH request
- // handler in fake round tripper. validatePatchApplication call
- // checks that this DELETE_ME label was deleted by the apply implementation in
- // kubectl.
- originalLabels := originalAccessor.GetLabels()
- originalLabels["DELETE_ME"] = "DELETE_ME"
- originalAccessor.SetLabels(originalLabels)
- original, err := runtime.Encode(unstructured.JSONFallbackEncoder{Encoder: codec}, originalObj)
- if err != nil {
- t.Fatal(err)
- }
- currentAccessor, err := meta.Accessor(currentObj)
- if err != nil {
- t.Fatal(err)
- }
- currentAnnotations := currentAccessor.GetAnnotations()
- if currentAnnotations == nil {
- currentAnnotations = make(map[string]string)
- }
- currentAnnotations[corev1.LastAppliedConfigAnnotation] = string(original)
- currentAccessor.SetAnnotations(currentAnnotations)
- current, err := runtime.Encode(unstructured.JSONFallbackEncoder{Encoder: codec}, currentObj)
- if err != nil {
- t.Fatal(err)
- }
- return currentAccessor.GetName(), current
- }
- func readAndAnnotateReplicationController(t *testing.T, filename string) (string, []byte) {
- rc1 := readReplicationControllerFromFile(t, filename)
- rc2 := readReplicationControllerFromFile(t, filename)
- return annotateRuntimeObject(t, rc1, rc2, "ReplicationController")
- }
- func readAndAnnotateService(t *testing.T, filename string) (string, []byte) {
- svc1 := readServiceFromFile(t, filename)
- svc2 := readServiceFromFile(t, filename)
- return annotateRuntimeObject(t, svc1, svc2, "Service")
- }
- func readAndAnnotateUnstructured(t *testing.T, filename string) (string, []byte) {
- obj1 := readUnstructuredFromFile(t, filename)
- obj2 := readUnstructuredFromFile(t, filename)
- return annotateRuntimeObject(t, obj1, obj2, "Widget")
- }
- func validatePatchApplication(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[corev1.LastAppliedConfigAnnotation]; !ok {
- t.Fatalf("patch does not contain annotation:\n%s\n", patch)
- }
- labelMap := walkMapPath(t, patchMap, []string{"metadata", "labels"})
- if deleteMe, ok := labelMap["DELETE_ME"]; !ok || deleteMe != nil {
- t.Fatalf("patch does not remove deleted key: DELETE_ME:\n%s\n", patch)
- }
- }
- func walkMapPath(t *testing.T, start map[string]interface{}, path []string) map[string]interface{} {
- finish := start
- for i := 0; i < len(path); i++ {
- var ok bool
- finish, ok = finish[path[i]].(map[string]interface{})
- if !ok {
- t.Fatalf("key:%s of path:%v not found in map:%v", path[i], path, start)
- }
- }
- return finish
- }
- func TestRunApplyPrintsValidObjectList(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- configMapList := readConfigMapList(t, filenameCM)
- pathCM := "/namespaces/test/configmaps"
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case strings.HasPrefix(p, pathCM) && m == "GET":
- fallthrough
- case strings.HasPrefix(p, pathCM) && m == "PATCH":
- var body io.ReadCloser
- switch p {
- case pathCM + "/test0":
- body = ioutil.NopCloser(bytes.NewReader(configMapList[0]))
- case pathCM + "/test1":
- body = ioutil.NopCloser(bytes.NewReader(configMapList[1]))
- default:
- t.Errorf("unexpected request to %s", p)
- }
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: body}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameCM)
- cmd.Flags().Set("output", "json")
- cmd.Flags().Set("dry-run", "true")
- cmd.Run(cmd, []string{})
- // ensure that returned list can be unmarshaled back into a configmap list
- cmList := corev1.List{}
- if err := runtime.DecodeInto(codec, buf.Bytes(), &cmList); err != nil {
- t.Fatal(err)
- }
- if len(cmList.Items) != 2 {
- t.Fatalf("Expected 2 items in the result; got %d", len(cmList.Items))
- }
- if !strings.Contains(string(cmList.Items[0].Raw), "key1") {
- t.Fatalf("Did not get first ConfigMap at the first position")
- }
- if !strings.Contains(string(cmList.Items[1].Raw), "key2") {
- t.Fatalf("Did not get second ConfigMap at the second position")
- }
- }
- func TestRunApplyViewLastApplied(t *testing.T) {
- _, rcBytesWithConfig := readReplicationController(t, filenameRCLASTAPPLIED)
- _, rcBytesWithArgs := readReplicationController(t, filenameRCLastAppliedArgs)
- nameRC, rcBytes := readReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- tests := []struct {
- name, nameRC, pathRC, filePath, outputFormat, expectedErr, expectedOut, selector string
- args []string
- respBytes []byte
- }{
- {
- name: "view with file",
- filePath: filenameRC,
- outputFormat: "",
- expectedErr: "",
- expectedOut: "test: 1234\n",
- selector: "",
- args: []string{},
- respBytes: rcBytesWithConfig,
- },
- {
- name: "test with file include `%s` in arguments",
- filePath: filenameRCArgs,
- outputFormat: "",
- expectedErr: "",
- expectedOut: "args: -random_flag=%s@domain.com\n",
- selector: "",
- args: []string{},
- respBytes: rcBytesWithArgs,
- },
- {
- name: "view with file json format",
- filePath: filenameRC,
- outputFormat: "json",
- expectedErr: "",
- expectedOut: "{\n \"test\": 1234\n}\n",
- selector: "",
- args: []string{},
- respBytes: rcBytesWithConfig,
- },
- {
- name: "view resource/name invalid format",
- filePath: "",
- outputFormat: "wide",
- expectedErr: "error: Unexpected -o output mode: wide, the flag 'output' must be one of yaml|json\nSee 'view-last-applied -h' for help and examples",
- expectedOut: "",
- selector: "",
- args: []string{"replicationcontroller", "test-rc"},
- respBytes: rcBytesWithConfig,
- },
- {
- name: "view resource with label",
- filePath: "",
- outputFormat: "",
- expectedErr: "",
- expectedOut: "test: 1234\n",
- selector: "name=test-rc",
- args: []string{"replicationcontroller"},
- respBytes: rcBytesWithConfig,
- },
- {
- name: "view resource without annotations",
- filePath: "",
- outputFormat: "",
- expectedErr: "error: no last-applied-configuration annotation found on resource: test-rc",
- expectedOut: "",
- selector: "",
- args: []string{"replicationcontroller", "test-rc"},
- respBytes: rcBytes,
- },
- {
- name: "view resource no match",
- filePath: "",
- outputFormat: "",
- expectedErr: "Error from server (NotFound): the server could not find the requested resource (get replicationcontrollers no-match)",
- expectedOut: "",
- selector: "",
- args: []string{"replicationcontroller", "no-match"},
- respBytes: nil,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- GroupVersion: schema.GroupVersion{Version: "v1"},
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(test.respBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == "/namespaces/test/replicationcontrollers" && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(test.respBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == "/namespaces/test/replicationcontrollers/no-match" && m == "GET":
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Pod{})}, nil
- case p == "/api/v1/namespaces/test" && m == "GET":
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Namespace{})}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- cmdutil.BehaviorOnFatal(func(str string, code int) {
- if str != test.expectedErr {
- t.Errorf("%s: unexpected error: %s\nexpected: %s", test.name, str, test.expectedErr)
- }
- })
- ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApplyViewLastApplied(tf, ioStreams)
- if test.filePath != "" {
- cmd.Flags().Set("filename", test.filePath)
- }
- if test.outputFormat != "" {
- cmd.Flags().Set("output", test.outputFormat)
- }
- if test.selector != "" {
- cmd.Flags().Set("selector", test.selector)
- }
- cmd.Run(cmd, test.args)
- if buf.String() != test.expectedOut {
- t.Fatalf("%s: unexpected output: %s\nexpected: %s", test.name, buf.String(), test.expectedOut)
- }
- })
- }
- }
- func TestApplyObjectWithoutAnnotation(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- nameRC, rcBytes := readReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(rcBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathRC && m == "PATCH":
- bodyRC := ioutil.NopCloser(bytes.NewReader(rcBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- // uses the name from the file, not the response
- expectRC := "replicationcontroller/" + nameRC + "\n"
- expectWarning := fmt.Sprintf(warningNoLastAppliedConfigAnnotation, "kubectl")
- if errBuf.String() != expectWarning {
- t.Fatalf("unexpected non-warning: %s\nexpected: %s", errBuf.String(), expectWarning)
- }
- if buf.String() != expectRC {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
- }
- }
- func TestApplyObject(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply when a local object is specified", func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathRC && m == "PATCH":
- validatePatchApplication(t, req)
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- // uses the name from the file, not the response
- expectRC := "replicationcontroller/" + nameRC + "\n"
- if buf.String() != expectRC {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- })
- }
- }
- func TestApplyObjectOutput(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- // Add some extra data to the post-patch object
- postPatchObj := &unstructured.Unstructured{}
- if err := json.Unmarshal(currentRC, &postPatchObj.Object); err != nil {
- t.Fatal(err)
- }
- postPatchLabels := postPatchObj.GetLabels()
- if postPatchLabels == nil {
- postPatchLabels = map[string]string{}
- }
- postPatchLabels["post-patch"] = "value"
- postPatchObj.SetLabels(postPatchLabels)
- postPatchData, err := json.Marshal(postPatchObj)
- if err != nil {
- t.Fatal(err)
- }
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply returns correct output", func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathRC && m == "PATCH":
- validatePatchApplication(t, req)
- bodyRC := ioutil.NopCloser(bytes.NewReader(postPatchData))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "yaml")
- cmd.Run(cmd, []string{})
- if !strings.Contains(buf.String(), "test-rc") {
- t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "test-rc")
- }
- if !strings.Contains(buf.String(), "post-patch: value") {
- t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "post-patch: value")
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- })
- }
- }
- func TestApplyRetry(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply retries on conflict error", func(t *testing.T) {
- firstPatch := true
- retry := false
- getCount := 0
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- getCount++
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathRC && m == "PATCH":
- if firstPatch {
- firstPatch = false
- statusErr := kubeerr.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first"))
- bodyBytes, _ := json.Marshal(statusErr)
- bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes))
- return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil
- }
- retry = true
- validatePatchApplication(t, req)
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- if !retry || getCount != 2 {
- t.Fatalf("apply didn't retry when get conflict error")
- }
- // uses the name from the file, not the response
- expectRC := "replicationcontroller/" + nameRC + "\n"
- if buf.String() != expectRC {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- })
- }
- }
- func TestApplyNonExistObject(t *testing.T) {
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers"
- pathNameRC := pathRC + "/" + nameRC
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == "/api/v1/namespaces/test" && m == "GET":
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
- case p == pathNameRC && m == "GET":
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
- case p == pathRC && m == "POST":
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 201, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- // uses the name from the file, not the response
- expectRC := "replicationcontroller/" + nameRC + "\n"
- if buf.String() != expectRC {
- t.Errorf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
- }
- }
- func TestApplyEmptyPatch(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- nameRC, _ := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers"
- pathNameRC := pathRC + "/" + nameRC
- verifyPost := false
- var body []byte
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- GroupVersion: schema.GroupVersion{Version: "v1"},
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == "/api/v1/namespaces/test" && m == "GET":
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
- case p == pathNameRC && m == "GET":
- if body == nil {
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
- }
- bodyRC := ioutil.NopCloser(bytes.NewReader(body))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathRC && m == "POST":
- body, _ = ioutil.ReadAll(req.Body)
- verifyPost = true
- bodyRC := ioutil.NopCloser(bytes.NewReader(body))
- return &http.Response{StatusCode: 201, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- // 1. apply non exist object
- ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- expectRC := "replicationcontroller/" + nameRC + "\n"
- if buf.String() != expectRC {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
- }
- if !verifyPost {
- t.Fatal("No server-side post call detected")
- }
- // 2. test apply already exist object, will not send empty patch request
- ioStreams, _, buf, _ = genericclioptions.NewTestIOStreams()
- cmd = NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- if buf.String() != expectRC {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
- }
- }
- func TestApplyMultipleObjectsAsList(t *testing.T) {
- testApplyMultipleObjects(t, true)
- }
- func TestApplyMultipleObjectsAsFiles(t *testing.T) {
- testApplyMultipleObjects(t, false)
- }
- func testApplyMultipleObjects(t *testing.T, asList bool) {
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- nameSVC, currentSVC := readAndAnnotateService(t, filenameSVC)
- pathSVC := "/namespaces/test/services/" + nameSVC
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply on multiple objects", func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathRC && m == "PATCH":
- validatePatchApplication(t, req)
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == pathSVC && m == "GET":
- bodySVC := ioutil.NopCloser(bytes.NewReader(currentSVC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodySVC}, nil
- case p == pathSVC && m == "PATCH":
- validatePatchApplication(t, req)
- bodySVC := ioutil.NopCloser(bytes.NewReader(currentSVC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodySVC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- if asList {
- cmd.Flags().Set("filename", filenameRCSVC)
- } else {
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("filename", filenameSVC)
- }
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- // Names should come from the REST response, NOT the files
- expectRC := "replicationcontroller/" + nameRC + "\n"
- expectSVC := "service/" + nameSVC + "\n"
- // Test both possible orders since output is non-deterministic.
- expectOne := expectRC + expectSVC
- expectTwo := expectSVC + expectRC
- if buf.String() != expectOne && buf.String() != expectTwo {
- t.Fatalf("unexpected output: %s\nexpected: %s OR %s", buf.String(), expectOne, expectTwo)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- })
- }
- }
- func readDeploymentFromFile(t *testing.T, file string) []byte {
- raw := readBytesFromFile(t, file)
- obj := &appsv1.Deployment{}
- if err := runtime.DecodeInto(codec, raw, obj); err != nil {
- t.Fatal(err)
- }
- objJSON, err := runtime.Encode(codec, obj)
- if err != nil {
- t.Fatal(err)
- }
- return objJSON
- }
- func TestApplyNULLPreservation(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- deploymentName := "nginx-deployment"
- deploymentPath := "/namespaces/test/deployments/" + deploymentName
- verifiedPatch := false
- deploymentBytes := readDeploymentFromFile(t, filenameDeployObjServerside)
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply preserves NULL fields", func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == deploymentPath && m == "GET":
- body := ioutil.NopCloser(bytes.NewReader(deploymentBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: body}, nil
- case p == deploymentPath && m == "PATCH":
- 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)
- }
- annotationMap := walkMapPath(t, patchMap, []string{"metadata", "annotations"})
- if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok {
- t.Fatalf("patch does not contain annotation:\n%s\n", patch)
- }
- strategy := walkMapPath(t, patchMap, []string{"spec", "strategy"})
- if value, ok := strategy["rollingUpdate"]; !ok || value != nil {
- t.Fatalf("patch did not retain null value in key: rollingUpdate:\n%s\n", patch)
- }
- verifiedPatch = true
- // 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.
- body := ioutil.NopCloser(bytes.NewReader(deploymentBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: body}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameDeployObjClientside)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- expected := "deployment.apps/" + deploymentName + "\n"
- if buf.String() != expected {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- if !verifiedPatch {
- t.Fatal("No server-side patch call detected")
- }
- })
- }
- }
- // TestUnstructuredApply checks apply operations on an unstructured object
- func TestUnstructuredApply(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- name, curr := readAndAnnotateUnstructured(t, filenameWidgetClientside)
- path := "/namespaces/test/widgets/" + name
- verifiedPatch := false
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply works correctly with unstructured objects", func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == path && m == "GET":
- body := ioutil.NopCloser(bytes.NewReader(curr))
- return &http.Response{
- StatusCode: 200,
- Header: cmdtesting.DefaultHeader(),
- Body: body}, nil
- case p == path && m == "PATCH":
- contentType := req.Header.Get("Content-Type")
- if contentType != "application/merge-patch+json" {
- t.Fatalf("Unexpected Content-Type: %s", contentType)
- }
- validatePatchApplication(t, req)
- verifiedPatch = true
- body := ioutil.NopCloser(bytes.NewReader(curr))
- return &http.Response{
- StatusCode: 200,
- Header: cmdtesting.DefaultHeader(),
- Body: body}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameWidgetClientside)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- expected := "widget.unit-test.test.com/" + name + "\n"
- if buf.String() != expected {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- if !verifiedPatch {
- t.Fatal("No server-side patch call detected")
- }
- })
- }
- }
- // TestUnstructuredIdempotentApply checks repeated apply operation on an unstructured object
- func TestUnstructuredIdempotentApply(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- serversideObject := readUnstructuredFromFile(t, filenameWidgetServerside)
- serversideData, err := runtime.Encode(unstructured.JSONFallbackEncoder{Encoder: codec}, serversideObject)
- if err != nil {
- t.Fatal(err)
- }
- path := "/namespaces/test/widgets/widget"
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test repeated apply operations on an unstructured object", func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == path && m == "GET":
- body := ioutil.NopCloser(bytes.NewReader(serversideData))
- return &http.Response{
- StatusCode: 200,
- Header: cmdtesting.DefaultHeader(),
- Body: body}, nil
- case p == path && m == "PATCH":
- // In idempotent updates, kubectl will resolve to an empty patch and not send anything to the server
- // Thus, if we reach this branch, kubectl is unnecessarily sending a patch.
- patch, err := ioutil.ReadAll(req.Body)
- if err != nil {
- t.Fatal(err)
- }
- t.Fatalf("Unexpected Patch: %s", patch)
- return nil, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.OpenAPISchemaFunc = fn
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameWidgetClientside)
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- expected := "widget.unit-test.test.com/widget\n"
- if buf.String() != expected {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- })
- }
- }
- func TestRunApplySetLastApplied(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- noExistRC, _ := readAndAnnotateReplicationController(t, filenameNoExistRC)
- noExistPath := "/namespaces/test/replicationcontrollers/" + noExistRC
- noAnnotationName, noAnnotationRC := readReplicationController(t, filenameRCNoAnnotation)
- noAnnotationPath := "/namespaces/test/replicationcontrollers/" + noAnnotationName
- tests := []struct {
- name, nameRC, pathRC, filePath, expectedErr, expectedOut, output string
- }{
- {
- name: "set with exist object",
- filePath: filenameRC,
- expectedErr: "",
- expectedOut: "replicationcontroller/test-rc\n",
- output: "name",
- },
- {
- name: "set with no-exist object",
- filePath: filenameNoExistRC,
- expectedErr: "Error from server (NotFound): the server could not find the requested resource (get replicationcontrollers no-exist)",
- expectedOut: "",
- output: "name",
- },
- {
- name: "set for the annotation does not exist on the live object",
- filePath: filenameRCNoAnnotation,
- expectedErr: "error: no last-applied-configuration annotation found on resource: no-annotation, to create the annotation, run the command with --create-annotation",
- expectedOut: "",
- output: "name",
- },
- {
- name: "set with exist object output json",
- filePath: filenameRCJSON,
- expectedErr: "",
- expectedOut: "replicationcontroller/test-rc\n",
- output: "name",
- },
- {
- name: "set test for a directory of files",
- filePath: dirName,
- expectedErr: "",
- expectedOut: "replicationcontroller/test-rc\nreplicationcontroller/test-rc\n",
- output: "name",
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.UnstructuredClient = &fake.RESTClient{
- GroupVersion: schema.GroupVersion{Version: "v1"},
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == pathRC && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == noAnnotationPath && m == "GET":
- bodyRC := ioutil.NopCloser(bytes.NewReader(noAnnotationRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == noExistPath && m == "GET":
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Pod{})}, nil
- case p == pathRC && m == "PATCH":
- checkPatchString(t, req)
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case p == "/api/v1/namespaces/test" && m == "GET":
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Namespace{})}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- cmdutil.BehaviorOnFatal(func(str string, code int) {
- if str != test.expectedErr {
- t.Errorf("%s: unexpected error: %s\nexpected: %s", test.name, str, test.expectedErr)
- }
- })
- ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApplySetLastApplied(tf, ioStreams)
- cmd.Flags().Set("filename", test.filePath)
- cmd.Flags().Set("output", test.output)
- cmd.Run(cmd, []string{})
- if buf.String() != test.expectedOut {
- t.Fatalf("%s: unexpected output: %s\nexpected: %s", test.name, buf.String(), test.expectedOut)
- }
- })
- }
- cmdutil.BehaviorOnFatal(func(str string, code int) {})
- }
- func checkPatchString(t *testing.T, req *http.Request) {
- checkString := string(readBytesFromFile(t, filenameRCPatchTest))
- 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[corev1.LastAppliedConfigAnnotation]; !ok {
- t.Fatalf("patch does not contain annotation:\n%s\n", patch)
- }
- resultString := annotationsMap["kubectl.kubernetes.io/last-applied-configuration"]
- if resultString != checkString {
- t.Fatalf("patch annotation is not correct, expect:%s\n but got:%s\n", checkString, resultString)
- }
- }
- func TestForceApply(t *testing.T) {
- cmdtesting.InitTestErrorHandler(t)
- scheme := runtime.NewScheme()
- nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
- pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
- pathRCList := "/namespaces/test/replicationcontrollers"
- expected := map[string]int{
- "getOk": 6,
- "getNotFound": 1,
- "getList": 0,
- "patch": 6,
- "delete": 1,
- "post": 1,
- }
- for _, fn := range testingOpenAPISchemaFns {
- t.Run("test apply with --force", func(t *testing.T) {
- deleted := false
- isScaledDownToZero := false
- counts := map[string]int{}
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
- tf.UnstructuredClient = &fake.RESTClient{
- NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case strings.HasSuffix(p, pathRC) && m == "GET":
- if deleted {
- counts["getNotFound"]++
- return &http.Response{StatusCode: 404, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte{}))}, nil
- }
- counts["getOk"]++
- var bodyRC io.ReadCloser
- if isScaledDownToZero {
- rcObj := readReplicationControllerFromFile(t, filenameRC)
- rcObj.Spec.Replicas = utilpointer.Int32Ptr(0)
- rcBytes, err := runtime.Encode(codec, rcObj)
- if err != nil {
- t.Fatal(err)
- }
- bodyRC = ioutil.NopCloser(bytes.NewReader(rcBytes))
- } else {
- bodyRC = ioutil.NopCloser(bytes.NewReader(currentRC))
- }
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case strings.HasSuffix(p, pathRCList) && m == "GET":
- counts["getList"]++
- rcObj := readUnstructuredFromFile(t, filenameRC)
- list := &unstructured.UnstructuredList{
- Object: map[string]interface{}{
- "apiVersion": "v1",
- "kind": "ReplicationControllerList",
- },
- Items: []unstructured.Unstructured{*rcObj},
- }
- listBytes, err := runtime.Encode(codec, list)
- if err != nil {
- t.Fatal(err)
- }
- bodyRCList := ioutil.NopCloser(bytes.NewReader(listBytes))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRCList}, nil
- case strings.HasSuffix(p, pathRC) && m == "PATCH":
- counts["patch"]++
- if counts["patch"] <= 6 {
- statusErr := kubeerr.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first"))
- bodyBytes, _ := json.Marshal(statusErr)
- bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes))
- return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil
- }
- t.Fatalf("unexpected request: %#v after %v tries\n%#v", req.URL, counts["patch"], req)
- return nil, nil
- case strings.HasSuffix(p, pathRC) && m == "PUT":
- counts["put"]++
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- isScaledDownToZero = true
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- case strings.HasSuffix(p, pathRCList) && m == "POST":
- counts["post"]++
- deleted = false
- isScaledDownToZero = false
- bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
- return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- fakeDynamicClient := dynamicfakeclient.NewSimpleDynamicClient(scheme)
- fakeDynamicClient.PrependReactor("delete", "replicationcontrollers", func(action clienttesting.Action) (bool, runtime.Object, error) {
- if deleteAction, ok := action.(clienttesting.DeleteAction); ok {
- if deleteAction.GetName() == nameRC {
- counts["delete"]++
- deleted = true
- return true, nil, nil
- }
- }
- return false, nil, nil
- })
- tf.FakeDynamicClient = fakeDynamicClient
- tf.OpenAPISchemaFunc = fn
- tf.Client = tf.UnstructuredClient
- tf.ClientConfigVal = &restclient.Config{}
- ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
- cmd := NewCmdApply("kubectl", tf, ioStreams)
- cmd.Flags().Set("filename", filenameRC)
- cmd.Flags().Set("output", "name")
- cmd.Flags().Set("force", "true")
- cmd.Run(cmd, []string{})
- for method, exp := range expected {
- if exp != counts[method] {
- t.Errorf("Unexpected amount of %q API calls, wanted %v got %v", method, exp, counts[method])
- }
- }
- if expected := "replicationcontroller/" + nameRC + "\n"; buf.String() != expected {
- t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
- }
- if errBuf.String() != "" {
- t.Fatalf("unexpected error output: %s", errBuf.String())
- }
- })
- }
- }
- func TestDryRunVerifier(t *testing.T) {
- dryRunVerifier := DryRunVerifier{
- Finder: cmdutil.NewCRDFinder(func() ([]schema.GroupKind, error) {
- return []schema.GroupKind{
- {
- Group: "crd.com",
- Kind: "MyCRD",
- },
- {
- Group: "crd.com",
- Kind: "MyNewCRD",
- },
- }, nil
- }),
- OpenAPIGetter: &fakeSchema,
- }
- err := dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "NodeProxyOptions"})
- if err == nil {
- t.Fatalf("NodeProxyOptions doesn't support dry-run, yet no error found")
- }
- err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
- if err != nil {
- t.Fatalf("Pod should support dry-run: %v", err)
- }
- err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
- if err != nil {
- t.Fatalf("MyCRD should support dry-run: %v", err)
- }
- err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "Random"})
- if err == nil {
- t.Fatalf("Random doesn't support dry-run, yet no error found")
- }
- }
- type EmptyOpenAPI struct{}
- func (EmptyOpenAPI) OpenAPISchema() (*openapi_v2.Document, error) {
- return &openapi_v2.Document{}, nil
- }
- func TestDryRunVerifierNoOpenAPI(t *testing.T) {
- dryRunVerifier := DryRunVerifier{
- Finder: cmdutil.NewCRDFinder(func() ([]schema.GroupKind, error) {
- return []schema.GroupKind{
- {
- Group: "crd.com",
- Kind: "MyCRD",
- },
- {
- Group: "crd.com",
- Kind: "MyNewCRD",
- },
- }, nil
- }),
- OpenAPIGetter: EmptyOpenAPI{},
- }
- err := dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
- if err == nil {
- t.Fatalf("Pod doesn't support dry-run, yet no error found")
- }
- err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
- if err == nil {
- t.Fatalf("MyCRD doesn't support dry-run, yet no error found")
- }
- }
|