[ARVADOS] created: f8192fbfdac74e88ac60dc8c1e652745873471a7
git at public.curoverse.com
git at public.curoverse.com
Tue Nov 24 12:55:11 EST 2015
at f8192fbfdac74e88ac60dc8c1e652745873471a7 (commit)
commit f8192fbfdac74e88ac60dc8c1e652745873471a7
Author: radhika <radhika at curoverse.com>
Date: Tue Nov 24 12:51:13 2015 -0500
7253: Refactor APIStub and KeepServerStub into a testing.go so that the code can be reused from various packages
diff --git a/sdk/go/arvadostest/testing.go b/sdk/go/arvadostest/testing.go
new file mode 100644
index 0000000..7d75952
--- /dev/null
+++ b/sdk/go/arvadostest/testing.go
@@ -0,0 +1,63 @@
+package arvadostest
+
+import (
+ "net/http"
+)
+
+// StatusAndBody struct with response status and body
+type StatusAndBody struct {
+ ResponseStatus int
+ ResponseBody string
+}
+
+// APIStub with Data map of path and StatusAndBody
+// Ex: /arvados/v1/keep_services = arvadostest.StatusAndBody{200, string(`{}`)}
+type APIStub struct {
+ Data map[string]StatusAndBody
+}
+
+func (stub *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
+ if req.URL.Path == "/redirect-loop" {
+ http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
+ return
+ }
+
+ pathResponse := stub.Data[req.URL.Path]
+ if pathResponse.ResponseBody != "" {
+ if pathResponse.ResponseStatus == -1 {
+ http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
+ } else {
+ resp.WriteHeader(pathResponse.ResponseStatus)
+ resp.Write([]byte(pathResponse.ResponseBody))
+ }
+ } else {
+ resp.WriteHeader(500)
+ resp.Write([]byte(``))
+ }
+}
+
+// KeepServerStub with Data map of path and StatusAndBody
+// Ex: /status.json = arvadostest.StatusAndBody{200, string(`{}`)}
+type KeepServerStub struct {
+ Data map[string]StatusAndBody
+}
+
+func (stub *KeepServerStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
+ if req.URL.Path == "/redirect-loop" {
+ http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
+ return
+ }
+
+ pathResponse := stub.Data[req.URL.Path]
+ if pathResponse.ResponseBody != "" {
+ if pathResponse.ResponseStatus == -1 {
+ http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
+ } else {
+ resp.WriteHeader(pathResponse.ResponseStatus)
+ resp.Write([]byte(pathResponse.ResponseBody))
+ }
+ } else {
+ resp.WriteHeader(500)
+ resp.Write([]byte(``))
+ }
+}
diff --git a/services/datamanager/keep/keep_test.go b/services/datamanager/keep/keep_test.go
index 42559af..454661a 100644
--- a/services/datamanager/keep/keep_test.go
+++ b/services/datamanager/keep/keep_test.go
@@ -12,6 +12,7 @@ import (
"testing"
"git.curoverse.com/arvados.git/sdk/go/arvadosclient"
+ "git.curoverse.com/arvados.git/sdk/go/arvadostest"
"git.curoverse.com/arvados.git/sdk/go/blockdigest"
"git.curoverse.com/arvados.git/sdk/go/keepclient"
@@ -93,59 +94,6 @@ func (s *KeepSuite) TestSendTrashListUnreachable(c *C) {
sendTrashListError(c, httptest.NewUnstartedServer(&TestHandler{}))
}
-type StatusAndBody struct {
- respStatus int
- responseBody string
-}
-
-type APIStub struct {
- data map[string]StatusAndBody
-}
-
-func (stub *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
- if req.URL.Path == "/redirect-loop" {
- http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
- return
- }
-
- pathResponse := stub.data[req.URL.Path]
- if pathResponse.responseBody != "" {
- if pathResponse.respStatus == -1 {
- http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
- } else {
- resp.WriteHeader(pathResponse.respStatus)
- resp.Write([]byte(pathResponse.responseBody))
- }
- } else {
- resp.WriteHeader(500)
- resp.Write([]byte(``))
- }
-}
-
-type KeepServerStub struct {
- data map[string]StatusAndBody
-}
-
-func (stub *KeepServerStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
- if req.URL.Path == "/redirect-loop" {
- http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
- return
- }
-
- pathResponse := stub.data[req.URL.Path]
- if pathResponse.responseBody != "" {
- if pathResponse.respStatus == -1 {
- http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
- } else {
- resp.WriteHeader(pathResponse.respStatus)
- resp.Write([]byte(pathResponse.responseBody))
- }
- } else {
- resp.WriteHeader(500)
- resp.Write([]byte(``))
- }
-}
-
type APITestData struct {
numServers int
serverType string
@@ -177,9 +125,9 @@ func testGetKeepServersFromAPI(c *C, testData APITestData, expectedError string)
}
ksJSON, _ := json.Marshal(keepServers)
- apiData := make(map[string]StatusAndBody)
- apiData["/arvados/v1/keep_services"] = StatusAndBody{testData.statusCode, string(ksJSON)}
- apiStub := APIStub{apiData}
+ apiStubData := make(map[string]arvadostest.StatusAndBody)
+ apiStubData["/arvados/v1/keep_services"] = arvadostest.StatusAndBody{testData.statusCode, string(ksJSON)}
+ apiStub := arvadostest.APIStub{apiStubData}
api := httptest.NewServer(&apiStub)
defer api.Close()
@@ -254,10 +202,10 @@ func (s *KeepSuite) TestGetKeepServers_ReadServerResponseWithTwoBlocks(c *C) {
}
func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) {
- ksData := make(map[string]StatusAndBody)
- ksData["/status.json"] = StatusAndBody{testData.statusStatusCode, string(`{}`)}
- ksData["/index"] = StatusAndBody{testData.indexStatusCode, testData.indexResponseBody}
- ksStub := KeepServerStub{ksData}
+ ksStubData := make(map[string]arvadostest.StatusAndBody)
+ ksStubData["/status.json"] = arvadostest.StatusAndBody{testData.statusStatusCode, string(`{}`)}
+ ksStubData["/index"] = arvadostest.StatusAndBody{testData.indexStatusCode, testData.indexResponseBody}
+ ksStub := arvadostest.KeepServerStub{ksStubData}
ks := httptest.NewServer(&ksStub)
defer ks.Close()
@@ -278,9 +226,9 @@ func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) {
}},
}
ksJSON, _ := json.Marshal(servers_list)
- apiData := make(map[string]StatusAndBody)
- apiData["/arvados/v1/keep_services"] = StatusAndBody{200, string(ksJSON)}
- apiStub := APIStub{apiData}
+ apiStubData := make(map[string]arvadostest.StatusAndBody)
+ apiStubData["/arvados/v1/keep_services"] = arvadostest.StatusAndBody{200, string(ksJSON)}
+ apiStub := arvadostest.APIStub{apiStubData}
api := httptest.NewServer(&apiStub)
defer api.Close()
-----------------------------------------------------------------------
hooks/post-receive
--
More information about the arvados-commits
mailing list