Replace goe/verify.Values with testify/require.Equal (#7993)

* testing: replace most goe/verify.Values with require.Equal

One difference between these two comparisons is that go/verify considers
nil slices/maps to be equal to empty slices/maps, where as testify/require
does not, and does not appear to provide any way to enable that behaviour.

Because of this difference some expected values were changed from empty
slices to nil slices, and some calls to verify.Values were left.

* Remove github.com/pascaldekloe/goe/verify

Reduce the number of assertion packages we use from 2 to 1
This commit is contained in:
Daniel Nephin 2020-06-02 12:41:25 -04:00 committed by GitHub
parent 9795b19e27
commit 99eb583ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 134 additions and 399 deletions

View File

@ -33,7 +33,6 @@ import (
"github.com/hashicorp/consul/types"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/serf/serf"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -1026,8 +1025,6 @@ func TestAgent_IndexChurn(t *testing.T) {
// verifyIndexChurn registers some things and runs anti-entropy a bunch of times
// in a row to make sure there are no index bumps.
func verifyIndexChurn(t *testing.T, tags []string) {
t.Helper()
a := NewTestAgent(t, "")
defer a.Shutdown()
@ -1115,7 +1112,7 @@ func verifyIndexChurn(t *testing.T, tags []string) {
if err := a.RPC("Health.ServiceNodes", args, &after); err != nil {
t.Fatalf("err: %v", err)
}
verify.Values(t, "", after, before)
require.Equal(t, before, after)
}
func TestAgent_AddCheck(t *testing.T) {

View File

@ -6,7 +6,7 @@ import (
"strings"
"testing"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
// TestParseFlags tests whether command line flags are properly parsed
@ -102,9 +102,17 @@ func TestParseFlags(t *testing.T) {
t.Fatalf("got error %v want %v", got, want)
}
flags.Args = fs.Args()
if !verify.Values(t, "flag", flags, tt.flags) {
t.FailNow()
// Normalize the expected value because require.Equal considers
// empty slices/maps and nil slices/maps to be different.
if len(tt.flags.Args) == 0 && flags.Args != nil {
tt.flags.Args = []string{}
}
if len(tt.flags.Config.NodeMeta) == 0 {
tt.flags.Config.NodeMeta = map[string]string{}
}
require.Equal(t, tt.flags, flags)
})
}
}

View File

@ -4,7 +4,7 @@ import (
"testing"
"time"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestMerge(t *testing.T) {
@ -39,7 +39,12 @@ func TestMerge(t *testing.T) {
"a": "b",
"c": "e",
},
Ports: Ports{DNS: pInt(2), HTTP: pInt(3)},
Ports: Ports{DNS: pInt(2), HTTP: pInt(3)},
SnapshotAgent: map[string]interface{}{},
TaggedAddresses: map[string]string{},
HTTPConfig: HTTPConfig{ResponseHeaders: map[string]string{}},
DNS: DNS{ServiceTTL: map[string]string{}},
Connect: Connect{CAConfig: map[string]interface{}{}},
},
},
}
@ -47,9 +52,7 @@ func TestMerge(t *testing.T) {
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
got, want := Merge(tt.cfgs...), tt.want
if !verify.Values(t, "", got, want) {
t.FailNow()
}
require.Equal(t, want, got)
})
}
}

View File

