2015-09-06 20:29:51 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-01-13 15:57:36 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2020-09-09 20:22:07 +00:00
|
|
|
"context"
|
2015-12-01 19:53:01 +00:00
|
|
|
"encoding/json"
|
2020-09-09 20:22:07 +00:00
|
|
|
"errors"
|
2017-08-29 18:11:19 +00:00
|
|
|
"fmt"
|
2022-01-13 15:57:36 +00:00
|
|
|
"io"
|
2015-09-06 20:29:51 +00:00
|
|
|
"net/http"
|
2015-12-01 19:53:01 +00:00
|
|
|
"net/http/httptest"
|
2019-01-19 00:01:12 +00:00
|
|
|
"net/url"
|
2016-08-17 19:11:59 +00:00
|
|
|
"strings"
|
2015-09-06 20:29:51 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-17 13:34:57 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2022-12-16 18:25:28 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-09-06 20:29:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type configCallback func(c *Config)
|
|
|
|
|
2017-08-25 00:03:29 +00:00
|
|
|
func makeACLClient(t *testing.T, cb1 configCallback,
|
|
|
|
cb2 testutil.ServerConfigCallback) (*Client, *testutil.TestServer, *ACLToken) {
|
|
|
|
client, server := makeClient(t, cb1, func(c *testutil.TestServerConfig) {
|
|
|
|
c.ACL.Enabled = true
|
|
|
|
if cb2 != nil {
|
|
|
|
cb2(c)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Get the root token
|
|
|
|
root, _, err := client.ACLTokens().Bootstrap(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to bootstrap ACLs: %v", err)
|
|
|
|
}
|
|
|
|
client.SetSecretID(root.SecretID)
|
|
|
|
return client, server, root
|
|
|
|
}
|
|
|
|
|
2015-09-06 20:29:51 +00:00
|
|
|
func makeClient(t *testing.T, cb1 configCallback,
|
|
|
|
cb2 testutil.ServerConfigCallback) (*Client, *testutil.TestServer) {
|
|
|
|
// Make client config
|
|
|
|
conf := DefaultConfig()
|
|
|
|
if cb1 != nil {
|
|
|
|
cb1(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create server
|
|
|
|
server := testutil.NewTestServer(t, cb2)
|
2015-09-14 20:13:52 +00:00
|
|
|
conf.Address = "http://" + server.HTTPAddr
|
2015-09-06 20:29:51 +00:00
|
|
|
|
|
|
|
// Create client
|
|
|
|
client, err := NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, server
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:53:01 +00:00
|
|
|
func TestRequestTime(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2015-12-01 19:53:01 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
d, err := json.Marshal(struct{ Done bool }{true})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-12-16 18:25:28 +00:00
|
|
|
_, _ = w.Write(d)
|
2015-12-01 19:53:01 +00:00
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
conf := DefaultConfig()
|
|
|
|
conf.Address = srv.URL
|
|
|
|
|
|
|
|
client, err := NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out interface{}
|
|
|
|
|
|
|
|
qm, err := client.query("/", &out, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("query err: %v", err)
|
|
|
|
}
|
|
|
|
if qm.RequestTime == 0 {
|
|
|
|
t.Errorf("bad request time: %d", qm.RequestTime)
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := client.put("/", struct{ S string }{"input"}, &out, nil)
|
2015-12-01 19:53:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write err: %v", err)
|
|
|
|
}
|
|
|
|
if wm.RequestTime == 0 {
|
|
|
|
t.Errorf("bad request time: %d", wm.RequestTime)
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:30:11 +00:00
|
|
|
wm, err = client.delete("/", nil, &out, nil)
|
2015-12-01 19:53:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("delete err: %v", err)
|
|
|
|
}
|
|
|
|
if wm.RequestTime == 0 {
|
|
|
|
t.Errorf("bad request time: %d", wm.RequestTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 20:29:51 +00:00
|
|
|
func TestDefaultConfig_env(t *testing.T) {
|
2022-08-30 18:49:03 +00:00
|
|
|
|
2022-06-25 14:20:52 +00:00
|
|
|
testURL := "http://1.2.3.4:5678"
|
2016-08-17 19:11:59 +00:00
|
|
|
auth := []string{"nomaduser", "12345"}
|
2017-09-07 23:56:15 +00:00
|
|
|
region := "test"
|
|
|
|
namespace := "dev"
|
2017-08-25 00:03:29 +00:00
|
|
|
token := "foobar"
|
2015-09-06 20:29:51 +00:00
|
|
|
|
2022-06-25 14:20:52 +00:00
|
|
|
t.Setenv("NOMAD_ADDR", testURL)
|
|
|
|
t.Setenv("NOMAD_REGION", region)
|
|
|
|
t.Setenv("NOMAD_NAMESPACE", namespace)
|
|
|
|
t.Setenv("NOMAD_HTTP_AUTH", strings.Join(auth, ":"))
|
|
|
|
t.Setenv("NOMAD_TOKEN", token)
|
2017-08-25 00:03:29 +00:00
|
|
|
|
2015-09-06 20:29:51 +00:00
|
|
|
config := DefaultConfig()
|
|
|
|
|
2022-06-25 14:20:52 +00:00
|
|
|
if config.Address != testURL {
|
|
|
|
t.Errorf("expected %q to be %q", config.Address, testURL)
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
2016-08-17 19:11:59 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
if config.Region != region {
|
|
|
|
t.Errorf("expected %q to be %q", config.Region, region)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Namespace != namespace {
|
|
|
|
t.Errorf("expected %q to be %q", config.Namespace, namespace)
|
|
|
|
}
|
|
|
|
|
2016-08-17 19:11:59 +00:00
|
|
|
if config.HttpAuth.Username != auth[0] {
|
|
|
|
t.Errorf("expected %q to be %q", config.HttpAuth.Username, auth[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HttpAuth.Password != auth[1] {
|
|
|
|
t.Errorf("expected %q to be %q", config.HttpAuth.Password, auth[1])
|
|
|
|
}
|
2017-08-25 00:03:29 +00:00
|
|
|
|
|
|
|
if config.SecretID != token {
|
|
|
|
t.Errorf("Expected %q to be %q", config.SecretID, token)
|
|
|
|
}
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetQueryOptions(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-06 20:29:51 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2017-02-20 19:12:34 +00:00
|
|
|
r, _ := c.newRequest("GET", "/v1/jobs")
|
2015-09-06 20:29:51 +00:00
|
|
|
q := &QueryOptions{
|
|
|
|
Region: "foo",
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: "bar",
|
2015-09-06 20:29:51 +00:00
|
|
|
AllowStale: true,
|
|
|
|
WaitIndex: 1000,
|
|
|
|
WaitTime: 100 * time.Second,
|
2017-10-12 22:16:33 +00:00
|
|
|
AuthToken: "foobar",
|
2022-03-09 01:54:17 +00:00
|
|
|
Reverse: true,
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
|
2022-02-10 17:50:34 +00:00
|
|
|
try := func(key, exp string) {
|
|
|
|
result := r.params.Get(key)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, exp, result)
|
2017-08-25 00:03:29 +00:00
|
|
|
}
|
2022-02-10 17:50:34 +00:00
|
|
|
|
|
|
|
// Check auth token is set
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, "foobar", r.token)
|
2022-02-10 17:50:34 +00:00
|
|
|
|
|
|
|
// Check query parameters are set
|
|
|
|
try("region", "foo")
|
|
|
|
try("namespace", "bar")
|
|
|
|
try("stale", "") // should not be present
|
|
|
|
try("index", "1000")
|
|
|
|
try("wait", "100000ms")
|
2022-03-09 01:54:17 +00:00
|
|
|
try("reverse", "true")
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:22:07 +00:00
|
|
|
func TestQueryOptionsContext(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2020-09-09 20:22:07 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
q := (&QueryOptions{
|
|
|
|
WaitIndex: 10000,
|
|
|
|
}).WithContext(ctx)
|
|
|
|
|
|
|
|
if q.ctx != ctx {
|
|
|
|
t.Fatalf("expected context to be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
_, _, err := c.Jobs().List(q)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatalf("expected job wait to fail with canceled, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteOptionsContext(t *testing.T) {
|
|
|
|
// No blocking query to test a real cancel of a pending request so
|
|
|
|
// just test that if we pass a pre-canceled context, writes fail quickly
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2020-09-09 20:22:07 +00:00
|
|
|
|
|
|
|
c, err := NewClient(DefaultConfig())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to initialize client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
w := (&WriteOptions{}).WithContext(ctx)
|
|
|
|
|
|
|
|
if w.ctx != ctx {
|
|
|
|
t.Fatalf("expected context to be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
_, _, err = c.Jobs().Deregister("jobid", true, w)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatalf("expected job to fail with canceled, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 20:29:51 +00:00
|
|
|
func TestSetWriteOptions(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-06 20:29:51 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2017-02-20 19:12:34 +00:00
|
|
|
r, _ := c.newRequest("GET", "/v1/jobs")
|
2015-09-06 20:29:51 +00:00
|
|
|
q := &WriteOptions{
|
2021-07-07 21:26:35 +00:00
|
|
|
Region: "foo",
|
|
|
|
Namespace: "bar",
|
|
|
|
AuthToken: "foobar",
|
|
|
|
IdempotencyToken: "idempotent",
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
|
|
|
|
if r.params.Get("region") != "foo" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
if r.params.Get("namespace") != "bar" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
2021-07-07 21:26:35 +00:00
|
|
|
if r.params.Get("idempotency_token") != "idempotent" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
2017-08-25 00:03:29 +00:00
|
|
|
if r.token != "foobar" {
|
|
|
|
t.Fatalf("bad: %v", r.token)
|
|
|
|
}
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestToHTTP(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-06 20:29:51 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2017-02-20 19:12:34 +00:00
|
|
|
r, _ := c.newRequest("DELETE", "/v1/jobs/foo")
|
2015-09-06 20:29:51 +00:00
|
|
|
q := &QueryOptions{
|
2017-09-07 23:56:15 +00:00
|
|
|
Region: "foo",
|
|
|
|
Namespace: "bar",
|
2017-10-12 22:16:33 +00:00
|
|
|
AuthToken: "foobar",
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
req, err := r.toHTTP()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Method != "DELETE" {
|
|
|
|
t.Fatalf("bad: %v", req)
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
if req.URL.RequestURI() != "/v1/jobs/foo?namespace=bar®ion=foo" {
|
2015-09-06 20:29:51 +00:00
|
|
|
t.Fatalf("bad: %v", req)
|
|
|
|
}
|
2017-08-25 00:03:29 +00:00
|
|
|
if req.Header.Get("X-Nomad-Token") != "foobar" {
|
|
|
|
t.Fatalf("bad: %v", req)
|
|
|
|
}
|
2015-09-06 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseQueryMeta(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-06 20:29:51 +00:00
|
|
|
resp := &http.Response{
|
|
|
|
Header: make(map[string][]string),
|
|
|
|
}
|
|
|
|
resp.Header.Set("X-Nomad-Index", "12345")
|
|
|
|
resp.Header.Set("X-Nomad-LastContact", "80")
|
|
|
|
resp.Header.Set("X-Nomad-KnownLeader", "true")
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
if err := parseQueryMeta(resp, qm); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if qm.LastIndex != 12345 {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
|
|
|
if qm.LastContact != 80*time.Millisecond {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
|
|
|
if !qm.KnownLeader {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseWriteMeta(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-06 20:29:51 +00:00
|
|
|
resp := &http.Response{
|
|
|
|
Header: make(map[string][]string),
|
|
|
|
}
|
|
|
|
resp.Header.Set("X-Nomad-Index", "12345")
|
|
|
|
|
|
|
|
wm := &WriteMeta{}
|
|
|
|
if err := parseWriteMeta(resp, wm); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wm.LastIndex != 12345 {
|
|
|
|
t.Fatalf("Bad: %v", wm)
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 21:26:26 +00:00
|
|
|
|
2021-01-05 01:07:35 +00:00
|
|
|
func TestClientHeader(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2021-01-05 01:07:35 +00:00
|
|
|
c, s := makeClient(t, func(c *Config) {
|
2021-01-06 01:39:18 +00:00
|
|
|
c.Headers = http.Header{
|
2021-01-05 15:43:24 +00:00
|
|
|
"Hello": []string{"World"},
|
2021-01-05 01:07:35 +00:00
|
|
|
}
|
|
|
|
}, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
r, _ := c.newRequest("GET", "/v1/jobs")
|
|
|
|
|
|
|
|
if r.header.Get("Hello") != "World" {
|
|
|
|
t.Fatalf("bad: %v", r.header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 21:26:26 +00:00
|
|
|
func TestQueryString(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-08 21:26:26 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2017-02-20 19:12:34 +00:00
|
|
|
r, _ := c.newRequest("PUT", "/v1/abc?foo=bar&baz=zip")
|
2017-09-07 23:56:15 +00:00
|
|
|
q := &WriteOptions{
|
|
|
|
Region: "foo",
|
|
|
|
Namespace: "bar",
|
|
|
|
}
|
2015-09-08 21:26:26 +00:00
|
|
|
r.setWriteOptions(q)
|
|
|
|
|
|
|
|
req, err := r.toHTTP()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
if uri := req.URL.RequestURI(); uri != "/v1/abc?baz=zip&foo=bar&namespace=bar®ion=foo" {
|
2015-09-08 21:26:26 +00:00
|
|
|
t.Fatalf("bad uri: %q", uri)
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 18:11:19 +00:00
|
|
|
|
|
|
|
func TestClient_NodeClient(t *testing.T) {
|
2022-12-16 18:25:28 +00:00
|
|
|
addr := "testdomain:4646"
|
2017-08-29 18:11:19 +00:00
|
|
|
tlsNode := func(string, *QueryOptions) (*Node, *QueryMeta, error) {
|
|
|
|
return &Node{
|
2019-11-12 14:47:18 +00:00
|
|
|
ID: generateUUID(),
|
2017-08-29 18:11:19 +00:00
|
|
|
Status: "ready",
|
2022-12-16 18:25:28 +00:00
|
|
|
HTTPAddr: addr,
|
2017-08-29 18:11:19 +00:00
|
|
|
TLSEnabled: true,
|
|
|
|
}, nil, nil
|
|
|
|
}
|
|
|
|
noTlsNode := func(string, *QueryOptions) (*Node, *QueryMeta, error) {
|
|
|
|
return &Node{
|
2019-11-12 14:47:18 +00:00
|
|
|
ID: generateUUID(),
|
2017-08-29 18:11:19 +00:00
|
|
|
Status: "ready",
|
2022-12-16 18:25:28 +00:00
|
|
|
HTTPAddr: addr,
|
2017-08-29 18:11:19 +00:00
|
|
|
TLSEnabled: false,
|
|
|
|
}, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
optionNoRegion := &QueryOptions{}
|
|
|
|
optionRegion := &QueryOptions{
|
|
|
|
Region: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
clientNoRegion, err := NewClient(DefaultConfig())
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-08-29 18:11:19 +00:00
|
|
|
|
|
|
|
regionConfig := DefaultConfig()
|
|
|
|
regionConfig.Region = "bar"
|
|
|
|
clientRegion, err := NewClient(regionConfig)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-08-29 18:11:19 +00:00
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
expectedTLSAddr := fmt.Sprintf("https://%s", addr)
|
|
|
|
expectedNoTLSAddr := fmt.Sprintf("http://%s", addr)
|
2017-08-29 18:11:19 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Node nodeLookup
|
|
|
|
QueryOptions *QueryOptions
|
|
|
|
Client *Client
|
|
|
|
ExpectedAddr string
|
|
|
|
ExpectedRegion string
|
|
|
|
ExpectedTLSServerName string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Node: tlsNode,
|
|
|
|
QueryOptions: optionNoRegion,
|
|
|
|
Client: clientNoRegion,
|
|
|
|
ExpectedAddr: expectedTLSAddr,
|
|
|
|
ExpectedRegion: "global",
|
|
|
|
ExpectedTLSServerName: "client.global.nomad",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: tlsNode,
|
|
|
|
QueryOptions: optionRegion,
|
|
|
|
Client: clientNoRegion,
|
|
|
|
ExpectedAddr: expectedTLSAddr,
|
|
|
|
ExpectedRegion: "foo",
|
|
|
|
ExpectedTLSServerName: "client.foo.nomad",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: tlsNode,
|
|
|
|
QueryOptions: optionRegion,
|
|
|
|
Client: clientRegion,
|
|
|
|
ExpectedAddr: expectedTLSAddr,
|
|
|
|
ExpectedRegion: "foo",
|
|
|
|
ExpectedTLSServerName: "client.foo.nomad",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: tlsNode,
|
|
|
|
QueryOptions: optionNoRegion,
|
|
|
|
Client: clientRegion,
|
|
|
|
ExpectedAddr: expectedTLSAddr,
|
|
|
|
ExpectedRegion: "bar",
|
|
|
|
ExpectedTLSServerName: "client.bar.nomad",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: noTlsNode,
|
|
|
|
QueryOptions: optionNoRegion,
|
|
|
|
Client: clientNoRegion,
|
|
|
|
ExpectedAddr: expectedNoTLSAddr,
|
|
|
|
ExpectedRegion: "global",
|
|
|
|
ExpectedTLSServerName: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: noTlsNode,
|
|
|
|
QueryOptions: optionRegion,
|
|
|
|
Client: clientNoRegion,
|
|
|
|
ExpectedAddr: expectedNoTLSAddr,
|
|
|
|
ExpectedRegion: "foo",
|
|
|
|
ExpectedTLSServerName: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: noTlsNode,
|
|
|
|
QueryOptions: optionRegion,
|
|
|
|
Client: clientRegion,
|
|
|
|
ExpectedAddr: expectedNoTLSAddr,
|
|
|
|
ExpectedRegion: "foo",
|
|
|
|
ExpectedTLSServerName: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: noTlsNode,
|
|
|
|
QueryOptions: optionNoRegion,
|
|
|
|
Client: clientRegion,
|
|
|
|
ExpectedAddr: expectedNoTLSAddr,
|
|
|
|
ExpectedRegion: "bar",
|
|
|
|
ExpectedTLSServerName: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
name := fmt.Sprintf("%s__%s__%s", c.ExpectedAddr, c.ExpectedRegion, c.ExpectedTLSServerName)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-12-16 18:25:28 +00:00
|
|
|
nodeClient, getErr := c.Client.getNodeClientImpl("testID", -1, c.QueryOptions, c.Node)
|
|
|
|
must.NoError(t, getErr)
|
|
|
|
must.Eq(t, c.ExpectedRegion, nodeClient.config.Region)
|
|
|
|
must.Eq(t, c.ExpectedAddr, nodeClient.config.Address)
|
|
|
|
must.NotNil(t, nodeClient.config.TLSConfig)
|
|
|
|
must.Eq(t, c.ExpectedTLSServerName, nodeClient.config.TLSConfig.TLSServerName)
|
2017-08-29 18:11:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 00:01:12 +00:00
|
|
|
|
|
|
|
func TestCloneHttpClient(t *testing.T) {
|
|
|
|
client := defaultHttpClient()
|
|
|
|
originalTransport := client.Transport.(*http.Transport)
|
|
|
|
originalTransport.Proxy = func(*http.Request) (*url.URL, error) {
|
2022-08-05 15:05:47 +00:00
|
|
|
return nil, errors.New("stub function")
|
2019-01-19 00:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("closing with negative timeout", func(t *testing.T) {
|
|
|
|
clone, err := cloneWithTimeout(client, -1)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.True(t, originalTransport == client.Transport, must.Sprint("original transport changed"))
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.True(t, client == clone)
|
2019-01-19 00:01:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("closing with positive timeout", func(t *testing.T) {
|
|
|
|
clone, err := cloneWithTimeout(client, 1*time.Second)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.True(t, originalTransport == client.Transport, must.Sprint("original transport changed"))
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.True(t, client != clone)
|
|
|
|
must.True(t, client.Transport != clone.Transport)
|
2019-01-19 00:01:12 +00:00
|
|
|
|
|
|
|
// test that proxy function is the same in clone
|
|
|
|
clonedProxy := clone.Transport.(*http.Transport).Proxy
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NotNil(t, clonedProxy)
|
2019-01-19 00:01:12 +00:00
|
|
|
_, err = clonedProxy(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Error(t, err)
|
|
|
|
must.EqError(t, err, "stub function")
|
2019-01-19 00:01:12 +00:00
|
|
|
|
|
|
|
// if we reset transport, the strutcs are equal
|
|
|
|
clone.Transport = originalTransport
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, client, clone)
|
2019-01-19 00:01:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2021-04-05 21:21:41 +00:00
|
|
|
|
|
|
|
func TestClient_HeaderRaceCondition(t *testing.T) {
|
|
|
|
conf := DefaultConfig()
|
|
|
|
conf.Headers = map[string][]string{
|
|
|
|
"test-header": {"a"},
|
|
|
|
}
|
|
|
|
client, err := NewClient(conf)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2021-04-05 21:21:41 +00:00
|
|
|
|
|
|
|
c := make(chan int)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
req, _ := client.newRequest("GET", "/any/path/will/do")
|
|
|
|
r, _ := req.toHTTP()
|
|
|
|
c <- len(r.Header)
|
|
|
|
}()
|
|
|
|
req, _ := client.newRequest("GET", "/any/path/will/do")
|
|
|
|
r, _ := req.toHTTP()
|
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
must.MapLen(t, 2, r.Header, must.Sprint("local request should have two headers"))
|
|
|
|
must.Eq(t, 2, <-c, must.Sprint("goroutine request should have two headers"))
|
|
|
|
must.MapLen(t, 1, conf.Headers, must.Sprint("config headers should not mutate"))
|
2021-04-05 21:21:41 +00:00
|
|
|
}
|
2022-01-13 15:57:36 +00:00
|
|
|
|
|
|
|
func TestClient_autoUnzip(t *testing.T) {
|
|
|
|
var client *Client = nil
|
|
|
|
|
|
|
|
try := func(resp *http.Response, exp error) {
|
|
|
|
err := client.autoUnzip(resp)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, exp, err)
|
2022-01-13 15:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// response object is nil
|
|
|
|
try(nil, nil)
|
|
|
|
|
|
|
|
// response.Body is nil
|
|
|
|
try(new(http.Response), nil)
|
|
|
|
|
|
|
|
// content-encoding is not gzip
|
|
|
|
try(&http.Response{
|
|
|
|
Header: http.Header{"Content-Encoding": []string{"text"}},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
// content-encoding is gzip but body is empty
|
|
|
|
try(&http.Response{
|
|
|
|
Header: http.Header{"Content-Encoding": []string{"gzip"}},
|
|
|
|
Body: io.NopCloser(bytes.NewBuffer([]byte{})),
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
// content-encoding is gzip but body is invalid gzip
|
|
|
|
try(&http.Response{
|
|
|
|
Header: http.Header{"Content-Encoding": []string{"gzip"}},
|
|
|
|
Body: io.NopCloser(bytes.NewBuffer([]byte("not a zip"))),
|
|
|
|
}, errors.New("unexpected EOF"))
|
|
|
|
|
|
|
|
// sample gzip payload
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := gzip.NewWriter(&b)
|
|
|
|
_, err := w.Write([]byte("hello world"))
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2022-01-13 15:57:36 +00:00
|
|
|
err = w.Close()
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2022-01-13 15:57:36 +00:00
|
|
|
|
|
|
|
// content-encoding is gzip and body is gzip data
|
|
|
|
try(&http.Response{
|
|
|
|
Header: http.Header{"Content-Encoding": []string{"gzip"}},
|
|
|
|
Body: io.NopCloser(&b),
|
|
|
|
}, nil)
|
|
|
|
}
|