Browse Source

fix(tests/discovery): use host as -peers parameter instead of url

Or it cannot test the functionality correctly.
Moreover, add TestDiscoveryNoWithBackupPeers as the test for it.
Yicheng Qin 12 years ago
parent
commit
bd56b15b6e
4 changed files with 49 additions and 1 deletions
  1. 10 0
      server/registry.go
  2. 5 0
      server/server.go
  3. 24 1
      tests/functional/discovery_test.go
  4. 10 0
      tests/functional/etcd_tls_test.go

+ 10 - 0
server/registry.go

@@ -94,6 +94,16 @@ func (r *Registry) clientURL(name string) (string, bool) {
 	return "", false
 	return "", false
 }
 }
 
 
+// Retrieves the host part of peer URL for a given node by name.
+func (r *Registry) PeerHost(name string) (string, bool) {
+	rawurl, ok := r.PeerURL(name)
+	if ok {
+		u, _ := url.Parse(rawurl)
+		return u.Host, ok
+	}
+	return rawurl, ok
+}
+
 // Retrieves the peer URL for a given node by name.
 // Retrieves the peer URL for a given node by name.
 func (r *Registry) PeerURL(name string) (string, bool) {
 func (r *Registry) PeerURL(name string) (string, bool) {
 	r.Lock()
 	r.Lock()

+ 5 - 0
server/server.go

@@ -79,6 +79,11 @@ func (s *Server) URL() string {
 	return s.url
 	return s.url
 }
 }
 
 
+// Returns the host part of Peer URL for a given node name.
+func (s *Server) PeerHost(name string) (string, bool) {
+	return s.registry.PeerHost(name)
+}
+
 // Retrives the Peer URL for a given node name.
 // Retrives the Peer URL for a given node name.
 func (s *Server) PeerURL(name string) (string, bool) {
 func (s *Server) PeerURL(name string) (string, bool) {
 	return s.registry.PeerURL(name)
 	return s.registry.PeerURL(name)

+ 24 - 1
tests/functional/discovery_test.go

@@ -65,7 +65,7 @@ func TestDiscoveryDownWithBackupPeers(t *testing.T) {
 		defer ts.Close()
 		defer ts.Close()
 
 
 		discover := ts.URL + "/v2/keys/_etcd/registry/1"
 		discover := ts.URL + "/v2/keys/_etcd/registry/1"
-		u, ok := s.PeerURL("ETCDTEST")
+		u, ok := s.PeerHost("ETCDTEST")
 		if !ok {
 		if !ok {
 			t.Fatalf("Couldn't find the URL")
 			t.Fatalf("Couldn't find the URL")
 		}
 		}
@@ -88,6 +88,29 @@ func TestDiscoveryDownWithBackupPeers(t *testing.T) {
 	})
 	})
 }
 }
 
 
+// TestDiscoveryNoWithBackupPeers ensures that etcd runs if it is started with
+// no discovery URL and a peer list.
+func TestDiscoveryNoWithBackupPeers(t *testing.T) {
+	etcdtest.RunServer(func(s *server.Server) {
+		u, ok := s.PeerHost("ETCDTEST")
+		if !ok {
+			t.Fatalf("Couldn't find the URL")
+		}
+		proc, err := startServer([]string{"-peers", u})
+
+		if err != nil {
+			t.Fatal(err.Error())
+		}
+		defer stopServer(proc)
+
+		client := http.Client{}
+		err = assertServerFunctional(client, "http")
+		if err != nil {
+			t.Fatal(err.Error())
+		}
+	})
+}
+
 // TestDiscoveryFirstPeer ensures that etcd starts as the leader if it
 // TestDiscoveryFirstPeer ensures that etcd starts as the leader if it
 // registers as the first peer.
 // registers as the first peer.
 func TestDiscoveryFirstPeer(t *testing.T) {
 func TestDiscoveryFirstPeer(t *testing.T) {

+ 10 - 0
tests/functional/etcd_tls_test.go

@@ -183,6 +183,16 @@ func assertServerFunctional(client http.Client, scheme string) error {
 		time.Sleep(1 * time.Second)
 		time.Sleep(1 * time.Second)
 
 
 		resp, err := client.PostForm(path, fields)
 		resp, err := client.PostForm(path, fields)
+		// If the status is Temporary Redirect, we should follow the
+		// new location, because the request did not go to the leader yet.
+		// TODO(yichengq): the difference between Temporary Redirect(307)
+		// and Created(201) could distinguish between leader and followers
+		for err == nil && resp.StatusCode == http.StatusTemporaryRedirect {
+			loc, _ := resp.Location()
+			newPath := loc.String()
+			resp, err = client.PostForm(newPath, fields)
+		}
+
 		if err == nil {
 		if err == nil {
 			if resp.StatusCode != 201 {
 			if resp.StatusCode != 201 {
 				return errors.New(fmt.Sprintf("resp.StatusCode == %s", resp.Status))
 				return errors.New(fmt.Sprintf("resp.StatusCode == %s", resp.Status))