Skip to content

Commit

Permalink
Added tests for keyring operations
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Oct 4, 2016
1 parent 1bc1b9b commit b269804
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
65 changes: 65 additions & 0 deletions command/agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
1 change: 0 additions & 1 deletion command/agent/server/serf.keyring

This file was deleted.

0 comments on commit b269804

Please sign in to comment.