mirror of https://github.com/status-im/consul.git
Merge pull request #8514 from hashicorp/dnephin/testing-improvements-1
testing: small improvements to TestSessionCreate and testutil.retry
This commit is contained in:
commit
51b08c645b
|
@ -1296,10 +1296,8 @@ func TestAllowedNets(t *testing.T) {
|
||||||
|
|
||||||
// assertIndex tests that X-Consul-Index is set and non-zero
|
// assertIndex tests that X-Consul-Index is set and non-zero
|
||||||
func assertIndex(t *testing.T, resp *httptest.ResponseRecorder) {
|
func assertIndex(t *testing.T, resp *httptest.ResponseRecorder) {
|
||||||
header := resp.Header().Get("X-Consul-Index")
|
t.Helper()
|
||||||
if header == "" || header == "0" {
|
require.NoError(t, checkIndex(resp))
|
||||||
t.Fatalf("Bad: %v", header)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkIndex is like assertIndex but returns an error
|
// checkIndex is like assertIndex but returns an error
|
||||||
|
|
|
@ -97,7 +97,7 @@ func TestSessionCreate(t *testing.T) {
|
||||||
"Checks": []types.CheckID{"consul"},
|
"Checks": []types.CheckID{"consul"},
|
||||||
"LockDelay": "20s",
|
"LockDelay": "20s",
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -158,7 +158,7 @@ func TestSessionCreate_NodeChecks(t *testing.T) {
|
||||||
"NodeChecks": []types.CheckID{structs.SerfCheckID},
|
"NodeChecks": []types.CheckID{structs.SerfCheckID},
|
||||||
"LockDelay": "20s",
|
"LockDelay": "20s",
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -216,7 +216,7 @@ func TestSessionCreate_Delete(t *testing.T) {
|
||||||
"LockDelay": "20s",
|
"LockDelay": "20s",
|
||||||
"Behavior": structs.SessionKeysDelete,
|
"Behavior": structs.SessionKeysDelete,
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -244,23 +244,21 @@ func TestSessionCreate_DefaultCheck(t *testing.T) {
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
||||||
|
|
||||||
// Associate session with node and 2 health checks
|
|
||||||
body := bytes.NewBuffer(nil)
|
|
||||||
enc := json.NewEncoder(body)
|
|
||||||
raw := map[string]interface{}{
|
raw := map[string]interface{}{
|
||||||
"Name": "my-cool-session",
|
"Name": "my-cool-session",
|
||||||
"Node": a.Config.NodeName,
|
"Node": a.Config.NodeName,
|
||||||
"LockDelay": "20s",
|
"LockDelay": "20s",
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
retry.Run(t, func(r *retry.R) {
|
retry.Run(t, func(r *retry.R) {
|
||||||
|
body := bytes.NewBuffer(nil)
|
||||||
|
enc := json.NewEncoder(body)
|
||||||
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
obj, err := a.srv.SessionCreate(resp, req)
|
obj, err := a.srv.SessionCreate(resp, req)
|
||||||
if err != nil {
|
require.NoError(r, err)
|
||||||
r.Fatalf("err: %v", err)
|
require.Equal(r, resp.Code, http.StatusOK)
|
||||||
}
|
|
||||||
|
|
||||||
want := structs.Session{
|
want := structs.Session{
|
||||||
ID: obj.(sessionCreateResponse).ID,
|
ID: obj.(sessionCreateResponse).ID,
|
||||||
|
@ -281,26 +279,23 @@ func TestSessionCreate_NoCheck(t *testing.T) {
|
||||||
|
|
||||||
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
||||||
|
|
||||||
t.Run("no check fields should yield default serfHealth", func(t *testing.T) {
|
raw := map[string]interface{}{
|
||||||
body := bytes.NewBuffer(nil)
|
"Name": "my-cool-session",
|
||||||
enc := json.NewEncoder(body)
|
"Node": a.Config.NodeName,
|
||||||
raw := map[string]interface{}{
|
"LockDelay": "20s",
|
||||||
"Name": "my-cool-session",
|
}
|
||||||
"Node": a.Config.NodeName,
|
|
||||||
"LockDelay": "20s",
|
|
||||||
}
|
|
||||||
enc.Encode(raw)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
t.Run("no check fields should yield default serfHealth", func(t *testing.T) {
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
retry.Run(t, func(r *retry.R) {
|
retry.Run(t, func(r *retry.R) {
|
||||||
|
body := bytes.NewBuffer(nil)
|
||||||
|
enc := json.NewEncoder(body)
|
||||||
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
obj, err := a.srv.SessionCreate(resp, req)
|
obj, err := a.srv.SessionCreate(resp, req)
|
||||||
if err != nil {
|
require.NoError(r, err)
|
||||||
r.Fatalf("err: %v", err)
|
require.Equal(r, resp.Code, http.StatusOK, resp.Body.String())
|
||||||
}
|
|
||||||
if obj == nil {
|
|
||||||
r.Fatalf("expected a session")
|
|
||||||
}
|
|
||||||
|
|
||||||
want := structs.Session{
|
want := structs.Session{
|
||||||
ID: obj.(sessionCreateResponse).ID,
|
ID: obj.(sessionCreateResponse).ID,
|
||||||
|
@ -315,23 +310,22 @@ func TestSessionCreate_NoCheck(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("overwrite nodechecks to associate with no checks", func(t *testing.T) {
|
t.Run("overwrite nodechecks to associate with no checks", func(t *testing.T) {
|
||||||
body := bytes.NewBuffer(nil)
|
|
||||||
enc := json.NewEncoder(body)
|
|
||||||
raw := map[string]interface{}{
|
raw := map[string]interface{}{
|
||||||
"Name": "my-cool-session",
|
"Name": "my-cool-session",
|
||||||
"Node": a.Config.NodeName,
|
"Node": a.Config.NodeName,
|
||||||
"NodeChecks": []string{},
|
"NodeChecks": []string{},
|
||||||
"LockDelay": "20s",
|
"LockDelay": "20s",
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
retry.Run(t, func(r *retry.R) {
|
retry.Run(t, func(r *retry.R) {
|
||||||
|
body := bytes.NewBuffer(nil)
|
||||||
|
enc := json.NewEncoder(body)
|
||||||
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
obj, err := a.srv.SessionCreate(resp, req)
|
obj, err := a.srv.SessionCreate(resp, req)
|
||||||
if err != nil {
|
require.NoError(r, err)
|
||||||
r.Fatalf("err: %v", err)
|
require.Equal(r, resp.Code, http.StatusOK)
|
||||||
}
|
|
||||||
|
|
||||||
want := structs.Session{
|
want := structs.Session{
|
||||||
ID: obj.(sessionCreateResponse).ID,
|
ID: obj.(sessionCreateResponse).ID,
|
||||||
|
@ -346,23 +340,23 @@ func TestSessionCreate_NoCheck(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("overwrite checks to associate with no checks", func(t *testing.T) {
|
t.Run("overwrite checks to associate with no checks", func(t *testing.T) {
|
||||||
body := bytes.NewBuffer(nil)
|
|
||||||
enc := json.NewEncoder(body)
|
|
||||||
raw := map[string]interface{}{
|
raw := map[string]interface{}{
|
||||||
"Name": "my-cool-session",
|
"Name": "my-cool-session",
|
||||||
"Node": a.Config.NodeName,
|
"Node": a.Config.NodeName,
|
||||||
"Checks": []string{},
|
"Checks": []string{},
|
||||||
"LockDelay": "20s",
|
"LockDelay": "20s",
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
retry.Run(t, func(r *retry.R) {
|
retry.Run(t, func(r *retry.R) {
|
||||||
|
body := bytes.NewBuffer(nil)
|
||||||
|
enc := json.NewEncoder(body)
|
||||||
|
require.NoError(r, enc.Encode(raw))
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
obj, err := a.srv.SessionCreate(resp, req)
|
obj, err := a.srv.SessionCreate(resp, req)
|
||||||
if err != nil {
|
require.NoError(r, err)
|
||||||
r.Fatalf("err: %v", err)
|
require.Equal(r, resp.Code, http.StatusOK)
|
||||||
}
|
|
||||||
|
|
||||||
want := structs.Session{
|
want := structs.Session{
|
||||||
ID: obj.(sessionCreateResponse).ID,
|
ID: obj.(sessionCreateResponse).ID,
|
||||||
|
@ -379,6 +373,7 @@ func TestSessionCreate_NoCheck(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTestSession(t *testing.T, srv *HTTPServer) string {
|
func makeTestSession(t *testing.T, srv *HTTPServer) string {
|
||||||
|
t.Helper()
|
||||||
url := "/v1/session/create"
|
url := "/v1/session/create"
|
||||||
req, _ := http.NewRequest("PUT", url, nil)
|
req, _ := http.NewRequest("PUT", url, nil)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -391,13 +386,14 @@ func makeTestSession(t *testing.T, srv *HTTPServer) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTestSessionDelete(t *testing.T, srv *HTTPServer) string {
|
func makeTestSessionDelete(t *testing.T, srv *HTTPServer) string {
|
||||||
|
t.Helper()
|
||||||
// Create Session with delete behavior
|
// Create Session with delete behavior
|
||||||
body := bytes.NewBuffer(nil)
|
body := bytes.NewBuffer(nil)
|
||||||
enc := json.NewEncoder(body)
|
enc := json.NewEncoder(body)
|
||||||
raw := map[string]interface{}{
|
raw := map[string]interface{}{
|
||||||
"Behavior": "delete",
|
"Behavior": "delete",
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
require.NoError(t, enc.Encode(raw))
|
||||||
|
|
||||||
url := "/v1/session/create"
|
url := "/v1/session/create"
|
||||||
req, _ := http.NewRequest("PUT", url, body)
|
req, _ := http.NewRequest("PUT", url, body)
|
||||||
|
@ -411,13 +407,14 @@ func makeTestSessionDelete(t *testing.T, srv *HTTPServer) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTestSessionTTL(t *testing.T, srv *HTTPServer, ttl string) string {
|
func makeTestSessionTTL(t *testing.T, srv *HTTPServer, ttl string) string {
|
||||||
|
t.Helper()
|
||||||
// Create Session with TTL
|
// Create Session with TTL
|
||||||
body := bytes.NewBuffer(nil)
|
body := bytes.NewBuffer(nil)
|
||||||
enc := json.NewEncoder(body)
|
enc := json.NewEncoder(body)
|
||||||
raw := map[string]interface{}{
|
raw := map[string]interface{}{
|
||||||
"TTL": ttl,
|
"TTL": ttl,
|
||||||
}
|
}
|
||||||
enc.Encode(raw)
|
require.NoError(t, enc.Encode(raw))
|
||||||
|
|
||||||
url := "/v1/session/create"
|
url := "/v1/session/create"
|
||||||
req, _ := http.NewRequest("PUT", url, body)
|
req, _ := http.NewRequest("PUT", url, body)
|
||||||
|
@ -586,9 +583,9 @@ func TestSessionGet(t *testing.T) {
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "/v1/session/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
retry.Run(t, func(r *retry.R) {
|
retry.Run(t, func(r *retry.R) {
|
||||||
|
req, _ := http.NewRequest("GET", "/v1/session/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
obj, err := a.srv.SessionGet(resp, req)
|
obj, err := a.srv.SessionGet(resp, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Fatalf("err: %v", err)
|
r.Fatalf("err: %v", err)
|
||||||
|
|
|
@ -153,10 +153,8 @@ func TestUiNodeInfo(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/internal/ui/node/%s", a.Config.NodeName), nil)
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/internal/ui/node/%s", a.Config.NodeName), nil)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
obj, err := a.srv.UINodeInfo(resp, req)
|
obj, err := a.srv.UINodeInfo(resp, req)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("err: %v", err)
|
require.Equal(t, resp.Code, http.StatusOK)
|
||||||
}
|
|
||||||
|
|
||||||
assertIndex(t, resp)
|
assertIndex(t, resp)
|
||||||
|
|
||||||
// Should be 1 node for the server
|
// Should be 1 node for the server
|
||||||
|
|
|
@ -23,6 +23,8 @@ import (
|
||||||
|
|
||||||
// Failer is an interface compatible with testing.T.
|
// Failer is an interface compatible with testing.T.
|
||||||
type Failer interface {
|
type Failer interface {
|
||||||
|
Helper()
|
||||||
|
|
||||||
// Log is called for the final test output
|
// Log is called for the final test output
|
||||||
Log(args ...interface{})
|
Log(args ...interface{})
|
||||||
|
|
||||||
|
@ -116,6 +118,7 @@ func dedup(a []string) string {
|
||||||
func run(r Retryer, t Failer, f func(r *R)) {
|
func run(r Retryer, t Failer, f func(r *R)) {
|
||||||
rr := &R{}
|
rr := &R{}
|
||||||
fail := func() {
|
fail := func() {
|
||||||
|
t.Helper()
|
||||||
out := dedup(rr.output)
|
out := dedup(rr.output)
|
||||||
if out != "" {
|
if out != "" {
|
||||||
t.Log(out)
|
t.Log(out)
|
||||||
|
|
Loading…
Reference in New Issue