@ -23,7 +23,6 @@ import (
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/types"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
@ -3860,7 +3859,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
}
// build/merge the config fragments
rt, err := b.BuildAndValidate()
actual, err := b.BuildAndValidate()
if err == nil && tt.err != "" {
t.Fatalf("got no error want %q", tt.err)
}
@ -3873,11 +3872,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
if err != nil && tt.err != "" && !strings.Contains(err.Error(), tt.err) {
t.Fatalf("error %q does not contain %q", err.Error(), tt.err)
}
// check the warnings
if !verify.Values(t, "warnings", b.Warnings, tt.warns) {
t.FailNow()
}
require.Equal(t, tt.warns, b.Warnings, "warnings")
// stop if we expected an error
if tt.err != "" {
@ -3894,19 +3889,14 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
x.Hostname = b.Hostname
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 }
patchedRT, err := x.Build()
expected, err := x.Build()
if err != nil {
t.Fatalf("build default failed: %s", err)
}
if tt.patch != nil {
tt.patch(&patchedRT)
}
// if err := x.Validate(wantRT); err != nil {
// t.Fatalf("validate default failed: %s", err)
// }
if got, want := rt, patchedRT; !verify.Values(t, "", got, want) {
t.FailNow()
tt.patch(&expected)
}
require.Equal(t, expected, actual)
})
}
}
@ -5752,6 +5742,7 @@ func TestFullConfig(t *testing.T) {
Config: map[string]interface{}{
"1CuJHVfw": "Kzqsa7yc",
},
Upstreams: structs.Upstreams{},
},
Weights: &structs.Weights{
Passing: 1,
@ -5861,6 +5852,8 @@ func TestFullConfig(t *testing.T) {
SerfAdvertiseAddrWAN: tcpAddr("78.63.37.19:8302"),
SerfBindAddrLAN: tcpAddr("99.43.63.15:8301"),
SerfBindAddrWAN: tcpAddr("67.88.33.19:8302"),
SerfAllowedCIDRsLAN: []net.IPNet{},
SerfAllowedCIDRsWAN: []net.IPNet{},
SessionTTLMin: 26627 * time.Second,
SkipLeaveOnInt: true,
StartJoinAddrsLAN: []string{"LR3hGDoG", "MwVpZ4Up"},
@ -5978,10 +5971,7 @@ func TestFullConfig(t *testing.T) {
t.Fatalf("Build: %s", err)
}
// verify that all fields are set
if !verify.Values(t, "runtime_config", rt, want) {
t.FailNow()
}
require.Equal(t, want, rt)
// at this point we have confirmed that the parsing worked
// for all fields but the validation will fail since certain

View File

@ -5,7 +5,7 @@ import (
"time"
"github.com/hashicorp/raft"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestPromotion(t *testing.T) {
@ -37,7 +37,6 @@ func TestPromotion(t *testing.T) {
servers: []raft.Server{
{ID: "a", Suffrage: raft.Voter},
},
promotions: []raft.Server{},
},
{
name: "one stable nonvoter, should be promoted",
@ -91,12 +90,13 @@ func TestPromotion(t *testing.T) {
{ID: "b", Suffrage: raft.Nonvoter},
{ID: "c", Suffrage: raft.Nonvoter},
},
promotions: []raft.Server{},
},
}
for _, tc := range cases {
promotions := PromoteStableServers(tc.conf, tc.health, tc.servers)
verify.Values(t, tc.name, tc.promotions, promotions)
t.Run(tc.name, func(t *testing.T) {
promotions := PromoteStableServers(tc.conf, tc.health, tc.servers)
require.Equal(t, tc.promotions, promotions)
})
}
}

View File

@ -17,7 +17,7 @@ import (
"github.com/hashicorp/consul/testrpc"
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/serf/coordinate"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
// generateRandomCoordinate creates a random coordinate. This mucks with the
@ -83,13 +83,13 @@ func TestCoordinate_Update(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
verify.Values(t, "", c, lib.CoordinateSet{})
require.Equal(t, lib.CoordinateSet{}, c)
_, c, err = state.Coordinate("node2", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
verify.Values(t, "", c, lib.CoordinateSet{})
require.Equal(t, lib.CoordinateSet{}, c)
// Send another update for the second node. It should take precedence
// since there will be two updates in the same batch.
@ -107,7 +107,7 @@ func TestCoordinate_Update(t *testing.T) {
expected := lib.CoordinateSet{
"": arg1.Coord,
}
verify.Values(t, "", c, expected)
require.Equal(t, expected, c)
_, c, err = state.Coordinate("node2", nil)
if err != nil {
@ -116,7 +116,7 @@ func TestCoordinate_Update(t *testing.T) {
expected = lib.CoordinateSet{
"": arg2.Coord,
}
verify.Values(t, "", c, expected)
require.Equal(t, expected, c)
// Register a bunch of additional nodes.
spamLen := s1.config.CoordinateUpdateBatchSize*s1.config.CoordinateUpdateMaxBatches + 1
@ -273,7 +273,7 @@ func TestCoordinate_ListDatacenters(t *testing.T) {
if err != nil {
t.Fatalf("bad: %v", err)
}
verify.Values(t, "", c, out[0].Coordinates[0].Coord)
require.Equal(t, out[0].Coordinates[0].Coord, c)
}
func TestCoordinate_ListNodes(t *testing.T) {
@ -335,9 +335,9 @@ func TestCoordinate_ListNodes(t *testing.T) {
resp.Coordinates[2].Node != "foo" {
r.Fatalf("bad: %v", resp.Coordinates)
}
verify.Values(t, "", resp.Coordinates[0].Coord, arg2.Coord) // bar
verify.Values(t, "", resp.Coordinates[1].Coord, arg3.Coord) // baz
verify.Values(t, "", resp.Coordinates[2].Coord, arg1.Coord) // foo
require.Equal(r, arg2.Coord, resp.Coordinates[0].Coord) // bar
require.Equal(r, arg3.Coord, resp.Coordinates[1].Coord) // baz
require.Equal(r, arg1.Coord, resp.Coordinates[2].Coord) // foo
})
}
@ -521,7 +521,7 @@ func TestCoordinate_Node(t *testing.T) {
resp.Coordinates[0].Node != "foo" {
r.Fatalf("bad: %v", resp.Coordinates)
}
verify.Values(t, "", resp.Coordinates[0].Coord, arg1.Coord) // foo
require.Equal(r, arg1.Coord, resp.Coordinates[0].Coord) // foo
})
}

View File

@ -21,7 +21,6 @@ import (
"github.com/hashicorp/raft"
"github.com/hashicorp/serf/coordinate"
"github.com/mitchellh/mapstructure"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -944,7 +943,7 @@ func TestFSM_ACL_CRUD(t *testing.T) {
}
bootstrap.ACL.CreateIndex = respACL.CreateIndex
bootstrap.ACL.ModifyIndex = respACL.ModifyIndex
verify.Values(t, "", respACL, &bootstrap.ACL)
require.Equal(t, &bootstrap.ACL, respACL)
}
func TestFSM_PreparedQuery_CRUD(t *testing.T) {

View File

@ -10,7 +10,7 @@ import (
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testrpc"
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestKVS_Apply(t *testing.T) {
@ -584,7 +584,7 @@ key "zip" {
actualKeys = append(actualKeys, entry.Key)
}
verify.Values(t, "", actualKeys, expectedKeys)
require.Equal(t, expectedKeys, actualKeys)
// list keys with a prefix that has list permissions should succeed
getKeysReq2 := structs.KeyListRequest{
@ -598,7 +598,7 @@ key "zip" {
actualKeys = keyList.Keys
verify.Values(t, "", actualKeys, expectedKeys)
require.Equal(t, expectedKeys, actualKeys)
}

View File

@ -13,7 +13,7 @@ import (
"github.com/hashicorp/consul/testrpc"
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/raft"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestOperator_RaftGetConfiguration(t *testing.T) {
@ -55,7 +55,7 @@ func TestOperator_RaftGetConfiguration(t *testing.T) {
},
Index: future.Index(),
}
verify.Values(t, "", reply, expected)
require.Equal(t, expected, reply)
}
func TestOperator_RaftGetConfiguration_ACLDeny(t *testing.T) {
@ -132,7 +132,7 @@ func TestOperator_RaftGetConfiguration_ACLDeny(t *testing.T) {
},
Index: future.Index(),
}
verify.Values(t, "", reply, expected)
require.Equal(t, expected, reply)
}
func TestOperator_RaftRemovePeerByAddress(t *testing.T) {

View File

@ -6,7 +6,7 @@ import (
"time"
"github.com/hashicorp/consul/agent/consul/autopilot"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestStateStore_Autopilot(t *testing.T) {
@ -117,7 +117,7 @@ func TestStateStore_Autopilot_Snapshot_Restore(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
verify.Values(t, "", before, snapped)
require.Equal(t, snapped, before, "autopilot snapshot")
s2 := testStateStore(t)
restore := s2.Restore()
@ -133,5 +133,5 @@ func TestStateStore_Autopilot_Snapshot_Restore(t *testing.T) {
if idx != 99 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", before, res)
require.Equal(t, res, before, "autopilot config")
}

View File

@ -13,7 +13,6 @@ import (
"github.com/hashicorp/consul/types"
"github.com/hashicorp/go-memdb"
uuid "github.com/hashicorp/go-uuid"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -180,9 +179,7 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
if err != nil {
t.Fatalf("got err %s want nil", err)
}
if got, want := out, node; !verify.Values(t, "GetNode", got, want) {
t.FailNow()
}
require.Equal(t, node, out)
_, out2, err := s.GetNodeID(nodeID)
if err != nil {
@ -191,9 +188,7 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
if out2 == nil {
t.Fatalf("out2 should not be nil")
}
if got, want := out, out2; !verify.Values(t, "GetNodeID", got, want) {
t.FailNow()
}
require.Equal(t, out, out2)
}
verifyNode()
@ -242,17 +237,13 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
if gotidx, wantidx := idx, uint64(2); err != nil || gotidx != wantidx {
t.Fatalf("got err, idx: %s, %d want nil, %d", err, gotidx, wantidx)
}
if got, want := out.Services, svcmap; !verify.Values(t, "NodeServices", got, want) {
t.FailNow()
}
require.Equal(t, svcmap, out.Services)
idx, r, err := s.NodeService("node1", "redis1", nil)
if gotidx, wantidx := idx, uint64(2); err != nil || gotidx != wantidx {
t.Fatalf("got err, idx: %s, %d want nil, %d", err, gotidx, wantidx)
}
if got, want := r, svcmap["redis1"]; !verify.Values(t, "NodeService", got, want) {
t.FailNow()
}
require.Equal(t, svcmap["redis1"], r)
}
verifyNode()
verifyService()
@ -284,17 +275,13 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
if gotidx, wantidx := idx, uint64(3); err != nil || gotidx != wantidx {
t.Fatalf("got err, idx: %s, %d want nil, %d", err, gotidx, wantidx)
}
if got, want := out, checks; !verify.Values(t, "NodeChecks", got, want) {
t.FailNow()
}
require.Equal(t, checks, out)
idx, c, err := s.NodeCheck("node1", "check1", nil)
if gotidx, wantidx := idx, uint64(3); err != nil || gotidx != wantidx {
t.Fatalf("got err, idx: %s, %d want nil, %d", err, gotidx, wantidx)
}
if got, want := c, checks[0]; !verify.Values(t, "NodeCheck", got, want) {
t.FailNow()
}
require.Equal(t, checks[0], c)
}
verifyNode()
verifyService()
@ -344,9 +331,7 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
if gotidx, wantidx := idx, uint64(4); err != nil || gotidx != wantidx {
t.Fatalf("got err, idx: %s, %d want nil, %d", err, gotidx, wantidx)
}
if got, want := out, checks; !verify.Values(t, "NodeChecks", got, want) {
t.FailNow()
}
require.Equal(t, checks, out)
}
verifyChecks()

View File

@ -8,8 +8,8 @@ import (
"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStore_CAConfig(t *testing.T) {
@ -127,7 +127,7 @@ func TestStore_CAConfig_Snapshot_Restore(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
verify.Values(t, "", before, snapped)
require.Equal(t, snapped, before)
s2 := testStateStore(t)
restore := s2.Restore()
@ -143,7 +143,7 @@ func TestStore_CAConfig_Snapshot_Restore(t *testing.T) {
if idx != 99 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", before, res)
require.Equal(t, res, before)
}
// Make sure we handle the case of a leftover blank CA config that
@ -162,7 +162,7 @@ func TestStore_CAConfig_Snapshot_Restore_BlankConfig(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
verify.Values(t, "", before, snapped)
require.Equal(t, snapped, before)
s2 := testStateStore(t)
restore := s2.Restore()

View File

@ -9,7 +9,7 @@ import (
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/serf/coordinate"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
// generateRandomCoordinate creates a random coordinate. This mucks with the
@ -40,14 +40,14 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
if idx != 0 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", all, structs.Coordinates{})
require.Nil(t, all)
coordinateWs := memdb.NewWatchSet()
_, coords, err := s.Coordinate("nope", coordinateWs)
if err != nil {
t.Fatalf("err: %s", err)
}
verify.Values(t, "", coords, lib.CoordinateSet{})
require.Equal(t, lib.CoordinateSet{}, coords)
// Make an update for nodes that don't exist and make sure they get
// ignored.
@ -78,7 +78,7 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
if idx != 1 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", all, structs.Coordinates{})
require.Nil(t, all)
coordinateWs = memdb.NewWatchSet()
idx, coords, err = s.Coordinate("node1", coordinateWs)
@ -108,7 +108,7 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
if idx != 3 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", all, updates)
require.Equal(t, updates, all)
// Also verify the per-node coordinate interface.
nodeWs := make([]memdb.WatchSet, len(updates))
@ -124,7 +124,7 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
expected := lib.CoordinateSet{
"": update.Coord,
}
verify.Values(t, "", coords, expected)
require.Equal(t, expected, coords)
}
// Update the coordinate for one of the nodes.
@ -149,7 +149,7 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
if idx != 4 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", all, updates)
require.Equal(t, updates, all)
// And check the per-node coordinate version of the same thing.
for _, update := range updates {
@ -163,7 +163,7 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
expected := lib.CoordinateSet{
"": update.Coord,
}
verify.Values(t, "", coords, expected)
require.Equal(t, expected, coords)
}
// Apply an invalid update and make sure it gets ignored.
@ -186,7 +186,7 @@ func TestStateStore_Coordinate_Updates(t *testing.T) {
if idx != 5 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", all, updates)
require.Equal(t, updates, all)
}
func TestStateStore_Coordinate_Cleanup(t *testing.T) {
@ -219,7 +219,7 @@ func TestStateStore_Coordinate_Cleanup(t *testing.T) {
"alpha": updates[0].Coord,
"beta": updates[1].Coord,
}
verify.Values(t, "", coords, expected)
require.Equal(t, expected, coords)
// Now delete the node.
if err := s.DeleteNode(3, "node1"); err != nil {
@ -231,7 +231,7 @@ func TestStateStore_Coordinate_Cleanup(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
verify.Values(t, "", coords, lib.CoordinateSet{})
require.Equal(t, lib.CoordinateSet{}, coords)
// Make sure the index got updated.
idx, all, err := s.Coordinates(nil)
@ -241,7 +241,7 @@ func TestStateStore_Coordinate_Cleanup(t *testing.T) {
if idx != 3 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", all, structs.Coordinates{})
require.Nil(t, all)
}
func TestStateStore_Coordinate_Snapshot_Restore(t *testing.T) {
@ -310,7 +310,7 @@ func TestStateStore_Coordinate_Snapshot_Restore(t *testing.T) {
// The snapshot will have the bad update in it, since we don't filter on
// the read side.
verify.Values(t, "", dump, append(updates, badUpdate))
require.Equal(t, append(updates, badUpdate), dump)
// Restore the values into a new state store.
func() {
@ -329,7 +329,7 @@ func TestStateStore_Coordinate_Snapshot_Restore(t *testing.T) {
if idx != 6 {
t.Fatalf("bad index: %d", idx)
}
verify.Values(t, "", res, updates)
require.Equal(t, updates, res)
// Check that the index was updated (note that it got passed
// in during the restore).

View File

@ -8,12 +8,10 @@ import (
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/types"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestStateStore_Txn_Intention(t *testing.T) {
require := require.New(t)
s := testStateStore(t)
// Create some intentions.
@ -45,8 +43,8 @@ func TestStateStore_Txn_Intention(t *testing.T) {
// Write the first two to the state store, leave the third
// to be created by the transaction operation.
require.NoError(s.IntentionSet(1, ixn1))
require.NoError(s.IntentionSet(2, ixn2))
require.NoError(t, s.IntentionSet(1, ixn1))
require.NoError(t, s.IntentionSet(2, ixn2))
// Set up a transaction that hits every operation.
ops := structs.TxnOps{
@ -76,14 +74,12 @@ func TestStateStore_Txn_Intention(t *testing.T) {
// Make sure the response looks as expected.
expected := structs.TxnResults{}
verify.Values(t, "", results, expected)
require.Equal(t, expected, results)
// Pull the resulting state store contents.
idx, actual, err := s.Intentions(nil)
require.NoError(err)
if idx != 3 {
t.Fatalf("bad index: %d", idx)
}
require.NoError(t, err)
require.Equal(t, uint64(3), idx, "wrong index")
// Make sure it looks as expected.
intentions := structs.Intentions{
@ -114,11 +110,10 @@ func TestStateStore_Txn_Intention(t *testing.T) {
},
},
}
verify.Values(t, "", actual, intentions)
require.Equal(t, intentions, actual)
}
func TestStateStore_Txn_Node(t *testing.T) {
require := require.New(t)
s := testStateStore(t)
// Create some nodes.
@ -195,22 +190,21 @@ func TestStateStore_Txn_Node(t *testing.T) {
Node: &nodes[1],
},
}
verify.Values(t, "", results, expected)
require.Equal(t, expected, results)
// Pull the resulting state store contents.
idx, actual, err := s.Nodes(nil)
require.NoError(err)
require.NoError(t, err)
if idx != 8 {
t.Fatalf("bad index: %d", idx)
}
// Make sure it looks as expected.
expectedNodes := structs.Nodes{&nodes[0], &nodes[1], &nodes[4]}
verify.Values(t, "", actual, expectedNodes)
require.Equal(t, expectedNodes, actual)
}
func TestStateStore_Txn_Service(t *testing.T) {
require := require.New(t)
s := testStateStore(t)
testRegisterNode(t, s, 1, "node1")
@ -284,6 +278,7 @@ func TestStateStore_Txn_Service(t *testing.T) {
ModifyIndex: 2,
},
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
Meta: map[string]string{},
},
},
&structs.TxnResult{
@ -310,11 +305,11 @@ func TestStateStore_Txn_Service(t *testing.T) {
},
},
}
verify.Values(t, "", results, expected)
require.Equal(t, expected, results)
// Pull the resulting state store contents.
idx, actual, err := s.NodeServices(nil, "node1", nil)
require.NoError(err)
require.NoError(t, err)
if idx != 6 {
t.Fatalf("bad index: %d", idx)
}
@ -340,6 +335,7 @@ func TestStateStore_Txn_Service(t *testing.T) {
},
Weights: &structs.Weights{Passing: 1, Warning: 1},
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
Meta: map[string]string{},
},
"svc5": &structs.NodeService{
ID: "svc5",
@ -362,11 +358,10 @@ func TestStateStore_Txn_Service(t *testing.T) {
},
},
}
verify.Values(t, "", actual, expectedServices)
require.Equal(t, expectedServices, actual)
}
func TestStateStore_Txn_Checks(t *testing.T) {
require := require.New(t)
s := testStateStore(t)
testRegisterNode(t, s, 1, "node1")
@ -462,11 +457,11 @@ func TestStateStore_Txn_Checks(t *testing.T) {
},
},
}
verify.Values(t, "", results, expected)
require.Equal(t, expected, results)
// Pull the resulting state store contents.
idx, actual, err := s.NodeChecks(nil, "node1", nil)
require.NoError(err)
require.NoError(t, err)
if idx != 6 {
t.Fatalf("bad index: %d", idx)
}
@ -504,7 +499,7 @@ func TestStateStore_Txn_Checks(t *testing.T) {
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
},
}
verify.Values(t, "", actual, expectedChecks)
require.Equal(t, expectedChecks, actual)
}
func TestStateStore_Txn_KVS(t *testing.T) {

View File

@ -18,7 +18,6 @@ import (
"github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/serf/coordinate"
"github.com/miekg/dns"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
@ -151,7 +150,7 @@ func TestEncodeKVasRFC1464(t *testing.T) {
for _, test := range tests {
answer := encodeKVasRFC1464(test.key, test.value)
verify.Values(t, "internalForm", answer, test.internalForm)
require.Equal(t, test.internalForm, answer)
}
}
@ -445,7 +444,7 @@ func TestDNSCycleRecursorCheck(t *testing.T) {
A: []byte{0xAC, 0x15, 0x2D, 0x43}, // 172 , 21, 45, 67
},
}
verify.Values(t, "Answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer)
}
func TestDNSCycleRecursorCheckAllFail(t *testing.T) {
t.Parallel()
@ -474,7 +473,7 @@ func TestDNSCycleRecursorCheckAllFail(t *testing.T) {
client := new(dns.Client)
in, _, _ := client.Exchange(m, agent.DNSAddr())
//Verify if we hit SERVFAIL from Consul
verify.Values(t, "Answer", in.Rcode, dns.RcodeServerFailure)
require.Equal(t, dns.RcodeServerFailure, in.Rcode)
}
func TestDNS_NodeLookup_CNAME(t *testing.T) {
t.Parallel()
@ -532,7 +531,7 @@ func TestDNS_NodeLookup_CNAME(t *testing.T) {
Txt: []string{"my_txt_value"},
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer)
}
func TestDNS_NodeLookup_TXT(t *testing.T) {
@ -665,7 +664,7 @@ func TestDNS_NodeLookup_ANY(t *testing.T) {
Txt: []string{"key=value"},
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer)
}
func TestDNS_NodeLookup_ANY_DontSuppressTXT(t *testing.T) {
@ -706,7 +705,7 @@ func TestDNS_NodeLookup_ANY_DontSuppressTXT(t *testing.T) {
Txt: []string{"key=value"},
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer)
}
func TestDNS_NodeLookup_A_SuppressTXT(t *testing.T) {
@ -739,7 +738,7 @@ func TestDNS_NodeLookup_A_SuppressTXT(t *testing.T) {
A: []byte{0x7f, 0x0, 0x0, 0x1}, // 127.0.0.1
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer)
// ensure TXT RR suppression
require.Len(t, in.Extra, 0)
@ -1581,14 +1580,14 @@ func TestDNS_ServiceLookupWithInternalServiceAddress(t *testing.T) {
Target: "foo.node.dc1.consul.",
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer, "answer")
wantExtra := []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: "foo.node.dc1.consul.", Rrtype: 0x1, Class: 0x1, Rdlength: 0x4},
A: []byte{0x7f, 0x0, 0x0, 0x1}, // 127.0.0.1
},
}
verify.Values(t, "extra", in.Extra, wantExtra)
require.Equal(t, wantExtra, in.Extra, "extra")
}
func TestDNS_ConnectServiceLookup(t *testing.T) {
@ -1975,7 +1974,7 @@ func TestDNS_NSRecords(t *testing.T) {
Ns: "server1.node.dc1.consul.",
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer, "answer")
wantExtra := []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: "server1.node.dc1.consul.", Rrtype: dns.TypeA, Class: dns.ClassINET, Rdlength: 0x4, Ttl: 0},
@ -1983,7 +1982,7 @@ func TestDNS_NSRecords(t *testing.T) {
},
}
verify.Values(t, "extra", in.Extra, wantExtra)
require.Equal(t, wantExtra, in.Extra, "extra")
}
func TestDNS_NSRecords_IPV6(t *testing.T) {
@ -2011,7 +2010,7 @@ func TestDNS_NSRecords_IPV6(t *testing.T) {
Ns: "server1.node.dc1.consul.",
},
}
verify.Values(t, "answer", in.Answer, wantAnswer)
require.Equal(t, wantAnswer, in.Answer, "answer")
wantExtra := []dns.RR{
&dns.AAAA{
Hdr: dns.RR_Header{Name: "server1.node.dc1.consul.", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Rdlength: 0x10, Ttl: 0},
@ -2019,7 +2018,7 @@ func TestDNS_NSRecords_IPV6(t *testing.T) {
},
}
verify.Values(t, "extra", in.Extra, wantExtra)
require.Equal(t, wantExtra, in.Extra, "extra")
}
@ -5670,7 +5669,7 @@ func TestDNS_ServiceLookup_MetaTXT(t *testing.T) {
Txt: []string{"key=value"},
},
}
verify.Values(t, "additional", in.Extra, wantAdditional)
require.Equal(t, wantAdditional, in.Extra)
}
func TestDNS_ServiceLookup_SuppressTXT(t *testing.T) {
@ -5713,7 +5712,7 @@ func TestDNS_ServiceLookup_SuppressTXT(t *testing.T) {
A: []byte{0x7f, 0x0, 0x0, 0x1}, // 127.0.0.1
},
}
verify.Values(t, "additional", in.Extra, wantAdditional)
require.Equal(t, wantAdditional, in.Extra)
}
func TestDNS_AddressLookup(t *testing.T) {

View File

@ -5,7 +5,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/serf/serf"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestBuild(t *testing.T) {
@ -69,7 +69,7 @@ func TestBuild(t *testing.T) {
if wantErr := tt.err; gotErr != wantErr {
t.Fatalf("got %v want %v", gotErr, wantErr)
}
verify.Values(t, "", ver, tt.ver)
require.Equal(t, tt.ver, ver)
})
}
}

View File

@ -8,7 +8,7 @@ import (
fuzz "github.com/google/gofuzz"
"github.com/hashicorp/consul/api"
"github.com/mitchellh/reflectwalk"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestCheckDefinition_Defaults(t *testing.T) {
@ -111,5 +111,5 @@ func TestCheckDefinitionToCheckType(t *testing.T) {
TTL: 3 * time.Second,
DeregisterCriticalServiceAfter: 4 * time.Second,
}
verify.Values(t, "", got.CheckType(), want)
require.Equal(t, want, got.CheckType())
}

View File

@ -6,7 +6,6 @@ import (
"testing"
"time"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
@ -47,12 +46,13 @@ func TestAgentStructs_CheckTypes(t *testing.T) {
{&CheckType{TTL: 20 * time.Second, Interval: 10 * time.Second}, fmt.Errorf("Interval and TTL cannot both be specified"), "Interval and TTL both set"},
}
for _, tc := range cases {
svc.Check = *tc.in
checks, err := svc.CheckTypes()
verify.Values(t, tc.desc, err.Error(), tc.err.Error())
if len(checks) != 0 {
t.Fatalf("bad: %#v", svc)
}
t.Run(tc.desc, func(t *testing.T) {
svc.Check = *tc.in
checks, err := svc.CheckTypes()
require.Error(t, err, tc.err.Error())
require.Len(t, checks, 0)
})
}
}

View File

@ -11,7 +11,7 @@ import (
"time"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestStringHash(t *testing.T) {
@ -121,7 +121,7 @@ func TestDurationFixer(t *testing.T) {
}
// Ensure we only processed the intended fieldnames
verify.Values(t, "", obj, expected)
require.Equal(t, expected, obj)
}
// helperProcessSentinel is a sentinel value that is put as the first

1
go.mod
View File

@ -68,7 +68,6 @@ require (
github.com/mitchellh/mapstructure v1.2.3
github.com/mitchellh/pointerstructure v1.0.0
github.com/mitchellh/reflectwalk v1.0.1
github.com/pascaldekloe/goe v0.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.8.1
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect

View File

@ -6,15 +6,15 @@ import (
"time"
"github.com/hashicorp/serf/coordinate"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)
func TestRTT_ComputeDistance(t *testing.T) {
tests := []struct {
desc string
a *coordinate.Coordinate
b *coordinate.Coordinate
dist float64
desc string
a *coordinate.Coordinate
b *coordinate.Coordinate
expected float64
}{
{
"10 ms",
@ -61,8 +61,8 @@ func TestRTT_ComputeDistance(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
dist := ComputeDistance(tt.a, tt.b)
verify.Values(t, "", dist, tt.dist)
actual := ComputeDistance(tt.a, tt.b)
require.Equal(t, tt.expected, actual)
})
}
}
@ -146,8 +146,9 @@ func TestRTT_Intersect(t *testing.T) {
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
r1, r2 := tt.a.Intersect(tt.b)
verify.Values(t, "", r1, tt.c1)
verify.Values(t, "", r2, tt.c2)
require.Equal(t, tt.c1, r1)
require.Equal(t, tt.c2, r2)
})
}
}

View File

@ -1,5 +0,0 @@
To the extent possible under law, Pascal S. de Kloe has waived all
copyright and related or neighboring rights to Go Enterprice. This
work is published from The Netherlands.
https://creativecommons.org/publicdomain/zero/1.0/legalcode

View File

@ -1,166 +0,0 @@
package verify
import (
"fmt"
"reflect"
"strings"
"testing"
)
// Values verifies that got has all the content, and only the content, defined by want.
// Note that NaN always results in a mismatch.
func Values(tb testing.TB, name string, got, want interface{}) (ok bool) {
t := travel{}
t.values(reflect.ValueOf(got), reflect.ValueOf(want), nil)
fail := t.report(name)
if fail != "" {
tb.Helper()
tb.Error(fail)
return false
}
return true
}
func (t *travel) values(got, want reflect.Value, path []*segment) {
if !want.IsValid() {
if got.IsValid() {
t.differ(path, "Unwanted %s", got.Type())
}
return
}
if !got.IsValid() {
t.differ(path, "Missing %s", want.Type())
return
}
if got.Type() != want.Type() {
t.differ(path, "Got type %s, want %s", got.Type(), want.Type())
return
}
switch got.Kind() {
case reflect.Struct:
seg := &segment{format: "/%s"}
path = append(path, seg)
var unexp []string
for i, n := 0, got.NumField(); i < n; i++ {
field := got.Type().Field(i)
if field.PkgPath != "" {
unexp = append(unexp, field.Name)
} else {
seg.x = field.Name
t.values(got.Field(i), want.Field(i), path)
}
}
path = path[:len(path)-1]
if len(unexp) != 0 && !reflect.DeepEqual(got.Interface(), want.Interface()) {
t.differ(path, "Type %s with unexported fields %q not equal", got.Type(), unexp)
}
case reflect.Slice, reflect.Array:
n := got.Len()
if n != want.Len() {
t.differ(path, "Got %d elements, want %d", n, want.Len())
return
}
seg := &segment{format: "[%d]"}
path = append(path, seg)
for i := 0; i < n; i++ {
seg.x = i
t.values(got.Index(i), want.Index(i), path)
}
path = path[:len(path)-1]
case reflect.Ptr:
if got.Pointer() != want.Pointer() {
t.values(got.Elem(), want.Elem(), path)
}
case reflect.Interface:
t.values(got.Elem(), want.Elem(), path)
case reflect.Map:
seg := &segment{}
path = append(path, seg)
for _, key := range want.MapKeys() {
applyKeySeg(seg, key)
t.values(got.MapIndex(key), want.MapIndex(key), path)
}
for _, key := range got.MapKeys() {
v := want.MapIndex(key)
if v.IsValid() {
continue
}
applyKeySeg(seg, key)
t.values(got.MapIndex(key), v, path)
}
path = path[:len(path)-1]
case reflect.Func:
if !(got.IsNil() && want.IsNil()) {
t.differ(path, "Can't compare functions")
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if a, b := got.Int(), want.Int(); a != b {
if a < 0xA && a > -0xA && b < 0xA && b > -0xA {
t.differ(path, fmt.Sprintf("Got %d, want %d", a, b))
} else {
t.differ(path, fmt.Sprintf("Got %d (0x%x), want %d (0x%x)", a, a, b, b))
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if a, b := got.Uint(), want.Uint(); a != b {
if a < 0xA && b < 0xA {
t.differ(path, fmt.Sprintf("Got %d, want %d", a, b))
} else {
t.differ(path, fmt.Sprintf("Got %d (0x%x), want %d (0x%x)", a, a, b, b))
}
}
case reflect.String:
if a, b := got.String(), want.String(); a != b {
t.differ(path, differMsg(a, b))
}
default:
if a, b := got.Interface(), want.Interface(); a != b {
t.differ(path, fmt.Sprintf("Got %v, want %v", a, b))
}
}
}
func applyKeySeg(dst *segment, key reflect.Value) {
if key.Kind() == reflect.String {
dst.format = "[%q]"
} else {
dst.format = "[%v]"
}
dst.x = key.Interface()
}
func differMsg(got, want string) string {
if len(got) < 9 || len(want) < 9 {
return fmt.Sprintf("Got %q, want %q", got, want)
}
got, want = fmt.Sprintf("%q", got), fmt.Sprintf("%q", want)
// find first character which differs
var i int
a, b := []rune(got), []rune(want)
for i = 0; i < len(a); i++ {
if i >= len(b) || a[i] != b[i] {
break
}
}
return fmt.Sprintf("Got %s, want %s\n %s^", got, want, strings.Repeat(" ", i))
}

View File

@ -1,68 +0,0 @@
// Package verify offers convenience routenes for content verification.
package verify
import (
"bytes"
"fmt"
"strings"
)
// travel is the verification state
type travel struct {
diffs []differ
}
// differ is a verification failure.
type differ struct {
// path is the expression to the content.
path string
// msg has a reason.
msg string
}
// segment is a differ.path component used for lazy formatting.
type segment struct {
format string
x interface{}
}
func (t *travel) differ(path []*segment, msg string, args ...interface{}) {
var buf bytes.Buffer
for _, s := range path {
buf.WriteString(fmt.Sprintf(s.format, s.x))
}
t.diffs = append(t.diffs, differ{
msg: fmt.Sprintf(msg, args...),
path: buf.String(),
})
}
func (t *travel) report(name string) string {
if len(t.diffs) == 0 {
return ""
}
var buf bytes.Buffer
buf.WriteString("verification for ")
buf.WriteString(name)
buf.WriteByte(':')
for _, d := range t.diffs {
buf.WriteByte('\n')
if d.path != "" {
buf.WriteString(d.path)
buf.WriteString(": ")
}
lines := strings.Split(d.msg, "\n")
buf.WriteString(lines[0])
for _, l := range lines[1:] {
buf.WriteByte('\n')
buf.WriteString(strings.Repeat(" ", len(d.path)+2))
buf.WriteString(l)
}
}
return buf.String()
}

2
vendor/modules.txt vendored
View File

@ -321,8 +321,6 @@ github.com/modern-go/reflect2
github.com/nicolai86/scaleway-sdk
# github.com/packethost/packngo v0.1.1-0.20180711074735-b9cb5096f54c
github.com/packethost/packngo
# github.com/pascaldekloe/goe v0.1.0
github.com/pascaldekloe/goe/verify
# github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/patrickmn/go-cache
# github.com/pierrec/lz4 v2.0.5+incompatible