2015-09-08 18:41:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-10-21 12:35:34 +00:00
|
|
|
"sort"
|
2019-10-25 14:32:20 +00:00
|
|
|
"strings"
|
2015-09-08 18:41:03 +00:00
|
|
|
"testing"
|
2019-10-07 20:19:32 +00:00
|
|
|
"time"
|
2015-09-14 20:16:42 +00:00
|
|
|
|
2019-03-29 18:47:40 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2022-12-22 14:21:00 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-09-08 18:41:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_Self(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-09-08 18:41:03 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// Get a handle on the Agent endpoints
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
// Query the endpoint
|
|
|
|
res, err := a.Self()
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2015-09-08 18:41:03 +00:00
|
|
|
|
|
|
|
// Check that we got a valid response
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NotEq(t, "", res.Member.Name, must.Sprint("missing member name"))
|
2015-09-08 19:13:39 +00:00
|
|
|
|
|
|
|
// Local cache was populated
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NotEq(t, "", a.nodeName, must.Sprint("cache should be populated"))
|
|
|
|
must.NotEq(t, "", a.datacenter, must.Sprint("cache should be populated"))
|
|
|
|
must.NotEq(t, "", a.region, must.Sprint("cache should be populated"))
|
2015-09-08 18:41:03 +00:00
|
|
|
}
|
2015-09-08 18:51:20 +00:00
|
|
|
|
|
|
|
func TestAgent_NodeName(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-09-08 18:51:20 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
// Query the agent for the node name
|
2023-01-01 18:57:26 +00:00
|
|
|
nodeName, err := a.NodeName()
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotEq(t, "", nodeName)
|
2015-09-08 18:51:20 +00:00
|
|
|
}
|
2015-09-08 19:01:54 +00:00
|
|
|
|
|
|
|
func TestAgent_Datacenter(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-09-08 19:01:54 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
// Query the agent for the datacenter
|
|
|
|
dc, err := a.Datacenter()
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, "dc1", dc)
|
2015-09-08 19:01:54 +00:00
|
|
|
}
|
2015-09-08 21:26:53 +00:00
|
|
|
|
|
|
|
func TestAgent_Join(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-09-11 22:40:54 +00:00
|
|
|
c1, s1 := makeClient(t, nil, nil)
|
|
|
|
defer s1.Stop()
|
2015-09-14 20:16:42 +00:00
|
|
|
a1 := c1.Agent()
|
2015-09-11 22:40:54 +00:00
|
|
|
|
2015-09-14 20:16:42 +00:00
|
|
|
_, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
2015-09-25 04:17:33 +00:00
|
|
|
c.Server.BootstrapExpect = 0
|
2015-09-11 22:40:54 +00:00
|
|
|
})
|
|
|
|
defer s2.Stop()
|
2015-09-08 21:26:53 +00:00
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Attempting to join a nonexistent host returns error
|
2015-09-11 22:40:54 +00:00
|
|
|
n, err := a1.Join("nope")
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Error(t, err)
|
|
|
|
must.Zero(t, 0, must.Sprint("should be zero errors"))
|
2015-09-08 21:26:53 +00:00
|
|
|
|
2015-09-11 22:40:54 +00:00
|
|
|
// Returns correctly if join succeeds
|
|
|
|
n, err = a1.Join(s2.SerfAddr)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.One(t, n)
|
2015-09-08 21:26:53 +00:00
|
|
|
}
|
2015-09-08 21:47:29 +00:00
|
|
|
|
|
|
|
func TestAgent_Members(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-09-08 21:47:29 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
// Query nomad for all the known members
|
|
|
|
mem, err := a.Members()
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2015-09-08 21:47:29 +00:00
|
|
|
|
|
|
|
// Check that we got the expected result
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, mem.Members)
|
|
|
|
must.NotEq(t, "", mem.Members[0].Name)
|
|
|
|
must.NotEq(t, "", mem.Members[0].Addr)
|
|
|
|
must.NotEq(t, 0, mem.Members[0].Port)
|
2015-09-08 21:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ForceLeave(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-09-08 21:47:29 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Force-leave on a nonexistent node does not error
|
2023-01-01 18:57:26 +00:00
|
|
|
err := a.ForceLeave("nope")
|
|
|
|
must.NoError(t, err)
|
2015-09-08 21:47:29 +00:00
|
|
|
|
|
|
|
// TODO: test force-leave on an existing node
|
|
|
|
}
|
2015-09-25 04:17:33 +00:00
|
|
|
|
2015-10-21 12:35:34 +00:00
|
|
|
func (a *AgentMember) String() string {
|
|
|
|
return "{Name: " + a.Name + " Region: " + a.Tags["region"] + " DC: " + a.Tags["dc"] + "}"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgents_Sort(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2015-10-21 12:35:34 +00:00
|
|
|
var sortTests = []struct {
|
|
|
|
in []*AgentMember
|
|
|
|
out []*AgentMember
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-2.vac.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-1.global",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "global", "dc": "dc1"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-1.vac.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
|
|
|
|
},
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-1.global",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "global", "dc": "dc1"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-1.vac.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-2.vac.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.pal.us-west",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.pal.us-west",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
|
|
|
},
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.pal.us-west",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.pal.us-west",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
|
|
|
},
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.tam.us-east",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.ber.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.ber.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
|
|
|
|
},
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.ams.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-01.ber.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-02.ber.europe",
|
2015-10-21 12:35:34 +00:00
|
|
|
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-1.global"},
|
|
|
|
{Name: "nomad-3.global"},
|
|
|
|
{Name: "nomad-2.global"},
|
2015-10-21 12:35:34 +00:00
|
|
|
},
|
|
|
|
[]*AgentMember{
|
2017-09-26 22:26:33 +00:00
|
|
|
{Name: "nomad-1.global"},
|
|
|
|
{Name: "nomad-2.global"},
|
|
|
|
{Name: "nomad-3.global"},
|
2015-10-21 12:35:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range sortTests {
|
|
|
|
sort.Sort(AgentMembersNameSort(tt.in))
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, tt.in, tt.out)
|
2015-10-21 12:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 23:43:00 +00:00
|
|
|
|
|
|
|
func TestAgent_Health(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 23:43:00 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
health, err := a.Health()
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.True(t, health.Server.Ok)
|
2017-10-13 23:43:00 +00:00
|
|
|
}
|
2019-10-07 20:19:32 +00:00
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
// TestAgent_MonitorWithNode tests the Monitor endpoint
|
|
|
|
// passing in a log level and node ie, which tests monitor
|
|
|
|
// functionality for a specific client node
|
2019-10-25 14:32:20 +00:00
|
|
|
func TestAgent_MonitorWithNode(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-22 14:21:00 +00:00
|
|
|
|
2019-10-25 14:32:20 +00:00
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
2022-12-22 14:21:00 +00:00
|
|
|
c.DevMode = true
|
2019-10-25 14:32:20 +00:00
|
|
|
})
|
2019-10-07 20:19:32 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
2022-12-22 14:21:00 +00:00
|
|
|
node := oneNodeFromNodeList(t, c.Nodes())
|
2019-10-25 14:32:20 +00:00
|
|
|
|
2019-10-10 19:30:37 +00:00
|
|
|
doneCh := make(chan struct{})
|
2019-10-24 16:47:46 +00:00
|
|
|
q := &QueryOptions{
|
|
|
|
Params: map[string]string{
|
2019-10-30 13:36:39 +00:00
|
|
|
"log_level": "debug",
|
|
|
|
"node_id": node.ID,
|
2019-10-24 16:47:46 +00:00
|
|
|
},
|
|
|
|
}
|
2019-10-24 20:55:23 +00:00
|
|
|
|
2019-11-01 14:33:28 +00:00
|
|
|
frames, errCh := agent.Monitor(doneCh, q)
|
2019-10-15 14:33:07 +00:00
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
// make a request to generate some logs
|
2019-11-01 14:33:28 +00:00
|
|
|
_, err := agent.NodeName()
|
2022-12-22 14:21:00 +00:00
|
|
|
must.NoError(t, err)
|
2019-10-15 14:33:07 +00:00
|
|
|
|
2019-10-25 14:32:20 +00:00
|
|
|
// Wait for a log message
|
|
|
|
OUTER:
|
2019-10-15 14:33:07 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-11-01 14:33:28 +00:00
|
|
|
case f := <-frames:
|
|
|
|
if strings.Contains(string(f.Data), "[DEBUG]") {
|
2019-10-25 14:32:20 +00:00
|
|
|
break OUTER
|
2019-10-15 14:33:07 +00:00
|
|
|
}
|
2019-11-01 14:33:28 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
t.Errorf("Error: %v", err)
|
2019-10-25 14:32:20 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
2022-12-22 14:21:00 +00:00
|
|
|
t.Fatal("failed to get a DEBUG log message")
|
2019-10-15 14:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 13:28:24 +00:00
|
|
|
|
|
|
|
// TestAgent_Monitor tests the Monitor endpoint
|
|
|
|
// passing in only a log level, which tests the servers
|
|
|
|
// monitor functionality
|
2019-10-25 14:32:20 +00:00
|
|
|
func TestAgent_Monitor(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2019-10-15 14:33:07 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
2019-10-24 16:47:46 +00:00
|
|
|
q := &QueryOptions{
|
|
|
|
Params: map[string]string{
|
2019-10-30 13:36:39 +00:00
|
|
|
"log_level": "debug",
|
2019-10-24 16:47:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:33:07 +00:00
|
|
|
doneCh := make(chan struct{})
|
2019-11-01 14:33:28 +00:00
|
|
|
frames, errCh := agent.Monitor(doneCh, q)
|
2019-10-10 19:30:37 +00:00
|
|
|
defer close(doneCh)
|
2019-10-07 20:19:32 +00:00
|
|
|
|
2019-10-10 19:30:37 +00:00
|
|
|
// make a request to generate some logs
|
2019-11-01 14:33:28 +00:00
|
|
|
_, err := agent.Region()
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2019-10-10 19:30:37 +00:00
|
|
|
|
2019-10-25 14:32:20 +00:00
|
|
|
// Wait for a log message
|
|
|
|
OUTER:
|
2019-10-10 19:30:37 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-11-01 14:33:28 +00:00
|
|
|
case log := <-frames:
|
|
|
|
if log == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-30 20:56:24 +00:00
|
|
|
if strings.Contains(string(log.Data), "[DEBUG]") {
|
2019-10-25 14:32:20 +00:00
|
|
|
break OUTER
|
2019-10-10 19:30:37 +00:00
|
|
|
}
|
2019-11-01 14:33:28 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
t.Fatalf("error: %v", err)
|
2019-10-25 14:32:20 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Unreachable(t, must.Sprint("failed to get DEBUG log message"))
|
2019-10-07 20:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 16:56:07 +00:00
|
|
|
|
|
|
|
func TestAgentCPUProfile(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2019-12-12 16:56:07 +00:00
|
|
|
|
|
|
|
c, s, token := makeACLClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
q := &QueryOptions{
|
|
|
|
AuthToken: token.SecretID,
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:52:13 +00:00
|
|
|
// Valid local request
|
|
|
|
{
|
2019-12-20 15:42:20 +00:00
|
|
|
opts := PprofOptions{
|
|
|
|
Seconds: 1,
|
|
|
|
}
|
|
|
|
resp, err := agent.CPUProfile(opts, q)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, resp)
|
2019-12-12 21:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid server request
|
|
|
|
{
|
2019-12-20 15:42:20 +00:00
|
|
|
opts := PprofOptions{
|
|
|
|
Seconds: 1,
|
|
|
|
ServerID: "unknown.global",
|
|
|
|
}
|
|
|
|
resp, err := agent.CPUProfile(opts, q)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Error(t, err)
|
|
|
|
must.ErrorContains(t, err, "500 (unknown Nomad server unknown.global)")
|
|
|
|
must.Nil(t, resp)
|
2019-12-12 21:52:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgentTrace(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2019-12-12 16:56:07 +00:00
|
|
|
|
|
|
|
c, s, token := makeACLClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
q := &QueryOptions{
|
|
|
|
AuthToken: token.SecretID,
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:42:20 +00:00
|
|
|
resp, err := agent.Trace(PprofOptions{}, q)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, resp)
|
2019-12-12 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgentProfile(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2019-12-12 16:56:07 +00:00
|
|
|
|
|
|
|
c, s, token := makeACLClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
q := &QueryOptions{
|
|
|
|
AuthToken: token.SecretID,
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-20 15:42:20 +00:00
|
|
|
resp, err := agent.Lookup("heap", PprofOptions{}, q)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, resp)
|
2019-12-12 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unknown profile
|
|
|
|
{
|
2019-12-20 15:42:20 +00:00
|
|
|
resp, err := agent.Lookup("invalid", PprofOptions{}, q)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Error(t, err)
|
|
|
|
must.ErrorContains(t, err, "Unexpected response code: 404")
|
|
|
|
must.Nil(t, resp)
|
2019-12-12 16:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 16:56:13 +00:00
|
|
|
|
|
|
|
func TestAgent_SchedulerWorkerConfig(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-01-06 16:56:13 +00:00
|
|
|
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
config, err := a.GetSchedulerWorkerConfig(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, config)
|
2022-01-06 16:56:13 +00:00
|
|
|
newConfig := SchedulerWorkerPoolArgs{NumSchedulers: 0, EnabledSchedulers: []string{"_core", "system"}}
|
|
|
|
resp, err := a.SetSchedulerWorkerConfig(newConfig, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotEq(t, config, resp)
|
2022-01-06 16:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_SchedulerWorkerConfig_BadRequest(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-01-06 16:56:13 +00:00
|
|
|
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
config, err := a.GetSchedulerWorkerConfig(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, config)
|
2022-01-06 16:56:13 +00:00
|
|
|
newConfig := SchedulerWorkerPoolArgs{NumSchedulers: -1, EnabledSchedulers: []string{"_core", "system"}}
|
|
|
|
_, err = a.SetSchedulerWorkerConfig(newConfig, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Error(t, err)
|
|
|
|
must.ErrorContains(t, err, "400 (Invalid request)")
|
2022-01-06 16:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_SchedulerWorkersInfo(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2022-01-06 16:56:13 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Agent()
|
|
|
|
|
|
|
|
info, err := a.GetSchedulerWorkersInfo(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, info)
|
2022-01-06 16:56:13 +00:00
|
|
|
defaultSchedulers := []string{"batch", "system", "sysbatch", "service", "_core"}
|
|
|
|
for _, worker := range info.Schedulers {
|
2023-01-01 18:57:26 +00:00
|
|
|
must.SliceContainsAll(t, defaultSchedulers, worker.EnabledSchedulers)
|
2022-01-06 16:56:13 +00:00
|
|
|
}
|
|
|
|
}
|