Browse Source

etcd: refactor tests

Xiang Li 11 years ago
parent
commit
00935c873f
3 changed files with 634 additions and 877 deletions
  1. 33 0
      etcd/etcd_test.go
  2. 574 853
      etcd/v2_http_kv_test.go
  3. 27 24
      etcd/v2_util.go

+ 33 - 0
etcd/etcd_test.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
+	"net/url"
 	"testing"
 	"testing"
 	"time"
 	"time"
 
 
@@ -42,6 +43,38 @@ func TestMultipleTLSNodes(t *testing.T) {
 	afterTest(t)
 	afterTest(t)
 }
 }
 
 
+func TestV2Redirect(t *testing.T) {
+	es, hs := buildCluster(3, false)
+	waitCluster(t, es)
+	u := hs[1].URL
+	ru := fmt.Sprintf("%s%s", hs[0].URL, "/v2/keys/foo")
+	tc := NewTestClient()
+
+	v := url.Values{}
+	v.Set("value", "XXX")
+	resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
+	if resp.StatusCode != http.StatusTemporaryRedirect {
+		t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusTemporaryRedirect)
+	}
+	location, err := resp.Location()
+	if err != nil {
+		t.Errorf("want err = %, want nil", err)
+	}
+
+	if location.String() != ru {
+		t.Errorf("location = %v, want %v", location.String(), ru)
+	}
+
+	resp.Body.Close()
+	for i := range es {
+		es[len(es)-i-1].Stop()
+	}
+	for i := range hs {
+		hs[len(hs)-i-1].Close()
+	}
+	afterTest(t)
+}
+
 func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
 func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
 	bootstrapper := 0
 	bootstrapper := 0
 	es := make([]*Server, number)
 	es := make([]*Server, number)

File diff suppressed because it is too large
+ 574 - 853
etcd/v2_http_kv_test.go


+ 27 - 24
etcd/v2_util.go

@@ -10,13 +10,17 @@ import (
 	"strings"
 	"strings"
 )
 )
 
 
+type testHttpClient struct {
+	*http.Client
+}
+
 // Creates a new HTTP client with KeepAlive disabled.
 // Creates a new HTTP client with KeepAlive disabled.
-func NewHTTPClient() *http.Client {
-	return &http.Client{Transport: &http.Transport{DisableKeepAlives: true}}
+func NewTestClient() *testHttpClient {
+	return &testHttpClient{&http.Client{Transport: &http.Transport{DisableKeepAlives: true}}}
 }
 }
 
 
 // Reads the body from the response and closes it.
 // Reads the body from the response and closes it.
-func ReadBody(resp *http.Response) []byte {
+func (t *testHttpClient) ReadBody(resp *http.Response) []byte {
 	if resp == nil {
 	if resp == nil {
 		return []byte{}
 		return []byte{}
 	}
 	}
@@ -26,53 +30,52 @@ func ReadBody(resp *http.Response) []byte {
 }
 }
 
 
 // Reads the body from the response and parses it as JSON.
 // Reads the body from the response and parses it as JSON.
-func ReadBodyJSON(resp *http.Response) map[string]interface{} {
+func (t *testHttpClient) ReadBodyJSON(resp *http.Response) map[string]interface{} {
 	m := make(map[string]interface{})
 	m := make(map[string]interface{})
-	b := ReadBody(resp)
+	b := t.ReadBody(resp)
 	if err := json.Unmarshal(b, &m); err != nil {
 	if err := json.Unmarshal(b, &m); err != nil {
 		panic(fmt.Sprintf("HTTP body JSON parse error: %v: %s", err, string(b)))
 		panic(fmt.Sprintf("HTTP body JSON parse error: %v: %s", err, string(b)))
 	}
 	}
 	return m
 	return m
 }
 }
 
 
-func Head(url string) (*http.Response, error) {
-	return send("HEAD", url, "application/json", nil)
+func (t *testHttpClient) Head(url string) (*http.Response, error) {
+	return t.send("HEAD", url, "application/json", nil)
 }
 }
 
 
-func Get(url string) (*http.Response, error) {
-	return send("GET", url, "application/json", nil)
+func (t *testHttpClient) Get(url string) (*http.Response, error) {
+	return t.send("GET", url, "application/json", nil)
 }
 }
 
 
-func Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
-	return send("POST", url, bodyType, body)
+func (t *testHttpClient) Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
+	return t.send("POST", url, bodyType, body)
 }
 }
 
 
-func PostForm(url string, data url.Values) (*http.Response, error) {
-	return Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
+func (t *testHttpClient) PostForm(url string, data url.Values) (*http.Response, error) {
+	return t.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
 }
 }
 
 
-func Put(url string, bodyType string, body io.Reader) (*http.Response, error) {
-	return send("PUT", url, bodyType, body)
+func (t *testHttpClient) Put(url string, bodyType string, body io.Reader) (*http.Response, error) {
+	return t.send("PUT", url, bodyType, body)
 }
 }
 
 
-func PutForm(url string, data url.Values) (*http.Response, error) {
-	return Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
+func (t *testHttpClient) PutForm(url string, data url.Values) (*http.Response, error) {
+	return t.Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
 }
 }
 
 
-func Delete(url string, bodyType string, body io.Reader) (*http.Response, error) {
-	return send("DELETE", url, bodyType, body)
+func (t *testHttpClient) Delete(url string, bodyType string, body io.Reader) (*http.Response, error) {
+	return t.send("DELETE", url, bodyType, body)
 }
 }
 
 
-func DeleteForm(url string, data url.Values) (*http.Response, error) {
-	return Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
+func (t *testHttpClient) DeleteForm(url string, data url.Values) (*http.Response, error) {
+	return t.Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
 }
 }
 
 
-func send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) {
-	c := NewHTTPClient()
+func (t *testHttpClient) send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) {
 	req, err := http.NewRequest(method, url, body)
 	req, err := http.NewRequest(method, url, body)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 	req.Header.Set("Content-Type", bodyType)
 	req.Header.Set("Content-Type", bodyType)
-	return c.Do(req)
+	return t.Do(req)
 }
 }

Some files were not shown because too many files changed in this diff