Rev 32: testservices/swiftservice: tweak the test infrastructure in http://bazaar.launchpad.net/+branch/goose

John Arbash Meinel john at arbash-meinel.com
Sat Dec 1 08:39:01 UTC 2012


At http://bazaar.launchpad.net/+branch/goose

------------------------------------------------------------
revno: 32 [merge]
revision-id: john at arbash-meinel.com-20121201083852-4361ag316kylic1h
parent: martin.packman at canonical.com-20121130173019-33owb9sbhch1wpsv
parent: john at arbash-meinel.com-20121129084955-0nd1ngko9aiv7jyq
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: goose
timestamp: Sat 2012-12-01 08:38:52 +0000
message:
  testservices/swiftservice: tweak the test infrastructure
  
  We had a function where all tests called the same function, and then asserted
  similar results. Instead of having that in the callers, move it into
  the called function.
  
  
  R=dimitern,dave.cheney
modified:
  testservices/swiftservice/service_http_test.go service_http_test.go-20121127185542-7auslljbtlircv7d-2
-------------- next part --------------
=== modified file 'testservices/swiftservice/service_http_test.go'
--- a/testservices/swiftservice/service_http_test.go	2012-11-28 20:15:37 +0000
+++ b/testservices/swiftservice/service_http_test.go	2012-11-29 08:49:55 +0000
@@ -35,7 +35,8 @@
 	s.HTTPSuite.TearDownSuite(c)
 }
 
-func (s *SwiftHTTPSuite) sendRequest(method, path string, body []byte) (*http.Response, error) {
+func (s *SwiftHTTPSuite) sendRequest(c *C, method, path string, body []byte,
+	expectedStatusCode int) (resp *http.Response) {
 	var req *http.Request
 	var err error
 	url := s.Server.URL + baseURL + path
@@ -44,14 +45,15 @@
 	} else {
 		req, err = http.NewRequest(method, url, nil)
 	}
-	if err != nil {
-		return nil, err
-	}
+	c.Assert(err, IsNil)
 	if token != "" {
 		req.Header.Add("X-Auth-Token", token)
 	}
 	client := &http.Client{}
-	return client.Do(req)
+	resp, err = client.Do(req)
+	c.Assert(err, IsNil)
+	c.Assert(resp.StatusCode, Equals, expectedStatusCode)
+	return resp
 }
 
 func (s *SwiftHTTPSuite) ensureNotContainer(name string, c *C) {
@@ -98,9 +100,7 @@
 func (s *SwiftHTTPSuite) TestPUTContainerMissingCreated(c *C) {
 	s.ensureNotContainer("test", c)
 
-	resp, err := s.sendRequest("PUT", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusCreated)
+	s.sendRequest(c, "PUT", "test", nil, http.StatusCreated)
 
 	s.removeContainer("test", c)
 }
@@ -108,9 +108,7 @@
 func (s *SwiftHTTPSuite) TestPUTContainerExistsAccepted(c *C) {
 	s.ensureContainer("test", c)
 
-	resp, err := s.sendRequest("PUT", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusAccepted)
+	s.sendRequest(c, "PUT", "test", nil, http.StatusAccepted)
 
 	s.removeContainer("test", c)
 }
@@ -118,9 +116,7 @@
 func (s *SwiftHTTPSuite) TestGETContainerMissingNotFound(c *C) {
 	s.ensureNotContainer("test", c)
 
-	resp, err := s.sendRequest("GET", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "GET", "test", nil, http.StatusNotFound)
 
 	s.ensureNotContainer("test", c)
 }
@@ -128,9 +124,7 @@
 func (s *SwiftHTTPSuite) TestGETContainerExistsOK(c *C) {
 	s.ensureContainer("test", c)
 
-	resp, err := s.sendRequest("GET", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusOK)
+	s.sendRequest(c, "GET", "test", nil, http.StatusOK)
 
 	s.removeContainer("test", c)
 }
@@ -138,17 +132,13 @@
 func (s *SwiftHTTPSuite) TestDELETEContainerMissingNotFound(c *C) {
 	s.ensureNotContainer("test", c)
 
-	resp, err := s.sendRequest("DELETE", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "DELETE", "test", nil, http.StatusNotFound)
 }
 
 func (s *SwiftHTTPSuite) TestDELETEContainerExistsNoContent(c *C) {
 	s.ensureContainer("test", c)
 
-	resp, err := s.sendRequest("DELETE", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNoContent)
+	s.sendRequest(c, "DELETE", "test", nil, http.StatusNoContent)
 
 	s.ensureNotContainer("test", c)
 }
@@ -158,9 +148,7 @@
 	s.ensureNotObject("test", "obj", c)
 
 	data := []byte("test data")
-	resp, err := s.sendRequest("PUT", "test/obj", data)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusCreated)
+	s.sendRequest(c, "PUT", "test/obj", data, http.StatusCreated)
 
 	s.ensureObjectData("test", "obj", data, c)
 	s.removeContainer("test", c)
