github.com/manicqin/nomad@v0.9.5/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  	t.Parallel()
    14  	httpTest(t, nil, func(s *TestAgent) {
    15  		// Directly manipulate the state
    16  		state := s.Agent.server.State()
    17  		eval1 := mock.Eval()
    18  		eval2 := mock.Eval()
    19  		err := state.UpsertEvals(1000,
    20  			[]*structs.Evaluation{eval1, eval2})
    21  		if err != nil {
    22  			t.Fatalf("err: %v", err)
    23  		}
    24  
    25  		// Make the HTTP request
    26  		req, err := http.NewRequest("GET", "/v1/evaluations", nil)
    27  		if err != nil {
    28  			t.Fatalf("err: %v", err)
    29  		}
    30  		respW := httptest.NewRecorder()
    31  
    32  		// Make the request
    33  		obj, err := s.Server.EvalsRequest(respW, req)
    34  		if err != nil {
    35  			t.Fatalf("err: %v", err)
    36  		}
    37  
    38  		// Check for the index
    39  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
    40  			t.Fatalf("missing index")
    41  		}
    42  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
    43  			t.Fatalf("missing known leader")
    44  		}
    45  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
    46  			t.Fatalf("missing last contact")
    47  		}
    48  
    49  		// Check the eval
    50  		e := obj.([]*structs.Evaluation)
    51  		if len(e) != 2 {
    52  			t.Fatalf("bad: %#v", e)
    53  		}
    54  	})
    55  }
    56  
    57  func TestHTTP_EvalPrefixList(t *testing.T) {
    58  	t.Parallel()
    59  	httpTest(t, nil, func(s *TestAgent) {
    60  		// Directly manipulate the state
    61  		state := s.Agent.server.State()
    62  		eval1 := mock.Eval()
    63  		eval1.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
    64  		eval2 := mock.Eval()
    65  		eval2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
    66  		err := state.UpsertEvals(1000,
    67  			[]*structs.Evaluation{eval1, eval2})
    68  		if err != nil {
    69  			t.Fatalf("err: %v", err)
    70  		}
    71  
    72  		// Make the HTTP request
    73  		req, err := http.NewRequest("GET", "/v1/evaluations?prefix=aaab", nil)
    74  		if err != nil {
    75  			t.Fatalf("err: %v", err)
    76  		}
    77  		respW := httptest.NewRecorder()
    78  
    79  		// Make the request
    80  		obj, err := s.Server.EvalsRequest(respW, req)
    81  		if err != nil {
    82  			t.Fatalf("err: %v", err)
    83  		}
    84  
    85  		// Check for the index
    86  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
    87  			t.Fatalf("missing index")
    88  		}
    89  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
    90  			t.Fatalf("missing known leader")
    91  		}
    92  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
    93  			t.Fatalf("missing last contact")
    94  		}
    95  
    96  		// Check the eval
    97  		e := obj.([]*structs.Evaluation)
    98  		if len(e) != 1 {
    99  			t.Fatalf("bad: %#v", e)
   100  		}
   101  
   102  		// Check the identifier
   103  		if e[0].ID != eval2.ID {
   104  			t.Fatalf("expected eval ID: %v, Actual: %v", eval2.ID, e[0].ID)
   105  		}
   106  	})
   107  }
   108  
   109  func TestHTTP_EvalAllocations(t *testing.T) {
   110  	t.Parallel()
   111  	httpTest(t, nil, func(s *TestAgent) {
   112  		// Directly manipulate the state
   113  		state := s.Agent.server.State()
   114  		alloc1 := mock.Alloc()
   115  		alloc2 := mock.Alloc()
   116  		alloc2.EvalID = alloc1.EvalID
   117  		state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
   118  		state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
   119  		err := state.UpsertAllocs(1000,
   120  			[]*structs.Allocation{alloc1, alloc2})
   121  		if err != nil {
   122  			t.Fatalf("err: %v", err)
   123  		}
   124  
   125  		// Make the HTTP request
   126  		req, err := http.NewRequest("GET",
   127  			"/v1/evaluation/"+alloc1.EvalID+"/allocations", nil)
   128  		if err != nil {
   129  			t.Fatalf("err: %v", err)
   130  		}
   131  		respW := httptest.NewRecorder()
   132  
   133  		// Make the request
   134  		obj, err := s.Server.EvalSpecificRequest(respW, req)
   135  		if err != nil {
   136  			t.Fatalf("err: %v", err)
   137  		}
   138  
   139  		// Check for the index
   140  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
   141  			t.Fatalf("missing index")
   142  		}
   143  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
   144  			t.Fatalf("missing known leader")
   145  		}
   146  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
   147  			t.Fatalf("missing last contact")
   148  		}
   149  
   150  		// Check the output
   151  		allocs := obj.([]*structs.AllocListStub)
   152  		if len(allocs) != 2 {
   153  			t.Fatalf("bad: %#v", allocs)
   154  		}
   155  	})
   156  }
   157  
   158  func TestHTTP_EvalQuery(t *testing.T) {
   159  	t.Parallel()
   160  	httpTest(t, nil, func(s *TestAgent) {
   161  		// Directly manipulate the state
   162  		state := s.Agent.server.State()
   163  		eval := mock.Eval()
   164  		err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
   165  		if err != nil {
   166  			t.Fatalf("err: %v", err)
   167  		}
   168  
   169  		// Make the HTTP request
   170  		req, err := http.NewRequest("GET", "/v1/evaluation/"+eval.ID, nil)
   171  		if err != nil {
   172  			t.Fatalf("err: %v", err)
   173  		}
   174  		respW := httptest.NewRecorder()
   175  
   176  		// Make the request
   177  		obj, err := s.Server.EvalSpecificRequest(respW, req)
   178  		if err != nil {
   179  			t.Fatalf("err: %v", err)
   180  		}
   181  
   182  		// Check for the index
   183  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
   184  			t.Fatalf("missing index")
   185  		}
   186  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
   187  			t.Fatalf("missing known leader")
   188  		}
   189  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
   190  			t.Fatalf("missing last contact")
   191  		}
   192  
   193  		// Check the job
   194  		e := obj.(*structs.Evaluation)
   195  		if e.ID != eval.ID {
   196  			t.Fatalf("bad: %#v", e)
   197  		}
   198  	})
   199  }