open-nomad/command/agent/stats_endpoint_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
3.1 KiB
Go
Raw Normal View History

package agent
import (
2018-01-12 23:05:11 +00:00
"fmt"
"net/http"
"net/http/httptest"
"testing"
2017-10-05 22:43:20 +00:00
"github.com/hashicorp/nomad/acl"
"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"
)
func TestClientStatsRequest(t *testing.T) {
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)
}
}
// 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
}
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)
require.Nil(err)
2018-01-12 23:05:11 +00:00
s.client = c
}
})
}
2017-10-05 22:43:20 +00:00
func TestClientStatsRequest_ACL(t *testing.T) {
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)
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)
}
})
}