Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use testify instead of t.Fatal or t.Error #1040

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 19 additions & 36 deletions pkg/cache/v3/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ func TestSnapshotCacheDeltaWatch(t *testing.T) {
}, stream.NewStreamState(true, nil), watches[typ])
}

if err := c.SetSnapshot(context.Background(), key, fixture.snapshot()); err != nil {
t.Fatal(err)
}
require.NoError(t, c.SetSnapshot(context.Background(), key, fixture.snapshot()))

versionMap := make(map[string]map[string]string)
for _, typ := range testTypes {
Expand Down Expand Up @@ -81,19 +79,15 @@ func TestSnapshotCacheDeltaWatch(t *testing.T) {
}, state, watches[typ])
}

if count := c.GetStatusInfo(key).GetNumDeltaWatches(); count != len(testTypes) {
t.Errorf("watches should be created for the latest version, saw %d watches expected %d", count, len(testTypes))
}
count := c.GetStatusInfo(key).GetNumDeltaWatches()
assert.Lenf(t, testTypes, count, "watches should be created for the latest version, saw %d watches expected %d", count, len(testTypes))

// set partially-versioned snapshot
snapshot2 := fixture.snapshot()
snapshot2.Resources[types.Endpoint] = cache.NewResources(fixture.version2, []types.Resource{resource.MakeEndpoint(clusterName, 9090)})
if err := c.SetSnapshot(context.Background(), key, snapshot2); err != nil {
t.Fatal(err)
}
if count := c.GetStatusInfo(key).GetNumDeltaWatches(); count != len(testTypes)-1 {
t.Errorf("watches should be preserved for all but one, got: %d open watches instead of the expected %d open watches", count, len(testTypes)-1)
}
require.NoError(t, c.SetSnapshot(context.Background(), key, snapshot2))
count = c.GetStatusInfo(key).GetNumDeltaWatches()
assert.Equalf(t, count, len(testTypes)-1, "watches should be preserved for all but one, got: %d open watches instead of the expected %d open watches", count, len(testTypes)-1)

// validate response for endpoints
select {
Expand Down Expand Up @@ -127,9 +121,7 @@ func TestDeltaRemoveResources(t *testing.T) {
}, *streams[typ], watches[typ])
}

if err := c.SetSnapshot(context.Background(), key, fixture.snapshot()); err != nil {
t.Fatal(err)
}
require.NoError(t, c.SetSnapshot(context.Background(), key, fixture.snapshot()))

