Rework sys/health tests to use structs and cmp (#24324)

This commit is contained in:
Nick Cabatoff 2023-12-04 08:34:25 -05:00 committed by GitHub
parent 699fc035e0
commit 85b3dba310
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 165 deletions

View File

@ -49,4 +49,5 @@ type HealthResponse struct {
ClusterName string `json:"cluster_name,omitempty"` ClusterName string `json:"cluster_name,omitempty"`
ClusterID string `json:"cluster_id,omitempty"` ClusterID string `json:"cluster_id,omitempty"`
LastWAL uint64 `json:"last_wal,omitempty"` LastWAL uint64 `json:"last_wal,omitempty"`
Enterprise bool `json:"enterprise"`
} }

View File

@ -7,9 +7,11 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"reflect"
"testing" "testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/constants" "github.com/hashicorp/vault/helper/constants"
"github.com/hashicorp/vault/sdk/helper/consts" "github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/vault" "github.com/hashicorp/vault/vault"
@ -20,73 +22,54 @@ func TestSysHealth_get(t *testing.T) {
ln, addr := TestServer(t, core) ln, addr := TestServer(t, core)
defer ln.Close() defer ln.Close()
resp, err := http.Get(addr + "/v1/sys/health") // Test without the client first since we want to verify the response code
raw, err := http.Get(addr + "/v1/sys/health")
if err != nil {
t.Fatalf("err: %s", err)
}
testResponseStatus(t, raw, 501)
// Test with the client because it's a bit easier to work with structs
config := api.DefaultConfig()
config.Address = addr
client, err := api.NewClient(config)
if err != nil {
t.Fatal(err)
}
resp, err := client.Sys().Health()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
var actual map[string]interface{} expected := &api.HealthResponse{
expected := map[string]interface{}{ Enterprise: constants.IsEnterprise,
"enterprise": constants.IsEnterprise, Initialized: false,
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(), Sealed: true,
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(), Standby: true,
"initialized": false, PerformanceStandby: false,
"sealed": true, ReplicationPerformanceMode: consts.ReplicationUnknown.GetPerformanceString(),
"standby": true, ReplicationDRMode: consts.ReplicationUnknown.GetDRString(),
"performance_standby": false,
} }
testResponseStatus(t, resp, 501) ignore := cmpopts.IgnoreFields(*expected, "ClusterName", "ClusterID", "ServerTimeUTC", "Version")
testResponseBody(t, resp, &actual) if diff := cmp.Diff(resp, expected, ignore); len(diff) > 0 {
expected["server_time_utc"] = actual["server_time_utc"] t.Fatal(diff)
expected["version"] = actual["version"]
if actual["cluster_name"] == nil {
delete(expected, "cluster_name")
} else {
expected["cluster_name"] = actual["cluster_name"]
}
if actual["cluster_id"] == nil {
delete(expected, "cluster_id")
} else {
expected["cluster_id"] = actual["cluster_id"]
}
delete(actual, "license")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
} }
keys, _ := vault.TestCoreInit(t, core) keys, _ := vault.TestCoreInit(t, core)
resp, err = http.Get(addr + "/v1/sys/health") raw, err = http.Get(addr + "/v1/sys/health")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
testResponseStatus(t, raw, 503)
actual = map[string]interface{}{} resp, err = client.Sys().Health()
expected = map[string]interface{}{ if err != nil {
"enterprise": constants.IsEnterprise, t.Fatalf("err: %s", err)
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(),
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(),
"initialized": true,
"sealed": true,
"standby": true,
"performance_standby": false,
} }
testResponseStatus(t, resp, 503) expected.Initialized = true
testResponseBody(t, resp, &actual) if diff := cmp.Diff(resp, expected, ignore); len(diff) > 0 {
expected["server_time_utc"] = actual["server_time_utc"] t.Fatal(diff)
expected["version"] = actual["version"]
if actual["cluster_name"] == nil {
delete(expected, "cluster_name")
} else {
expected["cluster_name"] = actual["cluster_name"]
}
if actual["cluster_id"] == nil {
delete(expected, "cluster_id")
} else {
expected["cluster_id"] = actual["cluster_id"]
}
delete(actual, "license")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
} }
for _, key := range keys { for _, key := range keys {
@ -94,38 +77,22 @@ func TestSysHealth_get(t *testing.T) {
t.Fatalf("unseal err: %s", err) t.Fatalf("unseal err: %s", err)
} }
} }
resp, err = http.Get(addr + "/v1/sys/health") raw, err = http.Get(addr + "/v1/sys/health")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
testResponseStatus(t, raw, 200)
actual = map[string]interface{}{} resp, err = client.Sys().Health()
expected = map[string]interface{}{ if err != nil {
"enterprise": constants.IsEnterprise, t.Fatalf("err: %s", err)
"replication_performance_mode": consts.ReplicationPerformanceDisabled.GetPerformanceString(),
"replication_dr_mode": consts.ReplicationDRDisabled.GetDRString(),
"initialized": true,
"sealed": false,
"standby": false,
"performance_standby": false,
} }
testResponseStatus(t, resp, 200) expected.Sealed = false
testResponseBody(t, resp, &actual) expected.Standby = false
expected["server_time_utc"] = actual["server_time_utc"] expected.ReplicationPerformanceMode = consts.ReplicationPerformanceDisabled.GetPerformanceString()
expected["version"] = actual["version"] expected.ReplicationDRMode = consts.ReplicationDRDisabled.GetDRString()
if actual["cluster_name"] == nil { if diff := cmp.Diff(resp, expected, ignore); len(diff) > 0 {
delete(expected, "cluster_name") t.Fatal(diff)
} else {
expected["cluster_name"] = actual["cluster_name"]
}
if actual["cluster_id"] == nil {
delete(expected, "cluster_id")
} else {
expected["cluster_id"] = actual["cluster_id"]
}
delete(actual, "license")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
} }
} }
@ -138,75 +105,53 @@ func TestSysHealth_customcodes(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
resp, err := http.Get(queryurl.String()) raw, err := http.Get(queryurl.String())
if err != nil {
t.Fatalf("err: %s", err)
}
testResponseStatus(t, raw, 581)
// Test with the client because it's a bit easier to work with structs
config := api.DefaultConfig()
config.Address = addr
client, err := api.NewClient(config)
if err != nil {
t.Fatal(err)
}
resp, err := client.Sys().Health()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
var actual map[string]interface{} expected := &api.HealthResponse{
expected := map[string]interface{}{ Enterprise: constants.IsEnterprise,
"enterprise": constants.IsEnterprise, Initialized: false,
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(), Sealed: true,
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(), Standby: true,
"initialized": false, PerformanceStandby: false,
"sealed": true, ReplicationPerformanceMode: consts.ReplicationUnknown.GetPerformanceString(),
"standby": true, ReplicationDRMode: consts.ReplicationUnknown.GetDRString(),
"performance_standby": false,
} }
testResponseStatus(t, resp, 581) ignore := cmpopts.IgnoreFields(*expected, "ClusterName", "ClusterID", "ServerTimeUTC", "Version")
testResponseBody(t, resp, &actual) if diff := cmp.Diff(resp, expected, ignore); len(diff) > 0 {
t.Fatal(diff)
expected["server_time_utc"] = actual["server_time_utc"]
expected["version"] = actual["version"]
if actual["cluster_name"] == nil {
delete(expected, "cluster_name")
} else {
expected["cluster_name"] = actual["cluster_name"]
}
if actual["cluster_id"] == nil {
delete(expected, "cluster_id")
} else {
expected["cluster_id"] = actual["cluster_id"]
}
delete(actual, "license")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
} }
keys, _ := vault.TestCoreInit(t, core) keys, _ := vault.TestCoreInit(t, core)
resp, err = http.Get(queryurl.String()) raw, err = http.Get(queryurl.String())
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
testResponseStatus(t, raw, 523)
actual = map[string]interface{}{} resp, err = client.Sys().Health()
expected = map[string]interface{}{ if err != nil {
"enterprise": constants.IsEnterprise, t.Fatalf("err: %s", err)
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(),
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(),
"initialized": true,
"sealed": true,
"standby": true,
"performance_standby": false,
} }
testResponseStatus(t, resp, 523) expected.Initialized = true
testResponseBody(t, resp, &actual) if diff := cmp.Diff(resp, expected, ignore); len(diff) > 0 {
t.Fatal(diff)
expected["server_time_utc"] = actual["server_time_utc"]
expected["version"] = actual["version"]
if actual["cluster_name"] == nil {
delete(expected, "cluster_name")
} else {
expected["cluster_name"] = actual["cluster_name"]
}
if actual["cluster_id"] == nil {
delete(expected, "cluster_id")
} else {
expected["cluster_id"] = actual["cluster_id"]
}
delete(actual, "license")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
} }
for _, key := range keys { for _, key := range keys {
@ -214,38 +159,22 @@ func TestSysHealth_customcodes(t *testing.T) {
t.Fatalf("unseal err: %s", err) t.Fatalf("unseal err: %s", err)
} }
} }
resp, err = http.Get(queryurl.String()) raw, err = http.Get(queryurl.String())
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
testResponseStatus(t, raw, 202)
actual = map[string]interface{}{} resp, err = client.Sys().Health()
expected = map[string]interface{}{ if err != nil {
"enterprise": constants.IsEnterprise, t.Fatalf("err: %s", err)
"replication_performance_mode": consts.ReplicationPerformanceDisabled.GetPerformanceString(),
"replication_dr_mode": consts.ReplicationDRDisabled.GetDRString(),
"initialized": true,
"sealed": false,
"standby": false,
"performance_standby": false,
} }
testResponseStatus(t, resp, 202) expected.Sealed = false
testResponseBody(t, resp, &actual) expected.Standby = false
expected["server_time_utc"] = actual["server_time_utc"] expected.ReplicationPerformanceMode = consts.ReplicationPerformanceDisabled.GetPerformanceString()
expected["version"] = actual["version"] expected.ReplicationDRMode = consts.ReplicationDRDisabled.GetDRString()
if actual["cluster_name"] == nil { if diff := cmp.Diff(resp, expected, ignore); len(diff) > 0 {
delete(expected, "cluster_name") t.Fatal(diff)
} else {
expected["cluster_name"] = actual["cluster_name"]
}
if actual["cluster_id"] == nil {
delete(expected, "cluster_id")
} else {
expected["cluster_id"] = actual["cluster_id"]
}
delete(actual, "license")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
} }
} }