github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/agent/stats_endpoint_test.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/hashicorp/nomad/acl" 10 "github.com/hashicorp/nomad/ci" 11 "github.com/hashicorp/nomad/helper/uuid" 12 "github.com/hashicorp/nomad/nomad/mock" 13 "github.com/hashicorp/nomad/nomad/structs" 14 "github.com/hashicorp/nomad/testutil" 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestClientStatsRequest(t *testing.T) { 20 ci.Parallel(t) 21 require := require.New(t) 22 httpTest(t, nil, func(s *TestAgent) { 23 24 // Local node, local resp 25 { 26 req, err := http.NewRequest("GET", "/v1/client/stats/?since=foo", nil) 27 if err != nil { 28 t.Fatalf("err: %v", err) 29 } 30 31 respW := httptest.NewRecorder() 32 _, err = s.Server.ClientStatsRequest(respW, req) 33 if err != nil { 34 t.Fatalf("unexpected err: %v", err) 35 } 36 } 37 38 // client stats from server, should not error 39 { 40 srv := s.server 41 s.server = nil 42 43 req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/stats?node_id=%s", uuid.Generate()), nil) 44 require.Nil(err) 45 46 respW := httptest.NewRecorder() 47 _, err = s.Server.ClientStatsRequest(respW, req) 48 require.NotNil(err) 49 require.Contains(err.Error(), "Unknown node") 50 51 s.server = srv 52 } 53 54 // no client, server resp 55 { 56 c := s.client 57 s.client = nil 58 59 testutil.WaitForResult(func() (bool, error) { 60 n, err := s.server.State().NodeByID(nil, c.NodeID()) 61 if err != nil { 62 return false, err 63 } 64 return n != nil, nil 65 }, func(err error) { 66 t.Fatalf("should have client: %v", err) 67 }) 68 69 req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/stats?node_id=%s", c.NodeID()), nil) 70 require.Nil(err) 71 72 respW := httptest.NewRecorder() 73 _, err = s.Server.ClientStatsRequest(respW, req) 74 require.Nil(err) 75 s.client = c 76 } 77 }) 78 } 79 80 func TestClientStatsRequest_ACL(t *testing.T) { 81 ci.Parallel(t) 82 assert := assert.New(t) 83 httpACLTest(t, nil, func(s *TestAgent) { 84 state := s.Agent.server.State() 85 req, err := http.NewRequest("GET", "/v1/client/stats/", nil) 86 assert.Nil(err) 87 88 // Try request without a token and expect failure 89 { 90 respW := httptest.NewRecorder() 91 _, err := s.Server.ClientStatsRequest(respW, req) 92 assert.NotNil(err) 93 assert.Equal(err.Error(), structs.ErrPermissionDenied.Error()) 94 } 95 96 // Try request with an invalid token and expect failure 97 { 98 respW := httptest.NewRecorder() 99 token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyDeny)) 100 setToken(req, token) 101 _, err := s.Server.ClientStatsRequest(respW, req) 102 assert.NotNil(err) 103 assert.Equal(err.Error(), structs.ErrPermissionDenied.Error()) 104 } 105 106 // Try request with a valid token 107 { 108 respW := httptest.NewRecorder() 109 token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.NodePolicy(acl.PolicyRead)) 110 setToken(req, token) 111 _, err := s.Server.ClientStatsRequest(respW, req) 112 assert.Nil(err) 113 assert.Equal(http.StatusOK, respW.Code) 114 } 115 116 // Try request with a management token 117 { 118 respW := httptest.NewRecorder() 119 setToken(req, s.RootToken) 120 _, err := s.Server.ClientStatsRequest(respW, req) 121 assert.Nil(err) 122 assert.Equal(http.StatusOK, respW.Code) 123 } 124 }) 125 }