2014-06-22 19:05:34 +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.
|
|
|
|
*/
|
|
|
|
|
2014-06-22 21:18:01 +00:00
|
|
|
package client
|
2014-06-22 19:05:34 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-24 21:57:09 +00:00
|
|
|
"bytes"
|
2014-07-17 23:09:29 +00:00
|
|
|
"encoding/base64"
|
2014-06-22 21:18:01 +00:00
|
|
|
"io/ioutil"
|
2014-06-26 23:10:38 +00:00
|
|
|
"net/http"
|
2014-06-22 19:05:34 +00:00
|
|
|
"net/http/httptest"
|
2014-10-29 02:48:13 +00:00
|
|
|
"net/url"
|
2014-06-22 19:05:34 +00:00
|
|
|
"reflect"
|
2014-07-17 23:09:29 +00:00
|
|
|
"strings"
|
2014-06-22 19:05:34 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-17 04:33:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-10-29 02:48:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
|
2014-09-11 23:01:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
|
2014-06-23 00:02:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-06-22 19:05:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-07-17 23:09:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-09-17 04:33:48 +00:00
|
|
|
watchjson "github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json"
|
2014-06-22 19:05:34 +00:00
|
|
|
)
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
func TestTransformResponse(t *testing.T) {
|
|
|
|
invalid := []byte("aaaaa")
|
|
|
|
uri, _ := url.Parse("http://localhost")
|
|
|
|
testCases := []struct {
|
|
|
|
Response *http.Response
|
|
|
|
Data []byte
|
|
|
|
Created bool
|
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{Response: &http.Response{StatusCode: 200}, Data: []byte{}},
|
|
|
|
{Response: &http.Response{StatusCode: 201}, Data: []byte{}, Created: true},
|
|
|
|
{Response: &http.Response{StatusCode: 199}, Error: true},
|
|
|
|
{Response: &http.Response{StatusCode: 500}, Error: true},
|
|
|
|
{Response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(invalid))}, Data: invalid},
|
|
|
|
{Response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(invalid))}, Data: invalid},
|
|
|
|
}
|
|
|
|
for i, test := range testCases {
|
|
|
|
r := NewRequest(nil, "", uri, testapi.Codec())
|
|
|
|
if test.Response.Body == nil {
|
|
|
|
test.Response.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
|
|
|
|
}
|
|
|
|
response, created, err := r.transformResponse(test.Response, &http.Request{})
|
|
|
|
hasErr := err != nil
|
|
|
|
if hasErr != test.Error {
|
|
|
|
t.Errorf("%d: unexpected error: %f %v", i, test.Error, err)
|
|
|
|
}
|
|
|
|
if !(test.Data == nil && response == nil) && !reflect.DeepEqual(test.Data, response) {
|
|
|
|
t.Errorf("%d: unexpected response: %#v %#v", i, test.Data, response)
|
|
|
|
}
|
|
|
|
if test.Created != created {
|
|
|
|
t.Errorf("%d: expected created %f, got %f", i, test.Created, created)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 19:05:34 +00:00
|
|
|
func TestDoRequestNewWay(t *testing.T) {
|
|
|
|
reqBody := "request body"
|
|
|
|
expectedObj := &api.Service{Port: 12345}
|
2014-09-11 23:01:29 +00:00
|
|
|
expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
|
2014-06-22 19:05:34 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
2014-10-29 02:48:13 +00:00
|
|
|
defer testServer.Close()
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
|
2014-08-28 13:56:38 +00:00
|
|
|
obj, err := c.Verb("POST").
|
2014-06-22 19:05:34 +00:00
|
|
|
Path("foo/bar").
|
|
|
|
Path("baz").
|
2014-08-05 22:23:33 +00:00
|
|
|
ParseSelectorParam("labels", "name=foo").
|
2014-06-22 19:05:34 +00:00
|
|
|
Timeout(time.Second).
|
|
|
|
Body([]byte(reqBody)).
|
2014-06-23 00:02:48 +00:00
|
|
|
Do().Get()
|
2014-06-22 19:05:34 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-09-11 23:01:29 +00:00
|
|
|
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo", "POST", &reqBody)
|
2014-06-22 19:05:34 +00:00
|
|
|
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 21:57:09 +00:00
|
|
|
func TestDoRequestNewWayReader(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2014-09-11 23:01:29 +00:00
|
|
|
reqBodyExpected, _ := v1beta1.Codec.Encode(reqObj)
|
2014-06-24 21:57:09 +00:00
|
|
|
expectedObj := &api.Service{Port: 12345}
|
2014-09-11 23:01:29 +00:00
|
|
|
expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
|
2014-06-24 21:57:09 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
|
2014-08-28 13:56:38 +00:00
|
|
|
obj, err := c.Verb("POST").
|
2014-06-24 21:57:09 +00:00
|
|
|
Path("foo/bar").
|
|
|
|
Path("baz").
|
2014-08-05 22:23:33 +00:00
|
|
|
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
|
2014-08-20 22:30:09 +00:00
|
|
|
Sync(true).
|
2014-06-24 21:57:09 +00:00
|
|
|
Timeout(time.Second).
|
|
|
|
Body(bytes.NewBuffer(reqBodyExpected)).
|
|
|
|
Do().Get()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
tmpStr := string(reqBodyExpected)
|
2014-08-20 22:30:09 +00:00
|
|
|
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&sync=true&timeout=1s", "POST", &tmpStr)
|
2014-06-24 21:57:09 +00:00
|
|
|
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 19:05:34 +00:00
|
|
|
func TestDoRequestNewWayObj(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2014-09-11 23:01:29 +00:00
|
|
|
reqBodyExpected, _ := v1beta2.Codec.Encode(reqObj)
|
2014-06-22 19:05:34 +00:00
|
|
|
expectedObj := &api.Service{Port: 12345}
|
2014-09-11 23:01:29 +00:00
|
|
|
expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
|
2014-06-22 19:05:34 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
|
2014-08-28 13:56:38 +00:00
|
|
|
obj, err := c.Verb("POST").
|
2014-06-22 19:05:34 +00:00
|
|
|
Path("foo/bar").
|
|
|
|
Path("baz").
|
2014-08-05 22:23:33 +00:00
|
|
|
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
|
2014-06-22 19:05:34 +00:00
|
|
|
Timeout(time.Second).
|
|
|
|
Body(reqObj).
|
2014-06-23 00:02:48 +00:00
|
|
|
Do().Get()
|
2014-06-22 19:05:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
tmpStr := string(reqBodyExpected)
|
2014-09-11 23:01:29 +00:00
|
|
|
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
|
2014-06-22 19:05:34 +00:00
|
|
|
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
|
|
|
|
func TestDoRequestNewWayFile(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2014-09-11 23:01:29 +00:00
|
|
|
reqBodyExpected, err := v1beta1.Codec.Encode(reqObj)
|
2014-08-03 23:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-22 21:18:01 +00:00
|
|
|
file, err := ioutil.TempFile("", "foo")
|
2014-08-03 23:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-22 21:18:01 +00:00
|
|
|
_, err = file.Write(reqBodyExpected)
|
2014-08-03 23:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
|
|
|
|
expectedObj := &api.Service{Port: 12345}
|
2014-09-11 23:01:29 +00:00
|
|
|
expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
|
2014-06-22 21:18:01 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
|
2014-10-24 17:16:02 +00:00
|
|
|
wasCreated := true
|
2014-08-28 13:56:38 +00:00
|
|
|
obj, err := c.Verb("POST").
|
2014-06-22 21:18:01 +00:00
|
|
|
Path("foo/bar").
|
|
|
|
Path("baz").
|
2014-08-05 22:23:33 +00:00
|
|
|
ParseSelectorParam("labels", "name=foo").
|
2014-06-22 21:18:01 +00:00
|
|
|
Timeout(time.Second).
|
|
|
|
Body(file.Name()).
|
2014-10-24 17:16:02 +00:00
|
|
|
Do().WasCreated(&wasCreated).Get()
|
2014-06-22 21:18:01 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-10-24 17:16:02 +00:00
|
|
|
if wasCreated {
|
|
|
|
t.Errorf("expected object was not created")
|
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
tmpStr := string(reqBodyExpected)
|
2014-08-20 22:30:09 +00:00
|
|
|
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
|
2014-06-22 21:18:01 +00:00
|
|
|
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 00:02:48 +00:00
|
|
|
|
2014-10-24 17:16:02 +00:00
|
|
|
func TestWasCreated(t *testing.T) {
|
|
|
|
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
|
|
|
reqBodyExpected, err := v1beta1.Codec.Encode(reqObj)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedObj := &api.Service{Port: 12345}
|
|
|
|
expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
|
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 201,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
|
|
|
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
|
|
|
|
wasCreated := false
|
|
|
|
obj, err := c.Verb("PUT").
|
|
|
|
Path("foo/bar").
|
|
|
|
Path("baz").
|
|
|
|
ParseSelectorParam("labels", "name=foo").
|
|
|
|
Timeout(time.Second).
|
|
|
|
Body(reqBodyExpected).
|
|
|
|
Do().WasCreated(&wasCreated).Get()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if !wasCreated {
|
|
|
|
t.Errorf("Expected object was created")
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpStr := string(reqBodyExpected)
|
|
|
|
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "PUT", &tmpStr)
|
|
|
|
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
func TestVerbs(t *testing.T) {
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{})
|
2014-06-23 00:02:48 +00:00
|
|
|
if r := c.Post(); r.verb != "POST" {
|
|
|
|
t.Errorf("Post verb is wrong")
|
|
|
|
}
|
|
|
|
if r := c.Put(); r.verb != "PUT" {
|
|
|
|
t.Errorf("Put verb is wrong")
|
|
|
|
}
|
|
|
|
if r := c.Get(); r.verb != "GET" {
|
|
|
|
t.Errorf("Get verb is wrong")
|
|
|
|
}
|
|
|
|
if r := c.Delete(); r.verb != "DELETE" {
|
|
|
|
t.Errorf("Delete verb is wrong")
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 21:57:09 +00:00
|
|
|
|
|
|
|
func TestAbsPath(t *testing.T) {
|
|
|
|
expectedPath := "/bar/foo"
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{})
|
2014-06-24 21:57:09 +00:00
|
|
|
r := c.Post().Path("/foo").AbsPath(expectedPath)
|
|
|
|
if r.path != expectedPath {
|
|
|
|
t.Errorf("unexpected path: %s, expected %s", r.path, expectedPath)
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 23:23:15 +00:00
|
|
|
|
|
|
|
func TestSync(t *testing.T) {
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{})
|
2014-06-25 23:23:15 +00:00
|
|
|
r := c.Get()
|
2014-07-04 04:23:21 +00:00
|
|
|
if r.sync {
|
2014-06-25 23:23:15 +00:00
|
|
|
t.Errorf("sync has wrong default")
|
|
|
|
}
|
|
|
|
r.Sync(false)
|
|
|
|
if r.sync {
|
|
|
|
t.Errorf("'Sync' doesn't work")
|
|
|
|
}
|
|
|
|
r.Sync(true)
|
|
|
|
if !r.sync {
|
|
|
|
t.Errorf("'Sync' doesn't work")
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 23:10:38 +00:00
|
|
|
|
2014-08-05 22:23:33 +00:00
|
|
|
func TestUintParam(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
name string
|
|
|
|
testVal uint64
|
|
|
|
expectStr string
|
|
|
|
}{
|
2014-08-28 13:56:38 +00:00
|
|
|
{"foo", 31415, "http://localhost?foo=31415"},
|
|
|
|
{"bar", 42, "http://localhost?bar=42"},
|
|
|
|
{"baz", 0, "http://localhost?baz=0"},
|
2014-08-05 22:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{})
|
2014-08-05 22:23:33 +00:00
|
|
|
r := c.Get().AbsPath("").UintParam(item.name, item.testVal)
|
|
|
|
if e, a := item.expectStr, r.finalURL(); e != a {
|
|
|
|
t.Errorf("expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 20:50:04 +00:00
|
|
|
func TestUnacceptableParamNames(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
name string
|
|
|
|
testVal string
|
|
|
|
expectSuccess bool
|
|
|
|
}{
|
|
|
|
{"sync", "foo", false},
|
|
|
|
{"timeout", "42", false},
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
|
2014-08-08 20:50:04 +00:00
|
|
|
for _, item := range table {
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{})
|
2014-08-08 20:50:04 +00:00
|
|
|
r := c.Get().setParam(item.name, item.testVal)
|
|
|
|
if e, a := item.expectSuccess, r.err == nil; e != a {
|
|
|
|
t.Errorf("expected %v, got %v (%v)", e, a, r.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 17:52:18 +00:00
|
|
|
func TestBody(t *testing.T) {
|
|
|
|
const data = "test payload"
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile("", "test_body")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("TempFile error: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := f.WriteString(data); err != nil {
|
|
|
|
t.Fatalf("TempFile.WriteString error: %v", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
c := NewOrDie(&Config{})
|
|
|
|
tests := []interface{}{[]byte(data), f.Name(), strings.NewReader(data)}
|
|
|
|
for i, tt := range tests {
|
|
|
|
r := c.Post().Body(tt)
|
|
|
|
if r.err != nil {
|
|
|
|
t.Errorf("%d: r.Body(%#v) error: %v", i, tt, r.err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(data))
|
|
|
|
if _, err := r.body.Read(buf); err != nil {
|
|
|
|
t.Errorf("%d: r.body.Read error: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
body := string(buf)
|
|
|
|
if body != data {
|
|
|
|
t.Errorf("%d: r.body = %q; want %q", i, body, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
func TestSetPoller(t *testing.T) {
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{})
|
2014-06-26 23:10:38 +00:00
|
|
|
r := c.Get()
|
2014-10-29 02:48:13 +00:00
|
|
|
if c.PollPeriod == 0 {
|
|
|
|
t.Errorf("polling should be on by default")
|
|
|
|
}
|
|
|
|
if r.poller == nil {
|
2014-06-26 23:10:38 +00:00
|
|
|
t.Errorf("polling should be on by default")
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
r.NoPoll()
|
|
|
|
if r.poller != nil {
|
|
|
|
t.Errorf("'NoPoll' doesn't work")
|
2014-06-26 23:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPolling(t *testing.T) {
|
2014-09-08 01:31:11 +00:00
|
|
|
objects := []runtime.Object{
|
Evolve the api.Status object with Reason/Details
Contains breaking API change on api.Status#Details (type change)
Turn Details from string -> StatusDetails - a general
bucket for keyed error behavior. Define an open enumeration
ReasonType exposed as Reason on the status object to provide
machine readable subcategorization beyond HTTP Status Code. Define
a human readable field Message which is common convention (previously
this was joined into Details).
Precedence order: HTTP Status Code, Reason, Details. apiserver would
impose restraints on the ReasonTypes defined by the main apiobject,
and ensure their use is consistent.
There are four long term scenarios this change supports:
1. Allow a client access to a machine readable field that can be
easily switched on for improving or translating the generic
server Message.
2. Return a 404 when a composite operation on multiple resources
fails with enough data so that a client can distinguish which
item does not exist. E.g. resource Parent and resource Child,
POST /parents/1/children to create a new Child, but /parents/1
is deleted. POST returns 404, ReasonTypeNotFound, and
Details.ID = "1", Details.Kind = "parent"
3. Allow a client to receive validation data that is keyed by
attribute for building user facing UIs around field submission.
Validation is usually expressed as map[string][]string, but
that type is less appropriate for many other uses.
4. Allow specific API errors to return more granular failure status
for specific operations. An example might be a minion proxy,
where the operation that failed may be both proxying OR the
minion itself. In this case a reason may be defined "proxy_failed"
corresponding to 502, where the Details field may be extended
to contain a nested error object.
At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
|
|
|
&api.Status{Status: api.StatusWorking, Details: &api.StatusDetails{ID: "1234"}},
|
|
|
|
&api.Status{Status: api.StatusWorking, Details: &api.StatusDetails{ID: "1234"}},
|
|
|
|
&api.Status{Status: api.StatusWorking, Details: &api.StatusDetails{ID: "1234"}},
|
|
|
|
&api.Status{Status: api.StatusWorking, Details: &api.StatusDetails{ID: "1234"}},
|
2014-06-26 23:10:38 +00:00
|
|
|
&api.Status{Status: api.StatusSuccess},
|
|
|
|
}
|
|
|
|
|
|
|
|
callNumber := 0
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-10-29 02:48:13 +00:00
|
|
|
if callNumber == 0 {
|
|
|
|
if r.URL.Path != "/api/v1beta1/" {
|
|
|
|
t.Fatalf("unexpected request URL path %s", r.URL.Path)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if r.URL.Path != "/api/v1beta1/operations/1234" {
|
|
|
|
t.Fatalf("unexpected request URL path %s", r.URL.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Logf("About to write %d", callNumber)
|
2014-09-11 23:01:29 +00:00
|
|
|
data, err := v1beta1.Codec.Encode(objects[callNumber])
|
2014-06-26 23:10:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected encode error")
|
|
|
|
}
|
|
|
|
callNumber++
|
|
|
|
w.Write(data)
|
|
|
|
}))
|
|
|
|
|
2014-09-30 00:15:00 +00:00
|
|
|
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
|
2014-10-29 02:48:13 +00:00
|
|
|
c.PollPeriod = 1 * time.Millisecond
|
2014-06-26 23:10:38 +00:00
|
|
|
trials := []func(){
|
|
|
|
func() {
|
|
|
|
// Check that we do indeed poll when asked to.
|
2014-10-29 02:48:13 +00:00
|
|
|
obj, err := c.Get().Do().Get()
|
2014-06-26 23:10:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v %#v", err, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s, ok := obj.(*api.Status); !ok || s.Status != api.StatusSuccess {
|
|
|
|
t.Errorf("Unexpected return object: %#v", obj)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if callNumber != len(objects) {
|
|
|
|
t.Errorf("Unexpected number of calls: %v", callNumber)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
// Check that we don't poll when asked not to.
|
2014-10-29 02:48:13 +00:00
|
|
|
obj, err := c.Get().NoPoll().Do().Get()
|
2014-06-26 23:10:38 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Unexpected non error: %v", obj)
|
|
|
|
return
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
if se, ok := err.(APIStatus); !ok || se.Status().Status != api.StatusWorking {
|
2014-06-26 23:10:38 +00:00
|
|
|
t.Errorf("Unexpected kind of error: %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if callNumber != 1 {
|
|
|
|
t.Errorf("Unexpected number of calls: %v", callNumber)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, f := range trials {
|
|
|
|
callNumber = 0
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
2014-07-17 23:09:29 +00:00
|
|
|
|
2014-09-30 00:15:00 +00:00
|
|
|
func authFromReq(r *http.Request) (*Config, bool) {
|
2014-07-17 23:09:29 +00:00
|
|
|
auth, ok := r.Header["Authorization"]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded := strings.Split(auth[0], " ")
|
|
|
|
if len(encoded) != 2 || encoded[0] != "Basic" {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(encoded[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
parts := strings.Split(string(decoded), ":")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
return &Config{Username: parts[0], Password: parts[1]}, true
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkAuth sets errors if the auth found in r doesn't match the expectation.
|
|
|
|
// TODO: Move to util, test in more places.
|
2014-09-30 00:15:00 +00:00
|
|
|
func checkAuth(t *testing.T, expect *Config, r *http.Request) {
|
2014-07-17 23:09:29 +00:00
|
|
|
foundAuth, found := authFromReq(r)
|
|
|
|
if !found {
|
|
|
|
t.Errorf("no auth found")
|
2014-09-30 00:15:00 +00:00
|
|
|
} else if e, a := expect, foundAuth; !reflect.DeepEqual(e, a) {
|
2014-07-17 23:09:29 +00:00
|
|
|
t.Fatalf("Wrong basic auth: wanted %#v, got %#v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatch(t *testing.T) {
|
|
|
|
var table = []struct {
|
|
|
|
t watch.EventType
|
2014-09-08 01:31:11 +00:00
|
|
|
obj runtime.Object
|
2014-07-17 23:09:29 +00:00
|
|
|
}{
|
2014-10-23 20:51:34 +00:00
|
|
|
{watch.Added, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "first"}}},
|
|
|
|
{watch.Modified, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "second"}}},
|
|
|
|
{watch.Deleted, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "last"}}},
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 00:15:00 +00:00
|
|
|
auth := &Config{Username: "user", Password: "pass"}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-07-17 23:09:29 +00:00
|
|
|
checkAuth(t, auth, r)
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
panic("need flusher!")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Transfer-Encoding", "chunked")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
flusher.Flush()
|
|
|
|
|
2014-09-17 04:33:48 +00:00
|
|
|
encoder := watchjson.NewEncoder(w, latest.Codec)
|
2014-07-17 23:09:29 +00:00
|
|
|
for _, item := range table {
|
2014-09-17 04:33:48 +00:00
|
|
|
if err := encoder.Encode(&watch.Event{item.t, item.obj}); err != nil {
|
2014-09-11 17:02:53 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-17 23:09:29 +00:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2014-09-30 00:15:00 +00:00
|
|
|
s, err := New(&Config{
|
|
|
|
Host: testServer.URL,
|
|
|
|
Version: "v1beta1",
|
|
|
|
Username: "user",
|
|
|
|
Password: "pass",
|
|
|
|
})
|
2014-08-28 13:56:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2014-07-17 23:09:29 +00:00
|
|
|
|
|
|
|
watching, err := s.Get().Path("path/to/watch/thing").Watch()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
got, ok := <-watching.ResultChan()
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected early close")
|
|
|
|
}
|
|
|
|
if e, a := item.t, got.Type; e != a {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
if e, a := item.obj, got.Object; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := <-watching.ResultChan()
|
|
|
|
if ok {
|
|
|
|
t.Fatal("Unexpected non-close")
|
|
|
|
}
|
|
|
|
}
|
2014-10-02 00:19:00 +00:00
|
|
|
|
|
|
|
func TestStream(t *testing.T) {
|
|
|
|
auth := &Config{Username: "user", Password: "pass"}
|
|
|
|
expectedBody := "expected body"
|
|
|
|
|
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
checkAuth(t, auth, r)
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
panic("need flusher!")
|
|
|
|
}
|
|
|
|
w.Header().Set("Transfer-Encoding", "chunked")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(expectedBody))
|
|
|
|
flusher.Flush()
|
|
|
|
}))
|
|
|
|
|
|
|
|
s, err := New(&Config{
|
|
|
|
Host: testServer.URL,
|
|
|
|
Version: "v1beta1",
|
|
|
|
Username: "user",
|
|
|
|
Password: "pass",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
readCloser, err := s.Get().Path("path/to/stream/thing").Stream()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
defer readCloser.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(readCloser)
|
|
|
|
resultBody := buf.String()
|
|
|
|
|
|
|
|
if expectedBody != resultBody {
|
|
|
|
t.Errorf("Expected %s, got %s", expectedBody, resultBody)
|
|
|
|
}
|
|
|
|
}
|