Commit bf5ae4bb authored by Brendan Burns's avatar Brendan Burns

Fork API types.

parent cb28f25b
{
"id": "nginxController",
"apiVersion": "v1beta1",
"desiredState": {
"replicas": 2,
"replicaSelector": {"name": "nginx"},
......
......@@ -157,7 +157,7 @@ func runAtomicPutTest(c *client.Client) {
var svc api.Service
err := c.Post().Path("services").Body(
api.Service{
JSONBase: api.JSONBase{ID: "atomicService"},
JSONBase: api.JSONBase{ID: "atomicService", APIVersion: "v1beta1"},
Port: 12345,
Labels: map[string]string{
"name": "atomicService",
......
......@@ -41,10 +41,10 @@ KUBE_COVER="-cover -covermode=atomic -coverprofile=\"tmp.out\""
cd "${KUBE_TARGET}"
if [ "$1" != "" ]; then
go test -race $KUBE_COVER "$KUBE_GO_PACKAGE/$1" "${@:2}"
go test -race -timeout 30s $KUBE_COVER "$KUBE_GO_PACKAGE/$1" "${@:2}"
exit 0
fi
for package in $(find_test_dirs); do
go test -race $KUBE_COVER "${KUBE_GO_PACKAGE}/${package}" "${@:2}"
go test -race -timeout 30s $KUBE_COVER "${KUBE_GO_PACKAGE}/${package}" "${@:2}"
done
......@@ -28,8 +28,11 @@ func TestAPIObject(t *testing.T) {
Object APIObject `yaml:"object,omitempty" json:"object,omitempty"`
EmptyObject APIObject `yaml:"emptyObject,omitempty" json:"emptyObject,omitempty"`
}
AddKnownTypes(EmbeddedTest{})
convert := func(obj interface{}) (interface{}, error) { return obj, nil }
AddKnownTypes("", EmbeddedTest{})
AddKnownTypes("v1beta1", EmbeddedTest{})
AddExternalConversion("EmbeddedTest", convert)
AddInternalConversion("EmbeddedTest", convert)
outer := &EmbeddedTest{
JSONBase: JSONBase{ID: "outer"},
......
......@@ -25,7 +25,7 @@ func runTest(t *testing.T, source interface{}) {
name := reflect.TypeOf(source).Name()
data, err := Encode(source)
if err != nil {
t.Errorf("%v: %v", name, err)
t.Errorf("%v: %v (%#v)", name, err, source)
return
}
obj2, err := Decode(data)
......@@ -34,17 +34,17 @@ func runTest(t *testing.T, source interface{}) {
return
}
if !reflect.DeepEqual(source, obj2) {
t.Errorf("%v: wanted %#v, got %#v", name, source, obj2)
t.Errorf("1: %v: wanted %#v, got %#v", name, source, obj2)
return
}
obj3 := reflect.New(reflect.TypeOf(source).Elem()).Interface()
err = DecodeInto(data, obj3)
if err != nil {
t.Errorf("%v: %v", name, err)
t.Errorf("2: %v: %v", name, err)
return
}
if !reflect.DeepEqual(source, obj3) {
t.Errorf("%v: wanted %#v, got %#v", name, source, obj3)
t.Errorf("3: %v: wanted %#v, got %#v", name, source, obj3)
return
}
}
......@@ -84,7 +84,10 @@ func TestTypes(t *testing.T) {
}
func TestNonPtr(t *testing.T) {
obj := interface{}(Pod{Labels: map[string]string{"name": "foo"}})
pod := Pod{
Labels: map[string]string{"name": "foo"},
}
obj := interface{}(pod)
data, err := Encode(obj)
obj2, err2 := Decode(data)
if err != nil || err2 != nil {
......@@ -93,13 +96,16 @@ func TestNonPtr(t *testing.T) {
if _, ok := obj2.(*Pod); !ok {
t.Errorf("Got wrong type")
}
if !reflect.DeepEqual(obj2, &Pod{Labels: map[string]string{"name": "foo"}}) {
t.Errorf("Something changed: %#v", obj2)
if !reflect.DeepEqual(obj2, &pod) {
t.Errorf("Expected:\n %#v,\n Got:\n %#v", &pod, obj2)
}
}
func TestPtr(t *testing.T) {
obj := interface{}(&Pod{Labels: map[string]string{"name": "foo"}})
pod := Pod{
Labels: map[string]string{"name": "foo"},
}
obj := interface{}(&pod)
data, err := Encode(obj)
obj2, err2 := Decode(data)
if err != nil || err2 != nil {
......@@ -108,8 +114,8 @@ func TestPtr(t *testing.T) {
if _, ok := obj2.(*Pod); !ok {
t.Errorf("Got wrong type")
}
if !reflect.DeepEqual(obj2, &Pod{Labels: map[string]string{"name": "foo"}}) {
t.Errorf("Something changed: %#v", obj2)
if !reflect.DeepEqual(obj2, &pod) {
t.Errorf("Expected:\n %#v,\n Got:\n %#v", &pod, obj2)
}
}
......@@ -122,8 +128,8 @@ func TestBadJSONRejection(t *testing.T) {
if _, err1 := Decode(badJSONUnknownType); err1 == nil {
t.Errorf("Did not reject despite use of unknown type: %s", badJSONUnknownType)
}
badJSONKindMismatch := []byte(`{"kind": "Pod"}`)
/*badJSONKindMismatch := []byte(`{"kind": "Pod"}`)
if err2 := DecodeInto(badJSONKindMismatch, &Minion{}); err2 == nil {
t.Errorf("Kind is set but doesn't match the object type: %s", badJSONKindMismatch)
}
}*/
}
......@@ -189,6 +189,7 @@ type JSONBase struct {
CreationTimestamp string `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
SelfLink string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"`
ResourceVersion uint64 `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
// PodStatus represents a status of a pod.
......
/*
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 v1beta1 is the v1beta1 version of the API
package v1beta1
......@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
......@@ -36,8 +37,17 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
func convert(obj interface{}) (interface{}, error) {
return obj, nil
}
func init() {
api.AddKnownTypes(Simple{}, SimpleList{})
api.AddKnownTypes("", Simple{}, SimpleList{})
api.AddKnownTypes("v1beta1", Simple{}, SimpleList{})
api.AddExternalConversion("Simple", convert)
api.AddInternalConversion("Simple", convert)
api.AddExternalConversion("SimpleList", convert)
api.AddInternalConversion("SimpleList", convert)
}
// TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove.
......@@ -154,6 +164,7 @@ func (storage *SimpleRESTStorage) WatchSingle(id string) (watch.Interface, error
func extractBody(response *http.Response, object interface{}) (string, error) {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
log.Printf("FOO: %s", body)
if err != nil {
return string(body), err
}
......@@ -198,7 +209,8 @@ func TestNonEmptyList(t *testing.T) {
simpleStorage := SimpleRESTStorage{
list: []Simple{
{
Name: "foo",
JSONBase: api.JSONBase{Kind: "Simple"},
Name: "foo",
},
},
}
......@@ -395,7 +407,9 @@ func TestCreate(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{Name: "foo"}
simple := Simple{
Name: "foo",
}
data, _ := api.Encode(simple)
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo", bytes.NewBuffer(data))
expectNoError(t, err)
......@@ -461,7 +475,9 @@ func TestSyncCreate(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{Name: "foo"}
simple := Simple{
Name: "foo",
}
data, _ := api.Encode(simple)
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo?sync=true", bytes.NewBuffer(data))
expectNoError(t, err)
......@@ -530,8 +546,12 @@ func TestOpGet(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{Name: "foo"}
data, _ := api.Encode(simple)
simple := Simple{
Name: "foo",
}
data, err := api.Encode(simple)
t.Log(string(data))
expectNoError(t, err)
request, err := http.NewRequest("POST", server.URL+"/prefix/version/foo", bytes.NewBuffer(data))
expectNoError(t, err)
response, err := client.Do(request)
......
......@@ -175,9 +175,7 @@ func TestGetController(t *testing.T) {
Response: Response{
StatusCode: 200,
Body: api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.ReplicationControllerState{
Replicas: 2,
},
......@@ -194,18 +192,14 @@ func TestGetController(t *testing.T) {
func TestUpdateController(t *testing.T) {
requestController := api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
JSONBase: api.JSONBase{ID: "foo"},
}
c := &testClient{
Request: testRequest{Method: "PUT", Path: "/replicationControllers/foo"},
Response: Response{
StatusCode: 200,
Body: api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.ReplicationControllerState{
Replicas: 2,
},
......@@ -231,18 +225,14 @@ func TestDeleteController(t *testing.T) {
func TestCreateController(t *testing.T) {
requestController := api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
JSONBase: api.JSONBase{ID: "foo"},
}
c := &testClient{
Request: testRequest{Method: "POST", Path: "/replicationControllers", Body: requestController},
Response: Response{
StatusCode: 200,
Body: api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.ReplicationControllerState{
Replicas: 2,
},
......
......@@ -99,7 +99,7 @@ func validateSyncReplication(t *testing.T, fakePodControl *FakePodControl, expec
}
func TestSyncReplicationControllerDoesNothing(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -119,7 +119,7 @@ func TestSyncReplicationControllerDoesNothing(t *testing.T) {
}
func TestSyncReplicationControllerDeletes(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -139,7 +139,7 @@ func TestSyncReplicationControllerDeletes(t *testing.T) {
}
func TestSyncReplicationControllerCreates(t *testing.T) {
body := "{ \"items\": [] }"
body, _ := api.Encode(makePodList(0))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -159,7 +159,7 @@ func TestSyncReplicationControllerCreates(t *testing.T) {
}
func TestCreateReplica(t *testing.T) {
body := "{}"
body, _ := api.Encode(api.Pod{})
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -172,6 +172,9 @@ func TestCreateReplica(t *testing.T) {
}
controllerSpec := api.ReplicationController{
JSONBase: api.JSONBase{
Kind: "ReplicationController",
},
DesiredState: api.ReplicationControllerState{
PodTemplate: api.PodTemplate{
DesiredState: api.PodState{
......@@ -195,7 +198,8 @@ func TestCreateReplica(t *testing.T) {
expectedPod := api.Pod{
JSONBase: api.JSONBase{
Kind: "Pod",
Kind: "Pod",
APIVersion: "v1beta1",
},
Labels: controllerSpec.DesiredState.PodTemplate.Labels,
DesiredState: controllerSpec.DesiredState.PodTemplate.DesiredState,
......@@ -207,12 +211,12 @@ func TestCreateReplica(t *testing.T) {
}
if !reflect.DeepEqual(expectedPod, actualPod) {
t.Logf("Body: %s", fakeHandler.RequestBody)
t.Errorf("Unexpected mismatch. Expected %#v, Got: %#v", expectedPod, actualPod)
t.Errorf("Unexpected mismatch. Expected\n %#v,\n Got:\n %#v", expectedPod, actualPod)
}
}
func TestHandleWatchResponseNotSet(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -233,7 +237,7 @@ func TestHandleWatchResponseNotSet(t *testing.T) {
}
func TestHandleWatchResponseNoNode(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -254,7 +258,7 @@ func TestHandleWatchResponseNoNode(t *testing.T) {
}
func TestHandleWatchResponseBadData(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -278,7 +282,7 @@ func TestHandleWatchResponseBadData(t *testing.T) {
}
func TestHandleWatchResponse(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -293,6 +297,7 @@ func TestHandleWatchResponse(t *testing.T) {
controller := makeReplicationController(2)
// TODO: fixme when etcd uses Encode/Decode
data, err := json.Marshal(controller)
if err != nil {
t.Errorf("Unexpected error: %v", err)
......@@ -312,7 +317,7 @@ func TestHandleWatchResponse(t *testing.T) {
}
func TestHandleWatchResponseDelete(t *testing.T) {
body, _ := json.Marshal(makePodList(2))
body, _ := api.Encode(makePodList(2))
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
......@@ -327,6 +332,7 @@ func TestHandleWatchResponseDelete(t *testing.T) {
controller := makeReplicationController(2)
// TODO: fixme when etcd writing uses api.Encode
data, err := json.Marshal(controller)
if err != nil {
t.Errorf("Unexpected error: %v", err)
......@@ -347,6 +353,7 @@ func TestHandleWatchResponseDelete(t *testing.T) {
func TestSyncronize(t *testing.T) {
controllerSpec1 := api.ReplicationController{
JSONBase: api.JSONBase{APIVersion: "v1beta1"},
DesiredState: api.ReplicationControllerState{
Replicas: 4,
PodTemplate: api.PodTemplate{
......@@ -367,6 +374,7 @@ func TestSyncronize(t *testing.T) {
},
}
controllerSpec2 := api.ReplicationController{
JSONBase: api.JSONBase{APIVersion: "v1beta1"},
DesiredState: api.ReplicationControllerState{
Replicas: 3,
PodTemplate: api.PodTemplate{
......@@ -405,7 +413,7 @@ func TestSyncronize(t *testing.T) {
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: "{}",
ResponseBody: "{\"apiVersion\": \"v1beta1\", \"kind\": \"PodList\"}",
T: t,
}
testServer := httptest.NewTLSServer(&fakeHandler)
......
......@@ -34,7 +34,7 @@ func DoParseTest(t *testing.T, storage string, obj interface{}) {
jsonData, _ := api.Encode(obj)
yamlData, _ := yaml.Marshal(obj)
t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
t.Logf("Intermediate json:\n%v\n", string(jsonData))
jsonGot, jsonErr := ToWireFormat(jsonData, storage)
yamlGot, yamlErr := ToWireFormat(yamlData, storage)
......@@ -56,7 +56,7 @@ func DoParseTest(t *testing.T, storage string, obj interface{}) {
func TestParsePod(t *testing.T) {
DoParseTest(t, "pods", api.Pod{
JSONBase: api.JSONBase{ID: "test pod"},
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "test pod", Kind: "Pod"},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
ID: "My manifest",
......@@ -73,7 +73,7 @@ func TestParsePod(t *testing.T) {
func TestParseService(t *testing.T) {
DoParseTest(t, "services", api.Service{
JSONBase: api.JSONBase{ID: "my service"},
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "my service", Kind: "Service"},
Port: 8080,
Labels: map[string]string{
"area": "staging",
......@@ -86,6 +86,7 @@ func TestParseService(t *testing.T) {
func TestParseController(t *testing.T) {
DoParseTest(t, "replicationControllers", api.ReplicationController{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "my controller", Kind: "ReplicationController"},
DesiredState: api.ReplicationControllerState{
Replicas: 9001,
PodTemplate: api.PodTemplate{
......
......@@ -32,7 +32,8 @@ func makePodList(count int) api.PodList {
for i := 0; i < count; i++ {
pods = append(pods, api.Pod{
JSONBase: api.JSONBase{
ID: fmt.Sprintf("pod%d", i),
ID: fmt.Sprintf("pod%d", i),
APIVersion: "v1beta1",
},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
......@@ -53,7 +54,8 @@ func makePodList(count int) api.PodList {
})
}
return api.PodList{
Items: pods,
JSONBase: api.JSONBase{APIVersion: "v1beta1", Kind: "PodList"},
Items: pods,
}
}
......
......@@ -21,6 +21,7 @@ import (
"io"
"reflect"
"testing"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
......@@ -39,18 +40,28 @@ func TestDecoder(t *testing.T) {
}
}()
action, got, err := decoder.Decode()
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if e, a := watch.Added, action; e != a {
t.Errorf("Expected %v, got %v", e, a)
}
if e, a := expect, got; !reflect.DeepEqual(e, a) {
t.Errorf("Expected %v, got %v", e, a)
done := make(chan struct{})
go func() {
action, got, err := decoder.Decode()
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if e, a := watch.Added, action; e != a {
t.Errorf("Expected %v, got %v", e, a)
}
if e, a := expect, got; !reflect.DeepEqual(e, a) {
t.Errorf("Expected %v, got %v", e, a)
}
close(done)
}()
select {
case <-done:
break
case <-time.After(10 * time.Second):
t.Error("Timeout")
}
done := make(chan struct{})
done = make(chan struct{})
go func() {
_, _, err := decoder.Decode()
......@@ -62,7 +73,12 @@ func TestDecoder(t *testing.T) {
decoder.Close()
<-done
select {
case <-done:
break
case <-time.After(10 * time.Second):
t.Error("Timeout")
}
}
func TestDecoder_SourceClose(t *testing.T) {
......@@ -81,5 +97,10 @@ func TestDecoder_SourceClose(t *testing.T) {
in.Close()
<-done
select {
case <-done:
break
case <-time.After(10 * time.Second):
t.Error("Timeout")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment