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

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/nomad/mock"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  func TestHTTP_EvalList(t *testing.T) {
    13  	httpTest(t, nil, func(s *TestServer) {
    14  		// Directly manipulate the state
    15  		state := s.Agent.server.State()
    16  		eval1 := mock.Eval()
    17  		eval2 := mock.Eval()
    18  		err := state.UpsertEvals(1000,
    19  			[]*structs.Evaluation{eval1, eval2})
    20  		if err != nil {
    21  			t.Fatalf("err: %v", err)
    22  		}
    23  
    24  		// Make the HTTP request
    25  		req, err := http.NewRequest("GET", "/v1/evaluations", nil)
    26  		if err != nil {
    27  			t.Fatalf("err: %v", err)
    28  		}
    29  		respW := httptest.NewRecorder()
    30  
    31  		// Make the request
    32  		obj, err := s.Server.EvalsRequest(respW, req)
    33  		if err != nil {
    34  			t.Fatalf("err: %v", err)
    35  		}
    36  
    37  		// Check for the index
    38  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
    39  			t.Fatalf("missing index")
    40  		}
    41  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
    42  			t.Fatalf("missing known leader")
    43  		}
    44  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
    45  			t.Fatalf("missing last contact")
    46  		}
    47  
    48  		// Check the job
    49  		e := obj.([]*structs.Evaluation)
    50  		if len(e) != 2 {
    51  			t.Fatalf("bad: %#v", e)
    52  		}
    53  	})
    54  }
    55  
    56  func TestHTTP_EvalAllocations(t *testing.T) {
    57  	httpTest(t, nil, func(s *TestServer) {
    58  		// Directly manipulate the state
    59  		state := s.Agent.server.State()
    60  		alloc1 := mock.Alloc()
    61  		alloc2 := mock.Alloc()
    62  		alloc2.EvalID = alloc1.EvalID
    63  		err := state.UpsertAllocs(1000,
    64  			[]*structs.Allocation{alloc1, alloc2})
    65  		if err != nil {
    66  			t.Fatalf("err: %v", err)
    67  		}
    68  
    69  		// Make the HTTP request
    70  		req, err := http.NewRequest("GET",
    71  			"/v1/evaluation/"+alloc1.EvalID+"/allocations", nil)
    72  		if err != nil {
    73  			t.Fatalf("err: %v", err)
    74  		}
    75  		respW := httptest.NewRecorder()
    76  
    77  		// Make the request
    78  		obj, err := s.Server.EvalSpecificRequest(respW, req)
    79  		if err != nil {
    80  			t.Fatalf("err: %v", err)
    81  		}
    82  
    83  		// Check for the index
    84  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
    85  			t.Fatalf("missing index")
    86  		}
    87  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
    88  			t.Fatalf("missing known leader")
    89  		}
    90  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
    91  			t.Fatalf("missing last contact")
    92  		}
    93  
    94  		// Check the ouptput
    95  		allocs := obj.([]*structs.AllocListStub)
    96  		if len(allocs) != 2 {
    97  			t.Fatalf("bad: %#v", allocs)
    98  		}
    99  	})
   100  }
   101  
   102  func TestHTTP_EvalQuery(t *testing.T) {
   103  	httpTest(t, nil, func(s *TestServer) {
   104  		// Directly manipulate the state
   105  		state := s.Agent.server.State()
   106  		eval := mock.Eval()
   107  		err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
   108  		if err != nil {
   109  			t.Fatalf("err: %v", err)
   110  		}
   111  
   112  		// Make the HTTP request
   113  		req, err := http.NewRequest("GET", "/v1/evaluation/"+eval.ID, nil)
   114  		if err != nil {
   115  			t.Fatalf("err: %v", err)
   116  		}
   117  		respW := httptest.NewRecorder()
   118  
   119  		// Make the request
   120  		obj, err := s.Server.EvalSpecificRequest(respW, req)
   121  		if err != nil {
   122  			t.Fatalf("err: %v", err)
   123  		}
   124  
   125  		// Check for the index
   126  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
   127  			t.Fatalf("missing index")
   128  		}
   129  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
   130  			t.Fatalf("missing known leader")
   131  		}
   132  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
   133  			t.Fatalf("missing last contact")
   134  		}
   135  
   136  		// Check the job
   137  		e := obj.(*structs.Evaluation)
   138  		if e.ID != eval.ID {
   139  			t.Fatalf("bad: %#v", e)
   140  		}
   141  	})
   142  }