123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /*
- 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 cmd
- import (
- "bytes"
- "net/http"
- "testing"
- "k8s.io/kubernetes/pkg/client/typed/dynamic"
- "k8s.io/kubernetes/pkg/client/unversioned/fake"
- )
- func TestExtraArgsFail(t *testing.T) {
- initTestErrorHandler(t)
- buf := bytes.NewBuffer([]byte{})
- f, _, _, _ := NewAPIFactory()
- c := NewCmdCreate(f, buf)
- if ValidateArgs(c, []string{"rc"}) == nil {
- t.Errorf("unexpected non-error")
- }
- }
- func TestCreateObject(t *testing.T) {
- initTestErrorHandler(t)
- _, _, rc := testData()
- rc.Items[0].Name = "redis-master-controller"
- f, tf, codec, _ := NewAPIFactory()
- ns := dynamic.ContentConfig().NegotiatedSerializer
- tf.Printer = &testPrinter{}
- tf.Client = &fake.RESTClient{
- NegotiatedSerializer: ns,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
- return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.Namespace = "test"
- buf := bytes.NewBuffer([]byte{})
- cmd := NewCmdCreate(f, buf)
- cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- // uses the name from the file, not the response
- if buf.String() != "replicationcontroller/redis-master-controller\n" {
- t.Errorf("unexpected output: %s", buf.String())
- }
- }
- func TestCreateMultipleObject(t *testing.T) {
- initTestErrorHandler(t)
- _, svc, rc := testData()
- f, tf, codec, _ := NewAPIFactory()
- ns := dynamic.ContentConfig().NegotiatedSerializer
- tf.Printer = &testPrinter{}
- tf.Client = &fake.RESTClient{
- NegotiatedSerializer: ns,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == "/namespaces/test/services" && m == http.MethodPost:
- return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
- case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
- return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.Namespace = "test"
- buf := bytes.NewBuffer([]byte{})
- cmd := NewCmdCreate(f, buf)
- cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
- cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.yaml")
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- // Names should come from the REST response, NOT the files
- if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
- t.Errorf("unexpected output: %s", buf.String())
- }
- }
- func TestCreateDirectory(t *testing.T) {
- initTestErrorHandler(t)
- _, _, rc := testData()
- rc.Items[0].Name = "name"
- f, tf, codec, _ := NewAPIFactory()
- ns := dynamic.ContentConfig().NegotiatedSerializer
- tf.Printer = &testPrinter{}
- tf.Client = &fake.RESTClient{
- NegotiatedSerializer: ns,
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
- return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- tf.Namespace = "test"
- buf := bytes.NewBuffer([]byte{})
- cmd := NewCmdCreate(f, buf)
- cmd.Flags().Set("filename", "../../../examples/guestbook/legacy")
- cmd.Flags().Set("output", "name")
- cmd.Run(cmd, []string{})
- if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
- t.Errorf("unexpected output: %s", buf.String())
- }
- }
|