Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Oct 4, 2016
1 parent 7a5362d commit 1bc1b9b
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ func (a *Agent) setupServer() error {
return fmt.Errorf("server config setup failed: %s", err)
}

// Sets up the keyring for gossip encryption
if err := a.setupKeyrings(conf); err != nil {
return fmt.Errorf("failed to configure keyring: %v", err)
}
Expand Down
10 changes: 3 additions & 7 deletions command/agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,17 @@ func (s *HTTPServer) KeyringOperationRequest(resp http.ResponseWriter, req *http
case "list":
sresp, err = kmgr.ListKeys()
case "install":
if err := s.getKey(req, &args); err != nil {
if err := decodeBody(req, &args); err != nil {
return nil, CodedError(500, err.Error())
}
sresp, err = kmgr.InstallKey(args.Key)
case "use":
if err := s.getKey(req, &args); err != nil {
if err := decodeBody(req, &args); err != nil {
return nil, CodedError(500, err.Error())
}
sresp, err = kmgr.UseKey(args.Key)
case "remove":
if err := s.getKey(req, &args); err != nil {
if err := decodeBody(req, &args); err != nil {
return nil, CodedError(500, err.Error())
}
sresp, err = kmgr.RemoveKey(args.Key)
Expand All @@ -217,10 +217,6 @@ func (s *HTTPServer) KeyringOperationRequest(resp http.ResponseWriter, req *http
return kresp, nil
}

func (s *HTTPServer) getKey(req *http.Request, args *structs.KeyringRequest) error {
return decodeBody(req, args)
}

type agentSelf struct {
Config *Config `json:"config"`
Member Member `json:"member,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions command/agent/config-test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ server {
retry_max = 3
retry_interval = "15s"
rejoin_after_leave = true
encrypt = "abc"
}
telemetry {
statsite_address = "127.0.0.1:1234"
Expand Down
1 change: 1 addition & 0 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestConfig_Parse(t *testing.T) {
RetryInterval: "15s",
RejoinAfterLeave: true,
RetryMaxAttempts: 3,
EncryptKey: "abc",
},
Telemetry: &Telemetry{
StatsiteAddr: "127.0.0.1:1234",
Expand Down
2 changes: 1 addition & 1 deletion command/agent/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
serfKeyring = "serf.keyring"
serfKeyring = "server/serf.keyring"
)

// initKeyring will create a keyring file at a given path.
Expand Down
85 changes: 85 additions & 0 deletions command/agent/keyring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package agent

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestAgent_LoadKeyrings(t *testing.T) {
key := "tbLJg26ZJyJ9pK3qhc9jig=="

// Should be no configured keyring file by default
dir1, agent1 := makeAgent(t, nil)
defer os.RemoveAll(dir1)
defer agent1.Shutdown()

c := agent1.server.GetConfig()
if c.SerfConfig.KeyringFile != "" {
t.Fatalf("bad: %#v", c.SerfConfig.KeyringFile)
}
if c.SerfConfig.MemberlistConfig.Keyring != nil {
t.Fatalf("keyring should not be loaded")
}

// Server should auto-load LAN and WAN keyring files
dir2, agent2 := makeAgent(t, func(c *Config) {
file := filepath.Join(c.DataDir, serfKeyring)
if err := initKeyring(file, key); err != nil {
t.Fatalf("err: %s", err)
}
})
defer os.RemoveAll(dir2)
defer agent2.Shutdown()

c = agent2.server.GetConfig()
if c.SerfConfig.KeyringFile == "" {
t.Fatalf("should have keyring file")
}
if c.SerfConfig.MemberlistConfig.Keyring == nil {
t.Fatalf("keyring should be loaded")
}
}

func TestAgent_InitKeyring(t *testing.T) {
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
key2 := "4leC33rgtXKIVUr9Nr0snQ=="
expected := fmt.Sprintf(`["%s"]`, key1)

dir, err := ioutil.TempDir("", "consul")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir)

file := filepath.Join(dir, "keyring")

// First initialize the keyring
if err := initKeyring(file, key1); err != nil {
t.Fatalf("err: %s", err)
}

content, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("err: %s", err)
}
if string(content) != expected {
t.Fatalf("bad: %s", content)
}

// Try initializing again with a different key
if err := initKeyring(file, key2); err != nil {
t.Fatalf("err: %s", err)
}

// Content should still be the same
content, err = ioutil.ReadFile(file)
if err != nil {
t.Fatalf("err: %s", err)
}
if string(content) != expected {
t.Fatalf("bad: %s", content)
}
}
1 change: 1 addition & 0 deletions command/agent/server/serf.keyring
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["tbLJg26ZJyJ9pK3qhc9jig=="]
27 changes: 27 additions & 0 deletions command/keygen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package command

import (
"encoding/base64"
"testing"

"github.com/mitchellh/cli"
)

func TestKeygenCommand(t *testing.T) {
ui := new(cli.MockUi)
c := &KeygenCommand{Meta: Meta{Ui: ui}}
code := c.Run(nil)
if code != 0 {
t.Fatalf("bad: %d", code)
}

output := ui.OutputWriter.String()
result, err := base64.StdEncoding.DecodeString(output)
if err != nil {
t.Fatalf("err: %s", err)
}

if len(result) != 16 {
t.Fatalf("bad: %#v", result)
}
}
7 changes: 7 additions & 0 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,17 @@ func (s *Server) Stats() map[string]map[string]string {
return stats
}

// Region retuns the region of the server
func (s *Server) Region() string {
return s.config.Region
}

// Datacenter returns the data center of the server
func (s *Server) Datacenter() string {
return s.config.Datacenter
}

// GetConfig returns the config of the server for testing purposes only
func (s *Server) GetConfig() *Config {
return s.config
}

0 comments on commit 1bc1b9b

Please sign in to comment.