2015-03-11 22:42:08 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-04-20 18:30:35 +00:00
|
|
|
"bytes"
|
2020-11-06 19:27:35 +00:00
|
|
|
"context"
|
2021-02-25 18:16:17 +00:00
|
|
|
"crypto/x509"
|
2021-10-06 17:57:06 +00:00
|
|
|
"encoding/base64"
|
2021-02-25 18:16:17 +00:00
|
|
|
"fmt"
|
2015-04-20 18:30:35 +00:00
|
|
|
"io"
|
2015-03-11 22:42:08 +00:00
|
|
|
"net/http"
|
2021-02-25 18:16:17 +00:00
|
|
|
"net/url"
|
2015-04-23 15:43:20 +00:00
|
|
|
"os"
|
2021-07-19 21:15:31 +00:00
|
|
|
"reflect"
|
2021-10-14 18:51:31 +00:00
|
|
|
"sort"
|
2018-01-25 00:57:49 +00:00
|
|
|
"strings"
|
2021-10-14 18:51:31 +00:00
|
|
|
"sync"
|
2015-03-11 22:42:08 +00:00
|
|
|
"testing"
|
2020-11-06 19:27:35 +00:00
|
|
|
"time"
|
2019-07-17 10:29:25 +00:00
|
|
|
|
2021-10-06 17:57:06 +00:00
|
|
|
"github.com/go-test/deep"
|
2021-05-27 17:25:25 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-10-14 18:51:31 +00:00
|
|
|
|
2019-07-17 10:29:25 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2015-03-11 22:42:08 +00:00
|
|
|
)
|
|
|
|
|
2015-04-23 15:43:20 +00:00
|
|
|
func init() {
|
|
|
|
// Ensure our special envvars are not present
|
2015-04-23 15:46:22 +00:00
|
|
|
os.Setenv("VAULT_ADDR", "")
|
2015-04-23 15:43:20 +00:00
|
|
|
os.Setenv("VAULT_TOKEN", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultConfig_envvar(t *testing.T) {
|
2015-04-23 15:46:22 +00:00
|
|
|
os.Setenv("VAULT_ADDR", "https://vault.mycompany.com")
|
|
|
|
defer os.Setenv("VAULT_ADDR", "")
|
2015-04-23 15:43:20 +00:00
|
|
|
|
|
|
|
config := DefaultConfig()
|
|
|
|
if config.Address != "https://vault.mycompany.com" {
|
|
|
|
t.Fatalf("bad: %s", config.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("VAULT_TOKEN", "testing")
|
|
|
|
defer os.Setenv("VAULT_TOKEN", "")
|
|
|
|
|
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if token := client.Token(); token != "testing" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 12:49:20 +00:00
|
|
|
func TestClientDefaultHttpClient(t *testing.T) {
|
|
|
|
_, err := NewClient(&Config{
|
|
|
|
HttpClient: http.DefaultClient,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:37:13 +00:00
|
|
|
func TestClientNilConfig(t *testing.T) {
|
|
|
|
client, err := NewClient(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if client == nil {
|
|
|
|
t.Fatal("expected a non-nil client")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientSetAddress(t *testing.T) {
|
|
|
|
client, err := NewClient(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := client.SetAddress("http://172.168.2.1:8300"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if client.addr.Host != "172.168.2.1:8300" {
|
|
|
|
t.Fatalf("bad: expected: '172.168.2.1:8300' actual: %q", client.addr.Host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 22:42:08 +00:00
|
|
|
func TestClientToken(t *testing.T) {
|
|
|
|
tokenValue := "foo"
|
2015-08-22 00:36:19 +00:00
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {}
|
2015-03-11 22:42:08 +00:00
|
|
|
|
|
|
|
config, ln := testHTTPServer(t, http.HandlerFunc(handler))
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-08-22 00:36:19 +00:00
|
|
|
client.SetToken(tokenValue)
|
2015-03-11 22:42:08 +00:00
|
|
|
|
|
|
|
// Verify the token is set
|
|
|
|
if v := client.Token(); v != tokenValue {
|
|
|
|
t.Fatalf("bad: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.ClearToken()
|
|
|
|
|
|
|
|
if v := client.Token(); v != "" {
|
|
|
|
t.Fatalf("bad: %s", v)
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 04:20:23 +00:00
|
|
|
|
2020-03-11 13:22:58 +00:00
|
|
|
func TestClientHostHeader(t *testing.T) {
|
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Write([]byte(req.Host))
|
|
|
|
}
|
|
|
|
config, ln := testHTTPServer(t, http.HandlerFunc(handler))
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
config.Address = strings.ReplaceAll(config.Address, "127.0.0.1", "localhost")
|
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the token manually
|
|
|
|
client.SetToken("foo")
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
resp, err := client.RawRequest(client.NewRequest(http.MethodPut, "/"))
|
2020-03-11 13:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the response
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
|
|
|
|
// Verify we got the response from the primary
|
|
|
|
if buf.String() != strings.ReplaceAll(config.Address, "http://", "") {
|
|
|
|
t.Fatalf("Bad address: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 00:57:49 +00:00
|
|
|
func TestClientBadToken(t *testing.T) {
|
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {}
|
|
|
|
|
|
|
|
config, ln := testHTTPServer(t, http.HandlerFunc(handler))
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.SetToken("foo")
|
2022-03-24 17:58:03 +00:00
|
|
|
_, err = client.RawRequest(client.NewRequest(http.MethodPut, "/"))
|
2018-01-25 00:57:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.SetToken("foo\u007f")
|
2022-03-24 17:58:03 +00:00
|
|
|
_, err = client.RawRequest(client.NewRequest(http.MethodPut, "/"))
|
2018-01-25 00:57:49 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "printable") {
|
|
|
|
t.Fatalf("expected error due to bad token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:30:35 +00:00
|
|
|
func TestClientRedirect(t *testing.T) {
|
|
|
|
primary := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Write([]byte("test"))
|
|
|
|
}
|
|
|
|
config, ln := testHTTPServer(t, http.HandlerFunc(primary))
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
standby := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Location", config.Address)
|
|
|
|
w.WriteHeader(307)
|
|
|
|
}
|
|
|
|
config2, ln2 := testHTTPServer(t, http.HandlerFunc(standby))
|
|
|
|
defer ln2.Close()
|
|
|
|
|
|
|
|
client, err := NewClient(config2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-03 14:36:59 +00:00
|
|
|
// Set the token manually
|
2015-04-20 18:30:35 +00:00
|
|
|
client.SetToken("foo")
|
|
|
|
|
|
|
|
// Do a raw "/" request
|
2022-03-24 17:58:03 +00:00
|
|
|
resp, err := client.RawRequest(client.NewRequest(http.MethodPut, "/"))
|
2015-04-20 18:30:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the response
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
|
|
|
|
// Verify we got the response from the primary
|
|
|
|
if buf.String() != "test" {
|
|
|
|
t.Fatalf("Bad: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 19:21:14 +00:00
|
|
|
|
2021-02-25 18:16:17 +00:00
|
|
|
func TestDefaulRetryPolicy(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
resp *http.Response
|
|
|
|
err error
|
|
|
|
expect bool
|
|
|
|
expectErr error
|
|
|
|
}{
|
|
|
|
"retry on error": {
|
|
|
|
err: fmt.Errorf("error"),
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
"don't retry connection failures": {
|
|
|
|
err: &url.Error{
|
|
|
|
Err: x509.UnknownAuthorityError{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"don't retry on 200": {
|
|
|
|
resp: &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"don't retry on 4xx": {
|
|
|
|
resp: &http.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"don't retry on 501": {
|
|
|
|
resp: &http.Response{
|
|
|
|
StatusCode: http.StatusNotImplemented,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"retry on 500": {
|
|
|
|
resp: &http.Response{
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
"retry on 5xx": {
|
|
|
|
resp: &http.Response{
|
|
|
|
StatusCode: http.StatusGatewayTimeout,
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
retry, err := DefaultRetryPolicy(context.Background(), test.resp, test.err)
|
|
|
|
if retry != test.expect {
|
|
|
|
t.Fatalf("expected to retry request: '%t', but actual result was: '%t'", test.expect, retry)
|
|
|
|
}
|
|
|
|
if err != test.expectErr {
|
|
|
|
t.Fatalf("expected error from retry policy: '%s', but actual result was: '%s'", err, test.expectErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
func TestClientEnvSettings(t *testing.T) {
|
|
|
|
cwd, _ := os.Getwd()
|
2022-04-06 15:21:46 +00:00
|
|
|
|
|
|
|
caCertBytes, err := os.ReadFile(cwd + "/test-fixtures/keys/cert.pem")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading %q cert file: %v", cwd+"/test-fixtures/keys/cert.pem", err)
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
oldCACert := os.Getenv(EnvVaultCACert)
|
2022-04-06 15:21:46 +00:00
|
|
|
oldCACertBytes := os.Getenv(EnvVaultCACertBytes)
|
2015-11-03 19:21:14 +00:00
|
|
|
oldCAPath := os.Getenv(EnvVaultCAPath)
|
|
|
|
oldClientCert := os.Getenv(EnvVaultClientCert)
|
|
|
|
oldClientKey := os.Getenv(EnvVaultClientKey)
|
2019-02-28 22:29:28 +00:00
|
|
|
oldSkipVerify := os.Getenv(EnvVaultSkipVerify)
|
2016-07-11 21:37:46 +00:00
|
|
|
oldMaxRetries := os.Getenv(EnvVaultMaxRetries)
|
2022-04-06 15:21:46 +00:00
|
|
|
|
2016-07-06 20:42:34 +00:00
|
|
|
os.Setenv(EnvVaultCACert, cwd+"/test-fixtures/keys/cert.pem")
|
2022-04-06 15:21:46 +00:00
|
|
|
os.Setenv(EnvVaultCACertBytes, string(caCertBytes))
|
2016-07-06 20:42:34 +00:00
|
|
|
os.Setenv(EnvVaultCAPath, cwd+"/test-fixtures/keys")
|
|
|
|
os.Setenv(EnvVaultClientCert, cwd+"/test-fixtures/keys/cert.pem")
|
|
|
|
os.Setenv(EnvVaultClientKey, cwd+"/test-fixtures/keys/key.pem")
|
2019-02-28 22:29:28 +00:00
|
|
|
os.Setenv(EnvVaultSkipVerify, "true")
|
2016-07-11 21:37:46 +00:00
|
|
|
os.Setenv(EnvVaultMaxRetries, "5")
|
2022-04-06 15:21:46 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
os.Setenv(EnvVaultCACert, oldCACert)
|
|
|
|
os.Setenv(EnvVaultCACertBytes, oldCACertBytes)
|
|
|
|
os.Setenv(EnvVaultCAPath, oldCAPath)
|
|
|
|
os.Setenv(EnvVaultClientCert, oldClientCert)
|
|
|
|
os.Setenv(EnvVaultClientKey, oldClientKey)
|
|
|
|
os.Setenv(EnvVaultSkipVerify, oldSkipVerify)
|
|
|
|
os.Setenv(EnvVaultMaxRetries, oldMaxRetries)
|
|
|
|
}()
|
2015-11-03 19:21:14 +00:00
|
|
|
|
|
|
|
config := DefaultConfig()
|
|
|
|
if err := config.ReadEnvironment(); err != nil {
|
|
|
|
t.Fatalf("error reading environment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := config.HttpClient.Transport.(*http.Transport).TLSClientConfig
|
|
|
|
if len(tlsConfig.RootCAs.Subjects()) == 0 {
|
|
|
|
t.Fatalf("bad: expected a cert pool with at least one subject")
|
|
|
|
}
|
2017-11-13 15:35:36 +00:00
|
|
|
if tlsConfig.GetClientCertificate == nil {
|
2017-11-12 17:34:56 +00:00
|
|
|
t.Fatalf("bad: expected client tls config to have a certificate getter")
|
2015-11-03 19:21:14 +00:00
|
|
|
}
|
|
|
|
if tlsConfig.InsecureSkipVerify != true {
|
2016-04-13 19:38:29 +00:00
|
|
|
t.Fatalf("bad: %v", tlsConfig.InsecureSkipVerify)
|
2015-11-03 19:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 13:48:31 +00:00
|
|
|
|
2019-07-17 10:29:25 +00:00
|
|
|
func TestClientDeprecatedEnvSettings(t *testing.T) {
|
|
|
|
oldInsecure := os.Getenv(EnvVaultInsecure)
|
|
|
|
os.Setenv(EnvVaultInsecure, "true")
|
|
|
|
defer os.Setenv(EnvVaultInsecure, oldInsecure)
|
|
|
|
|
|
|
|
config := DefaultConfig()
|
|
|
|
if err := config.ReadEnvironment(); err != nil {
|
|
|
|
t.Fatalf("error reading environment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := config.HttpClient.Transport.(*http.Transport).TLSClientConfig
|
|
|
|
if tlsConfig.InsecureSkipVerify != true {
|
|
|
|
t.Fatalf("bad: %v", tlsConfig.InsecureSkipVerify)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:23:59 +00:00
|
|
|
func TestClientEnvNamespace(t *testing.T) {
|
|
|
|
var seenNamespace string
|
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
seenNamespace = req.Header.Get(consts.NamespaceHeaderName)
|
|
|
|
}
|
|
|
|
config, ln := testHTTPServer(t, http.HandlerFunc(handler))
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
oldVaultNamespace := os.Getenv(EnvVaultNamespace)
|
|
|
|
defer os.Setenv(EnvVaultNamespace, oldVaultNamespace)
|
|
|
|
os.Setenv(EnvVaultNamespace, "test")
|
|
|
|
|
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
_, err = client.RawRequest(client.NewRequest(http.MethodGet, "/"))
|
2019-03-25 18:23:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if seenNamespace != "test" {
|
|
|
|
t.Fatalf("Bad: %s", seenNamespace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:42:06 +00:00
|
|
|
func TestParsingRateAndBurst(t *testing.T) {
|
|
|
|
var (
|
|
|
|
correctFormat = "400:400"
|
|
|
|
observedRate, observedBurst, err = parseRateLimit(correctFormat)
|
2018-05-11 20:28:03 +00:00
|
|
|
expectedRate, expectedBurst = float64(400), 400
|
2018-05-11 14:42:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if expectedRate != observedRate {
|
|
|
|
t.Errorf("Expected rate %v but found %v", expectedRate, observedRate)
|
|
|
|
}
|
|
|
|
if expectedBurst != observedBurst {
|
2019-03-19 16:34:13 +00:00
|
|
|
t.Errorf("Expected burst %v but found %v", expectedBurst, observedBurst)
|
2018-05-11 14:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsingRateOnly(t *testing.T) {
|
|
|
|
var (
|
|
|
|
correctFormat = "400"
|
|
|
|
observedRate, observedBurst, err = parseRateLimit(correctFormat)
|
2018-05-11 20:28:03 +00:00
|
|
|
expectedRate, expectedBurst = float64(400), 400
|
2018-05-11 14:42:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if expectedRate != observedRate {
|
|
|
|
t.Errorf("Expected rate %v but found %v", expectedRate, observedRate)
|
|
|
|
}
|
|
|
|
if expectedBurst != observedBurst {
|
2019-03-19 16:34:13 +00:00
|
|
|
t.Errorf("Expected burst %v but found %v", expectedBurst, observedBurst)
|
2018-05-11 14:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsingErrorCase(t *testing.T) {
|
2021-04-08 16:43:39 +00:00
|
|
|
incorrectFormat := "foobar"
|
|
|
|
_, _, err := parseRateLimit(incorrectFormat)
|
2018-05-11 14:42:06 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error, found no error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:48:31 +00:00
|
|
|
func TestClientTimeoutSetting(t *testing.T) {
|
|
|
|
oldClientTimeout := os.Getenv(EnvVaultClientTimeout)
|
|
|
|
os.Setenv(EnvVaultClientTimeout, "10")
|
|
|
|
defer os.Setenv(EnvVaultClientTimeout, oldClientTimeout)
|
|
|
|
config := DefaultConfig()
|
|
|
|
config.ReadEnvironment()
|
2018-05-25 18:38:06 +00:00
|
|
|
_, err := NewClient(config)
|
2017-07-18 13:48:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 12:46:54 +00:00
|
|
|
|
|
|
|
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
return rt(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNonTransportRoundTripper(t *testing.T) {
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: roundTripperFunc(http.DefaultTransport.RoundTrip),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := NewClient(&Config{
|
|
|
|
HttpClient: client,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 18:08:30 +00:00
|
|
|
|
|
|
|
func TestClone(t *testing.T) {
|
2021-10-14 18:51:31 +00:00
|
|
|
type fields struct{}
|
2021-07-19 21:15:31 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
config *Config
|
|
|
|
headers *http.Header
|
2021-12-22 22:07:26 +00:00
|
|
|
token string
|
2021-07-19 21:15:31 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default",
|
|
|
|
config: DefaultConfig(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cloneHeaders",
|
|
|
|
config: &Config{
|
|
|
|
CloneHeaders: true,
|
|
|
|
},
|
|
|
|
headers: &http.Header{
|
|
|
|
"X-foo": []string{"bar"},
|
|
|
|
"X-baz": []string{"qux"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-14 18:51:31 +00:00
|
|
|
{
|
|
|
|
name: "preventStaleReads",
|
|
|
|
config: &Config{
|
|
|
|
ReadYourWrites: true,
|
|
|
|
},
|
|
|
|
},
|
2021-12-22 22:07:26 +00:00
|
|
|
{
|
|
|
|
name: "cloneToken",
|
|
|
|
config: &Config{
|
|
|
|
CloneToken: true,
|
|
|
|
},
|
|
|
|
token: "cloneToken",
|
|
|
|
},
|
2020-11-06 19:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 21:15:31 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-12-22 22:07:26 +00:00
|
|
|
parent, err := NewClient(tt.config)
|
2021-07-19 21:15:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewClient failed: %v", err)
|
|
|
|
}
|
2020-11-06 19:27:35 +00:00
|
|
|
|
2021-07-19 21:15:31 +00:00
|
|
|
// Set all of the things that we provide setter methods for, which modify config values
|
2021-12-22 22:07:26 +00:00
|
|
|
err = parent.SetAddress("http://example.com:8080")
|
2021-07-19 21:15:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SetAddress failed: %v", err)
|
|
|
|
}
|
2020-11-06 19:27:35 +00:00
|
|
|
|
2021-07-19 21:15:31 +00:00
|
|
|
clientTimeout := time.Until(time.Now().AddDate(0, 0, 1))
|
2021-12-22 22:07:26 +00:00
|
|
|
parent.SetClientTimeout(clientTimeout)
|
2021-05-27 17:25:25 +00:00
|
|
|
|
2021-07-19 21:15:31 +00:00
|
|
|
checkRetry := func(ctx context.Context, resp *http.Response, err error) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
parent.SetCheckRetry(checkRetry)
|
2020-11-06 19:27:35 +00:00
|
|
|
|
2021-12-22 22:07:26 +00:00
|
|
|
parent.SetLogger(hclog.NewNullLogger())
|
2017-10-27 18:08:30 +00:00
|
|
|
|
2021-12-22 22:07:26 +00:00
|
|
|
parent.SetLimiter(5.0, 10)
|
|
|
|
parent.SetMaxRetries(5)
|
|
|
|
parent.SetOutputCurlString(true)
|
2022-04-27 23:35:18 +00:00
|
|
|
parent.SetOutputPolicy(true)
|
2021-12-22 22:07:26 +00:00
|
|
|
parent.SetSRVLookup(true)
|
2021-07-19 21:15:31 +00:00
|
|
|
|
|
|
|
if tt.headers != nil {
|
2021-12-22 22:07:26 +00:00
|
|
|
parent.SetHeaders(*tt.headers)
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 22:07:26 +00:00
|
|
|
if tt.token != "" {
|
|
|
|
parent.SetToken(tt.token)
|
|
|
|
}
|
|
|
|
|
|
|
|
clone, err := parent.Clone()
|
2021-07-19 21:15:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Clone failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.Address() != clone.Address() {
|
|
|
|
t.Fatalf("addresses don't match: %v vs %v", parent.Address(), clone.Address())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.ClientTimeout() != clone.ClientTimeout() {
|
|
|
|
t.Fatalf("timeouts don't match: %v vs %v", parent.ClientTimeout(), clone.ClientTimeout())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.CheckRetry() != nil && clone.CheckRetry() == nil {
|
|
|
|
t.Fatal("checkRetry functions don't match. clone is nil.")
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if (parent.Limiter() != nil && clone.Limiter() == nil) || (parent.Limiter() == nil && clone.Limiter() != nil) {
|
|
|
|
t.Fatalf("limiters don't match: %v vs %v", parent.Limiter(), clone.Limiter())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.Limiter().Limit() != clone.Limiter().Limit() {
|
|
|
|
t.Fatalf("limiter limits don't match: %v vs %v", parent.Limiter().Limit(), clone.Limiter().Limit())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.Limiter().Burst() != clone.Limiter().Burst() {
|
|
|
|
t.Fatalf("limiter bursts don't match: %v vs %v", parent.Limiter().Burst(), clone.Limiter().Burst())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.MaxRetries() != clone.MaxRetries() {
|
|
|
|
t.Fatalf("maxRetries don't match: %v vs %v", parent.MaxRetries(), clone.MaxRetries())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2022-04-08 16:58:50 +00:00
|
|
|
if parent.OutputCurlString() == clone.OutputCurlString() {
|
|
|
|
t.Fatalf("outputCurlString was copied over when it shouldn't have been: %v and %v", parent.OutputCurlString(), clone.OutputCurlString())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.SRVLookup() != clone.SRVLookup() {
|
|
|
|
t.Fatalf("SRVLookup doesn't match: %v vs %v", parent.SRVLookup(), clone.SRVLookup())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
|
|
|
if tt.config.CloneHeaders {
|
2021-12-22 22:07:26 +00:00
|
|
|
if !reflect.DeepEqual(parent.Headers(), clone.Headers()) {
|
|
|
|
t.Fatalf("Headers() don't match: %v vs %v", parent.Headers(), clone.Headers())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if parent.config.CloneHeaders != clone.config.CloneHeaders {
|
|
|
|
t.Fatalf("config.CloneHeaders doesn't match: %v vs %v", parent.config.CloneHeaders, clone.config.CloneHeaders)
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
|
|
|
if tt.headers != nil {
|
2021-12-22 22:07:26 +00:00
|
|
|
if !reflect.DeepEqual(*tt.headers, clone.Headers()) {
|
|
|
|
t.Fatalf("expected headers %v, actual %v", *tt.headers, clone.Headers())
|
2021-07-19 21:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if tt.config.ReadYourWrites && parent.replicationStateStore == nil {
|
2021-10-14 18:51:31 +00:00
|
|
|
t.Fatalf("replicationStateStore is nil")
|
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
if tt.config.CloneToken {
|
|
|
|
if tt.token == "" {
|
|
|
|
t.Fatalf("test requires a non-empty token")
|
|
|
|
}
|
|
|
|
if parent.config.CloneToken != clone.config.CloneToken {
|
|
|
|
t.Fatalf("config.CloneToken doesn't match: %v vs %v", parent.config.CloneToken, clone.config.CloneToken)
|
|
|
|
}
|
|
|
|
if parent.token != clone.token {
|
|
|
|
t.Fatalf("tokens do not match: %v vs %v", parent.token, clone.token)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// assumes `VAULT_TOKEN` is unset or has an empty value.
|
|
|
|
expected := ""
|
|
|
|
if clone.token != expected {
|
|
|
|
t.Fatalf("expected clone's token %q, actual %q", expected, clone.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(parent.replicationStateStore, clone.replicationStateStore) {
|
|
|
|
t.Fatalf("expected replicationStateStore %v, actual %v", parent.replicationStateStore,
|
|
|
|
clone.replicationStateStore)
|
2021-10-14 18:51:31 +00:00
|
|
|
}
|
2021-07-19 21:15:31 +00:00
|
|
|
})
|
2020-11-06 19:27:35 +00:00
|
|
|
}
|
2017-10-27 18:08:30 +00:00
|
|
|
}
|
2020-01-09 22:56:34 +00:00
|
|
|
|
|
|
|
func TestSetHeadersRaceSafe(t *testing.T) {
|
|
|
|
client, err1 := NewClient(nil)
|
|
|
|
if err1 != nil {
|
|
|
|
t.Fatalf("NewClient failed: %v", err1)
|
|
|
|
}
|
|
|
|
|
|
|
|
start := make(chan interface{})
|
|
|
|
done := make(chan interface{})
|
|
|
|
|
|
|
|
testPairs := map[string]string{
|
|
|
|
"soda": "rootbeer",
|
|
|
|
"veggie": "carrots",
|
|
|
|
"fruit": "apples",
|
|
|
|
"color": "red",
|
|
|
|
"protein": "egg",
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range testPairs {
|
|
|
|
tmpKey := key
|
|
|
|
tmpValue := value
|
|
|
|
go func() {
|
|
|
|
<-start
|
|
|
|
// This test fails if here, you replace client.AddHeader(tmpKey, tmpValue) with:
|
|
|
|
// headerCopy := client.Header()
|
|
|
|
// headerCopy.AddHeader(tmpKey, tmpValue)
|
|
|
|
// client.SetHeader(headerCopy)
|
|
|
|
client.AddHeader(tmpKey, tmpValue)
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start everyone at once.
|
|
|
|
close(start)
|
|
|
|
|
|
|
|
// Wait until everyone is done.
|
|
|
|
for i := 0; i < len(testPairs); i++ {
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all the test pairs are in the resulting
|
|
|
|
// headers.
|
|
|
|
resultingHeaders := client.Headers()
|
|
|
|
for key, value := range testPairs {
|
|
|
|
if resultingHeaders.Get(key) != value {
|
|
|
|
t.Fatal("expected " + value + " for " + key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 17:57:06 +00:00
|
|
|
|
|
|
|
func TestMergeReplicationStates(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
old []string
|
|
|
|
new string
|
|
|
|
expected []string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
name: "empty-old",
|
|
|
|
old: nil,
|
|
|
|
new: "v1:cid:1:0:",
|
|
|
|
expected: []string{"v1:cid:1:0:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "old-smaller",
|
|
|
|
old: []string{"v1:cid:1:0:"},
|
|
|
|
new: "v1:cid:2:0:",
|
|
|
|
expected: []string{"v1:cid:2:0:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "old-bigger",
|
|
|
|
old: []string{"v1:cid:2:0:"},
|
|
|
|
new: "v1:cid:1:0:",
|
|
|
|
expected: []string{"v1:cid:2:0:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mixed-single",
|
|
|
|
old: []string{"v1:cid:1:0:"},
|
|
|
|
new: "v1:cid:0:1:",
|
|
|
|
expected: []string{"v1:cid:0:1:", "v1:cid:1:0:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mixed-single-alt",
|
|
|
|
old: []string{"v1:cid:0:1:"},
|
|
|
|
new: "v1:cid:1:0:",
|
|
|
|
expected: []string{"v1:cid:0:1:", "v1:cid:1:0:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mixed-double",
|
|
|
|
old: []string{"v1:cid:0:1:", "v1:cid:1:0:"},
|
|
|
|
new: "v1:cid:2:0:",
|
|
|
|
expected: []string{"v1:cid:0:1:", "v1:cid:2:0:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "newer-both",
|
|
|
|
old: []string{"v1:cid:0:1:", "v1:cid:1:0:"},
|
|
|
|
new: "v1:cid:2:1:",
|
|
|
|
expected: []string{"v1:cid:2:1:"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b64enc := func(ss []string) []string {
|
|
|
|
var ret []string
|
|
|
|
for _, s := range ss {
|
|
|
|
ret = append(ret, base64.StdEncoding.EncodeToString([]byte(s)))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
b64dec := func(ss []string) []string {
|
|
|
|
var ret []string
|
|
|
|
for _, s := range ss {
|
|
|
|
d, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ret = append(ret, string(d))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
out := b64dec(MergeReplicationStates(b64enc(tc.old), base64.StdEncoding.EncodeToString([]byte(tc.new))))
|
|
|
|
if diff := deep.Equal(out, tc.expected); len(diff) != 0 {
|
|
|
|
t.Errorf("got=%v, expected=%v, diff=%v", out, tc.expected, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 18:51:31 +00:00
|
|
|
|
|
|
|
func TestReplicationStateStore_recordState(t *testing.T) {
|
|
|
|
b64enc := func(s string) string {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
expected []string
|
|
|
|
resp []*Response
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "single",
|
|
|
|
resp: []*Response{
|
|
|
|
{
|
|
|
|
Response: &http.Response{
|
|
|
|
Header: map[string][]string{
|
|
|
|
HeaderIndex: {
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
resp: []*Response{
|
|
|
|
{
|
|
|
|
Response: &http.Response{
|
|
|
|
Header: map[string][]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple",
|
|
|
|
resp: []*Response{
|
|
|
|
{
|
|
|
|
Response: &http.Response{
|
|
|
|
Header: map[string][]string{
|
|
|
|
HeaderIndex: {
|
|
|
|
b64enc("v1:cid:0:1:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Response: &http.Response{
|
|
|
|
Header: map[string][]string{
|
|
|
|
HeaderIndex: {
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
b64enc("v1:cid:0:1:"),
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicates",
|
|
|
|
resp: []*Response{
|
|
|
|
{
|
|
|
|
Response: &http.Response{
|
|
|
|
Header: map[string][]string{
|
|
|
|
HeaderIndex: {
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Response: &http.Response{
|
|
|
|
Header: map[string][]string{
|
|
|
|
HeaderIndex: {
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
b64enc("v1:cid:1:0:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
w := &replicationStateStore{}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, r := range tt.resp {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(r *Response) {
|
|
|
|
defer wg.Done()
|
|
|
|
w.recordState(r)
|
|
|
|
}(r)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.expected, w.store) {
|
|
|
|
t.Errorf("recordState(): expected states %v, actual %v", tt.expected, w.store)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplicationStateStore_requireState(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
states []string
|
|
|
|
req []*Request
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
states: []string{},
|
|
|
|
req: []*Request{
|
|
|
|
{
|
|
|
|
Headers: make(http.Header),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic",
|
|
|
|
states: []string{
|
|
|
|
"v1:cid:0:1:",
|
|
|
|
"v1:cid:1:0:",
|
|
|
|
},
|
|
|
|
req: []*Request{
|
|
|
|
{
|
|
|
|
Headers: make(http.Header),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"v1:cid:0:1:",
|
|
|
|
"v1:cid:1:0:",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
store := &replicationStateStore{
|
|
|
|
store: tt.states,
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, r := range tt.req {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(r *Request) {
|
|
|
|
defer wg.Done()
|
|
|
|
store.requireState(r)
|
|
|
|
}(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
var actual []string
|
|
|
|
for _, r := range tt.req {
|
|
|
|
if values := r.Headers.Values(HeaderIndex); len(values) > 0 {
|
|
|
|
actual = append(actual, values...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(actual)
|
|
|
|
if !reflect.DeepEqual(tt.expected, actual) {
|
|
|
|
t.Errorf("requireState(): expected states %v, actual %v", tt.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_ReadYourWrites(t *testing.T) {
|
|
|
|
b64enc := func(s string) string {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set(HeaderIndex, strings.TrimLeft(req.URL.Path, "/"))
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
handler http.Handler
|
|
|
|
wantStates []string
|
|
|
|
values [][]string
|
|
|
|
clone bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "multiple_duplicates",
|
|
|
|
clone: false,
|
|
|
|
handler: handler,
|
|
|
|
wantStates: []string{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
},
|
|
|
|
values: [][]string{
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
b64enc("v1:cid:0:2:"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
b64enc("v1:cid:0:2:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic_clone",
|
|
|
|
clone: true,
|
|
|
|
handler: handler,
|
|
|
|
wantStates: []string{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
},
|
|
|
|
values: [][]string{
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:3:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple_clone",
|
|
|
|
clone: true,
|
|
|
|
handler: handler,
|
|
|
|
wantStates: []string{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
},
|
|
|
|
values: [][]string{
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
b64enc("v1:cid:0:2:"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:3:"),
|
|
|
|
b64enc("v1:cid:0:1:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple_duplicates_clone",
|
|
|
|
clone: true,
|
|
|
|
handler: handler,
|
|
|
|
wantStates: []string{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
},
|
|
|
|
values: [][]string{
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
b64enc("v1:cid:0:2:"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
b64enc("v1:cid:0:4:"),
|
|
|
|
b64enc("v1:cid:0:2:"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
testRequest := func(client *Client, val string) {
|
2022-03-24 17:58:03 +00:00
|
|
|
req := client.NewRequest(http.MethodGet, "/"+val)
|
2021-10-14 18:51:31 +00:00
|
|
|
req.Headers.Set(HeaderIndex, val)
|
|
|
|
resp, err := client.RawRequestWithContext(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate that the server provided a valid header value in its response
|
|
|
|
actual := resp.Header.Get(HeaderIndex)
|
|
|
|
if actual != val {
|
|
|
|
t.Errorf("expected header value %v, actual %v", val, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config, ln := testHTTPServer(t, handler)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
config.ReadYourWrites = true
|
|
|
|
config.Address = fmt.Sprintf("http://%s", ln.Addr())
|
|
|
|
parent, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < len(tt.values); i++ {
|
|
|
|
var c *Client
|
|
|
|
if tt.clone {
|
|
|
|
c, err = parent.Clone()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c = parent
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range tt.values[i] {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(val string) {
|
|
|
|
defer wg.Done()
|
|
|
|
testRequest(c, val)
|
|
|
|
}(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.wantStates, parent.replicationStateStore.states()) {
|
|
|
|
t.Errorf("expected states %v, actual %v", tt.wantStates, parent.replicationStateStore.states())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 21:14:39 +00:00
|
|
|
|
|
|
|
func TestClient_SetReadYourWrites(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
config *Config
|
|
|
|
calls []bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "false",
|
|
|
|
config: &Config{},
|
|
|
|
calls: []bool{false},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "true",
|
|
|
|
config: &Config{},
|
|
|
|
calls: []bool{true},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-false",
|
|
|
|
config: &Config{},
|
|
|
|
calls: []bool{false, false},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-true",
|
|
|
|
config: &Config{},
|
|
|
|
calls: []bool{true, true},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-mix",
|
|
|
|
config: &Config{},
|
|
|
|
calls: []bool{false, true, false, true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assertSetReadYourRights := func(t *testing.T, c *Client, v bool, s *replicationStateStore) {
|
|
|
|
t.Helper()
|
|
|
|
c.SetReadYourWrites(v)
|
|
|
|
if c.config.ReadYourWrites != v {
|
|
|
|
t.Fatalf("expected config.ReadYourWrites %#v, actual %#v", v, c.config.ReadYourWrites)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s, c.replicationStateStore) {
|
|
|
|
t.Fatalf("expected replicationStateStore %#v, actual %#v", s, c.replicationStateStore)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Client{
|
|
|
|
config: tt.config,
|
|
|
|
}
|
|
|
|
for i, v := range tt.calls {
|
|
|
|
var expectStateStore *replicationStateStore
|
|
|
|
if v {
|
|
|
|
if c.replicationStateStore == nil {
|
|
|
|
c.replicationStateStore = &replicationStateStore{
|
|
|
|
store: []string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.replicationStateStore.store = append(c.replicationStateStore.store,
|
|
|
|
fmt.Sprintf("%s-%d", tt.name, i))
|
|
|
|
expectStateStore = c.replicationStateStore
|
|
|
|
}
|
|
|
|
assertSetReadYourRights(t, c, v, expectStateStore)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 22:07:26 +00:00
|
|
|
|
|
|
|
func TestClient_SetCloneToken(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
calls []bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "false",
|
|
|
|
calls: []bool{false},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "true",
|
|
|
|
calls: []bool{true},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi",
|
|
|
|
calls: []bool{true, false, true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Client{
|
|
|
|
config: &Config{},
|
|
|
|
}
|
|
|
|
|
|
|
|
var expected bool
|
|
|
|
for _, v := range tt.calls {
|
|
|
|
actual := c.CloneToken()
|
|
|
|
if expected != actual {
|
|
|
|
t.Fatalf("expected %v, actual %v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = v
|
|
|
|
c.SetCloneToken(expected)
|
|
|
|
actual = c.CloneToken()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("SetCloneToken(): expected %v, actual %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 16:50:21 +00:00
|
|
|
|
|
|
|
func TestClientWithNamespace(t *testing.T) {
|
|
|
|
var ns string
|
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ns = req.Header.Get(consts.NamespaceHeaderName)
|
|
|
|
}
|
|
|
|
config, ln := testHTTPServer(t, http.HandlerFunc(handler))
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
// set up a client with a namespace
|
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
ogNS := "test"
|
|
|
|
client.SetNamespace(ogNS)
|
|
|
|
_, err = client.rawRequestWithContext(
|
|
|
|
context.Background(),
|
|
|
|
client.NewRequest(http.MethodGet, "/"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if ns != ogNS {
|
|
|
|
t.Fatalf("Expected namespace: \"%s\", got \"%s\"", ogNS, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a call with a temporary namespace
|
|
|
|
newNS := "new-namespace"
|
|
|
|
_, err = client.WithNamespace(newNS).rawRequestWithContext(
|
|
|
|
context.Background(),
|
|
|
|
client.NewRequest(http.MethodGet, "/"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if ns != newNS {
|
|
|
|
t.Fatalf("Expected new namespace: \"%s\", got \"%s\"", newNS, ns)
|
|
|
|
}
|
|
|
|
// ensure client has not been modified
|
|
|
|
_, err = client.rawRequestWithContext(
|
|
|
|
context.Background(),
|
|
|
|
client.NewRequest(http.MethodGet, "/"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if ns != ogNS {
|
|
|
|
t.Fatalf("Expected original namespace: \"%s\", got \"%s\"", ogNS, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make call with empty ns
|
|
|
|
_, err = client.WithNamespace("").rawRequestWithContext(
|
|
|
|
context.Background(),
|
|
|
|
client.NewRequest(http.MethodGet, "/"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if ns != "" {
|
|
|
|
t.Fatalf("Expected no namespace, got \"%s\"", ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure client has not been modified
|
|
|
|
if client.Namespace() != ogNS {
|
|
|
|
t.Fatalf("Expected original namespace: \"%s\", got \"%s\"", ogNS, client.Namespace())
|
|
|
|
}
|
|
|
|
}
|