2015-09-07 02:08:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-10-17 17:48:04 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-09-07 02:08:05 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-09-25 03:39:03 +00:00
|
|
|
"strings"
|
2015-09-07 02:08:05 +00:00
|
|
|
"testing"
|
2016-10-17 17:48:04 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-07 02:08:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_AgentSelf(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestServer) {
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/self", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AgentSelfRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
|
|
|
self := obj.(agentSelf)
|
|
|
|
if self.Config == nil {
|
|
|
|
t.Fatalf("bad: %#v", self)
|
|
|
|
}
|
|
|
|
if len(self.Stats) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", self)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentJoin(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestServer) {
|
|
|
|
// Determine the join address
|
|
|
|
member := s.Agent.Server().LocalMember()
|
|
|
|
addr := fmt.Sprintf("%s:%d", member.Addr, member.Port)
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("PUT",
|
|
|
|
fmt.Sprintf("/v1/agent/join?address=%s&address=%s", addr, addr), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AgentJoinRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
|
|
|
join := obj.(joinResult)
|
|
|
|
if join.NumJoined != 2 {
|
|
|
|
t.Fatalf("bad: %#v", join)
|
|
|
|
}
|
|
|
|
if join.Error != "" {
|
|
|
|
t.Fatalf("bad: %#v", join)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentMembers(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestServer) {
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/members", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AgentMembersRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
2016-11-08 21:01:56 +00:00
|
|
|
members := obj.(structs.ServerMembersResponse)
|
|
|
|
if len(members.Members) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", members.Members)
|
2015-09-07 02:08:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentForceLeave(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestServer) {
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/agent/force-leave?node=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.AgentForceLeaveRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-09-25 03:39:03 +00:00
|
|
|
|
|
|
|
func TestHTTP_AgentSetServers(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestServer) {
|
2016-06-01 07:34:46 +00:00
|
|
|
// Establish a baseline number of servers
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/servers", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
2015-09-25 03:39:03 +00:00
|
|
|
// Create the request
|
2016-06-01 07:34:46 +00:00
|
|
|
req, err = http.NewRequest("PUT", "/v1/agent/servers", nil)
|
2015-09-25 03:39:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request
|
2016-06-01 07:34:46 +00:00
|
|
|
respW = httptest.NewRecorder()
|
2015-09-25 03:39:03 +00:00
|
|
|
_, err = s.Server.AgentServersRequest(respW, req)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "missing server address") {
|
|
|
|
t.Fatalf("expected missing servers error, got: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a valid request
|
2016-09-26 18:57:06 +00:00
|
|
|
req, err = http.NewRequest("PUT", "/v1/agent/servers?address=127.0.0.1%3A4647&address=127.0.0.2%3A4647&address=127.0.0.3%3A4647", nil)
|
2015-09-25 03:39:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
_, err = s.Server.AgentServersRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the servers again
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/servers", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request and check the result
|
2016-06-01 07:34:46 +00:00
|
|
|
expected := map[string]bool{
|
|
|
|
"127.0.0.1:4647": true,
|
|
|
|
"127.0.0.2:4647": true,
|
2016-09-26 18:57:06 +00:00
|
|
|
"127.0.0.3:4647": true,
|
2016-06-01 07:34:46 +00:00
|
|
|
}
|
2016-09-26 18:57:06 +00:00
|
|
|
out, err := s.Server.AgentServersRequest(respW, req)
|
2015-09-25 03:39:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
servers := out.([]string)
|
2016-09-26 18:57:06 +00:00
|
|
|
if n := len(servers); n != len(expected) {
|
|
|
|
t.Fatalf("expected %d servers, got: %d: %v", len(expected), n, servers)
|
2016-06-01 07:34:46 +00:00
|
|
|
}
|
|
|
|
received := make(map[string]bool, len(servers))
|
|
|
|
for _, server := range servers {
|
|
|
|
received[server] = true
|
|
|
|
}
|
|
|
|
foundCount := 0
|
|
|
|
for k, _ := range received {
|
|
|
|
_, found := expected[k]
|
|
|
|
if found {
|
|
|
|
foundCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundCount != len(expected) {
|
|
|
|
t.Fatalf("bad servers result")
|
2015-09-25 03:39:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-10-17 17:48:04 +00:00
|
|
|
|
|
|
|
func TestHTTP_AgentListKeys(t *testing.T) {
|
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
|
|
|
}, func(s *TestServer) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/list", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
kresp := out.(structs.KeyringResponse)
|
|
|
|
if len(kresp.Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %v", kresp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentInstallKey(t *testing.T) {
|
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
key2 := "wH1Bn9hlJ0emgWB1JttVRA=="
|
|
|
|
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
|
|
|
}, func(s *TestServer) {
|
|
|
|
b, err := json.Marshal(&structs.KeyringRequest{Key: key2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/install", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
_, err = s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/keyring/list", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
kresp := out.(structs.KeyringResponse)
|
|
|
|
if len(kresp.Keys) != 2 {
|
|
|
|
t.Fatalf("bad: %v", kresp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentRemoveKey(t *testing.T) {
|
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
key2 := "wH1Bn9hlJ0emgWB1JttVRA=="
|
|
|
|
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
|
|
|
}, func(s *TestServer) {
|
|
|
|
b, err := json.Marshal(&structs.KeyringRequest{Key: key2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/install", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/keyring/remove", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
if _, err = s.Server.KeyringOperationRequest(respW, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/keyring/list", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
kresp := out.(structs.KeyringResponse)
|
|
|
|
if len(kresp.Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %v", kresp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|