From 3cdf6480f05218d6d427e48316d7284b2598f279 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 19 Jun 2014 14:27:52 +0100 Subject: [PATCH 1/2] Assert that stubbed requests are actually called in dockerclient test Signed-off-by: Aanand Prasad --- backends/dockerclient_test.go | 105 ++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 29 deletions(-) diff --git a/backends/dockerclient_test.go b/backends/dockerclient_test.go index 4c5948893a..cd8e5405c0 100644 --- a/backends/dockerclient_test.go +++ b/backends/dockerclient_test.go @@ -57,6 +57,7 @@ func TestLs(t *testing.T) { if !reflect.DeepEqual(names, expected) { t.Fatalf("expected %#v, got %#v", expected, names) } + server.Check() } func TestSpawn(t *testing.T) { @@ -73,6 +74,7 @@ func TestSpawn(t *testing.T) { if err != nil { t.Fatal(err) } + server.Check() } func TestAttachToChild(t *testing.T) { @@ -85,6 +87,7 @@ func TestAttachToChild(t *testing.T) { }) i := instance(t, server) child(t, server, i, name) + server.Check() } func TestStartChild(t *testing.T) { @@ -107,6 +110,7 @@ func TestStartChild(t *testing.T) { if err != nil { t.Fatal(err) } + server.Check() } func TestStopChild(t *testing.T) { @@ -128,42 +132,85 @@ func TestStopChild(t *testing.T) { if err != nil { t.Fatal(err) } + server.Check() } -func makeServer(t *testing.T, stubs ...*requestStub) *httptest.Server { +type stubRecord struct { + stub *requestStub + summary string + callCount int +} + +type stubServer struct { + *httptest.Server + + stubs []*stubRecord + t *testing.T +} + +func makeServer(t *testing.T, stubs ...*requestStub) *stubServer { + s := &stubServer{ + stubs: []*stubRecord{}, + t: t, + } + + s.Server = httptest.NewServer(http.HandlerFunc(s.ServeRequest)) + for _, stub := range stubs { stub.reqPath = fmt.Sprintf("/v1.11%s", stub.reqPath) + s.stubs = append(s.stubs, &stubRecord{ + stub: stub, + summary: fmt.Sprintf("%s %s", stub.reqMethod, stub.reqPath), + callCount: 0, + }) } - stubSummaries := []string{} - for _, stub := range stubs { - stubSummaries = append(stubSummaries, fmt.Sprintf("%s %s", stub.reqMethod, stub.reqPath)) - } - - return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - for _, stub := range stubs { - if r.Method == stub.reqMethod && r.URL.Path == stub.reqPath { - body, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Fatal(err) - } - if string(body) != stub.reqBody { - t.Fatalf("expected body: %#v, got body: %#v", stub.reqBody, string(body)) - } - - if stub.resStatus > 0 { - w.WriteHeader(stub.resStatus) - } - w.Write([]byte(stub.resBody)) - return - } - } - - t.Fatalf("Unexpected request: '%s %s'.\nStubs: %#v", r.Method, r.URL.Path, stubSummaries) - })) + return s } -func instance(t *testing.T, server *httptest.Server) *beam.Object { +func (s *stubServer) Check() { + for _, record := range s.stubs { + if record.callCount != 1 { + s.t.Fatalf("Expected %#v to be called exactly once, but was called %d times", + record.summary, record.callCount) + } + } +} + +func (s *stubServer) ServeRequest(w http.ResponseWriter, r *http.Request) { + for _, record := range s.stubs { + stub := record.stub + if r.Method == stub.reqMethod && r.URL.Path == stub.reqPath { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + s.t.Fatal(err) + } + if string(body) != stub.reqBody { + s.t.Fatalf("expected body: %#v, got body: %#v", stub.reqBody, string(body)) + } + + if stub.resStatus > 0 { + w.WriteHeader(stub.resStatus) + } + w.Write([]byte(stub.resBody)) + + record.callCount += 1 + return + } + } + + s.t.Fatalf("Unexpected request: '%s %s'.\nStubs: %#v", r.Method, r.URL.Path, s.AllSummaries()) +} + +func (s *stubServer) AllSummaries() []string { + summaries := []string{} + for _, record := range s.stubs { + summaries = append(summaries, record.summary) + } + return summaries +} + +func instance(t *testing.T, server *stubServer) *beam.Object { url := "tcp://localhost:4243" if server != nil { url = strings.Replace(server.URL, "http://", "tcp://", 1) @@ -177,7 +224,7 @@ func instance(t *testing.T, server *httptest.Server) *beam.Object { return instance } -func child(t *testing.T, server *httptest.Server, i *beam.Object, name string) *beam.Object { +func child(t *testing.T, server *stubServer, i *beam.Object, name string) *beam.Object { _, child, err := i.Attach(name) if err != nil { t.Fatal(err) From 4805276763850e221981332272e0b8e058aa6879 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 19 Jun 2014 15:36:02 +0100 Subject: [PATCH 2/2] Test for dockerclient Get Signed-off-by: Aanand Prasad --- backends/dockerclient_test.go | 36 ++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/backends/dockerclient_test.go b/backends/dockerclient_test.go index cd8e5405c0..6322081111 100644 --- a/backends/dockerclient_test.go +++ b/backends/dockerclient_test.go @@ -19,6 +19,8 @@ type requestStub struct { resStatus int resBody string + + callCount int } func TestBackendSpawn(t *testing.T) { @@ -90,6 +92,30 @@ func TestAttachToChild(t *testing.T) { server.Check() } +func TestGetChild(t *testing.T) { + name := "foo" + expectedContent := `{"Image": "busybox", "Command": ["/bin/true"]}` + + server := makeServer(t, &requestStub{ + reqMethod: "GET", + reqPath: fmt.Sprintf("/containers/%s/json", name), + + resBody: expectedContent, + + callCount: 2, + }) + i := instance(t, server) + c := child(t, server, i, name) + content, err := c.Get() + if err != nil { + t.Fatal(err) + } + if content != expectedContent { + t.Fatalf("Expected %#v, got %#v", expectedContent, content) + } + server.Check() +} + func TestStartChild(t *testing.T) { name := "foo" server := makeServer(t, &requestStub{ @@ -158,6 +184,10 @@ func makeServer(t *testing.T, stubs ...*requestStub) *stubServer { for _, stub := range stubs { stub.reqPath = fmt.Sprintf("/v1.11%s", stub.reqPath) + if stub.callCount == 0 { + stub.callCount = 1 + } + s.stubs = append(s.stubs, &stubRecord{ stub: stub, summary: fmt.Sprintf("%s %s", stub.reqMethod, stub.reqPath), @@ -170,9 +200,9 @@ func makeServer(t *testing.T, stubs ...*requestStub) *stubServer { func (s *stubServer) Check() { for _, record := range s.stubs { - if record.callCount != 1 { - s.t.Fatalf("Expected %#v to be called exactly once, but was called %d times", - record.summary, record.callCount) + if record.callCount != record.stub.callCount { + s.t.Fatalf("Expected %#v to be called %d times, but was called %d times", + record.summary, record.stub.callCount, record.callCount) } } }