agent: more tests

This commit is contained in:
Ryan Uber 2015-04-28 13:06:02 -07:00
parent 18356328c4
commit 35f5a65fb7
3 changed files with 64 additions and 6 deletions

View File

@ -252,7 +252,7 @@ func TestHTTPAgentRegisterCheck(t *testing.T) {
defer srv.agent.Shutdown()
// Register node
req, err := http.NewRequest("GET", "/v1/agent/check/register", nil)
req, err := http.NewRequest("GET", "/v1/agent/check/register?token=abc123", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -280,6 +280,11 @@ func TestHTTPAgentRegisterCheck(t *testing.T) {
if _, ok := srv.agent.checkTTLs["test"]; !ok {
t.Fatalf("missing test check ttl")
}
// Ensure the token was configured
if token := srv.agent.state.CheckToken("test"); token == "" {
t.Fatalf("missing token")
}
}
func TestHTTPAgentDeregisterCheck(t *testing.T) {
@ -419,7 +424,7 @@ func TestHTTPAgentRegisterService(t *testing.T) {
defer srv.agent.Shutdown()
// Register node
req, err := http.NewRequest("GET", "/v1/agent/service/register", nil)
req, err := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -463,6 +468,11 @@ func TestHTTPAgentRegisterService(t *testing.T) {
if len(srv.agent.checkTTLs) != 3 {
t.Fatalf("missing test check ttls: %v", srv.agent.checkTTLs)
}
// Ensure the token was configured
if token := srv.agent.state.ServiceToken("test"); token == "" {
t.Fatalf("missing token")
}
}
func TestHTTPAgentDeregisterService(t *testing.T) {

View File

@ -126,9 +126,18 @@ func (l *localState) isPaused() bool {
// SetServiceToken configures the provided token for the service ID.
// The token will be used to perform service registration operations.
func (l *localState) SetServiceToken(id, token string) {
if token != "" {
l.Lock()
defer l.Unlock()
l.serviceTokens[id] = token
}
}
// RemoveServiceToken is used to remove a configured service token.
func (l *localState) RemoveServiceToken(id string) {
l.Lock()
defer l.Unlock()
l.serviceTokens[id] = token
delete(l.serviceTokens, id)
}
// ServiceToken returns the configured ACL token for the given
@ -192,9 +201,18 @@ func (l *localState) Services() map[string]*structs.NodeService {
// SetCheckToken is used to configure an ACL token for a specific
// health check. The token is used during check registration operations.
func (l *localState) SetCheckToken(id, token string) {
if token != "" {
l.Lock()
defer l.Unlock()
l.checkTokens[id] = token
}
}
// RemoveCheckToken is used to remove a configured check token.
func (l *localState) RemoveCheckToken(id string) {
l.Lock()
defer l.Unlock()
l.checkTokens[id] = token
delete(l.checkTokens, id)
}
// CheckToken is used to return the configured health check token, or

View File

@ -616,21 +616,51 @@ func TestAgentAntiEntropy_deleteCheck_fails(t *testing.T) {
}
func TestAgent_serviceTokens(t *testing.T) {
config := nextConfig()
config.ACLToken = "default"
l := new(localState)
l.Init(nil, nil)
l.Init(config, nil)
// Returns default when no token is set
if token := l.ServiceToken("redis"); token != "default" {
t.Fatalf("bad: %s", token)
}
// Returns configured token
l.SetServiceToken("redis", "abc123")
if token := l.ServiceToken("redis"); token != "abc123" {
t.Fatalf("bad: %s", token)
}
// Removes token
l.RemoveServiceToken("redis")
if token := l.ServiceToken("redis"); token != "default" {
t.Fatalf("bad: %s", token)
}
}
func TestAgent_checkTokens(t *testing.T) {
config := nextConfig()
config.ACLToken = "default"
l := new(localState)
l.Init(nil, nil)
l.Init(config, nil)
// Returns default when no token is set
if token := l.CheckToken("mem"); token != "default" {
t.Fatalf("bad: %s", token)
}
// Returns configured token
l.SetCheckToken("mem", "abc123")
if token := l.CheckToken("mem"); token != "abc123" {
t.Fatalf("bad: %s", token)
}
// Removes token
l.RemoveCheckToken("mem")
if token := l.CheckToken("mem"); token != "default" {
t.Fatalf("bad: %s", token)
}
}
var testRegisterRules = `