github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/tests/functional/remove_node_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
    11  )
    12  
    13  // remove the node and node rejoin with previous log
    14  func TestRemoveNode(t *testing.T) {
    15  	procAttr := new(os.ProcAttr)
    16  	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
    17  
    18  	clusterSize := 3
    19  	argGroup, etcds, _ := CreateCluster(clusterSize, procAttr, false)
    20  	defer DestroyCluster(etcds)
    21  
    22  	time.Sleep(time.Second)
    23  
    24  	c := etcd.NewClient(nil)
    25  
    26  	c.SyncCluster()
    27  
    28  	rmReq, _ := http.NewRequest("DELETE", "http://127.0.0.1:7001/remove/node3", nil)
    29  
    30  	client := &http.Client{}
    31  	for i := 0; i < 2; i++ {
    32  		for i := 0; i < 2; i++ {
    33  			client.Do(rmReq)
    34  
    35  			fmt.Println("send remove to node3 and wait for its exiting")
    36  			etcds[2].Wait()
    37  
    38  			resp, err := c.Get("_etcd/machines", false, false)
    39  
    40  			if err != nil {
    41  				panic(err)
    42  			}
    43  
    44  			if len(resp.Node.Nodes) != 2 {
    45  				t.Fatal("cannot remove peer")
    46  			}
    47  
    48  			if i == 1 {
    49  				// rejoin with log
    50  				etcds[2], err = os.StartProcess(EtcdBinPath, argGroup[2], procAttr)
    51  			} else {
    52  				// rejoin without log
    53  				etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2], "-f"), procAttr)
    54  			}
    55  
    56  			if err != nil {
    57  				panic(err)
    58  			}
    59  
    60  			time.Sleep(time.Second)
    61  
    62  			resp, err = c.Get("_etcd/machines", false, false)
    63  
    64  			if err != nil {
    65  				panic(err)
    66  			}
    67  
    68  			if len(resp.Node.Nodes) != 3 {
    69  				t.Fatalf("add peer fails #1 (%d != 3)", len(resp.Node.Nodes))
    70  			}
    71  		}
    72  
    73  		// first kill the node, then remove it, then add it back
    74  		for i := 0; i < 2; i++ {
    75  			etcds[2].Kill()
    76  			fmt.Println("kill node3 and wait for its exiting")
    77  			etcds[2].Wait()
    78  
    79  			client.Do(rmReq)
    80  
    81  			resp, err := c.Get("_etcd/machines", false, false)
    82  
    83  			if err != nil {
    84  				panic(err)
    85  			}
    86  
    87  			if len(resp.Node.Nodes) != 2 {
    88  				t.Fatal("cannot remove peer")
    89  			}
    90  
    91  			if i == 1 {
    92  				// rejoin with log
    93  				etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2]), procAttr)
    94  			} else {
    95  				// rejoin without log
    96  				etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2], "-f"), procAttr)
    97  			}
    98  
    99  			if err != nil {
   100  				panic(err)
   101  			}
   102  
   103  			time.Sleep(time.Second)
   104  
   105  			resp, err = c.Get("_etcd/machines", false, false)
   106  
   107  			if err != nil {
   108  				panic(err)
   109  			}
   110  
   111  			if len(resp.Node.Nodes) != 3 {
   112  				t.Fatalf("add peer fails #2 (%d != 3)", len(resp.Node.Nodes))
   113  			}
   114  		}
   115  	}
   116  }