2019-02-15 01:10:36 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-05-22 16:21:47 +00:00
|
|
|
"net/http"
|
2019-02-15 01:10:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-05-22 16:21:47 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-02-15 01:10:36 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
2019-02-15 01:10:36 +00:00
|
|
|
)
|
|
|
|
|
2019-02-28 18:05:55 +00:00
|
|
|
func TestAPIProxy(t *testing.T) {
|
2019-02-15 01:10:36 +00:00
|
|
|
cleanup, client, _, _ := setupClusterAndAgent(namespace.RootContext(nil), t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
2019-02-19 21:53:29 +00:00
|
|
|
proxier, err := NewAPIProxy(&APIProxyConfig{
|
|
|
|
Client: client,
|
2019-02-15 01:10:36 +00:00
|
|
|
Logger: logging.NewVaultLogger(hclog.Trace),
|
|
|
|
})
|
2019-02-19 21:53:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-15 01:10:36 +00:00
|
|
|
|
|
|
|
r := client.NewRequest("GET", "/v1/sys/health")
|
2019-02-15 18:40:03 +00:00
|
|
|
req, err := r.ToHTTP()
|
2019-02-15 01:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := proxier.Send(namespace.RootContext(nil), &SendRequest{
|
2019-02-15 18:40:03 +00:00
|
|
|
Request: req,
|
2019-02-15 01:10:36 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result api.HealthResponse
|
|
|
|
err = jsonutil.DecodeJSONFromReader(resp.Response.Body, &result)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !result.Initialized || result.Sealed || result.Standby {
|
2019-05-22 16:21:47 +00:00
|
|
|
t.Fatalf("bad sys/health response: %#v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIProxy_queryParams(t *testing.T) {
|
|
|
|
// Set up an agent that points to a standby node for this particular test
|
|
|
|
// since it needs to proxy a /sys/health?standbyok=true request to a standby
|
|
|
|
cleanup, client, _, _ := setupClusterAndAgentOnStandby(namespace.RootContext(nil), t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
proxier, err := NewAPIProxy(&APIProxyConfig{
|
|
|
|
Client: client,
|
|
|
|
Logger: logging.NewVaultLogger(hclog.Trace),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := client.NewRequest("GET", "/v1/sys/health")
|
|
|
|
req, err := r.ToHTTP()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a query parameter for testing
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("standbyok", "true")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
resp, err := proxier.Send(namespace.RootContext(nil), &SendRequest{
|
|
|
|
Request: req,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result api.HealthResponse
|
|
|
|
err = jsonutil.DecodeJSONFromReader(resp.Response.Body, &result)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !result.Initialized || result.Sealed || !result.Standby {
|
|
|
|
t.Fatalf("bad sys/health response: %#v", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Response.StatusCode != http.StatusOK {
|
|
|
|
t.Fatalf("exptected standby to return 200, got: %v", resp.Response.StatusCode)
|
2019-02-15 01:10:36 +00:00
|
|
|
}
|
|
|
|
}
|