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