config: remove t.Parallel and some unused fields from tests

Use go-cmp for better diffs in tests
This commit is contained in:
Daniel Nephin 2020-11-20 14:08:17 -05:00
parent 6f996ad41e
commit a9734b69c5
4 changed files with 19 additions and 31 deletions

View File

@ -7,7 +7,6 @@ import (
)
func TestBuildAndValidate_HTTPMaxConnsPerClientExceedsRLimit(t *testing.T) {
t.Parallel()
hcl := `
limits{
# We put a very high value to be sure to fail

View File

@ -148,7 +148,7 @@ func NewBuilder(opts BuilderOpts) (*Builder, error) {
// we need to merge all slice values defined in flags before we
// merge the config files since the flag values for slices are
// otherwise appended instead of prepended.
slices, values := b.splitSlicesAndValues(opts.Config)
slices, values := splitSlicesAndValues(opts.Config)
b.Head = append(b.Head, newSource("flags.slices", slices))
for _, path := range opts.ConfigFiles {
sources, err := b.sourcesFromPath(path, opts.ConfigFormat)
@ -1494,7 +1494,7 @@ func addrsUnique(inuse map[string]string, name string, addrs []net.Addr) error {
// splitSlicesAndValues moves all slice values defined in c to 'slices'
// and all other values to 'values'.
func (b *Builder) splitSlicesAndValues(c Config) (slices, values Config) {
func splitSlicesAndValues(c Config) (slices, values Config) {
v, t := reflect.ValueOf(c), reflect.TypeOf(c)
rs, rv := reflect.New(t), reflect.New(t)

View File

@ -12,7 +12,7 @@ var entTokenConfigSanitize = `"EnterpriseConfig": {},`
func entFullRuntimeConfig(rt *RuntimeConfig) {}
var enterpriseReadReplicaWarnings []string = []string{enterpriseConfigKeyError{key: "read_replica"}.Error()}
var enterpriseReadReplicaWarnings = []string{enterpriseConfigKeyError{key: "read_replica"}.Error()}
var enterpriseConfigKeyWarnings []string

View File

@ -19,6 +19,8 @@ import (
"time"
"github.com/armon/go-metrics/prometheus"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/cache"
@ -35,10 +37,9 @@ import (
type configTest struct {
desc string
args []string
pre, post func()
pre func()
json, jsontail []string
hcl, hcltail []string
skipformat bool
privatev4 func() ([]*net.IPAddr, error)
publicv6 func() ([]*net.IPAddr, error)
patch func(rt *RuntimeConfig)
@ -4821,7 +4822,6 @@ func TestBuilder_BuildAndValidate_ConfigFlagsAndEdgecases(t *testing.T) {
}
func testConfig(t *testing.T, tests []configTest, dataDir string) {
t.Helper()
for _, tt := range tests {
for pass, format := range []string{"json", "hcl"} {
// clean data dir before every test
@ -4837,22 +4837,15 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
// json and hcl sources need to be in sync
// to make sure we're generating the same config
if len(tt.json) != len(tt.hcl) && !tt.skipformat {
if len(tt.json) != len(tt.hcl) {
t.Fatal(tt.desc, ": JSON and HCL test case out of sync")
}
// select the source
srcs, tails := tt.json, tt.jsontail
if format == "hcl" {
srcs, tails = tt.hcl, tt.hcltail
}
// If we're skipping a format and the current format is empty,
// then skip it!
if tt.skipformat && len(srcs) == 0 {
continue
}
// build the description
var desc []string
if !flagsOnly {
@ -4863,8 +4856,8 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
}
t.Run(strings.Join(desc, ":"), func(t *testing.T) {
// first parse the flags
flags := BuilderOpts{}
fs := flag.NewFlagSet("", flag.ContinueOnError)
AddFlags(fs, &flags)
err := fs.Parse(tt.args)
@ -4876,17 +4869,10 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
if tt.pre != nil {
tt.pre()
}
defer func() {
if tt.post != nil {
tt.post()
}
}()
// Then create a builder with the flags.
b, err := NewBuilder(flags)
if err != nil {
t.Fatal("NewBuilder", err)
}
require.NoError(t, err)
patchBuilderShims(b)
if tt.hostname != nil {
@ -4899,7 +4885,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
b.getPublicIPv6 = tt.publicv6
}
// read the source fragements
// read the source fragments
for i, data := range srcs {
b.Sources = append(b.Sources, FileSource{
Name: fmt.Sprintf("src-%d.%s", i, format),
@ -4915,7 +4901,6 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
})
}
// build/merge the config fragments
actual, err := b.BuildAndValidate()
if err == nil && tt.err != "" {
t.Fatalf("got no error want %q", tt.err)
@ -4945,9 +4930,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
x.getPrivateIPv4 = func() ([]*net.IPAddr, error) { return []*net.IPAddr{ipAddr("10.0.0.1")}, nil }
x.getPublicIPv6 = func() ([]*net.IPAddr, error) { return []*net.IPAddr{ipAddr("dead:beef::1")}, nil }
expected, err := x.Build()
if err != nil {
t.Fatalf("build default failed: %s", err)
}
require.NoError(t, err)
if tt.patch != nil {
tt.patch(&expected)
}
@ -4961,12 +4944,19 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
if tt.patchActual != nil {
tt.patchActual(&actual)
}
require.Equal(t, expected, actual)
assertDeepEqual(t, expected, actual, cmpopts.EquateEmpty())
})
}
}
}
func assertDeepEqual(t *testing.T, x, y interface{}, opts ...cmp.Option) {
t.Helper()
if diff := cmp.Diff(x, y, opts...); diff != "" {
t.Fatalf("assertion failed: values are not equal\n--- expected\n+++ actual\n%v", diff)
}
}
func TestNewBuilder_InvalidConfigFormat(t *testing.T) {
_, err := NewBuilder(BuilderOpts{ConfigFormat: "yaml"})
require.Error(t, err)
@ -7398,7 +7388,6 @@ func TestNonZero(t *testing.T) {
}
func TestConfigDecodeBytes(t *testing.T) {
t.Parallel()
// Test with some input
src := []byte("abc")
key := base64.StdEncoding.EncodeToString(src)