github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/nomad/operator_endpoint_test.go (about) 1 package nomad 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/net-rpc-msgpackrpc" 10 "github.com/hashicorp/nomad/nomad/structs" 11 "github.com/hashicorp/nomad/testutil" 12 "github.com/hashicorp/raft" 13 ) 14 15 func TestOperator_RaftGetConfiguration(t *testing.T) { 16 s1 := testServer(t, nil) 17 defer s1.Shutdown() 18 codec := rpcClient(t, s1) 19 testutil.WaitForLeader(t, s1.RPC) 20 21 arg := structs.GenericRequest{ 22 QueryOptions: structs.QueryOptions{ 23 Region: s1.config.Region, 24 }, 25 } 26 var reply structs.RaftConfigurationResponse 27 if err := msgpackrpc.CallWithCodec(codec, "Operator.RaftGetConfiguration", &arg, &reply); err != nil { 28 t.Fatalf("err: %v", err) 29 } 30 31 future := s1.raft.GetConfiguration() 32 if err := future.Error(); err != nil { 33 t.Fatalf("err: %v", err) 34 } 35 if len(future.Configuration().Servers) != 1 { 36 t.Fatalf("bad: %v", future.Configuration().Servers) 37 } 38 me := future.Configuration().Servers[0] 39 expected := structs.RaftConfigurationResponse{ 40 Servers: []*structs.RaftServer{ 41 &structs.RaftServer{ 42 ID: me.ID, 43 Node: fmt.Sprintf("%v.%v", s1.config.NodeName, s1.config.Region), 44 Address: me.Address, 45 Leader: true, 46 Voter: true, 47 }, 48 }, 49 Index: future.Index(), 50 } 51 if !reflect.DeepEqual(reply, expected) { 52 t.Fatalf("bad: got %+v; want %+v", reply, expected) 53 } 54 } 55 56 func TestOperator_RaftRemovePeerByAddress(t *testing.T) { 57 s1 := testServer(t, nil) 58 defer s1.Shutdown() 59 codec := rpcClient(t, s1) 60 testutil.WaitForLeader(t, s1.RPC) 61 62 // Try to remove a peer that's not there. 63 arg := structs.RaftPeerByAddressRequest{ 64 Address: raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", getPort())), 65 } 66 arg.Region = s1.config.Region 67 var reply struct{} 68 err := msgpackrpc.CallWithCodec(codec, "Operator.RaftRemovePeerByAddress", &arg, &reply) 69 if err == nil || !strings.Contains(err.Error(), "not found in the Raft configuration") { 70 t.Fatalf("err: %v", err) 71 } 72 73 // Add it manually to Raft. 74 { 75 future := s1.raft.AddPeer(arg.Address) 76 if err := future.Error(); err != nil { 77 t.Fatalf("err: %v", err) 78 } 79 } 80 81 // Make sure it's there. 82 { 83 future := s1.raft.GetConfiguration() 84 if err := future.Error(); err != nil { 85 t.Fatalf("err: %v", err) 86 } 87 configuration := future.Configuration() 88 if len(configuration.Servers) != 2 { 89 t.Fatalf("bad: %v", configuration) 90 } 91 } 92 93 // Remove it, now it should go through. 94 if err := msgpackrpc.CallWithCodec(codec, "Operator.RaftRemovePeerByAddress", &arg, &reply); err != nil { 95 t.Fatalf("err: %v", err) 96 } 97 98 // Make sure it's not there. 99 { 100 future := s1.raft.GetConfiguration() 101 if err := future.Error(); err != nil { 102 t.Fatalf("err: %v", err) 103 } 104 configuration := future.Configuration() 105 if len(configuration.Servers) != 1 { 106 t.Fatalf("bad: %v", configuration) 107 } 108 } 109 }