func NewTestGenericEtcdRegistry(t *testing.T) (*tools.FakeEtcdClient, *Etcd) { f := tools.NewFakeEtcdClient(t) f.TestIndex = true h := tools.EtcdHelper{f, testapi.Codec(), tools.RuntimeVersionAdapter{testapi.MetadataAccessor()}} return f, &Etcd{ NewFunc: func() runtime.Object { return &api.Pod{} }, NewListFunc: func() runtime.Object { return &api.PodList{} }, EndpointName: "pods", KeyRoot: "/registry/pods", KeyFunc: func(id string) string { return path.Join("/registry/pods", id) }, Helper: h, } }
func TestHelperUpdate(t *testing.T) { expectPut := func(req *http.Request) bool { if req.Method != "PUT" { t.Errorf("unexpected method: %#v", req) return false } parts := splitPath(req.URL.Path) if parts[1] != "bar" { t.Errorf("url doesn't contain namespace: %#v", req.URL) return false } if parts[2] != "foo" { t.Errorf("url doesn't contain name: %#v", req) return false } return true } tests := []struct { Resp *http.Response RespFunc client.HTTPClientFunc HttpErr error Overwrite bool Object runtime.Object ExpectObject runtime.Object Err bool Req func(*http.Request) bool }{ { HttpErr: errors.New("failure"), Err: true, }, { Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, Resp: &http.Response{ StatusCode: http.StatusNotFound, Body: objBody(&api.Status{Status: api.StatusFailure}), }, Err: true, }, { Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, ExpectObject: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, Resp: &http.Response{ StatusCode: http.StatusOK, Body: objBody(&api.Status{Status: api.StatusSuccess}), }, Req: expectPut, }, { Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, ExpectObject: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}}, Overwrite: true, RespFunc: func(req *http.Request) (*http.Response, error) { if req.Method == "PUT" { return &http.Response{StatusCode: http.StatusOK, Body: objBody(&api.Status{Status: api.StatusSuccess})}, nil } return &http.Response{StatusCode: http.StatusOK, Body: objBody(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}})}, nil }, Req: expectPut, }, { Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}}, ExpectObject: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}}, Resp: &http.Response{StatusCode: http.StatusOK, Body: objBody(&api.Status{Status: api.StatusSuccess})}, Req: expectPut, }, } for i, test := range tests { client := &client.FakeRESTClient{ Codec: testapi.Codec(), Resp: test.Resp, Err: test.HttpErr, } if test.RespFunc != nil { client.Client = test.RespFunc } modifier := &Helper{ RESTClient: client, Codec: testapi.Codec(), Versioner: testapi.MetadataAccessor(), } data := []byte{} if test.Object != nil { data = []byte(runtime.EncodeOrDie(testapi.Codec(), test.Object)) } err := modifier.Update("bar", "foo", test.Overwrite, data) if (err != nil) != test.Err { t.Errorf("%d: unexpected error: %t %v", i, test.Err, err) } if err != nil { continue } if test.Req != nil && !test.Req(client.Req) { t.Errorf("%d: unexpected request: %#v", i, client.Req) } body, err := ioutil.ReadAll(client.Req.Body) if err != nil { t.Fatalf("%d: unexpected error: %#v", i, err) } t.Logf("got body: %s", string(body)) expect := []byte{} if test.ExpectObject != nil { expect = []byte(runtime.EncodeOrDie(testapi.Codec(), test.ExpectObject)) } if !reflect.DeepEqual(expect, body) { t.Errorf("%d: unexpected body: %s", i, string(body)) } } }
func NewTestEventEtcdRegistry(t *testing.T) (*tools.FakeEtcdClient, generic.Registry) { f := tools.NewFakeEtcdClient(t) f.TestIndex = true h := tools.EtcdHelper{f, testapi.Codec(), tools.RuntimeVersionAdapter{testapi.MetadataAccessor()}} return f, NewEtcdRegistry(h, testTTL) }
func TestRESTHelperCreate(t *testing.T) { expectPost := func(req *http.Request) bool { if req.Method != "POST" { t.Errorf("unexpected method: %#v", req) return false } if req.URL.Query().Get("namespace") != "bar" { t.Errorf("url doesn't contain namespace: %#v", req) return false } return true } tests := []struct { Resp *http.Response RespFunc httpClientFunc HttpErr error Modify bool Object runtime.Object ExpectObject runtime.Object Err bool Req func(*http.Request) bool }{ { HttpErr: errors.New("failure"), Err: true, }, { Resp: &http.Response{ StatusCode: http.StatusNotFound, Body: objBody(&api.Status{Status: api.StatusFailure}), }, Err: true, }, { Resp: &http.Response{ StatusCode: http.StatusOK, Body: objBody(&api.Status{Status: api.StatusSuccess}), }, Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, ExpectObject: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, Req: expectPost, }, { Modify: false, Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}}, ExpectObject: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}}, Resp: &http.Response{StatusCode: http.StatusOK, Body: objBody(&api.Status{Status: api.StatusSuccess})}, Req: expectPost, }, { Modify: true, Object: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10"}}, ExpectObject: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, Resp: &http.Response{StatusCode: http.StatusOK, Body: objBody(&api.Status{Status: api.StatusSuccess})}, Req: expectPost, }, } for i, test := range tests { client := &FakeRESTClient{ Resp: test.Resp, Err: test.HttpErr, } if test.RespFunc != nil { client.Client = test.RespFunc } modifier := &RESTHelper{ RESTClient: client, Codec: testapi.Codec(), Versioner: testapi.MetadataAccessor(), } data := []byte{} if test.Object != nil { data = []byte(runtime.EncodeOrDie(testapi.Codec(), test.Object)) } err := modifier.Create("bar", test.Modify, data) if (err != nil) != test.Err { t.Errorf("%d: unexpected error: %f %v", i, test.Err, err) } if err != nil { continue } if test.Req != nil && !test.Req(client.Req) { t.Errorf("%d: unexpected request: %#v", i, client.Req) } body, err := ioutil.ReadAll(client.Req.Body) if err != nil { t.Fatalf("%d: unexpected error: %#v", i, err) } t.Logf("got body: %s", string(body)) expect := []byte{} if test.ExpectObject != nil { expect = []byte(runtime.EncodeOrDie(testapi.Codec(), test.ExpectObject)) } if !reflect.DeepEqual(expect, body) { t.Errorf("%d: unexpected body: %s", i, string(body)) } } }