mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Add test
This commit is contained in:
parent
b1d8a41049
commit
7667c7de42
@ -123,6 +123,7 @@ func (s *Server) Verb(verb string) *Request {
|
||||
return &Request{
|
||||
verb: verb,
|
||||
s: s,
|
||||
path: "/",
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,7 +182,7 @@ func (r *Request) Do() (interface{}, error) {
|
||||
if r.err != nil {
|
||||
return nil, r.err
|
||||
}
|
||||
finalUrl := path.Join(r.s.rawUrl, r.path)
|
||||
finalUrl := r.s.rawUrl + r.path
|
||||
query := url.Values{}
|
||||
if r.selector != nil {
|
||||
query.Add("labels", r.selector.String())
|
||||
|
@ -22,7 +22,9 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
@ -170,6 +172,43 @@ func TestDoRequest(t *testing.T) {
|
||||
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
|
||||
}
|
||||
|
||||
func TestDoRequestNewWay(t *testing.T) {
|
||||
reqBody := "request body"
|
||||
expectedObj := &api.Service{Port: 12345}
|
||||
expectedBody, _ := api.Encode(expectedObj)
|
||||
fakeHandler := util.FakeHandler{
|
||||
StatusCode: 200,
|
||||
ResponseBody: string(expectedBody),
|
||||
T: t,
|
||||
}
|
||||
testServer := httptest.NewTLSServer(&fakeHandler)
|
||||
auth := client.AuthInfo{User: "user", Password: "pass"}
|
||||
s := New(testServer.URL, &auth)
|
||||
obj, err := s.Verb("POST").
|
||||
Path("foo/bar").
|
||||
Path("baz").
|
||||
Selector("name=foo").
|
||||
Timeout(time.Second).
|
||||
Body([]byte(reqBody)).
|
||||
Do()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v %#v", err, err)
|
||||
return
|
||||
}
|
||||
if obj == nil {
|
||||
t.Error("nil obj")
|
||||
} else if !reflect.DeepEqual(obj, expectedObj) {
|
||||
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/foo/bar/baz", "POST", &reqBody)
|
||||
if fakeHandler.RequestReceived.URL.RawQuery != "labels=name%3Dfoo&timeout=1s" {
|
||||
t.Errorf("Unexpected query: %v", fakeHandler.RequestReceived.URL.RawQuery)
|
||||
}
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunController(t *testing.T) {
|
||||
fakeClient := FakeKubeClient{}
|
||||
name := "name"
|
||||
|
@ -165,19 +165,11 @@ func (h *HumanReadablePrinter) printStatus(status *api.Status, w io.Writer) erro
|
||||
// TODO replace this with something that returns a concrete printer object, rather than
|
||||
// having the secondary switch below.
|
||||
func (h *HumanReadablePrinter) Print(data []byte, output io.Writer) error {
|
||||
w := tabwriter.NewWriter(output, 20, 5, 3, ' ', 0)
|
||||
defer w.Flush()
|
||||
var mapObj map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(data), &mapObj); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Don't complain about empty objects returned by DELETE commands.
|
||||
if len(mapObj) == 0 {
|
||||
fmt.Fprint(w, "<empty>")
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, contains := mapObj["kind"]; !contains {
|
||||
return fmt.Errorf("unexpected object with no 'kind' field: %s", data)
|
||||
}
|
||||
@ -189,7 +181,9 @@ func (h *HumanReadablePrinter) Print(data []byte, output io.Writer) error {
|
||||
return h.PrintObj(obj, output)
|
||||
}
|
||||
|
||||
func (h *HumanReadablePrinter) PrintObj(obj interface{}, output io.Writer) {
|
||||
func (h *HumanReadablePrinter) PrintObj(obj interface{}, output io.Writer) error {
|
||||
w := tabwriter.NewWriter(output, 20, 5, 3, ' ', 0)
|
||||
defer w.Flush()
|
||||
switch o := obj.(type) {
|
||||
case *api.Pod:
|
||||
h.printHeader(podColumns, w)
|
||||
@ -212,6 +206,7 @@ func (h *HumanReadablePrinter) PrintObj(obj interface{}, output io.Writer) {
|
||||
case *api.Status:
|
||||
return h.printStatus(o, w)
|
||||
default:
|
||||
return h.unknown(data, w)
|
||||
_, err := fmt.Fprintf(w, "Error: unknown type %#v", obj)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user