github.com/thomasobenaus/nomad@v0.11.1/command/agent/scaling_endpoint_test.go (about) 1 package agent 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/hashicorp/nomad/nomad/mock" 11 "github.com/hashicorp/nomad/nomad/structs" 12 ) 13 14 func TestHTTP_ScalingPoliciesList(t *testing.T) { 15 t.Parallel() 16 httpTest(t, nil, func(s *TestAgent) { 17 for i := 0; i < 3; i++ { 18 // Create the job 19 job, _ := mock.JobWithScalingPolicy() 20 21 args := structs.JobRegisterRequest{ 22 Job: job, 23 WriteRequest: structs.WriteRequest{ 24 Region: "global", 25 Namespace: structs.DefaultNamespace, 26 }, 27 } 28 var resp structs.JobRegisterResponse 29 if err := s.Agent.RPC("Job.Register", &args, &resp); err != nil { 30 t.Fatalf("err: %v", err) 31 } 32 } 33 34 // Make the HTTP request 35 req, err := http.NewRequest("GET", "/v1/scaling/policies", nil) 36 if err != nil { 37 t.Fatalf("err: %v", err) 38 } 39 respW := httptest.NewRecorder() 40 41 // Make the request 42 obj, err := s.Server.ScalingPoliciesRequest(respW, req) 43 if err != nil { 44 t.Fatalf("err: %v", err) 45 } 46 47 // Check for the index 48 if respW.Header().Get("X-Nomad-Index") == "" { 49 t.Fatalf("missing index") 50 } 51 if respW.Header().Get("X-Nomad-KnownLeader") != "true" { 52 t.Fatalf("missing known leader") 53 } 54 if respW.Header().Get("X-Nomad-LastContact") == "" { 55 t.Fatalf("missing last contact") 56 } 57 58 // Check the list 59 l := obj.([]*structs.ScalingPolicyListStub) 60 if len(l) != 3 { 61 t.Fatalf("bad: %#v", l) 62 } 63 }) 64 } 65 66 func TestHTTP_ScalingPolicyGet(t *testing.T) { 67 t.Parallel() 68 require := require.New(t) 69 httpTest(t, nil, func(s *TestAgent) { 70 // Create the job 71 job, p := mock.JobWithScalingPolicy() 72 args := structs.JobRegisterRequest{ 73 Job: job, 74 WriteRequest: structs.WriteRequest{ 75 Region: "global", 76 Namespace: structs.DefaultNamespace, 77 }, 78 } 79 var resp structs.JobRegisterResponse 80 err := s.Agent.RPC("Job.Register", &args, &resp) 81 require.NoError(err) 82 83 // Make the HTTP request 84 req, err := http.NewRequest("GET", "/v1/scaling/policy/"+p.ID, nil) 85 require.NoError(err) 86 respW := httptest.NewRecorder() 87 88 // Make the request 89 obj, err := s.Server.ScalingPolicySpecificRequest(respW, req) 90 require.NoError(err) 91 92 // Check for the index 93 if respW.Header().Get("X-Nomad-Index") == "" { 94 t.Fatalf("missing index") 95 } 96 if respW.Header().Get("X-Nomad-KnownLeader") != "true" { 97 t.Fatalf("missing known leader") 98 } 99 if respW.Header().Get("X-Nomad-LastContact") == "" { 100 t.Fatalf("missing last contact") 101 } 102 103 // Check the policy 104 require.Equal(p.ID, obj.(*structs.ScalingPolicy).ID) 105 }) 106 }