2016-05-28 00:01:08 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2018-01-12 23:05:11 +00:00
|
|
|
"fmt"
|
2016-05-28 00:01:08 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2017-10-05 22:43:20 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-01-13 00:02:21 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-10-05 22:43:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-01-12 23:05:11 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2017-10-05 22:43:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-12 23:05:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-05-28 00:01:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientStatsRequest(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-12 23:05:11 +00:00
|
|
|
require := require.New(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2018-01-12 23:05:11 +00:00
|
|
|
|
|
|
|
// Local node, local resp
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/stats/?since=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientStatsRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:45:10 +00:00
|
|
|
// client stats from server, should not error
|
2018-01-12 23:05:11 +00:00
|
|
|
{
|
|
|
|
srv := s.server
|
|
|
|
s.server = nil
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/stats?node_id=%s", uuid.Generate()), nil)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientStatsRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "Unknown node")
|
|
|
|
|
|
|
|
s.server = srv
|
2016-05-28 00:01:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 23:05:11 +00:00
|
|
|
// no client, server resp
|
|
|
|
{
|
|
|
|
c := s.client
|
|
|
|
s.client = nil
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
n, err := s.server.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return n != nil, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have client: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/stats?node_id=%s", c.NodeID()), nil)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientStatsRequest(respW, req)
|
2018-05-30 19:45:10 +00:00
|
|
|
require.Nil(err)
|
2018-01-12 23:05:11 +00:00
|
|
|
s.client = c
|
2016-05-28 00:01:08 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-10-05 22:43:20 +00:00
|
|
|
|
|
|
|
func TestClientStatsRequest_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-10-05 22:43:20 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/stats/", nil)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.ClientStatsRequest(respW, req)
|
|
|
|
assert.NotNil(err)
|
2023-01-27 13:29:53 +00:00
|
|
|
assert.ErrorContains(err, structs.ErrPermissionDenied.Error())
|
2017-10-05 22:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientStatsRequest(respW, req)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientStatsRequest(respW, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(http.StatusOK, respW.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.ClientStatsRequest(respW, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(http.StatusOK, respW.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|