...

Source file src/k8s.io/kubectl/pkg/cmd/create/create_test.go

Documentation: k8s.io/kubectl/pkg/cmd/create

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package create
    18  
    19  import (
    20  	"net/http"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  	"k8s.io/cli-runtime/pkg/genericiooptions"
    25  	"k8s.io/cli-runtime/pkg/resource"
    26  	"k8s.io/client-go/rest/fake"
    27  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    28  	"k8s.io/kubectl/pkg/scheme"
    29  )
    30  
    31  func TestExtraArgsFail(t *testing.T) {
    32  	cmdtesting.InitTestErrorHandler(t)
    33  
    34  	f := cmdtesting.NewTestFactory()
    35  	defer f.Cleanup()
    36  
    37  	c := NewCmdCreate(f, genericiooptions.NewTestIOStreamsDiscard())
    38  	ioStreams, _, _, _ := genericiooptions.NewTestIOStreams()
    39  	options := NewCreateOptions(ioStreams)
    40  	if options.Complete(f, c, []string{"rc"}) == nil {
    41  		t.Errorf("unexpected non-error")
    42  	}
    43  }
    44  
    45  func TestCreateObject(t *testing.T) {
    46  	cmdtesting.InitTestErrorHandler(t)
    47  	_, _, rc := cmdtesting.TestData()
    48  	rc.Items[0].Name = "redis-master-controller"
    49  
    50  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
    51  	defer tf.Cleanup()
    52  
    53  	codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    54  
    55  	tf.UnstructuredClient = &fake.RESTClient{
    56  		GroupVersion:         schema.GroupVersion{Version: "v1"},
    57  		NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    58  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    59  			switch p, m := req.URL.Path, req.Method; {
    60  			case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
    61  				return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
    62  			default:
    63  				t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
    64  				return nil, nil
    65  			}
    66  		}),
    67  	}
    68  
    69  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
    70  	cmd := NewCmdCreate(tf, ioStreams)
    71  	cmd.Flags().Set("filename", "../../../testdata/redis-master-controller.yaml")
    72  	cmd.Flags().Set("output", "name")
    73  	cmd.Run(cmd, []string{})
    74  
    75  	// uses the name from the file, not the response
    76  	if buf.String() != "replicationcontroller/redis-master-controller\n" {
    77  		t.Errorf("unexpected output: %s", buf.String())
    78  	}
    79  }
    80  
    81  func TestCreateMultipleObject(t *testing.T) {
    82  	cmdtesting.InitTestErrorHandler(t)
    83  	_, svc, rc := cmdtesting.TestData()
    84  
    85  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
    86  	defer tf.Cleanup()
    87  
    88  	codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    89  
    90  	tf.UnstructuredClient = &fake.RESTClient{
    91  		GroupVersion:         schema.GroupVersion{Version: "v1"},
    92  		NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    93  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    94  			switch p, m := req.URL.Path, req.Method; {
    95  			case p == "/namespaces/test/services" && m == http.MethodPost:
    96  				return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &svc.Items[0])}, nil
    97  			case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
    98  				return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
    99  			default:
   100  				t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
   101  				return nil, nil
   102  			}
   103  		}),
   104  	}
   105  
   106  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
   107  	cmd := NewCmdCreate(tf, ioStreams)
   108  	cmd.Flags().Set("filename", "../../../testdata/redis-master-controller.yaml")
   109  	cmd.Flags().Set("filename", "../../../testdata/frontend-service.yaml")
   110  	cmd.Flags().Set("output", "name")
   111  	cmd.Run(cmd, []string{})
   112  
   113  	// Names should come from the REST response, NOT the files
   114  	if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
   115  		t.Errorf("unexpected output: %s", buf.String())
   116  	}
   117  }
   118  
   119  func TestCreateDirectory(t *testing.T) {
   120  	cmdtesting.InitTestErrorHandler(t)
   121  	_, _, rc := cmdtesting.TestData()
   122  	rc.Items[0].Name = "name"
   123  
   124  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
   125  	defer tf.Cleanup()
   126  
   127  	codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
   128  
   129  	tf.UnstructuredClient = &fake.RESTClient{
   130  		GroupVersion:         schema.GroupVersion{Version: "v1"},
   131  		NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
   132  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
   133  			switch p, m := req.URL.Path, req.Method; {
   134  			case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
   135  				return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
   136  			default:
   137  				t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
   138  				return nil, nil
   139  			}
   140  		}),
   141  	}
   142  
   143  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
   144  	cmd := NewCmdCreate(tf, ioStreams)
   145  	cmd.Flags().Set("filename", "../../../testdata/replace/legacy")
   146  	cmd.Flags().Set("output", "name")
   147  	cmd.Run(cmd, []string{})
   148  
   149  	if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
   150  		t.Errorf("unexpected output: %s", buf.String())
   151  	}
   152  }
   153  

View as plain text