2014-12-31 23:35:52 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-12-31 23:35:52 +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.
|
|
|
|
*/
|
|
|
|
|
2015-03-26 01:52:12 +00:00
|
|
|
package cmd
|
2014-12-31 23:35:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2017-10-28 01:31:42 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
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"
|
2014-12-31 23:35:52 +00:00
|
|
|
)
|
|
|
|
|
2015-03-24 18:21:52 +00:00
|
|
|
func TestExtraArgsFail(t *testing.T) {
|
2015-12-02 20:20:10 +00:00
|
|
|
initTestErrorHandler(t)
|
2015-03-24 18:21:52 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-09-21 23:41:07 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2015-03-24 18:21:52 +00:00
|
|
|
|
2016-10-18 22:53:26 +00:00
|
|
|
f, _, _, _ := cmdtesting.NewAPIFactory()
|
2016-09-21 23:41:07 +00:00
|
|
|
c := NewCmdCreate(f, buf, errBuf)
|
2017-10-19 15:00:50 +00:00
|
|
|
options := CreateOptions{}
|
|
|
|
if options.ValidateArgs(c, []string{"rc"}) == nil {
|
2015-03-24 18:21:52 +00:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 23:35:52 +00:00
|
|
|
func TestCreateObject(t *testing.T) {
|
2015-12-02 20:20:10 +00:00
|
|
|
initTestErrorHandler(t)
|
2015-02-05 23:20:27 +00:00
|
|
|
_, _, rc := testData()
|
|
|
|
rc.Items[0].Name = "redis-master-controller"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2016-10-18 22:53:26 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 23:35:52 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2017-01-22 18:45:30 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 23:35:52 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2016-08-08 22:32:18 +00:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
|
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
|
2014-12-31 23:35:52 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 23:35:52 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-09-21 23:41:07 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2016-09-21 23:41:07 +00:00
|
|
|
cmd := NewCmdCreate(f, buf, errBuf)
|
2016-03-23 23:03:07 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
|
2015-07-01 22:47:43 +00:00
|
|
|
cmd.Flags().Set("output", "name")
|
2014-12-31 23:35:52 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
|
|
// uses the name from the file, not the response
|
2015-07-01 22:47:43 +00:00
|
|
|
if buf.String() != "replicationcontroller/redis-master-controller\n" {
|
2014-12-31 23:35:52 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateMultipleObject(t *testing.T) {
|
2015-12-02 20:20:10 +00:00
|
|
|
initTestErrorHandler(t)
|
2015-02-05 23:20:27 +00:00
|
|
|
_, svc, rc := testData()
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2016-10-18 22:53:26 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 23:35:52 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2017-01-22 18:45:30 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 23:35:52 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2016-08-08 22:32:18 +00:00
|
|
|
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
|
2014-12-31 23:35:52 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 23:35:52 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-09-21 23:41:07 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2016-09-21 23:41:07 +00:00
|
|
|
cmd := NewCmdCreate(f, buf, errBuf)
|
2016-03-23 23:03:07 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
|
2015-06-22 23:27:44 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.yaml")
|
2015-07-01 22:47:43 +00:00
|
|
|
cmd.Flags().Set("output", "name")
|
2014-12-31 23:35:52 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
2015-02-06 16:57:52 +00:00
|
|
|
// Names should come from the REST response, NOT the files
|
2015-07-01 22:47:43 +00:00
|
|
|
if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
|
2014-12-31 23:35:52 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateDirectory(t *testing.T) {
|
2015-12-02 20:20:10 +00:00
|
|
|
initTestErrorHandler(t)
|
2016-03-23 23:03:07 +00:00
|
|
|
_, _, rc := testData()
|
2015-02-05 23:20:27 +00:00
|
|
|
rc.Items[0].Name = "name"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2016-10-18 22:53:26 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 23:35:52 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2017-01-22 18:45:30 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 23:35:52 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2016-08-08 22:32:18 +00:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
|
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
|
2014-12-31 23:35:52 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 23:35:52 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-09-21 23:41:07 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2016-09-21 23:41:07 +00:00
|
|
|
cmd := NewCmdCreate(f, buf, errBuf)
|
2016-03-23 23:03:07 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/legacy")
|
2015-07-01 22:47:43 +00:00
|
|
|
cmd.Flags().Set("output", "name")
|
2014-12-31 23:35:52 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
2016-03-23 23:03:07 +00:00
|
|
|
if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
|
2014-12-31 23:35:52 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|