2015-10-21 14:41:42 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-10-21 14:41:42 +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
|
2015-10-21 14:41:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2017-10-28 01:31:42 +00:00
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-01-24 15:00:24 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2018-02-21 17:10:38 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2016-10-18 22:53:26 +00:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-04-19 21:43:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2018-02-21 17:10:38 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2015-10-21 14:41:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateNamespace(t *testing.T) {
|
2017-10-28 01:31:42 +00:00
|
|
|
namespaceObject := &v1.Namespace{}
|
2015-10-21 14:41:42 +00:00
|
|
|
namespaceObject.Name = "my-namespace"
|
2018-02-22 14:52:10 +00:00
|
|
|
tf := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2018-05-07 12:32:20 +00:00
|
|
|
codec := legacyscheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
|
2018-02-21 17:10:38 +00:00
|
|
|
ns := legacyscheme.Codecs
|
|
|
|
|
2015-10-21 14:41:42 +00:00
|
|
|
tf.Client = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Version: "v1"},
|
2016-05-04 10:24:16 +00:00
|
|
|
NegotiatedSerializer: ns,
|
2015-10-21 14:41:42 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces" && m == "POST":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 201, Header: defaultHeader(), Body: objBody(codec, namespaceObject)}, nil
|
2015-10-21 14:41:42 +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 := NewCmdCreateNamespace(tf, ioStreams)
|
2015-10-21 14:41:42 +00:00
|
|
|
cmd.Flags().Set("output", "name")
|
|
|
|
cmd.Run(cmd, []string{namespaceObject.Name})
|
2018-02-21 01:14:21 +00:00
|
|
|
expectedOutput := "namespace/" + namespaceObject.Name + "\n"
|
2015-10-21 14:41:42 +00:00
|
|
|
if buf.String() != expectedOutput {
|
2017-01-13 04:54:15 +00:00
|
|
|
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
|
2015-10-21 14:41:42 +00:00
|
|
|
}
|
|
|
|
}
|