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.
|
|
|
|
*/
|
|
|
|
|
2018-04-10 14:21:50 +00:00
|
|
|
package create
|
2014-12-31 23:35:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2017-10-28 01:31:42 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2018-08-21 10:46:39 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions/resource"
|
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-02-21 17:10:38 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2014-12-31 23:35:52 +00:00
|
|
|
)
|
|
|
|
|
2015-03-24 18:21:52 +00:00
|
|
|
func TestExtraArgsFail(t *testing.T) {
|
2018-10-05 12:38:38 +00:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
2015-03-24 18:21:52 +00:00
|
|
|
|
2018-02-22 14:52:10 +00:00
|
|
|
f := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer f.Cleanup()
|
|
|
|
|
2018-04-19 21:43:28 +00:00
|
|
|
c := NewCmdCreate(f, genericclioptions.NewTestIOStreamsDiscard())
|
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) {
|
2018-10-05 12:38:38 +00:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
|
|
|
_, _, rc := cmdtesting.TestData()
|
2015-02-05 23:20:27 +00:00
|
|
|
rc.Items[0].Name = "redis-master-controller"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
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 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2018-10-05 12:38:38 +00:00
|
|
|
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
|
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:
|
2018-10-05 12:38:38 +00:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.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
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:43:28 +00:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdCreate(tf, ioStreams)
|
2018-03-15 22:32:24 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/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
|
2018-02-21 01:14:21 +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) {
|
2018-10-05 12:38:38 +00:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
|
|
|
_, svc, rc := cmdtesting.TestData()
|
2014-12-31 23:35:52 +00:00
|
|
|
|
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 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2018-10-05 12:38:38 +00:00
|
|
|
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
|
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:
|
2018-10-05 12:38:38 +00:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &svc.Items[0])}, nil
|
2016-08-08 22:32:18 +00:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
|
2018-10-05 12:38:38 +00:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.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
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:43:28 +00:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdCreate(tf, ioStreams)
|
2018-03-15 22:32:24 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/legacy/redis-master-controller.yaml")
|
|
|
|
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/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
|
2018-02-21 01:14:21 +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) {
|
2018-10-05 12:38:38 +00:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
|
|
|
_, _, rc := cmdtesting.TestData()
|
2015-02-05 23:20:27 +00:00
|
|
|
rc.Items[0].Name = "name"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
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 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2018-10-05 12:38:38 +00:00
|
|
|
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
|
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:
|
2018-10-05 12:38:38 +00:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.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
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:43:28 +00:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdCreate(tf, ioStreams)
|
2018-03-15 22:32:24 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/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{})
|
|
|
|
|
2018-02-21 01:14:21 +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())
|
|
|
|
}
|
|
|
|
}
|