Make tests pass again

pull/6/head
Daniel Smith 2014-09-07 21:14:18 -07:00
parent 48ce23ac91
commit fc09f988b4
30 changed files with 270 additions and 228 deletions

View File

@ -179,7 +179,7 @@ func runReplicationControllerTest(c *client.Client) {
}
glog.Infof("Creating replication controllers")
if _, err := c.CreateReplicationController(controllerRequest); err != nil {
if _, err := c.CreateReplicationController(&controllerRequest); err != nil {
glog.Fatalf("Unexpected error: %#v", err)
}
glog.Infof("Done creating replication controllers")
@ -194,7 +194,7 @@ func runReplicationControllerTest(c *client.Client) {
if err != nil {
glog.Fatalf("FAILED: unable to get pods to list: %v", err)
}
if err := wait.Poll(time.Second, time.Second*10, podsOnMinions(c, pods)); err != nil {
if err := wait.Poll(time.Second, time.Second*10, podsOnMinions(c, *pods)); err != nil {
glog.Fatalf("FAILED: pods never started running %v", err)
}
@ -204,7 +204,7 @@ func runReplicationControllerTest(c *client.Client) {
func runAtomicPutTest(c *client.Client) {
var svc api.Service
err := c.Post().Path("services").Body(
api.Service{
&api.Service{
JSONBase: api.JSONBase{ID: "atomicservice", APIVersion: "v1beta1"},
Port: 12345,
Labels: map[string]string{

View File

@ -58,11 +58,11 @@ var (
imageName = flag.String("image", "", "Image used when updating a replicationController. Will apply to the first container in the pod template.")
)
var parser = kubecfg.NewParser(map[string]interface{}{
"pods": api.Pod{},
"services": api.Service{},
"replicationControllers": api.ReplicationController{},
"minions": api.Minion{},
var parser = kubecfg.NewParser(map[string]runtime.Object{
"pods": &api.Pod{},
"services": &api.Service{},
"replicationControllers": &api.ReplicationController{},
"minions": &api.Minion{},
})
func usage() {

View File

@ -38,15 +38,15 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
func convert(obj interface{}) (interface{}, error) {
func convert(obj runtime.Object) (runtime.Object, error) {
return obj, nil
}
var codec = runtime.DefaultCodec
func init() {
runtime.AddKnownTypes("", Simple{}, SimpleList{})
runtime.AddKnownTypes("v1beta1", Simple{}, SimpleList{})
runtime.DefaultScheme.AddKnownTypes("", &Simple{}, &SimpleList{})
runtime.DefaultScheme.AddKnownTypes("v1beta1", &Simple{}, &SimpleList{})
}
type Simple struct {
@ -54,11 +54,15 @@ type Simple struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
}
func (*Simple) IsAnAPIObject() {}
type SimpleList struct {
api.JSONBase `yaml:",inline" json:",inline"`
Items []Simple `yaml:"items,omitempty" json:"items,omitempty"`
}
func (*SimpleList) IsAnAPIObject() {}
type SimpleRESTStorage struct {
errors map[string]error
list []Simple
@ -78,43 +82,43 @@ type SimpleRESTStorage struct {
// If non-nil, called inside the WorkFunc when answering update, delete, create.
// obj receives the original input to the update, delete, or create call.
injectedFunction func(obj interface{}) (returnObj interface{}, err error)
injectedFunction func(obj runtime.Object) (returnObj runtime.Object, err error)
}
func (storage *SimpleRESTStorage) List(labels.Selector) (interface{}, error) {
func (storage *SimpleRESTStorage) List(labels.Selector) (runtime.Object, error) {
result := &SimpleList{
Items: storage.list,
}
return result, storage.errors["list"]
}
func (storage *SimpleRESTStorage) Get(id string) (interface{}, error) {
return storage.item, storage.errors["get"]
func (storage *SimpleRESTStorage) Get(id string) (runtime.Object, error) {
return runtime.DefaultScheme.CopyOrDie(&storage.item), storage.errors["get"]
}
func (storage *SimpleRESTStorage) Delete(id string) (<-chan interface{}, error) {
func (storage *SimpleRESTStorage) Delete(id string) (<-chan runtime.Object, error) {
storage.deleted = id
if err := storage.errors["delete"]; err != nil {
return nil, err
}
return MakeAsync(func() (interface{}, error) {
return MakeAsync(func() (runtime.Object, error) {
if storage.injectedFunction != nil {
return storage.injectedFunction(id)
return storage.injectedFunction(&Simple{JSONBase: api.JSONBase{ID: id}})
}
return &api.Status{Status: api.StatusSuccess}, nil
}), nil
}
func (storage *SimpleRESTStorage) New() interface{} {
func (storage *SimpleRESTStorage) New() runtime.Object {
return &Simple{}
}
func (storage *SimpleRESTStorage) Create(obj interface{}) (<-chan interface{}, error) {
func (storage *SimpleRESTStorage) Create(obj runtime.Object) (<-chan runtime.Object, error) {
storage.created = obj.(*Simple)
if err := storage.errors["create"]; err != nil {
return nil, err
}
return MakeAsync(func() (interface{}, error) {
return MakeAsync(func() (runtime.Object, error) {
if storage.injectedFunction != nil {
return storage.injectedFunction(obj)
}
@ -122,12 +126,12 @@ func (storage *SimpleRESTStorage) Create(obj interface{}) (<-chan interface{}, e
}), nil
}
func (storage *SimpleRESTStorage) Update(obj interface{}) (<-chan interface{}, error) {
func (storage *SimpleRESTStorage) Update(obj runtime.Object) (<-chan runtime.Object, error) {
storage.updated = obj.(*Simple)
if err := storage.errors["update"]; err != nil {
return nil, err
}
return MakeAsync(func() (interface{}, error) {
return MakeAsync(func() (runtime.Object, error) {
if storage.injectedFunction != nil {
return storage.injectedFunction(obj)
}
@ -156,7 +160,7 @@ func (storage *SimpleRESTStorage) ResourceLocation(id string) (string, error) {
return id, nil
}
func extractBody(response *http.Response, object interface{}) (string, error) {
func extractBody(response *http.Response, object runtime.Object) (string, error) {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
@ -398,7 +402,7 @@ func TestUpdate(t *testing.T) {
handler := Handle(storage, codec, "/prefix/version")
server := httptest.NewServer(handler)
item := Simple{
item := &Simple{
Name: "bar",
}
body, err := codec.Encode(item)
@ -428,7 +432,7 @@ func TestUpdateMissing(t *testing.T) {
handler := Handle(storage, codec, "/prefix/version")
server := httptest.NewServer(handler)
item := Simple{
item := &Simple{
Name: "bar",
}
body, err := codec.Encode(item)
@ -457,7 +461,7 @@ func TestCreate(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{
simple := &Simple{
Name: "foo",
}
data, _ := codec.Encode(simple)
@ -497,7 +501,7 @@ func TestCreateNotFound(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{Name: "foo"}
simple := &Simple{Name: "foo"}
data, _ := codec.Encode(simple)
request, err := http.NewRequest("POST", server.URL+"/prefix/version/simple", bytes.NewBuffer(data))
if err != nil {
@ -528,7 +532,7 @@ func TestParseTimeout(t *testing.T) {
func TestSyncCreate(t *testing.T) {
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
time.Sleep(5 * time.Millisecond)
return obj, nil
},
@ -539,7 +543,7 @@ func TestSyncCreate(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{
simple := &Simple{
Name: "foo",
}
data, _ := codec.Encode(simple)
@ -566,7 +570,7 @@ func TestSyncCreate(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(itemOut, simple) {
if !reflect.DeepEqual(&itemOut, simple) {
t.Errorf("Unexpected data: %#v, expected %#v (%s)", itemOut, simple, string(body))
}
if response.StatusCode != http.StatusOK {
@ -600,7 +604,7 @@ func expectApiStatus(t *testing.T, method, url string, data []byte, code int) *a
func TestAsyncDelayReturnsError(t *testing.T) {
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
return nil, apierrs.NewAlreadyExists("foo", "bar")
},
}
@ -617,7 +621,7 @@ func TestAsyncDelayReturnsError(t *testing.T) {
func TestAsyncCreateError(t *testing.T) {
ch := make(chan struct{})
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
<-ch
return nil, apierrs.NewAlreadyExists("foo", "bar")
},
@ -626,7 +630,7 @@ func TestAsyncCreateError(t *testing.T) {
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
server := httptest.NewServer(handler)
simple := Simple{Name: "foo"}
simple := &Simple{Name: "foo"}
data, _ := codec.Encode(simple)
status := expectApiStatus(t, "POST", fmt.Sprintf("%s/prefix/version/foo", server.URL), data, http.StatusAccepted)
@ -662,18 +666,21 @@ func TestAsyncCreateError(t *testing.T) {
}
}
type UnregisteredAPIObject struct {
Value string
}
func (*UnregisteredAPIObject) IsAnAPIObject() {}
func TestWriteJSONDecodeError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
type T struct {
Value string
}
writeJSON(http.StatusOK, runtime.DefaultCodec, &T{"Undecodable"}, w)
writeJSON(http.StatusOK, runtime.DefaultCodec, &UnregisteredAPIObject{"Undecodable"}, w)
}))
status := expectApiStatus(t, "GET", server.URL, nil, http.StatusInternalServerError)
if status.Reason != api.StatusReasonUnknown {
t.Errorf("unexpected reason %#v", status)
}
if !strings.Contains(status.Message, "type apiserver.T is not registered") {
if !strings.Contains(status.Message, "type apiserver.UnregisteredAPIObject is not registered") {
t.Errorf("unexpected message %#v", status)
}
}
@ -705,7 +712,7 @@ func TestSyncCreateTimeout(t *testing.T) {
testOver := make(chan struct{})
defer close(testOver)
storage := SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
// Eliminate flakes by ensuring the create operation takes longer than this test.
<-testOver
return obj, nil
@ -716,7 +723,7 @@ func TestSyncCreateTimeout(t *testing.T) {
}, codec, "/prefix/version")
server := httptest.NewServer(handler)
simple := Simple{Name: "foo"}
simple := &Simple{Name: "foo"}
data, _ := codec.Encode(simple)
itemOut := expectApiStatus(t, "POST", server.URL+"/prefix/version/foo?sync=true&timeout=4ms", data, http.StatusAccepted)
if itemOut.Status != api.StatusWorking || itemOut.Details == nil || itemOut.Details.ID == "" {

View File

@ -28,12 +28,13 @@ import (
// TODO: remove dependency on api, apiserver should be generic
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
func TestOperation(t *testing.T) {
ops := NewOperations()
c := make(chan interface{})
c := make(chan runtime.Object)
op := ops.NewOperation(c)
// Allow context switch, so that op's ID can get added to the map and Get will work.
// This is just so we can test Get. Ordinary users have no need to call Get immediately
@ -41,7 +42,7 @@ func TestOperation(t *testing.T) {
time.Sleep(time.Millisecond)
go func() {
time.Sleep(500 * time.Millisecond)
c <- "All done"
c <- &Simple{JSONBase: api.JSONBase{ID: "All done"}}
}()
if op.expired(time.Now().Add(-time.Minute)) {
@ -89,7 +90,7 @@ func TestOperation(t *testing.T) {
t.Errorf("expire failed to remove the operation %#v", ops)
}
if op.result.(string) != "All done" {
if op.result.(*Simple).ID != "All done" {
t.Errorf("Got unexpected result: %#v", op.result)
}
}
@ -98,7 +99,7 @@ func TestOperationsList(t *testing.T) {
testOver := make(chan struct{})
defer close(testOver)
simpleStorage := &SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
// Eliminate flakes by ensuring the create operation takes longer than this test.
<-testOver
return obj, nil
@ -111,7 +112,7 @@ func TestOperationsList(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{
simple := &Simple{
Name: "foo",
}
data, err := codec.Encode(simple)
@ -154,7 +155,7 @@ func TestOpGet(t *testing.T) {
testOver := make(chan struct{})
defer close(testOver)
simpleStorage := &SimpleRESTStorage{
injectedFunction: func(obj interface{}) (interface{}, error) {
injectedFunction: func(obj runtime.Object) (runtime.Object, error) {
// Eliminate flakes by ensuring the create operation takes longer than this test.
<-testOver
return obj, nil
@ -167,7 +168,7 @@ func TestOpGet(t *testing.T) {
server := httptest.NewServer(handler)
client := http.Client{}
simple := Simple{
simple := &Simple{
Name: "foo",
}
data, err := codec.Encode(simple)

View File

@ -26,12 +26,13 @@ import (
"code.google.com/p/go.net/websocket"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
var watchTestTable = []struct {
t watch.EventType
obj interface{}
obj runtime.Object
}{
{watch.Added, &Simple{Name: "A Name"}},
{watch.Modified, &Simple{Name: "Another Name"}},
@ -56,7 +57,7 @@ func TestWatchWebsocket(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
try := func(action watch.EventType, object interface{}) {
try := func(action watch.EventType, object runtime.Object) {
// Send
simpleStorage.fakeWatch.Action(action, object)
// Test receive
@ -113,7 +114,7 @@ func TestWatchHTTP(t *testing.T) {
decoder := json.NewDecoder(response.Body)
try := func(action watch.EventType, object interface{}) {
try := func(action watch.EventType, object runtime.Object) {
// Send
simpleStorage.fakeWatch.Action(action, object)
// Test receive

View File

@ -129,7 +129,7 @@ func (c *Fake) WatchServices(label, field labels.Selector, resourceVersion uint6
func (c *Fake) ListEndpoints(selector labels.Selector) (*api.EndpointsList, error) {
c.Actions = append(c.Actions, FakeAction{Action: "list-endpoints"})
return runtime.DefaultScheme.CopyOrDie(&c.EndpointsList).(*api.EndpointsList), nil
return runtime.DefaultScheme.CopyOrDie(&c.EndpointsList).(*api.EndpointsList), c.Err
}
func (c *Fake) WatchEndpoints(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) {

View File

@ -85,7 +85,7 @@ func newReplicationController(replicas int) api.ReplicationController {
}
}
func newPodList(count int) api.PodList {
func newPodList(count int) *api.PodList {
pods := []api.Pod{}
for i := 0; i < count; i++ {
pods = append(pods, api.Pod{
@ -94,7 +94,7 @@ func newPodList(count int) api.PodList {
},
})
}
return api.PodList{
return &api.PodList{
Items: pods,
}
}
@ -169,7 +169,7 @@ func TestSyncReplicationControllerCreates(t *testing.T) {
}
func TestCreateReplica(t *testing.T) {
body, _ := runtime.DefaultCodec.Encode(api.Pod{})
body, _ := runtime.DefaultCodec.Encode(&api.Pod{})
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
@ -292,7 +292,7 @@ func TestSyncronize(t *testing.T) {
}
fakeControllerHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: runtime.EncodeOrDie(&api.ReplicationControllerList{
ResponseBody: runtime.DefaultScheme.EncodeOrDie(&api.ReplicationControllerList{
Items: []api.ReplicationController{
controllerSpec1,
controllerSpec2,

View File

@ -27,6 +27,15 @@ import (
"github.com/golang/glog"
)
// Master is used to announce the current elected master.
type Master string
// IsAnAPIObject is used solely so we can work with the watch package.
// TODO: Either fix watch so this isn't necessary, or make this a real API Object.
// TODO: when it becomes clear how this package will be used, move these declarations to
// to the proper place.
func (Master) IsAnAPIObject() {}
// NewEtcdMasterElector returns an implementation of election.MasterElector backed by etcd.
func NewEtcdMasterElector(h tools.EtcdGetSet) MasterElector {
return &etcdMasterElector{etcd: h}
@ -58,7 +67,7 @@ func (e *etcdMasterElector) run(path, id string) {
case m := <-masters:
e.events <- watch.Event{
Type: watch.Modified,
Object: m,
Object: Master(m),
}
case e := <-errors:
glog.Errorf("error in election: %v", e)

View File

@ -31,7 +31,7 @@ func TestEtcdMasterOther(t *testing.T) {
master := NewEtcdMasterElector(etcd)
w := master.Elect(path, "bar")
result := <-w.ResultChan()
if result.Type != watch.Modified || result.Object.(string) != "baz" {
if result.Type != watch.Modified || result.Object.(Master) != "baz" {
t.Errorf("unexpected event: %#v", result)
}
w.Stop()
@ -52,7 +52,7 @@ func TestEtcdMasterNoOther(t *testing.T) {
master := NewEtcdMasterElector(e)
w := master.Elect(path, "bar")
result := <-w.ResultChan()
if result.Type != watch.Modified || result.Object.(string) != "bar" {
if result.Type != watch.Modified || result.Object.(Master) != "bar" {
t.Errorf("unexpected event: %#v", result)
}
w.Stop()
@ -91,7 +91,7 @@ func TestEtcdMasterNoOtherThenConflict(t *testing.T) {
master := NewEtcdMasterElector(e)
w := master.Elect(path, "bar")
result := <-w.ResultChan()
if result.Type != watch.Modified || result.Object.(string) != "bar" {
if result.Type != watch.Modified || result.Object.(Master) != "bar" {
t.Errorf("unexpected event: %#v", result)
}
w.Stop()

View File

@ -46,7 +46,7 @@ func TestUpdateWithPods(t *testing.T) {
}
Update("foo", &fakeClient, 0, "")
if len(fakeClient.Actions) != 5 {
t.Errorf("Unexpected action list %#v", fakeClient.Actions)
t.Fatalf("Unexpected action list %#v", fakeClient.Actions)
}
validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t)
validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[1], t)
@ -94,7 +94,7 @@ func TestUpdateWithNewImage(t *testing.T) {
}
validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t)
newCtrl := *runtime.CopyOrDie(fakeClient.Ctrl).(*api.ReplicationController)
newCtrl := runtime.DefaultScheme.CopyOrDie(&fakeClient.Ctrl).(*api.ReplicationController)
newCtrl.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image = "fooImage:2"
validateAction(client.FakeAction{Action: "update-controller", Value: newCtrl}, fakeClient.Actions[1], t)
@ -114,7 +114,7 @@ func TestRunController(t *testing.T) {
if len(fakeClient.Actions) != 1 || fakeClient.Actions[0].Action != "create-controller" {
t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
}
controller := fakeClient.Actions[0].Value.(api.ReplicationController)
controller := fakeClient.Actions[0].Value.(*api.ReplicationController)
if controller.ID != name ||
controller.DesiredState.Replicas != replicas ||
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
@ -133,7 +133,7 @@ func TestRunControllerWithService(t *testing.T) {
fakeClient.Actions[1].Action != "create-service" {
t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
}
controller := fakeClient.Actions[0].Value.(api.ReplicationController)
controller := fakeClient.Actions[0].Value.(*api.ReplicationController)
if controller.ID != name ||
controller.DesiredState.Replicas != replicas ||
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
@ -152,7 +152,7 @@ func TestStopController(t *testing.T) {
fakeClient.Actions[0].Value.(string) != name {
t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0])
}
controller := fakeClient.Actions[1].Value.(api.ReplicationController)
controller := fakeClient.Actions[1].Value.(*api.ReplicationController)
if fakeClient.Actions[1].Action != "update-controller" ||
controller.DesiredState.Replicas != 0 {
t.Errorf("Unexpected Action: %#v", fakeClient.Actions[1])
@ -171,7 +171,7 @@ func TestResizeController(t *testing.T) {
fakeClient.Actions[0].Value.(string) != name {
t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0])
}
controller := fakeClient.Actions[1].Value.(api.ReplicationController)
controller := fakeClient.Actions[1].Value.(*api.ReplicationController)
if fakeClient.Actions[1].Action != "update-controller" ||
controller.DesiredState.Replicas != 17 {
t.Errorf("Unexpected Action: %#v", fakeClient.Actions[1])

View File

@ -27,10 +27,11 @@ type Parser struct {
storageToType map[string]reflect.Type
}
func NewParser(objectMap map[string]interface{}) *Parser {
// NewParser creates a new parser.
func NewParser(objectMap map[string]runtime.Object) *Parser {
typeMap := make(map[string]reflect.Type)
for name, obj := range objectMap {
typeMap[name] = reflect.TypeOf(obj)
typeMap[name] = reflect.TypeOf(obj).Elem()
}
return &Parser{typeMap}
}

View File

@ -25,14 +25,14 @@ import (
)
func TestParseBadStorage(t *testing.T) {
p := NewParser(map[string]interface{}{})
p := NewParser(map[string]runtime.Object{})
_, err := p.ToWireFormat([]byte("{}"), "badstorage")
if err == nil {
t.Errorf("Expected error, received none")
}
}
func DoParseTest(t *testing.T, storage string, obj interface{}, p *Parser) {
func DoParseTest(t *testing.T, storage string, obj runtime.Object, p *Parser) {
jsonData, _ := runtime.DefaultCodec.Encode(obj)
yamlData, _ := yaml.Marshal(obj)
t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
@ -56,14 +56,14 @@ func DoParseTest(t *testing.T, storage string, obj interface{}, p *Parser) {
}
}
var testParser = NewParser(map[string]interface{}{
"pods": api.Pod{},
"services": api.Service{},
"replicationControllers": api.ReplicationController{},
var testParser = NewParser(map[string]runtime.Object{
"pods": &api.Pod{},
"services": &api.Service{},
"replicationControllers": &api.ReplicationController{},
})
func TestParsePod(t *testing.T) {
DoParseTest(t, "pods", api.Pod{
DoParseTest(t, "pods", &api.Pod{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "test pod", Kind: "Pod"},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
@ -80,7 +80,7 @@ func TestParsePod(t *testing.T) {
}
func TestParseService(t *testing.T) {
DoParseTest(t, "services", api.Service{
DoParseTest(t, "services", &api.Service{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "my service", Kind: "Service"},
Port: 8080,
Labels: map[string]string{
@ -93,7 +93,7 @@ func TestParseService(t *testing.T) {
}
func TestParseController(t *testing.T) {
DoParseTest(t, "replicationControllers", api.ReplicationController{
DoParseTest(t, "replicationControllers", &api.ReplicationController{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "my controller", Kind: "ReplicationController"},
DesiredState: api.ReplicationControllerState{
Replicas: 9001,
@ -119,13 +119,15 @@ type TestParseType struct {
Data string `json:"data" yaml:"data"`
}
func (*TestParseType) IsAnAPIObject() {}
func TestParseCustomType(t *testing.T) {
runtime.AddKnownTypes("", TestParseType{})
runtime.AddKnownTypes("v1beta1", TestParseType{})
parser := NewParser(map[string]interface{}{
"custom": TestParseType{},
runtime.DefaultScheme.AddKnownTypes("", &TestParseType{})
runtime.DefaultScheme.AddKnownTypes("v1beta1", &TestParseType{})
parser := NewParser(map[string]runtime.Object{
"custom": &TestParseType{},
})
DoParseTest(t, "custom", TestParseType{
DoParseTest(t, "custom", &TestParseType{
JSONBase: api.JSONBase{APIVersion: "", ID: "my custom object", Kind: "TestParseType"},
Data: "test data",
}, parser)

View File

@ -62,7 +62,7 @@ type YAMLPrinter struct{}
// Print parses the data as JSON, re-formats as YAML and prints the YAML.
func (y *YAMLPrinter) Print(data []byte, w io.Writer) error {
var obj runtime.Object
var obj interface{}
if err := json.Unmarshal(data, &obj); err != nil {
return err
}

View File

@ -67,7 +67,7 @@ func TestYAMLPrinterPrint(t *testing.T) {
t.Errorf("Test data and unmarshaled data are not equal: %#v vs %#v", poutput, testData)
}
obj := api.Pod{
obj := &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
}
buf.Reset()
@ -77,8 +77,8 @@ func TestYAMLPrinterPrint(t *testing.T) {
if err != nil {
t.Errorf("Unexpeted error: %#v", err)
}
if !reflect.DeepEqual(obj, objOut) {
t.Errorf("Unexpected inequality: %#v vs %#v", obj, objOut)
if !reflect.DeepEqual(obj, &objOut) {
t.Errorf("Unexpected inequality: %#v vs %#v", obj, &objOut)
}
}
@ -91,7 +91,7 @@ func TestIdentityPrinter(t *testing.T) {
t.Errorf("Bytes are not equal: %s vs %s", str, buff.String())
}
obj := api.Pod{
obj := &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
}
buff.Reset()
@ -100,7 +100,7 @@ func TestIdentityPrinter(t *testing.T) {
if err != nil {
t.Errorf("Unexpeted error: %#v", err)
}
if !reflect.DeepEqual(&obj, objOut) {
if !reflect.DeepEqual(obj, objOut) {
t.Errorf("Unexpected inequality: %#v vs %#v", obj, objOut)
}
}
@ -109,8 +109,12 @@ type TestPrintType struct {
Data string
}
func (*TestPrintType) IsAnAPIObject() {}
type TestUnknownType struct{}
func (*TestUnknownType) IsAnAPIObject() {}
func PrintCustomType(obj *TestPrintType, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s", obj.Data)
return err

View File

@ -41,7 +41,7 @@ func NewTestEtcdRegistry(client tools.EtcdClient) *Registry {
func TestEtcdGetPod(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.Set("/registry/pods/foo", runtime.EncodeOrDie(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/pods/foo", runtime.DefaultScheme.EncodeOrDie(&api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
pod, err := registry.GetPod("foo")
if err != nil {
@ -77,9 +77,9 @@ func TestEtcdCreatePod(t *testing.T) {
},
E: tools.EtcdErrorNotFound,
}
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(&api.ContainerManifestList{}), 0)
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.DefaultScheme.EncodeOrDie(&api.ContainerManifestList{}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreatePod(api.Pod{
err := registry.CreatePod(&api.Pod{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -133,13 +133,13 @@ func TestEtcdCreatePodAlreadyExisting(t *testing.T) {
fakeClient.Data["/registry/pods/foo"] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.Pod{JSONBase: api.JSONBase{ID: "foo"}}),
},
},
E: nil,
}
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreatePod(api.Pod{
err := registry.CreatePod(&api.Pod{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -165,7 +165,7 @@ func TestEtcdCreatePodWithContainersError(t *testing.T) {
E: tools.EtcdErrorValueRequired,
}
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreatePod(api.Pod{
err := registry.CreatePod(&api.Pod{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -205,7 +205,7 @@ func TestEtcdCreatePodWithContainersNotFound(t *testing.T) {
E: tools.EtcdErrorNotFound,
}
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreatePod(api.Pod{
err := registry.CreatePod(&api.Pod{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -264,13 +264,13 @@ func TestEtcdCreatePodWithExistingContainers(t *testing.T) {
},
E: tools.EtcdErrorNotFound,
}
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(api.ContainerManifestList{
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.DefaultScheme.EncodeOrDie(&api.ContainerManifestList{
Items: []api.ContainerManifest{
{ID: "bar"},
},
}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreatePod(api.Pod{
err := registry.CreatePod(&api.Pod{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -325,11 +325,11 @@ func TestEtcdDeletePod(t *testing.T) {
fakeClient.TestIndex = true
key := "/registry/pods/foo"
fakeClient.Set(key, runtime.EncodeOrDie(api.Pod{
fakeClient.Set(key, runtime.DefaultScheme.EncodeOrDie(&api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.PodState{Host: "machine"},
}), 0)
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(&api.ContainerManifestList{
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.DefaultScheme.EncodeOrDie(&api.ContainerManifestList{
Items: []api.ContainerManifest{
{ID: "foo"},
},
@ -361,11 +361,11 @@ func TestEtcdDeletePodMultipleContainers(t *testing.T) {
fakeClient.TestIndex = true
key := "/registry/pods/foo"
fakeClient.Set(key, runtime.EncodeOrDie(api.Pod{
fakeClient.Set(key, runtime.DefaultScheme.EncodeOrDie(&api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.PodState{Host: "machine"},
}), 0)
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(&api.ContainerManifestList{
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.DefaultScheme.EncodeOrDie(&api.ContainerManifestList{
Items: []api.ContainerManifest{
{ID: "foo"},
{ID: "bar"},
@ -445,13 +445,13 @@ func TestEtcdListPods(t *testing.T) {
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(api.Pod{
Value: runtime.DefaultScheme.EncodeOrDie(&api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.PodState{Host: "machine"},
}),
},
{
Value: runtime.EncodeOrDie(api.Pod{
Value: runtime.DefaultScheme.EncodeOrDie(&api.Pod{
JSONBase: api.JSONBase{ID: "bar"},
DesiredState: api.PodState{Host: "machine"},
}),
@ -520,10 +520,10 @@ func TestEtcdListControllers(t *testing.T) {
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}),
},
{
Value: runtime.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "bar"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.ReplicationController{JSONBase: api.JSONBase{ID: "bar"}}),
},
},
},
@ -543,7 +543,7 @@ func TestEtcdListControllers(t *testing.T) {
func TestEtcdGetController(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.Set("/registry/controllers/foo", runtime.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/controllers/foo", runtime.DefaultScheme.EncodeOrDie(&api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
ctrl, err := registry.GetController("foo")
if err != nil {
@ -593,7 +593,7 @@ func TestEtcdDeleteController(t *testing.T) {
func TestEtcdCreateController(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateController(api.ReplicationController{
err := registry.CreateController(&api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -619,10 +619,10 @@ func TestEtcdCreateController(t *testing.T) {
func TestEtcdCreateControllerAlreadyExisting(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.Set("/registry/controllers/foo", runtime.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/controllers/foo", runtime.DefaultScheme.EncodeOrDie(&api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateController(api.ReplicationController{
err := registry.CreateController(&api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
@ -636,9 +636,9 @@ func TestEtcdUpdateController(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.TestIndex = true
resp, _ := fakeClient.Set("/registry/controllers/foo", runtime.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
resp, _ := fakeClient.Set("/registry/controllers/foo", runtime.DefaultScheme.EncodeOrDie(&api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.UpdateController(api.ReplicationController{
err := registry.UpdateController(&api.ReplicationController{
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex},
DesiredState: api.ReplicationControllerState{
Replicas: 2,
@ -662,10 +662,10 @@ func TestEtcdListServices(t *testing.T) {
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "foo"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.Service{JSONBase: api.JSONBase{ID: "foo"}}),
},
{
Value: runtime.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "bar"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.Service{JSONBase: api.JSONBase{ID: "bar"}}),
},
},
},
@ -686,7 +686,7 @@ func TestEtcdListServices(t *testing.T) {
func TestEtcdCreateService(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateService(api.Service{
err := registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo"},
})
if err != nil {
@ -711,9 +711,9 @@ func TestEtcdCreateService(t *testing.T) {
func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.Set("/registry/services/specs/foo", runtime.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/services/specs/foo", runtime.DefaultScheme.EncodeOrDie(&api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateService(api.Service{
err := registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo"},
})
if !errors.IsAlreadyExists(err) {
@ -723,7 +723,7 @@ func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
func TestEtcdGetService(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.Set("/registry/services/specs/foo", runtime.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/services/specs/foo", runtime.DefaultScheme.EncodeOrDie(&api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
service, err := registry.GetService("foo")
if err != nil {
@ -775,7 +775,7 @@ func TestEtcdUpdateService(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.TestIndex = true
resp, _ := fakeClient.Set("/registry/services/specs/foo", runtime.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
resp, _ := fakeClient.Set("/registry/services/specs/foo", runtime.DefaultScheme.EncodeOrDie(&api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
testService := api.Service{
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex},
@ -786,7 +786,7 @@ func TestEtcdUpdateService(t *testing.T) {
"baz": "bar",
},
}
err := registry.UpdateService(testService)
err := registry.UpdateService(&testService)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@ -812,10 +812,10 @@ func TestEtcdListEndpoints(t *testing.T) {
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:8345"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:8345"}}),
},
{
Value: runtime.EncodeOrDie(api.Endpoints{JSONBase: api.JSONBase{ID: "bar"}}),
Value: runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "bar"}}),
},
},
},
@ -841,7 +841,7 @@ func TestEtcdGetEndpoints(t *testing.T) {
Endpoints: []string{"127.0.0.1:34855"},
}
fakeClient.Set("/registry/services/endpoints/foo", runtime.EncodeOrDie(endpoints), 0)
fakeClient.Set("/registry/services/endpoints/foo", runtime.DefaultScheme.EncodeOrDie(endpoints), 0)
got, err := registry.GetEndpoints("foo")
if err != nil {
@ -862,9 +862,9 @@ func TestEtcdUpdateEndpoints(t *testing.T) {
Endpoints: []string{"baz", "bar"},
}
fakeClient.Set("/registry/services/endpoints/foo", runtime.EncodeOrDie(api.Endpoints{}), 0)
fakeClient.Set("/registry/services/endpoints/foo", runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{}), 0)
err := registry.UpdateEndpoints(endpoints)
err := registry.UpdateEndpoints(&endpoints)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

View File

@ -28,10 +28,10 @@ func TestMinionRegistryStorage(t *testing.T) {
m := NewRegistry([]string{"foo", "bar"})
ms := NewRegistryStorage(m)
if obj, err := ms.Get("foo"); err != nil || obj.(api.Minion).ID != "foo" {
if obj, err := ms.Get("foo"); err != nil || obj.(*api.Minion).ID != "foo" {
t.Errorf("missing expected object")
}
if obj, err := ms.Get("bar"); err != nil || obj.(api.Minion).ID != "bar" {
if obj, err := ms.Get("bar"); err != nil || obj.(*api.Minion).ID != "bar" {
t.Errorf("missing expected object")
}
if _, err := ms.Get("baz"); err != ErrDoesNotExist {
@ -43,10 +43,10 @@ func TestMinionRegistryStorage(t *testing.T) {
t.Errorf("insert failed")
}
obj := <-c
if m, ok := obj.(api.Minion); !ok || m.ID != "baz" {
if m, ok := obj.(*api.Minion); !ok || m.ID != "baz" {
t.Errorf("insert return value was weird: %#v", obj)
}
if obj, err := ms.Get("baz"); err != nil || obj.(api.Minion).ID != "baz" {
if obj, err := ms.Get("baz"); err != nil || obj.(*api.Minion).ID != "baz" {
t.Errorf("insert didn't actually insert")
}
@ -78,7 +78,7 @@ func TestMinionRegistryStorage(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"},
},
}
if !reflect.DeepEqual(list.(api.MinionList).Items, expect) {
if !reflect.DeepEqual(list.(*api.MinionList).Items, expect) {
t.Errorf("Unexpected list value: %#v", list)
}
}

View File

@ -32,7 +32,7 @@ import (
"github.com/fsouza/go-dockerclient"
)
func expectApiStatusError(t *testing.T, ch <-chan interface{}, msg string) {
func expectApiStatusError(t *testing.T, ch <-chan runtime.Object, msg string) {
out := <-ch
status, ok := out.(*api.Status)
if !ok {
@ -44,7 +44,7 @@ func expectApiStatusError(t *testing.T, ch <-chan interface{}, msg string) {
}
}
func expectPod(t *testing.T, ch <-chan interface{}) (*api.Pod, bool) {
func expectPod(t *testing.T, ch <-chan runtime.Object) (*api.Pod, bool) {
out := <-ch
pod, ok := out.(*api.Pod)
if !ok || pod == nil {

View File

@ -35,11 +35,11 @@ func (r *ControllerRegistry) GetController(ID string) (*api.ReplicationControlle
return &api.ReplicationController{}, r.Err
}
func (r *ControllerRegistry) CreateController(controller api.ReplicationController) error {
func (r *ControllerRegistry) CreateController(controller *api.ReplicationController) error {
return r.Err
}
func (r *ControllerRegistry) UpdateController(controller api.ReplicationController) error {
func (r *ControllerRegistry) UpdateController(controller *api.ReplicationController) error {
return r.Err
}

View File

@ -68,19 +68,19 @@ func (r *PodRegistry) GetPod(podId string) (*api.Pod, error) {
return r.Pod, r.Err
}
func (r *PodRegistry) CreatePod(pod api.Pod) error {
func (r *PodRegistry) CreatePod(pod *api.Pod) error {
r.Lock()
defer r.Unlock()
r.Pod = &pod
r.mux.Action(watch.Added, &pod)
r.Pod = pod
r.mux.Action(watch.Added, pod)
return r.Err
}
func (r *PodRegistry) UpdatePod(pod api.Pod) error {
func (r *PodRegistry) UpdatePod(pod *api.Pod) error {
r.Lock()
defer r.Unlock()
r.Pod = &pod
r.mux.Action(watch.Modified, &pod)
r.Pod = pod
r.mux.Action(watch.Modified, pod)
return r.Err
}

View File

@ -42,9 +42,9 @@ func (r *ServiceRegistry) ListServices() (*api.ServiceList, error) {
return &r.List, r.Err
}
func (r *ServiceRegistry) CreateService(svc api.Service) error {
r.Service = &svc
r.List.Items = append(r.List.Items, svc)
func (r *ServiceRegistry) CreateService(svc *api.Service) error {
r.Service = svc
r.List.Items = append(r.List.Items, *svc)
return r.Err
}
@ -58,7 +58,7 @@ func (r *ServiceRegistry) DeleteService(id string) error {
return r.Err
}
func (r *ServiceRegistry) UpdateService(svc api.Service) error {
func (r *ServiceRegistry) UpdateService(svc *api.Service) error {
r.UpdatedID = svc.ID
return r.Err
}
@ -76,8 +76,8 @@ func (r *ServiceRegistry) GetEndpoints(id string) (*api.Endpoints, error) {
return &r.Endpoints, r.Err
}
func (r *ServiceRegistry) UpdateEndpoints(e api.Endpoints) error {
r.Endpoints = e
func (r *ServiceRegistry) UpdateEndpoints(e *api.Endpoints) error {
r.Endpoints = *e
return r.Err
}

View File

@ -89,7 +89,7 @@ func TestServiceStorageValidatesCreate(t *testing.T) {
func TestServiceRegistryUpdate(t *testing.T) {
registry := registrytest.NewServiceRegistry()
registry.CreateService(api.Service{
registry.CreateService(&api.Service{
Port: 6502,
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz1"},
@ -118,7 +118,7 @@ func TestServiceRegistryUpdate(t *testing.T) {
func TestServiceStorageValidatesUpdate(t *testing.T) {
registry := registrytest.NewServiceRegistry()
registry.CreateService(api.Service{
registry.CreateService(&api.Service{
Port: 6502,
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
@ -200,7 +200,7 @@ func TestServiceRegistryDelete(t *testing.T) {
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
svc := api.Service{
svc := &api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
}
@ -220,7 +220,7 @@ func TestServiceRegistryDeleteExternal(t *testing.T) {
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
svc := api.Service{
svc := &api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
CreateExternalLoadBalancer: true,
@ -263,7 +263,7 @@ func TestServiceRegistryGet(t *testing.T) {
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
registry.CreateService(api.Service{
registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
})
@ -282,7 +282,7 @@ func TestServiceRegistryResourceLocation(t *testing.T) {
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
registry.CreateService(api.Service{
registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
})
@ -310,11 +310,11 @@ func TestServiceRegistryList(t *testing.T) {
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
registry.CreateService(api.Service{
registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
})
registry.CreateService(api.Service{
registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo2"},
Selector: map[string]string{"bar2": "baz2"},
})

View File

@ -24,7 +24,6 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/coreos/go-etcd/etcd"
@ -40,15 +39,16 @@ type TestResource struct {
Value int `json:"value" yaml:"value,omitempty"`
}
var scheme *conversion.Scheme
func (*TestResource) IsAnAPIObject() {}
var scheme *runtime.Scheme
var codec = runtime.DefaultCodec
var versioner = runtime.DefaultResourceVersioner
func init() {
scheme = conversion.NewScheme()
scheme.ExternalVersion = "v1beta1"
scheme.AddKnownTypes("", TestResource{})
scheme.AddKnownTypes("v1beta1", TestResource{})
scheme = runtime.NewScheme("", "v1beta1")
scheme.AddKnownTypes("", &TestResource{})
scheme.AddKnownTypes("v1beta1", &TestResource{})
}
func TestIsEtcdNotFound(t *testing.T) {
@ -166,7 +166,7 @@ func TestExtractObjNotFoundErr(t *testing.T) {
}
func TestSetObj(t *testing.T) {
obj := api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
obj := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
fakeClient := NewFakeEtcdClient(t)
helper := EtcdHelper{fakeClient, codec, versioner}
err := helper.SetObj("/some/key", obj)
@ -191,7 +191,7 @@ func TestSetObjWithVersion(t *testing.T) {
fakeClient.Data["/some/key"] = EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(obj),
Value: runtime.DefaultScheme.EncodeOrDie(obj),
ModifiedIndex: 1,
},
},
@ -214,7 +214,7 @@ func TestSetObjWithVersion(t *testing.T) {
}
func TestSetObjWithoutResourceVersioner(t *testing.T) {
obj := api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
obj := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
fakeClient := NewFakeEtcdClient(t)
helper := EtcdHelper{fakeClient, codec, nil}
err := helper.SetObj("/some/key", obj)
@ -241,7 +241,7 @@ func TestAtomicUpdate(t *testing.T) {
// Create a new node.
fakeClient.ExpectNotFoundGet("/some/key")
obj := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: 1}
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in runtime.Object) (runtime.Object, error) {
return obj, nil
})
if err != nil {
@ -260,7 +260,7 @@ func TestAtomicUpdate(t *testing.T) {
// Update an existing node.
callbackCalled := false
objUpdate := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: 2}
err = helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
err = helper.AtomicUpdate("/some/key", &TestResource{}, func(in runtime.Object) (runtime.Object, error) {
callbackCalled = true
if in.(*TestResource).Value != 1 {
@ -295,7 +295,7 @@ func TestAtomicUpdateNoChange(t *testing.T) {
// Create a new node.
fakeClient.ExpectNotFoundGet("/some/key")
obj := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: 1}
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in runtime.Object) (runtime.Object, error) {
return obj, nil
})
if err != nil {
@ -306,7 +306,7 @@ func TestAtomicUpdateNoChange(t *testing.T) {
callbackCalled := false
objUpdate := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: 1}
fakeClient.Err = errors.New("should not be called")
err = helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
err = helper.AtomicUpdate("/some/key", &TestResource{}, func(in runtime.Object) (runtime.Object, error) {
callbackCalled = true
return objUpdate, nil
})
@ -338,7 +338,7 @@ func TestAtomicUpdate_CreateCollision(t *testing.T) {
defer wgDone.Done()
firstCall := true
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in runtime.Object) (runtime.Object, error) {
defer func() { firstCall = false }()
if firstCall {
@ -348,7 +348,7 @@ func TestAtomicUpdate_CreateCollision(t *testing.T) {
}
currValue := in.(*TestResource).Value
obj := TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: currValue + 1}
obj := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: currValue + 1}
return obj, nil
})
if err != nil {

View File

@ -33,7 +33,7 @@ func TestWatchInterpretations(t *testing.T) {
podFoo := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
podBar := &api.Pod{JSONBase: api.JSONBase{ID: "bar"}}
podBaz := &api.Pod{JSONBase: api.JSONBase{ID: "baz"}}
firstLetterIsB := func(obj interface{}) bool {
firstLetterIsB := func(obj runtime.Object) bool {
return obj.(*api.Pod).ID[0] == 'b'
}
@ -44,66 +44,66 @@ func TestWatchInterpretations(t *testing.T) {
nodeValue string
expectEmit bool
expectType watch.EventType
expectObject interface{}
expectObject runtime.Object
}{
"create": {
actions: []string{"create", "get"},
nodeValue: runtime.EncodeOrDie(podBar),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podBar),
expectEmit: true,
expectType: watch.Added,
expectObject: podBar,
},
"create but filter blocks": {
actions: []string{"create", "get"},
nodeValue: runtime.EncodeOrDie(podFoo),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podFoo),
expectEmit: false,
},
"delete": {
actions: []string{"delete"},
prevNodeValue: runtime.EncodeOrDie(podBar),
prevNodeValue: runtime.DefaultScheme.EncodeOrDie(podBar),
expectEmit: true,
expectType: watch.Deleted,
expectObject: podBar,
},
"delete but filter blocks": {
actions: []string{"delete"},
nodeValue: runtime.EncodeOrDie(podFoo),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podFoo),
expectEmit: false,
},
"modify appears to create 1": {
actions: []string{"set", "compareAndSwap"},
nodeValue: runtime.EncodeOrDie(podBar),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podBar),
expectEmit: true,
expectType: watch.Added,
expectObject: podBar,
},
"modify appears to create 2": {
actions: []string{"set", "compareAndSwap"},
prevNodeValue: runtime.EncodeOrDie(podFoo),
nodeValue: runtime.EncodeOrDie(podBar),
prevNodeValue: runtime.DefaultScheme.EncodeOrDie(podFoo),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podBar),
expectEmit: true,
expectType: watch.Added,
expectObject: podBar,
},
"modify appears to delete": {
actions: []string{"set", "compareAndSwap"},
prevNodeValue: runtime.EncodeOrDie(podBar),
nodeValue: runtime.EncodeOrDie(podFoo),
prevNodeValue: runtime.DefaultScheme.EncodeOrDie(podBar),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podFoo),
expectEmit: true,
expectType: watch.Deleted,
expectObject: podBar, // Should return last state that passed the filter!
},
"modify modifies": {
actions: []string{"set", "compareAndSwap"},
prevNodeValue: runtime.EncodeOrDie(podBar),
nodeValue: runtime.EncodeOrDie(podBaz),
prevNodeValue: runtime.DefaultScheme.EncodeOrDie(podBar),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podBaz),
expectEmit: true,
expectType: watch.Modified,
expectObject: podBaz,
},
"modify ignores": {
actions: []string{"set", "compareAndSwap"},
nodeValue: runtime.EncodeOrDie(podFoo),
nodeValue: runtime.DefaultScheme.EncodeOrDie(podFoo),
expectEmit: false,
},
}
@ -259,7 +259,7 @@ func TestWatchEtcdState(t *testing.T) {
{
Action: "create",
Node: &etcd.Node{
Value: string(runtime.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
Value: string(runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
},
},
},
@ -273,12 +273,12 @@ func TestWatchEtcdState(t *testing.T) {
{
Action: "compareAndSwap",
Node: &etcd.Node{
Value: string(runtime.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:9000"}})),
Value: string(runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:9000"}})),
CreatedIndex: 1,
ModifiedIndex: 2,
},
PrevNode: &etcd.Node{
Value: string(runtime.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
Value: string(runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
CreatedIndex: 1,
ModifiedIndex: 1,
},
@ -295,7 +295,7 @@ func TestWatchEtcdState(t *testing.T) {
R: &etcd.Response{
Action: "get",
Node: &etcd.Node{
Value: string(runtime.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
Value: string(runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
CreatedIndex: 1,
ModifiedIndex: 1,
},
@ -308,12 +308,12 @@ func TestWatchEtcdState(t *testing.T) {
{
Action: "compareAndSwap",
Node: &etcd.Node{
Value: string(runtime.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:9000"}})),
Value: string(runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:9000"}})),
CreatedIndex: 1,
ModifiedIndex: 2,
},
PrevNode: &etcd.Node{
Value: string(runtime.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
Value: string(runtime.DefaultScheme.EncodeOrDie(&api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{}})),
CreatedIndex: 1,
ModifiedIndex: 1,
},
@ -370,7 +370,7 @@ func TestWatchFromZeroIndex(t *testing.T) {
EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(pod),
Value: runtime.DefaultScheme.EncodeOrDie(pod),
CreatedIndex: 1,
ModifiedIndex: 1,
},
@ -385,7 +385,7 @@ func TestWatchFromZeroIndex(t *testing.T) {
EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(pod),
Value: runtime.DefaultScheme.EncodeOrDie(pod),
CreatedIndex: 1,
ModifiedIndex: 2,
},
@ -443,13 +443,13 @@ func TestWatchListFromZeroIndex(t *testing.T) {
Dir: true,
Nodes: etcd.Nodes{
&etcd.Node{
Value: runtime.EncodeOrDie(pod),
Value: runtime.DefaultScheme.EncodeOrDie(pod),
CreatedIndex: 1,
ModifiedIndex: 1,
Nodes: etcd.Nodes{},
},
&etcd.Node{
Value: runtime.EncodeOrDie(pod),
Value: runtime.DefaultScheme.EncodeOrDie(pod),
CreatedIndex: 2,
ModifiedIndex: 2,
Nodes: etcd.Nodes{},

View File

@ -24,6 +24,11 @@ type FilterFunc func(in Event) (out Event, keep bool)
// Putting a filter on a watch, as an unavoidable side-effect due to the way
// go channels work, effectively causes the watch's event channel to have its
// queue length increased by one.
//
// WARNING: filter has a fatal flaw, in that it can't properly update the
// Type field (Add/Modified/Deleted) to reflect items beginning to pass the
// filter when they previously didn't.
//
func Filter(w Interface, f FilterFunc) Interface {
fw := &filteredWatch{
incoming: w,

View File

@ -23,16 +23,16 @@ import (
func TestFilter(t *testing.T) {
table := []Event{
{Added, "foo"},
{Added, "bar"},
{Added, "baz"},
{Added, "qux"},
{Added, "zoo"},
{Added, testType("foo")},
{Added, testType("bar")},
{Added, testType("baz")},
{Added, testType("qux")},
{Added, testType("zoo")},
}
source := NewFake()
filtered := Filter(source, func(e Event) (Event, bool) {
return e, e.Object.(string)[0] != 'b'
return e, e.Object.(testType)[0] != 'b'
})
go func() {
@ -48,7 +48,7 @@ func TestFilter(t *testing.T) {
if !ok {
break
}
got = append(got, event.Object.(string))
got = append(got, string(event.Object.(testType)))
}
if e, a := []string{"foo", "qux", "zoo"}, got; !reflect.DeepEqual(e, a) {
@ -59,11 +59,11 @@ func TestFilter(t *testing.T) {
func TestFilterStop(t *testing.T) {
source := NewFake()
filtered := Filter(source, func(e Event) (Event, bool) {
return e, e.Object.(string)[0] != 'b'
return e, e.Object.(testType)[0] != 'b'
})
go func() {
source.Add("foo")
source.Add(testType("foo"))
filtered.Stop()
}()
@ -73,7 +73,7 @@ func TestFilterStop(t *testing.T) {
if !ok {
break
}
got = append(got, event.Object.(string))
got = append(got, string(event.Object.(testType)))
}
if e, a := []string{"foo"}, got; !reflect.DeepEqual(e, a) {

View File

@ -20,13 +20,15 @@ import (
"io"
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
type fakeDecoder struct {
items chan Event
}
func (f fakeDecoder) Decode() (action EventType, object interface{}, err error) {
func (f fakeDecoder) Decode() (action EventType, object runtime.Object, err error) {
item, open := <-f.items
if !open {
return action, nil, io.EOF
@ -40,7 +42,7 @@ func (f fakeDecoder) Close() {
func TestStreamWatcher(t *testing.T) {
table := []Event{
{Added, "foo"},
{Added, testType("foo")},
}
fd := fakeDecoder{make(chan Event, 5)}

View File

@ -20,17 +20,21 @@ import (
"testing"
)
type testType string
func (testType) IsAnAPIObject() {}
func TestFake(t *testing.T) {
f := NewFake()
table := []struct {
t EventType
s string
s testType
}{
{Added, "foo"},
{Modified, "qux"},
{Modified, "bar"},
{Deleted, "bar"},
{Added, testType("foo")},
{Modified, testType("qux")},
{Modified, testType("bar")},
{Deleted, testType("bar")},
}
// Prove that f implements Interface by phrasing this as a function.
@ -43,7 +47,7 @@ func TestFake(t *testing.T) {
if e, a := expect.t, got.Type; e != a {
t.Fatalf("Expected %v, got %v", e, a)
}
if a, ok := got.Object.(string); !ok || a != expect.s {
if a, ok := got.Object.(testType); !ok || a != expect.s {
t.Fatalf("Expected %v, got %v", expect.s, a)
}
}
@ -54,10 +58,10 @@ func TestFake(t *testing.T) {
}
sender := func() {
f.Add("foo")
f.Action(Modified, "qux")
f.Modify("bar")
f.Delete("bar")
f.Add(testType("foo"))
f.Action(Modified, testType("qux"))
f.Modify(testType("bar"))
f.Delete(testType("bar"))
f.Stop()
}

View File

@ -113,7 +113,7 @@ func TestPollMinions(t *testing.T) {
ml := &api.MinionList{Items: item.minions}
handler := util.FakeHandler{
StatusCode: 200,
ResponseBody: runtime.EncodeOrDie(ml),
ResponseBody: runtime.DefaultScheme.EncodeOrDie(ml),
T: t,
}
mux := http.NewServeMux()
@ -140,7 +140,7 @@ func TestDefaultErrorFunc(t *testing.T) {
testPod := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
handler := util.FakeHandler{
StatusCode: 200,
ResponseBody: runtime.EncodeOrDie(testPod),
ResponseBody: runtime.DefaultScheme.EncodeOrDie(testPod),
T: t,
}
mux := http.NewServeMux()
@ -259,7 +259,7 @@ func TestBind(t *testing.T) {
t.Errorf("Unexpected error: %v", err)
continue
}
expectedBody := runtime.EncodeOrDie(item.binding)
expectedBody := runtime.DefaultScheme.EncodeOrDie(item.binding)
handler.ValidateRequest(t, "/api/v1beta1/bindings", "POST", &expectedBody)
}
}

View File

@ -62,7 +62,7 @@ func TestClient(t *testing.T) {
}
// get a validation error
pod := api.Pod{
pod := &api.Pod{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta2",

View File

@ -33,17 +33,22 @@ func init() {
type stringCodec struct{}
func (c stringCodec) Encode(obj interface{}) ([]byte, error) {
return []byte(obj.(string)), nil
type fakeAPIObject string
func (*fakeAPIObject) IsAnAPIObject() {}
func (c stringCodec) Encode(obj runtime.Object) ([]byte, error) {
return []byte(*obj.(*fakeAPIObject)), nil
}
func (c stringCodec) Decode(data []byte) (interface{}, error) {
return string(data), nil
func (c stringCodec) Decode(data []byte) (runtime.Object, error) {
o := fakeAPIObject(data)
return &o, nil
}
func (c stringCodec) DecodeInto(data []byte, obj interface{}) error {
o := obj.(*string)
*o = string(data)
func (c stringCodec) DecodeInto(data []byte, obj runtime.Object) error {
o := obj.(*fakeAPIObject)
*o = fakeAPIObject(data)
return nil
}
@ -51,7 +56,8 @@ func TestSetObj(t *testing.T) {
client := newEtcdClient()
helper := tools.EtcdHelper{Client: client, Codec: stringCodec{}}
withEtcdKey(func(key string) {
if err := helper.SetObj(key, "object"); err != nil {
fakeObject := fakeAPIObject("object")
if err := helper.SetObj(key, &fakeObject); err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp, err := client.Get(key, false, false)
@ -72,7 +78,7 @@ func TestExtractObj(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
s := ""
s := fakeAPIObject("")
if err := helper.ExtractObj(key, &s, false); err != nil {
t.Fatalf("unexpected error: %v", err)
}
@ -86,7 +92,7 @@ func TestWatch(t *testing.T) {
client := newEtcdClient()
helper := tools.EtcdHelper{Client: client, Codec: runtime.DefaultCodec, ResourceVersioner: runtime.DefaultResourceVersioner}
withEtcdKey(func(key string) {
resp, err := client.Set(key, runtime.EncodeOrDie(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0)
resp, err := client.Set(key, runtime.DefaultScheme.EncodeOrDie(&api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}