github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/nomad/alloc_endpoint_test.go (about)

     1  package nomad
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/net-rpc-msgpackrpc"
     8  	"github.com/hashicorp/nomad/nomad/mock"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/hashicorp/nomad/testutil"
    11  )
    12  
    13  func TestAllocEndpoint_List(t *testing.T) {
    14  	s1 := testServer(t, nil)
    15  	defer s1.Shutdown()
    16  	codec := rpcClient(t, s1)
    17  	testutil.WaitForLeader(t, s1.RPC)
    18  
    19  	// Create the register request
    20  	alloc := mock.Alloc()
    21  	state := s1.fsm.State()
    22  	err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
    23  	if err != nil {
    24  		t.Fatalf("err: %v", err)
    25  	}
    26  
    27  	// Lookup the jobs
    28  	get := &structs.AllocListRequest{
    29  		QueryOptions: structs.QueryOptions{Region: "global"},
    30  	}
    31  	var resp structs.AllocListResponse
    32  	if err := msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp); err != nil {
    33  		t.Fatalf("err: %v", err)
    34  	}
    35  	if resp.Index != 1000 {
    36  		t.Fatalf("Bad index: %d %d", resp.Index, 1000)
    37  	}
    38  
    39  	if len(resp.Allocations) != 1 {
    40  		t.Fatalf("bad: %#v", resp.Allocations)
    41  	}
    42  	if resp.Allocations[0].ID != alloc.ID {
    43  		t.Fatalf("bad: %#v", resp.Allocations[0])
    44  	}
    45  }
    46  
    47  func TestAllocEndpoint_GetAlloc(t *testing.T) {
    48  	s1 := testServer(t, nil)
    49  	defer s1.Shutdown()
    50  	codec := rpcClient(t, s1)
    51  	testutil.WaitForLeader(t, s1.RPC)
    52  
    53  	// Create the register request
    54  	alloc := mock.Alloc()
    55  	state := s1.fsm.State()
    56  	err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
    57  	if err != nil {
    58  		t.Fatalf("err: %v", err)
    59  	}
    60  
    61  	// Lookup the jobs
    62  	get := &structs.AllocSpecificRequest{
    63  		AllocID:      alloc.ID,
    64  		QueryOptions: structs.QueryOptions{Region: "global"},
    65  	}
    66  	var resp structs.SingleAllocResponse
    67  	if err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp); err != nil {
    68  		t.Fatalf("err: %v", err)
    69  	}
    70  	if resp.Index != 1000 {
    71  		t.Fatalf("Bad index: %d %d", resp.Index, 1000)
    72  	}
    73  
    74  	if !reflect.DeepEqual(alloc, resp.Alloc) {
    75  		t.Fatalf("bad: %#v", resp.Alloc)
    76  	}
    77  }