2014-12-31 18:35:52 -05:00
|
|
|
/*
|
2016-06-02 20:25:58 -04:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-12-31 18:35:52 -05: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 10:21:50 -04:00
|
|
|
package create
|
2014-12-31 18:35:52 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2017-10-27 21:31:42 -04:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2018-08-21 06:46:39 -04:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
2019-03-06 09:54:25 -05:00
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
2017-01-24 10:00:24 -05:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2016-10-18 18:53:26 -04:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-02-21 12:10:38 -05:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2014-12-31 18:35:52 -05:00
|
|
|
)
|
|
|
|
|
|
2015-03-24 14:21:52 -04:00
|
|
|
func TestExtraArgsFail(t *testing.T) {
|
2018-10-05 08:38:38 -04:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
2015-03-24 14:21:52 -04:00
|
|
|
|
2018-02-22 09:52:10 -05:00
|
|
|
f := cmdtesting.NewTestFactory()
|
2018-03-08 17:23:55 -05:00
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
2018-04-19 17:43:28 -04:00
|
|
|
c := NewCmdCreate(f, genericclioptions.NewTestIOStreamsDiscard())
|
2017-10-19 11:00:50 -04:00
|
|
|
options := CreateOptions{}
|
|
|
|
|
if options.ValidateArgs(c, []string{"rc"}) == nil {
|
2015-03-24 14:21:52 -04:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-31 18:35:52 -05:00
|
|
|
func TestCreateObject(t *testing.T) {
|
2018-10-05 08:38:38 -04:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
|
|
|
|
_, _, rc := cmdtesting.TestData()
|
2015-02-05 18:20:27 -05:00
|
|
|
rc.Items[0].Name = "redis-master-controller"
|
2014-12-31 18:35:52 -05:00
|
|
|
|
2018-05-24 09:33:36 -04:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 17:23:55 -05:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
|
2018-08-02 13:29:16 -04:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 12:10:38 -05:00
|
|
|
|
2017-01-22 13:45:30 -05:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-27 21:31:42 -04:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2018-10-05 08:38:38 -04:00
|
|
|
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
|
2015-11-11 14:54:58 -05:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 18:35:52 -05:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2016-08-08 18:32:18 -04:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
|
2018-10-05 08:38:38 -04:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
|
2014-12-31 18:35:52 -05:00
|
|
|
default:
|
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 17:43:28 -04:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
|
cmd := NewCmdCreate(tf, ioStreams)
|
2018-03-15 18:32:24 -04:00
|
|
|
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/legacy/redis-master-controller.yaml")
|
2015-07-01 18:47:43 -04:00
|
|
|
cmd.Flags().Set("output", "name")
|
2014-12-31 18:35:52 -05:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
|
|
|
|
// uses the name from the file, not the response
|
2018-02-20 20:14:21 -05:00
|
|
|
if buf.String() != "replicationcontroller/redis-master-controller\n" {
|
2014-12-31 18:35:52 -05:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreateMultipleObject(t *testing.T) {
|
2018-10-05 08:38:38 -04:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
|
|
|
|
_, svc, rc := cmdtesting.TestData()
|
2014-12-31 18:35:52 -05:00
|
|
|
|
2018-05-24 09:33:36 -04:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 17:23:55 -05:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
|
2018-08-02 13:29:16 -04:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 12:10:38 -05:00
|
|
|
|
2017-01-22 13:45:30 -05:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-27 21:31:42 -04:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2018-10-05 08:38:38 -04:00
|
|
|
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
|
2015-11-11 14:54:58 -05:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 18:35:52 -05:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2016-08-08 18:32:18 -04:00
|
|
|
case p == "/namespaces/test/services" && m == http.MethodPost:
|
2018-10-05 08:38:38 -04:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &svc.Items[0])}, nil
|
2016-08-08 18:32:18 -04:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
|
2018-10-05 08:38:38 -04:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
|
2014-12-31 18:35:52 -05:00
|
|
|
default:
|
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 17:43:28 -04:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
|
cmd := NewCmdCreate(tf, ioStreams)
|
2018-03-15 18:32:24 -04: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 18:47:43 -04:00
|
|
|
cmd.Flags().Set("output", "name")
|
2014-12-31 18:35:52 -05:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
2015-02-06 11:57:52 -05:00
|
|
|
// Names should come from the REST response, NOT the files
|
2018-02-20 20:14:21 -05:00
|
|
|
if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
|
2014-12-31 18:35:52 -05:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreateDirectory(t *testing.T) {
|
2018-10-05 08:38:38 -04:00
|
|
|
cmdtesting.InitTestErrorHandler(t)
|
|
|
|
|
_, _, rc := cmdtesting.TestData()
|
2015-02-05 18:20:27 -05:00
|
|
|
rc.Items[0].Name = "name"
|
2014-12-31 18:35:52 -05:00
|
|
|
|
2018-05-24 09:33:36 -04:00
|
|
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
2018-03-08 17:23:55 -05:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
|
2018-08-02 13:29:16 -04:00
|
|
|
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 12:10:38 -05:00
|
|
|
|
2017-01-22 13:45:30 -05:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-10-27 21:31:42 -04:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2018-10-05 08:38:38 -04:00
|
|
|
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
|
2015-11-11 14:54:58 -05:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 18:35:52 -05:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2016-08-08 18:32:18 -04:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
|
2018-10-05 08:38:38 -04:00
|
|
|
return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
|
2014-12-31 18:35:52 -05:00
|
|
|
default:
|
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 17:43:28 -04:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
|
cmd := NewCmdCreate(tf, ioStreams)
|
2018-03-15 18:32:24 -04:00
|
|
|
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/legacy")
|
2015-07-01 18:47:43 -04:00
|
|
|
cmd.Flags().Set("output", "name")
|
2014-12-31 18:35:52 -05:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
2018-02-20 20:14:21 -05:00
|
|
|
if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
|
2014-12-31 18:35:52 -05:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
|
}
|
|
|
|
|
}
|