2014-12-31 23:35:52 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2015-03-24 18:21:52 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
|
2014-12-31 23:35:52 +00:00
|
|
|
)
|
|
|
|
|
2015-03-24 18:21:52 +00:00
|
|
|
func TestExtraArgsFail(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
f, _, _ := NewAPIFactory()
|
|
|
|
c := f.NewCmdCreate(buf)
|
|
|
|
if cmd.ValidateArgs(c, []string{"rc"}) == nil {
|
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 23:35:52 +00:00
|
|
|
func TestCreateObject(t *testing.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
|
|
|
|
|
|
|
f, tf, codec := NewAPIFactory()
|
|
|
|
tf.Printer = &testPrinter{}
|
|
|
|
tf.Client = &client.FakeRESTClient{
|
|
|
|
Codec: codec,
|
|
|
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2015-02-05 23:20:27 +00:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == "POST":
|
|
|
|
return &http.Response{StatusCode: 201, 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{})
|
|
|
|
|
|
|
|
cmd := f.NewCmdCreate(buf)
|
2015-02-05 23:20:27 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master-controller.json")
|
2014-12-31 23:35:52 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
|
|
// uses the name from the file, not the response
|
2015-02-05 23:20:27 +00:00
|
|
|
if buf.String() != "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-02-05 23:20:27 +00:00
|
|
|
_, svc, rc := testData()
|
2014-12-31 23:35:52 +00:00
|
|
|
|
|
|
|
f, tf, codec := NewAPIFactory()
|
|
|
|
tf.Printer = &testPrinter{}
|
|
|
|
tf.Client = &client.FakeRESTClient{
|
|
|
|
Codec: codec,
|
|
|
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2015-01-19 21:50:00 +00:00
|
|
|
case p == "/namespaces/test/services" && m == "POST":
|
2014-12-31 23:35:52 +00:00
|
|
|
return &http.Response{StatusCode: 201, Body: objBody(codec, &svc.Items[0])}, nil
|
2015-02-05 23:20:27 +00:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == "POST":
|
|
|
|
return &http.Response{StatusCode: 201, 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{})
|
|
|
|
|
|
|
|
cmd := f.NewCmdCreate(buf)
|
2015-02-05 23:20:27 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master-controller.json")
|
2014-12-31 23:35:52 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.json")
|
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
2015-02-06 16:57:52 +00:00
|
|
|
// Names should come from the REST response, NOT the files
|
2015-02-05 23:20:27 +00:00
|
|
|
if buf.String() != "rc1\nbaz\n" {
|
2014-12-31 23:35:52 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateDirectory(t *testing.T) {
|
2015-02-05 23:20:27 +00:00
|
|
|
_, svc, rc := testData()
|
|
|
|
rc.Items[0].Name = "name"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
|
|
|
f, tf, codec := NewAPIFactory()
|
|
|
|
tf.Printer = &testPrinter{}
|
|
|
|
tf.Client = &client.FakeRESTClient{
|
|
|
|
Codec: codec,
|
|
|
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2015-01-19 21:50:00 +00:00
|
|
|
case p == "/namespaces/test/services" && m == "POST":
|
2014-12-31 23:35:52 +00:00
|
|
|
return &http.Response{StatusCode: 201, Body: objBody(codec, &svc.Items[0])}, nil
|
2015-01-19 21:50:00 +00:00
|
|
|
case p == "/namespaces/test/replicationcontrollers" && m == "POST":
|
2015-02-05 23:20:27 +00:00
|
|
|
return &http.Response{StatusCode: 201, 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{})
|
|
|
|
|
|
|
|
cmd := f.NewCmdCreate(buf)
|
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook")
|
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
2015-02-05 23:20:27 +00:00
|
|
|
if buf.String() != "name\nbaz\nname\nbaz\nname\nbaz\n" {
|
2014-12-31 23:35:52 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|