From b2698045c7adf43132b529bbd9907c0281cdcd0e Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Tue, 4 Oct 2016 09:12:13 -0700 Subject: [PATCH] Added tests for keyring operations --- command/agent/agent_endpoint_test.go | 65 ++++++++++++++++++++++++++++ command/agent/server/serf.keyring | 1 - 2 files changed, 65 insertions(+), 1 deletion(-) delete mode 100644 command/agent/server/serf.keyring diff --git a/command/agent/agent_endpoint_test.go b/command/agent/agent_endpoint_test.go index c79f9dae509..4132f63798e 100644 --- a/command/agent/agent_endpoint_test.go +++ b/command/agent/agent_endpoint_test.go @@ -1,11 +1,15 @@ package agent import ( + "bytes" + "encoding/json" "fmt" "net/http" "net/http/httptest" "strings" "testing" + + "github.com/hashicorp/nomad/nomad/structs" ) func TestHTTP_AgentSelf(t *testing.T) { @@ -177,3 +181,64 @@ func TestHTTP_AgentSetServers(t *testing.T) { } }) } + +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) + } + }) +} diff --git a/command/agent/server/serf.keyring b/command/agent/server/serf.keyring deleted file mode 100644 index b27c67887b5..00000000000 --- a/command/agent/server/serf.keyring +++ /dev/null @@ -1 +0,0 @@ -["tbLJg26ZJyJ9pK3qhc9jig=="] \ No newline at end of file