for _, typ := range testTypes {
t.Run(typ, func(t *testing.T) {
Expand Down Expand Up @@ -157,16 +149,13 @@ func TestDeltaRemoveResources(t *testing.T) {
}, *streams[typ], watches[typ])
}

if count := c.GetStatusInfo(key).GetNumDeltaWatches(); count != len(testTypes) {
t.Errorf("watches should be created for the latest version, saw %d watches expected %d", count, len(testTypes))
}
count := c.GetStatusInfo(key).GetNumDeltaWatches()
assert.Lenf(t, testTypes, count, "watches should be created for the latest version, saw %d watches expected %d", count, len(testTypes))

// set a partially versioned snapshot with no endpoints
snapshot2 := fixture.snapshot()
snapshot2.Resources[types.Endpoint] = cache.NewResources(fixture.version2, []types.Resource{})
if err := c.SetSnapshot(context.Background(), key, snapshot2); err != nil {
t.Fatal(err)
}
require.NoError(t, c.SetSnapshot(context.Background(), key, snapshot2))

// validate response for endpoints
select {
Expand Down Expand Up @@ -196,13 +185,10 @@ func TestConcurrentSetDeltaWatch(t *testing.T) {
responses := make(chan cache.DeltaResponse, 1)
if i < 25 {
snap, err := cache.NewSnapshot("", map[rsrc.Type][]types.Resource{})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
snap.Resources[types.Endpoint] = cache.NewResources(version, []types.Resource{resource.MakeEndpoint(clusterName, uint32(i))})
if err := c.SetSnapshot(context.Background(), key, snap); err != nil {
t.Fatalf("snapshot failed: %s", err)
}
err = c.SetSnapshot(context.Background(), key, snap)
require.NoErrorf(t, err, "snapshot failed")
} else {
cancel := c.CreateDeltaWatch(&discovery.DeltaDiscoveryRequest{
Node: &core.Node{
Expand Down Expand Up @@ -282,17 +268,14 @@ func TestSnapshotCacheDeltaWatchCancel(t *testing.T) {
cancel()
}
// c.GetStatusKeys() should return at least 1 because we register a node ID with the above watch creations
if keys := c.GetStatusKeys(); len(keys) == 0 {
t.Errorf("expected to see a status info registered for watch, saw %d entries", len(keys))
}
keys := c.GetStatusKeys()
assert.NotEmptyf(t, keys, "expected to see a status info registered for watch, saw %d entries", len(keys))

for _, typ := range testTypes {
if count := c.GetStatusInfo(key).GetNumDeltaWatches(); count > 0 {
t.Errorf("watches should be released for %s", typ)
}
count := c.GetStatusInfo(key).GetNumDeltaWatches()
assert.LessOrEqualf(t, count, 0, "watches should be released for %s", typ)
}

if s := c.GetStatusInfo("missing"); s != nil {
t.Errorf("should not return a status for unknown key: got %#v", s)
}
s := c.GetStatusInfo("missing")
assert.Nilf(t, s, "should not return a status for unknown key: got %#v", s)
}
94 changes: 27 additions & 67 deletions pkg/cache/v3/linear_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,17 @@ func testResource(s string) types.Resource {
func verifyResponse(t *testing.T, ch <-chan Response, version string, num int) {
t.Helper()
r := <-ch
if r.GetRequest().GetTypeUrl() != testType {
t.Errorf("unexpected empty request type URL: %q", r.GetRequest().GetTypeUrl())
}
if r.GetContext() == nil {
t.Errorf("unexpected empty response context")
}
assert.Equalf(t, testType, r.GetRequest().GetTypeUrl(), "unexpected empty request type URL: %q", r.GetRequest().GetTypeUrl())
assert.NotNilf(t, r.GetContext(), "unexpected empty response context")
out, err := r.GetDiscoveryResponse()
if err != nil {
t.Fatal(err)
}
if out.GetVersionInfo() == "" {
t.Error("unexpected response empty version")
}
if n := len(out.GetResources()); n != num {
t.Errorf("unexpected number of responses: got %d, want %d", n, num)
}
if version != "" && out.GetVersionInfo() != version {
t.Errorf("unexpected version: got %q, want %q", out.GetVersionInfo(), version)
}
if out.GetTypeUrl() != testType {
t.Errorf("unexpected type URL: %q", out.GetTypeUrl())
require.NoError(t, err)
assert.NotEqualf(t, "", out.GetVersionInfo(), "unexpected response empty version")
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved
n := len(out.GetResources())
assert.Equalf(t, n, num, "unexpected number of responses: got %d, want %d", n, num)
if version != "" {
assert.Equalf(t, out.GetVersionInfo(), version, "unexpected version: got %q, want %q", out.GetVersionInfo(), version)
}
assert.Equalf(t, testType, out.GetTypeUrl(), "unexpected type URL: %q", out.GetTypeUrl())
}

type resourceInfo struct {
Expand All @@ -73,16 +62,10 @@ type resourceInfo struct {
func validateDeltaResponse(t *testing.T, resp DeltaResponse, resources []resourceInfo, deleted []string) {
t.Helper()

if resp.GetDeltaRequest().GetTypeUrl() != testType {
t.Errorf("unexpected empty request type URL: %q", resp.GetDeltaRequest().GetTypeUrl())
}
assert.Equalf(t, testType, resp.GetDeltaRequest().GetTypeUrl(), "unexpected empty request type URL: %q", resp.GetDeltaRequest().GetTypeUrl())
out, err := resp.GetDeltaDiscoveryResponse()
if err != nil {
t.Fatal(err)
}
if len(out.GetResources()) != len(resources) {
t.Errorf("unexpected number of responses: got %d, want %d", len(out.GetResources()), len(resources))
}
require.NoError(t, err)
assert.Equalf(t, len(out.GetResources()), len(resources), "unexpected number of responses: got %d, want %d", len(out.GetResources()), len(resources))
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved
for _, r := range resources {
found := false
for _, r1 := range out.GetResources() {
Expand All @@ -95,16 +78,10 @@ func validateDeltaResponse(t *testing.T, resp DeltaResponse, resources []resourc
break
}
}
if !found {
t.Errorf("resource with name %q not found in response", r.name)
}
}
if out.GetTypeUrl() != testType {
t.Errorf("unexpected type URL: %q", out.GetTypeUrl())
}
if len(out.GetRemovedResources()) != len(deleted) {
t.Errorf("unexpected number of removed resurces: got %d, want %d", len(out.GetRemovedResources()), len(deleted))
assert.Truef(t, found, "resource with name %q not found in response", r.name)
}
assert.Equalf(t, testType, out.GetTypeUrl(), "unexpected type URL: %q", out.GetTypeUrl())
assert.Equalf(t, len(out.GetRemovedResources()), len(deleted), "unexpected number of removed resurces: got %d, want %d", len(out.GetRemovedResources()), len(deleted))
for _, r := range deleted {
found := false
for _, rr := range out.GetRemovedResources() {
Expand All @@ -113,9 +90,7 @@ func validateDeltaResponse(t *testing.T, resp DeltaResponse, resources []resourc
break
}
}
if !found {
t.Errorf("Expected resource %s to be deleted", r)
}
assert.Truef(t, found, "Expected resource %s to be deleted", r)
}
}

Expand All @@ -134,32 +109,25 @@ func verifyDeltaResponse(t *testing.T, ch <-chan DeltaResponse, resources []reso

func checkWatchCount(t *testing.T, c *LinearCache, name string, count int) {
t.Helper()
if i := c.NumWatches(name); i != count {
t.Errorf("unexpected number of watches for %q: got %d, want %d", name, i, count)
}
i := c.NumWatches(name)
assert.Equalf(t, i, count, "unexpected number of watches for %q: got %d, want %d", name, i, count)
}

func checkDeltaWatchCount(t *testing.T, c *LinearCache, count int) {
t.Helper()
if i := c.NumDeltaWatches(); i != count {
t.Errorf("unexpected number of delta watches: got %d, want %d", i, count)
}
i := c.NumDeltaWatches()
assert.Equalf(t, i, count, "unexpected number of delta watches: got %d, want %d", i, count)
}

func checkVersionMapNotSet(t *testing.T, c *LinearCache) {
t.Helper()
if c.versionMap != nil {
t.Errorf("version map is set on the cache with %d elements", len(c.versionMap))
}
assert.Nilf(t, c.versionMap, "version map is set on the cache with %d elements", len(c.versionMap))
}

func checkVersionMapSet(t *testing.T, c *LinearCache) {
t.Helper()
if c.versionMap == nil {
t.Errorf("version map is not set on the cache")
} else if len(c.versionMap) != len(c.resources) {
t.Errorf("version map has the wrong number of elements: %d instead of %d expected", len(c.versionMap), len(c.resources))
}
assert.NotNilf(t, c.versionMap, "version map is not set on the cache")
assert.Equalf(t, len(c.versionMap), len(c.resources), "version map has the wrong number of elements: %d instead of %d expected", len(c.versionMap), len(c.resources))
}

func mustBlock(t *testing.T, w <-chan Response) {
Expand All @@ -180,9 +148,7 @@ func mustBlockDelta(t *testing.T, w <-chan DeltaResponse) {

func hashResource(t *testing.T, resource types.Resource) string {
marshaledResource, err := MarshalResource(resource)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
v := HashResource(marshaledResource)
if v == "" {
t.Fatal(errors.New("failed to build resource version"))
Expand Down Expand Up @@ -213,17 +179,13 @@ func TestLinearCornerCases(t *testing.T) {
streamState := stream.NewStreamState(false, map[string]string{})
c := NewLinearCache(testType)
err := c.UpdateResource("a", nil)
if err == nil {
t.Error("expected error on nil resource")
}
require.Errorf(t, err, "expected error on nil resource")
// create an incorrect type URL request
w := make(chan Response, 1)
c.CreateWatch(&Request{TypeUrl: "test"}, streamState, w)
select {
case r := <-w:
if r != nil {
t.Error("response should be nil")
}
assert.Nilf(t, r, "response should be nil")
default:
t.Error("should receive nil response")
}
Expand Down Expand Up @@ -325,9 +287,7 @@ func TestLinearGetResources(t *testing.T) {

resources := c.GetResources()

if !reflect.DeepEqual(expectedResources, resources) {
t.Errorf("resources are not equal. got: %v want: %v", resources, expectedResources)
}
assert.Truef(t, reflect.DeepEqual(expectedResources, resources), "resources are not equal. got: %v want: %v", resources, expectedResources)
}

func TestLinearVersionPrefix(t *testing.T) {
Expand Down
63 changes: 24 additions & 39 deletions pkg/cache/v3/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ func TestValidate(t *testing.T) {
}},
}

if err := invalidRoute.Validate(); err == nil {
t.Error("expected an error")
}
if err := invalidRoute.GetVirtualHosts()[0].Validate(); err == nil {
t.Error("expected an error")
}
err := invalidRoute.Validate()
require.Errorf(t, err, "expected an error")
err = invalidRoute.GetVirtualHosts()[0].Validate()
assert.Errorf(t, err, "expected an error")
}

type customResource struct {
Expand All @@ -96,33 +94,24 @@ func (cs *customResource) GetName() string { return customName }
var _ types.ResourceWithName = &customResource{}

func TestGetResourceName(t *testing.T) {
if name := cache.GetResourceName(testEndpoint); name != clusterName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testEndpoint, name, clusterName)
}
if name := cache.GetResourceName(testCluster); name != clusterName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testCluster, name, clusterName)
}
if name := cache.GetResourceName(testRoute); name != routeName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testRoute, name, routeName)
}
if name := cache.GetResourceName(testScopedRoute); name != scopedRouteName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testScopedRoute, name, scopedRouteName)
}
if name := cache.GetResourceName(testVirtualHost); name != virtualHostName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testVirtualHost, name, virtualHostName)
}
if name := cache.GetResourceName(testListener); name != listenerName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testListener, name, listenerName)
}
if name := cache.GetResourceName(testRuntime); name != runtimeName {
t.Errorf("GetResourceName(%v) => got %q, want %q", testRuntime, name, runtimeName)
}
if name := cache.GetResourceName(&customResource{}); name != customName {
t.Errorf("GetResourceName(nil) => got %q, want %q", name, customName)
}
if name := cache.GetResourceName(nil); name != "" {
t.Errorf("GetResourceName(nil) => got %q, want none", name)
}
name := cache.GetResourceName(testEndpoint)
assert.Equalf(t, clusterName, name, "GetResourceName(%v) => got %q, want %q", testEndpoint, name, clusterName)
name = cache.GetResourceName(testCluster)
assert.Equalf(t, clusterName, name, "GetResourceName(%v) => got %q, want %q", testCluster, name, clusterName)
name = cache.GetResourceName(testRoute)
assert.Equalf(t, routeName, name, "GetResourceName(%v) => got %q, want %q", testRoute, name, routeName)
name = cache.GetResourceName(testScopedRoute)
assert.Equalf(t, scopedRouteName, name, "GetResourceName(%v) => got %q, want %q", testScopedRoute, name, scopedRouteName)
name = cache.GetResourceName(testVirtualHost)
assert.Equalf(t, virtualHostName, name, "GetResourceName(%v) => got %q, want %q", testVirtualHost, name, virtualHostName)
name = cache.GetResourceName(testListener)
assert.Equalf(t, listenerName, name, "GetResourceName(%v) => got %q, want %q", testListener, name, listenerName)
name = cache.GetResourceName(testRuntime)
assert.Equalf(t, runtimeName, name, "GetResourceName(%v) => got %q, want %q", testRuntime, name, runtimeName)
name = cache.GetResourceName(&customResource{})
assert.Equalf(t, customName, name, "GetResourceName(nil) => got %q, want %q", name, customName)
name = cache.GetResourceName(nil)
assert.Emptyf(t, name, "GetResourceName(nil) => got %q, want none", name)
}

func TestGetResourceNames(t *testing.T) {
Expand Down Expand Up @@ -218,9 +207,7 @@ func TestGetResourceReferences(t *testing.T) {
}
for _, cs := range cases {
names := cache.GetResourceReferences(cache.IndexResourcesByName([]types.ResourceWithTTL{{Resource: cs.in}}))
if !reflect.DeepEqual(names, cs.out) {
t.Errorf("GetResourceReferences(%v) => got %v, want %v", cs.in, names, cs.out)
}
assert.Truef(t, reflect.DeepEqual(names, cs.out), "GetResourceReferences(%v) => got %v, want %v", cs.in, names, cs.out)
}
}

Expand All @@ -238,7 +225,5 @@ func TestGetAllResourceReferencesReturnsExpectedRefs(t *testing.T) {
resources[types.ScopedRoute] = cache.NewResources("1", []types.Resource{testScopedRoute})
actual := cache.GetAllResourceReferences(resources)

if !reflect.DeepEqual(actual, expected) {
t.Errorf("GetAllResourceReferences(%v) => got %v, want %v", resources, actual, expected)
}
assert.Truef(t, reflect.DeepEqual(actual, expected), "GetAllResourceReferences(%v) => got %v, want %v", resources, actual, expected)
}
Loading
Loading