github.com/uchennaokeke444/nomad@v0.11.8/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  
    91  		testEvent := structs.NewTaskEvent(structs.TaskSiblingFailed)
    92  		var events1 []*structs.TaskEvent
    93  		events1 = append(events1, testEvent)
    94  		taskState := &structs.TaskState{Events: events1}
    95  		a1.TaskStates = make(map[string]*structs.TaskState)
    96  		a1.TaskStates["test"] = taskState
    97  
    98  		a2 := mock.Alloc()
    99  		a2.JobID = j.ID
   100  		a2.DeploymentID = d.ID
   101  
   102  		// Create a test event
   103  		testEvent2 := structs.NewTaskEvent(structs.TaskSiblingFailed)
   104  		var events2 []*structs.TaskEvent
   105  		events2 = append(events2, testEvent2)
   106  		taskState2 := &structs.TaskState{Events: events2}
   107  		a2.TaskStates = make(map[string]*structs.TaskState)
   108  		a2.TaskStates["test"] = taskState2
   109  
   110  		assert.Nil(state.UpsertJob(998, j), "UpsertJob")
   111  		assert.Nil(state.UpsertDeployment(999, d), "UpsertDeployment")
   112  		assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a1, a2}), "UpsertAllocs")
   113  
   114  		// Make the HTTP request
   115  		req, err := http.NewRequest("GET", "/v1/deployment/allocations/"+d.ID, nil)
   116  		assert.Nil(err, "HTTP Request")
   117  		respW := httptest.NewRecorder()
   118  
   119  		// Make the request
   120  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   121  		assert.Nil(err, "DeploymentSpecificRequest")
   122  
   123  		// Check for the index
   124  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   125  		assert.Equal("true", respW.HeaderMap.Get("X-Nomad-KnownLeader"), "missing known leader")
   126  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-LastContact"), "missing last contact")
   127  
   128  		// Check the output
   129  		allocs := obj.([]*structs.AllocListStub)
   130  		assert.Len(allocs, 2, "Deployment Allocs")
   131  		expectedMsg := "Task's sibling failed"
   132  		displayMsg1 := allocs[0].TaskStates["test"].Events[0].DisplayMessage
   133  		assert.Equal(expectedMsg, displayMsg1, "DisplayMessage should be set")
   134  		displayMsg2 := allocs[0].TaskStates["test"].Events[0].DisplayMessage
   135  		assert.Equal(expectedMsg, displayMsg2, "DisplayMessage should be set")
   136  	})
   137  }
   138  
   139  func TestHTTP_DeploymentQuery(t *testing.T) {
   140  	t.Parallel()
   141  	assert := assert.New(t)
   142  	httpTest(t, nil, func(s *TestAgent) {
   143  		// Directly manipulate the state
   144  		state := s.Agent.server.State()
   145  		d := mock.Deployment()
   146  		assert.Nil(state.UpsertDeployment(1000, d), "UpsertDeployment")
   147  
   148  		// Make the HTTP request
   149  		req, err := http.NewRequest("GET", "/v1/deployment/"+d.ID, nil)
   150  		assert.Nil(err, "HTTP Request")
   151  		respW := httptest.NewRecorder()
   152  
   153  		// Make the request
   154  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   155  		assert.Nil(err, "Deployment Request")
   156  
   157  		// Check for the index
   158  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   159  		assert.Equal("true", respW.HeaderMap.Get("X-Nomad-KnownLeader"), "missing known leader")
   160  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-LastContact"), "missing last contact")
   161  
   162  		// Check the job
   163  		out := obj.(*structs.Deployment)
   164  		assert.Equal(d.ID, out.ID, "ID mismatch")
   165  	})
   166  }
   167  
   168  func TestHTTP_DeploymentPause(t *testing.T) {
   169  	t.Parallel()
   170  	assert := assert.New(t)
   171  	httpTest(t, nil, func(s *TestAgent) {
   172  		// Directly manipulate the state
   173  		state := s.Agent.server.State()
   174  		j := mock.Job()
   175  		d := mock.Deployment()
   176  		d.JobID = j.ID
   177  		assert.Nil(state.UpsertJob(999, j), "UpsertJob")
   178  		assert.Nil(state.UpsertDeployment(1000, d), "UpsertDeployment")
   179  
   180  		// Create the pause request
   181  		args := structs.DeploymentPauseRequest{
   182  			DeploymentID: d.ID,
   183  			Pause:        false,
   184  			WriteRequest: structs.WriteRequest{
   185  				Region:    "global",
   186  				Namespace: structs.DefaultNamespace,
   187  			},
   188  		}
   189  		buf := encodeReq(args)
   190  
   191  		// Make the HTTP request
   192  		req, err := http.NewRequest("PUT", "/v1/deployment/pause/"+d.ID, buf)
   193  		assert.Nil(err, "HTTP Request")
   194  		respW := httptest.NewRecorder()
   195  
   196  		// Make the request
   197  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   198  		assert.Nil(err, "Deployment Request")
   199  
   200  		// Check the response
   201  		resp := obj.(structs.DeploymentUpdateResponse)
   202  		assert.NotZero(resp.EvalID, "Expect Eval")
   203  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   204  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   205  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   206  	})
   207  }
   208  
   209  func TestHTTP_DeploymentPromote(t *testing.T) {
   210  	t.Parallel()
   211  	assert := assert.New(t)
   212  	httpTest(t, nil, func(s *TestAgent) {
   213  		// Directly manipulate the state
   214  		state := s.Agent.server.State()
   215  		j := mock.Job()
   216  		d := mock.Deployment()
   217  		d.JobID = j.ID
   218  		assert.Nil(state.UpsertJob(999, j), "UpsertJob")
   219  		assert.Nil(state.UpsertDeployment(1000, d), "UpsertDeployment")
   220  
   221  		// Create the pause request
   222  		args := structs.DeploymentPromoteRequest{
   223  			DeploymentID: d.ID,
   224  			All:          true,
   225  			WriteRequest: structs.WriteRequest{
   226  				Region:    "global",
   227  				Namespace: structs.DefaultNamespace,
   228  			},
   229  		}
   230  		buf := encodeReq(args)
   231  
   232  		// Make the HTTP request
   233  		req, err := http.NewRequest("PUT", "/v1/deployment/pause/"+d.ID, buf)
   234  		assert.Nil(err, "HTTP Request")
   235  		respW := httptest.NewRecorder()
   236  
   237  		// Make the request
   238  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   239  		assert.Nil(err, "Deployment Request")
   240  
   241  		// Check the response
   242  		resp := obj.(structs.DeploymentUpdateResponse)
   243  		assert.NotZero(resp.EvalID, "Expect Eval")
   244  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   245  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   246  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   247  	})
   248  }
   249  
   250  func TestHTTP_DeploymentAllocHealth(t *testing.T) {
   251  	t.Parallel()
   252  	assert := assert.New(t)
   253  	httpTest(t, nil, func(s *TestAgent) {
   254  		// Directly manipulate the state
   255  		state := s.Agent.server.State()
   256  		j := mock.Job()
   257  		d := mock.Deployment()
   258  		d.JobID = j.ID
   259  		a := mock.Alloc()
   260  		a.JobID = j.ID
   261  		a.DeploymentID = d.ID
   262  		assert.Nil(state.UpsertJob(998, j), "UpsertJob")
   263  		assert.Nil(state.UpsertDeployment(999, d), "UpsertDeployment")
   264  		assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}), "UpsertAllocs")
   265  
   266  		// Create the pause request
   267  		args := structs.DeploymentAllocHealthRequest{
   268  			DeploymentID:         d.ID,
   269  			HealthyAllocationIDs: []string{a.ID},
   270  			WriteRequest: structs.WriteRequest{
   271  				Region:    "global",
   272  				Namespace: structs.DefaultNamespace,
   273  			},
   274  		}
   275  		buf := encodeReq(args)
   276  
   277  		// Make the HTTP request
   278  		req, err := http.NewRequest("PUT", "/v1/deployment/allocation-health/"+d.ID, buf)
   279  		assert.Nil(err, "HTTP Request")
   280  		respW := httptest.NewRecorder()
   281  
   282  		// Make the request
   283  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   284  		assert.Nil(err, "Deployment Request")
   285  
   286  		// Check the response
   287  		resp := obj.(structs.DeploymentUpdateResponse)
   288  		assert.NotZero(resp.EvalID, "Expect Eval")
   289  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   290  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   291  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   292  	})
   293  }
   294  
   295  func TestHTTP_DeploymentFail(t *testing.T) {
   296  	t.Parallel()
   297  	assert := assert.New(t)
   298  	httpTest(t, nil, func(s *TestAgent) {
   299  		// Directly manipulate the state
   300  		state := s.Agent.server.State()
   301  		j := mock.Job()
   302  		d := mock.Deployment()
   303  		d.JobID = j.ID
   304  		assert.Nil(state.UpsertJob(998, j), "UpsertJob")
   305  		assert.Nil(state.UpsertDeployment(999, d), "UpsertDeployment")
   306  
   307  		// Make the HTTP request
   308  		req, err := http.NewRequest("PUT", "/v1/deployment/fail/"+d.ID, nil)
   309  		assert.Nil(err, "HTTP Request")
   310  		respW := httptest.NewRecorder()
   311  
   312  		// Make the request
   313  		obj, err := s.Server.DeploymentSpecificRequest(respW, req)
   314  		assert.Nil(err, "Deployment Request")
   315  
   316  		// Check the response
   317  		resp := obj.(structs.DeploymentUpdateResponse)
   318  		assert.NotZero(resp.EvalID, "Expect Eval")
   319  		assert.NotZero(resp.EvalCreateIndex, "Expect Eval")
   320  		assert.NotZero(resp.DeploymentModifyIndex, "Expect Deployment to be Modified")
   321  		assert.NotZero(respW.HeaderMap.Get("X-Nomad-Index"), "missing index")
   322  	})
   323  }