github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/agent/deployment_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  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestHTTP_DeploymentList(t *testing.T) {
    14  	t.Parallel()
    15  	assert := assert.New(t)
    16  	httpTest(t, nil, func(s *TestAgent) {
    17  		// Directly manipulate the state
    18  		state := s.Agent.server.State()
    19  		d1 := mock.Deployment()
    20  		d2 := mock.Deployment()
    21  		assert.Nil(state.UpsertDeployment(999, d1), "UpsertDeployment")
    22  		assert.Nil(state.UpsertDeployment(1000, d2), "UpsertDeployment")
    23  
    24  		// Make the HTTP request
    25  		req, err := http.NewRequest("GET", "/v1/deployments", nil)
    26  		assert.Nil(err, "HTTP Request")
    27  		respW := httptest.NewRecorder()
    28  
    29  		// Make the request
    30  		obj, err := s.Server.DeploymentsRequest(respW, req)
    31  		assert.Nil(err, "Deployment Request")
    32  
    33  		// Check for the index
    34  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
    35  		assert.Equal("true", respW.HeaderMap.Get("X-Nomad-KnownLeader"), "missing known leader")
    36  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-LastContact"), "missing last contact")
    37  
    38  		// Check the deployments
    39  		deploys := obj.([]*structs.Deployment)
    40  		assert.Len(deploys, 2, "Deployments")
    41  	})
    42  }
    43  
    44  func TestHTTP_DeploymentPrefixList(t *testing.T) {
    45  	t.Parallel()
    46  	assert := assert.New(t)
    47  	httpTest(t, nil, func(s *TestAgent) {
    48  		// Directly manipulate the state
    49  		state := s.Agent.server.State()
    50  		d1 := mock.Deployment()
    51  		d1.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
    52  		d2 := mock.Deployment()
    53  		d2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
    54  		assert.Nil(state.UpsertDeployment(999, d1), "UpsertDeployment")
    55  		assert.Nil(state.UpsertDeployment(1000, d2), "UpsertDeployment")
    56  
    57  		// Make the HTTP request
    58  		req, err := http.NewRequest("GET", "/v1/deployments?prefix=aaab", nil)
    59  		assert.Nil(err, "HTTP Request")
    60  		respW := httptest.NewRecorder()
    61  
    62  		// Make the request
    63  		obj, err := s.Server.DeploymentsRequest(respW, req)
    64  		assert.Nil(err, "Deployment Request")
    65  
    66  		// Check for the index
    67  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
    68  		assert.Equal("true", respW.HeaderMap.Get("X-Nomad-KnownLeader"), "missing known leader")
    69  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-LastContact"), "missing last contact")
    70  
    71  		// Check the deployments
    72  		deploys := obj.([]*structs.Deployment)
    73  		assert.Len(deploys, 1, "Deployments")
    74  		assert.Equal(d1.ID, deploys[0].ID, "Wrong Deployment")
    75  	})
    76  }
    77  
    78  func TestHTTP_DeploymentAllocations(t *testing.T) {
    79  	t.Parallel()
    80  	assert := assert.New(t)
    81  	httpTest(t, nil, func(s *TestAgent) {
    82  		// Directly manipulate the state
    83  		state := s.Agent.server.State()
    84  		j := mock.Job()
    85  		d := mock.Deployment()
    86  		d.JobID = j.ID
    87  		a1 := mock.Alloc()
    88  		a1.JobID = j.ID
    89  		a1.DeploymentID = d.ID
    90  		a2 := mock.Alloc()
    91  		a2.JobID = j.ID
    92  		a2.DeploymentID = d.ID
    93  		assert.Nil(state.UpsertJob(998, j), "UpsertJob")
    94  		assert.Nil(state.UpsertDeployment(999, d), "UpsertDeployment")
    95  		assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a1, a2}), "UpsertAllocs")
    96  
    97  		// Make the HTTP request
    98  		req, err := http.NewRequest("GET", "/v1/deployment/allocations/"+d.ID, nil)
    99  		assert.Nil(err, "HTTP Request")
   100  		respW := httptest.NewRecorder()
   101  
   102  		// Make the request
   103  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   104  		assert.Nil(err, "DeploymentSpecificRequest")
   105  
   106  		// Check for the index
   107  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   108  		assert.Equal("true", respW.HeaderMap.Get("X-Nomad-KnownLeader"), "missing known leader")
   109  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-LastContact"), "missing last contact")
   110  
   111  		// Check the ouptput
   112  		allocs := obj.([]*structs.AllocListStub)
   113  		assert.Len(allocs, 2, "Deployment Allocs")
   114  	})
   115  }
   116  
   117  func TestHTTP_DeploymentQuery(t *testing.T) {
   118  	t.Parallel()
   119  	assert := assert.New(t)
   120  	httpTest(t, nil, func(s *TestAgent) {
   121  		// Directly manipulate the state
   122  		state := s.Agent.server.State()
   123  		d := mock.Deployment()
   124  		assert.Nil(state.UpsertDeployment(1000, d), "UpsertDeployment")
   125  
   126  		// Make the HTTP request
   127  		req, err := http.NewRequest("GET", "/v1/deployment/"+d.ID, nil)
   128  		assert.Nil(err, "HTTP Request")
   129  		respW := httptest.NewRecorder()
   130  
   131  		// Make the request
   132  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   133  		assert.Nil(err, "Deployment Request")
   134  
   135  		// Check for the index
   136  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   137  		assert.Equal("true", respW.HeaderMap.Get("X-Nomad-KnownLeader"), "missing known leader")
   138  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-LastContact"), "missing last contact")
   139  
   140  		// Check the job
   141  		out := obj.(*structs.Deployment)
   142  		assert.Equal(d.ID, out.ID, "ID mismatch")
   143  	})
   144  }
   145  
   146  func TestHTTP_DeploymentPause(t *testing.T) {
   147  	t.Parallel()
   148  	assert := assert.New(t)
   149  	httpTest(t, nil, func(s *TestAgent) {
   150  		// Directly manipulate the state
   151  		state := s.Agent.server.State()
   152  		j := mock.Job()
   153  		d := mock.Deployment()
   154  		d.JobID = j.ID
   155  		assert.Nil(state.UpsertJob(999, j), "UpsertJob")
   156  		assert.Nil(state.UpsertDeployment(1000, d), "UpsertDeployment")
   157  
   158  		// Create the pause request
   159  		args := structs.DeploymentPauseRequest{
   160  			DeploymentID: d.ID,
   161  			Pause:        false,
   162  			WriteRequest: structs.WriteRequest{Region: "global"},
   163  		}
   164  		buf := encodeReq(args)
   165  
   166  		// Make the HTTP request
   167  		req, err := http.NewRequest("PUT", "/v1/deployment/pause/"+d.ID, buf)
   168  		assert.Nil(err, "HTTP Request")
   169  		respW := httptest.NewRecorder()
   170  
   171  		// Make the request
   172  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   173  		assert.Nil(err, "Deployment Request")
   174  
   175  		// Check the response
   176  		resp := obj.(structs.DeploymentUpdateResponse)
   177  		assert.NotZero(resp.EvalID, "Expect Eval")
   178  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   179  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   180  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   181  	})
   182  }
   183  
   184  func TestHTTP_DeploymentPromote(t *testing.T) {
   185  	t.Parallel()
   186  	assert := assert.New(t)
   187  	httpTest(t, nil, func(s *TestAgent) {
   188  		// Directly manipulate the state
   189  		state := s.Agent.server.State()
   190  		j := mock.Job()
   191  		d := mock.Deployment()
   192  		d.JobID = j.ID
   193  		assert.Nil(state.UpsertJob(999, j), "UpsertJob")
   194  		assert.Nil(state.UpsertDeployment(1000, d), "UpsertDeployment")
   195  
   196  		// Create the pause request
   197  		args := structs.DeploymentPromoteRequest{
   198  			DeploymentID: d.ID,
   199  			All:          true,
   200  			WriteRequest: structs.WriteRequest{Region: "global"},
   201  		}
   202  		buf := encodeReq(args)
   203  
   204  		// Make the HTTP request
   205  		req, err := http.NewRequest("PUT", "/v1/deployment/pause/"+d.ID, buf)
   206  		assert.Nil(err, "HTTP Request")
   207  		respW := httptest.NewRecorder()
   208  
   209  		// Make the request
   210  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   211  		assert.Nil(err, "Deployment Request")
   212  
   213  		// Check the response
   214  		resp := obj.(structs.DeploymentUpdateResponse)
   215  		assert.NotZero(resp.EvalID, "Expect Eval")
   216  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   217  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   218  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   219  	})
   220  }
   221  
   222  func TestHTTP_DeploymentAllocHealth(t *testing.T) {
   223  	t.Parallel()
   224  	assert := assert.New(t)
   225  	httpTest(t, nil, func(s *TestAgent) {
   226  		// Directly manipulate the state
   227  		state := s.Agent.server.State()
   228  		j := mock.Job()
   229  		d := mock.Deployment()
   230  		d.JobID = j.ID
   231  		a := mock.Alloc()
   232  		a.JobID = j.ID
   233  		a.DeploymentID = d.ID
   234  		assert.Nil(state.UpsertJob(998, j), "UpsertJob")
   235  		assert.Nil(state.UpsertDeployment(999, d), "UpsertDeployment")
   236  		assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}), "UpsertAllocs")
   237  
   238  		// Create the pause request
   239  		args := structs.DeploymentAllocHealthRequest{
   240  			DeploymentID:         d.ID,
   241  			HealthyAllocationIDs: []string{a.ID},
   242  			WriteRequest:         structs.WriteRequest{Region: "global"},
   243  		}
   244  		buf := encodeReq(args)
   245  
   246  		// Make the HTTP request
   247  		req, err := http.NewRequest("PUT", "/v1/deployment/allocation-health/"+d.ID, buf)
   248  		assert.Nil(err, "HTTP Request")
   249  		respW := httptest.NewRecorder()
   250  
   251  		// Make the request
   252  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   253  		assert.Nil(err, "Deployment Request")
   254  
   255  		// Check the response
   256  		resp := obj.(structs.DeploymentUpdateResponse)
   257  		assert.NotZero(resp.EvalID, "Expect Eval")
   258  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   259  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   260  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   261  	})
   262  }
   263  
   264  func TestHTTP_DeploymentFail(t *testing.T) {
   265  	t.Parallel()
   266  	assert := assert.New(t)
   267  	httpTest(t, nil, func(s *TestAgent) {
   268  		// Directly manipulate the state
   269  		state := s.Agent.server.State()
   270  		j := mock.Job()
   271  		d := mock.Deployment()
   272  		d.JobID = j.ID
   273  		assert.Nil(state.UpsertJob(998, j), "UpsertJob")
   274  		assert.Nil(state.UpsertDeployment(999, d), "UpsertDeployment")
   275  
   276  		// Make the HTTP request
   277  		req, err := http.NewRequest("PUT", "/v1/deployment/fail/"+d.ID, nil)
   278  		assert.Nil(err, "HTTP Request")
   279  		respW := httptest.NewRecorder()
   280  
   281  		// Make the request
   282  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   283  		assert.Nil(err, "Deployment Request")
   284  
   285  		// Check the response
   286  		resp := obj.(structs.DeploymentUpdateResponse)
   287  		assert.NotZero(resp.EvalID, "Expect Eval")
   288  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   289  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   290  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   291  	})
   292  }