@@ -172,9 +160,7 @@
 	s.ensureObject("test", "obj", data, c)
 
 	newdata := []byte("new test data")
-	resp, err := s.sendRequest("PUT", "test/obj", newdata)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusCreated)
+	s.sendRequest(c, "PUT", "test/obj", newdata, http.StatusCreated)
 
 	s.ensureObjectData("test", "obj", newdata, c)
 	s.removeContainer("test", c)
@@ -184,9 +170,7 @@
 	s.ensureNotContainer("test", c)
 
 	data := []byte("test data")
-	resp, err := s.sendRequest("PUT", "test/obj", data)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "PUT", "test/obj", data, http.StatusNotFound)
 
 	s.ensureNotContainer("test", c)
 }
@@ -195,9 +179,7 @@
 	s.ensureContainer("test", c)
 	s.ensureNotObject("test", "obj", c)
 
-	resp, err := s.sendRequest("GET", "test/obj", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "GET", "test/obj", nil, http.StatusNotFound)
 
 	s.removeContainer("test", c)
 }
@@ -205,9 +187,7 @@
 func (s *SwiftHTTPSuite) TestGETObjectContainerMissingNotFound(c *C) {
 	s.ensureNotContainer("test", c)
 
-	resp, err := s.sendRequest("GET", "test/obj", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "GET", "test/obj", nil, http.StatusNotFound)
 
 	s.ensureNotContainer("test", c)
 }
@@ -217,9 +197,7 @@
 	s.ensureContainer("test", c)
 	s.ensureObject("test", "obj", data, c)
 
-	resp, err := s.sendRequest("GET", "test/obj", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusOK)
+	resp := s.sendRequest(c, "GET", "test/obj", nil, http.StatusOK)
 
 	s.ensureObjectData("test", "obj", data, c)
 
@@ -235,9 +213,7 @@
 	s.ensureContainer("test", c)
 	s.ensureNotObject("test", "obj", c)
 
-	resp, err := s.sendRequest("DELETE", "test/obj", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "DELETE", "test/obj", nil, http.StatusNotFound)
 
 	s.removeContainer("test", c)
 }
@@ -245,9 +221,7 @@
 func (s *SwiftHTTPSuite) TestDELETEObjectContainerMissingNotFound(c *C) {
 	s.ensureNotContainer("test", c)
 
-	resp, err := s.sendRequest("DELETE", "test/obj", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
+	s.sendRequest(c, "DELETE", "test/obj", nil, http.StatusNotFound)
 
 	s.ensureNotContainer("test", c)
 }
@@ -257,9 +231,7 @@
 	s.ensureContainer("test", c)
 	s.ensureObject("test", "obj", data, c)
 
-	resp, err := s.sendRequest("DELETE", "test/obj", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusNoContent)
+	s.sendRequest(c, "DELETE", "test/obj", nil, http.StatusNoContent)
 
 	s.removeContainer("test", c)
 }
@@ -270,16 +242,10 @@
 		token = oldtoken
 	}()
 	token = ""
-	resp, err := s.sendRequest("GET", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized)
+	s.sendRequest(c, "GET", "test", nil, http.StatusUnauthorized)
 
 	token = "invalid"
-	resp, err = s.sendRequest("PUT", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized)
+	s.sendRequest(c, "PUT", "test", nil, http.StatusUnauthorized)
 
-	resp, err = s.sendRequest("DELETE", "test", nil)
-	c.Assert(err, IsNil)
-	c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized)
+	s.sendRequest(c, "DELETE", "test", nil, http.StatusUnauthorized)
 }



More information about the bazaar-commits mailing list