github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/command/agent/alloc_endpoint_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  )
    12  
    13  func TestHTTP_AllocsList(t *testing.T) {
    14  	httpTest(t, nil, func(s *TestServer) {
    15  		// Directly manipulate the state
    16  		state := s.Agent.server.State()
    17  		alloc1 := mock.Alloc()
    18  		alloc2 := mock.Alloc()
    19  		state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
    20  		state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
    21  		err := state.UpsertAllocs(1000,
    22  			[]*structs.Allocation{alloc1, alloc2})
    23  		if err != nil {
    24  			t.Fatalf("err: %v", err)
    25  		}
    26  
    27  		// Make the HTTP request
    28  		req, err := http.NewRequest("GET", "/v1/allocations", nil)
    29  		if err != nil {
    30  			t.Fatalf("err: %v", err)
    31  		}
    32  		respW := httptest.NewRecorder()
    33  
    34  		// Make the request
    35  		obj, err := s.Server.AllocsRequest(respW, req)
    36  		if err != nil {
    37  			t.Fatalf("err: %v", err)
    38  		}
    39  
    40  		// Check for the index
    41  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
    42  			t.Fatalf("missing index")
    43  		}
    44  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
    45  			t.Fatalf("missing known leader")
    46  		}
    47  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
    48  			t.Fatalf("missing last contact")
    49  		}
    50  
    51  		// Check the alloc
    52  		n := obj.([]*structs.AllocListStub)
    53  		if len(n) != 2 {
    54  			t.Fatalf("bad: %#v", n)
    55  		}
    56  	})
    57  }
    58  
    59  func TestHTTP_AllocsPrefixList(t *testing.T) {
    60  	httpTest(t, nil, func(s *TestServer) {
    61  		// Directly manipulate the state
    62  		state := s.Agent.server.State()
    63  
    64  		alloc1 := mock.Alloc()
    65  		alloc1.ID = "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706"
    66  		alloc2 := mock.Alloc()
    67  		alloc2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
    68  		summary1 := mock.JobSummary(alloc1.JobID)
    69  		summary2 := mock.JobSummary(alloc2.JobID)
    70  		if err := state.UpsertJobSummary(998, summary1); err != nil {
    71  			t.Fatal(err)
    72  		}
    73  		if err := state.UpsertJobSummary(999, summary2); err != nil {
    74  			t.Fatal(err)
    75  		}
    76  		if err := state.UpsertAllocs(1000,
    77  			[]*structs.Allocation{alloc1, alloc2}); err != nil {
    78  			t.Fatalf("err: %v", err)
    79  		}
    80  
    81  		// Make the HTTP request
    82  		req, err := http.NewRequest("GET", "/v1/allocations?prefix=aaab", nil)
    83  		if err != nil {
    84  			t.Fatalf("err: %v", err)
    85  		}
    86  		respW := httptest.NewRecorder()
    87  
    88  		// Make the request
    89  		obj, err := s.Server.AllocsRequest(respW, req)
    90  		if err != nil {
    91  			t.Fatalf("err: %v", err)
    92  		}
    93  
    94  		// Check for the index
    95  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
    96  			t.Fatalf("missing index")
    97  		}
    98  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
    99  			t.Fatalf("missing known leader")
   100  		}
   101  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
   102  			t.Fatalf("missing last contact")
   103  		}
   104  
   105  		// Check the alloc
   106  		n := obj.([]*structs.AllocListStub)
   107  		if len(n) != 1 {
   108  			t.Fatalf("bad: %#v", n)
   109  		}
   110  
   111  		// Check the identifier
   112  		if n[0].ID != alloc2.ID {
   113  			t.Fatalf("expected alloc ID: %v, Actual: %v", alloc2.ID, n[0].ID)
   114  		}
   115  	})
   116  }
   117  
   118  func TestHTTP_AllocQuery(t *testing.T) {
   119  	httpTest(t, nil, func(s *TestServer) {
   120  		// Directly manipulate the state
   121  		state := s.Agent.server.State()
   122  		alloc := mock.Alloc()
   123  		if err := state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)); err != nil {
   124  			t.Fatal(err)
   125  		}
   126  		err := state.UpsertAllocs(1000,
   127  			[]*structs.Allocation{alloc})
   128  		if err != nil {
   129  			t.Fatalf("err: %v", err)
   130  		}
   131  
   132  		// Make the HTTP request
   133  		req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil)
   134  		if err != nil {
   135  			t.Fatalf("err: %v", err)
   136  		}
   137  		respW := httptest.NewRecorder()
   138  
   139  		// Make the request
   140  		obj, err := s.Server.AllocSpecificRequest(respW, req)
   141  		if err != nil {
   142  			t.Fatalf("err: %v", err)
   143  		}
   144  
   145  		// Check for the index
   146  		if respW.HeaderMap.Get("X-Nomad-Index") == "" {
   147  			t.Fatalf("missing index")
   148  		}
   149  		if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
   150  			t.Fatalf("missing known leader")
   151  		}
   152  		if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
   153  			t.Fatalf("missing last contact")
   154  		}
   155  
   156  		// Check the job
   157  		a := obj.(*structs.Allocation)
   158  		if a.ID != alloc.ID {
   159  			t.Fatalf("bad: %#v", a)
   160  		}
   161  	})
   162  }
   163  
   164  func TestHTTP_AllocStats(t *testing.T) {
   165  	httpTest(t, nil, func(s *TestServer) {
   166  		// Make the HTTP request
   167  		req, err := http.NewRequest("GET", "/v1/client/allocation/123/foo", nil)
   168  		if err != nil {
   169  			t.Fatalf("err: %v", err)
   170  		}
   171  		respW := httptest.NewRecorder()
   172  
   173  		// Make the request
   174  		_, err = s.Server.ClientAllocRequest(respW, req)
   175  		if !strings.Contains(err.Error(), resourceNotFoundErr) {
   176  			t.Fatalf("err: %v", err)
   177  		}
   178  	})
   179  }
   180  
   181  func TestHTTP_AllocSnapshot(t *testing.T) {
   182  	httpTest(t, nil, func(s *TestServer) {
   183  		// Make the HTTP request
   184  		req, err := http.NewRequest("GET", "/v1/client/allocation/123/snapshot", nil)
   185  		if err != nil {
   186  			t.Fatalf("err: %v", err)
   187  		}
   188  		respW := httptest.NewRecorder()
   189  
   190  		// Make the request
   191  		_, err = s.Server.ClientAllocRequest(respW, req)
   192  		if !strings.Contains(err.Error(), allocNotFoundErr) {
   193  			t.Fatalf("err: %v", err)
   194  		}
   195  	})
   196  }