Skip to content

Commit

Permalink
Fix unittest update wrongly inverted an assert
Browse files Browse the repository at this point in the history
Signed-off-by: Valerian Roche <valerian.roche@datadoghq.com>
  • Loading branch information
valerian-roche committed Aug 25, 2022
1 parent a36df79 commit af7a06d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
23 changes: 14 additions & 9 deletions pkg/cache/v3/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestDeltaRemoveResources(t *testing.T) {
watches := make(map[string]chan cache.DeltaResponse)
streams := make(map[string]*stream.StreamState)

// At this stage the cache is empty, so a watch is opened
for _, typ := range testTypes {
watches[typ] = make(chan cache.DeltaResponse, 1)
state := stream.NewStreamState(true, make(map[string]string))
Expand All @@ -127,13 +128,17 @@ func TestDeltaRemoveResources(t *testing.T) {
}, streams[typ], watches[typ])
}

require.NoError(t, c.SetSnapshot(context.Background(), key, fixture.snapshot()))
snapshot := fixture.snapshot()
snapshot.Resources[types.Endpoint] = cache.NewResources(fixture.version, []types.Resource{
testEndpoint,
resource.MakeEndpoint("otherCluster", 8080),
})
require.NoError(t, c.SetSnapshot(context.Background(), key, snapshot))

for _, typ := range testTypes {
t.Run(typ, func(t *testing.T) {
select {
case out := <-watches[typ]:
snapshot := fixture.snapshot()
assertResourceMapEqual(t, cache.IndexRawResourcesByName(out.(*cache.RawDeltaResponse).Resources), snapshot.GetResources(typ))
nextVersionMap := out.GetNextVersionMap()
streams[typ].SetResourceVersions(nextVersionMap)
Expand All @@ -158,21 +163,21 @@ func TestDeltaRemoveResources(t *testing.T) {

assert.Equal(t, len(testTypes), c.GetStatusInfo(key).GetNumDeltaWatches(), "watches should be created for the latest version")

// set a partially versioned snapshot with no endpoints
// set a partially versioned snapshot with only one endpoint
snapshot2 := fixture.snapshot()
snapshot2.Resources[types.Endpoint] = cache.NewResources(fixture.version2, []types.Resource{})
snapshot2.Resources[types.Endpoint] = cache.NewResources(fixture.version2, []types.Resource{
testEndpoint, // this cluster is not changed, we do not expect it back in "resources"
})
require.NoError(t, c.SetSnapshot(context.Background(), key, snapshot2))

// validate response for endpoints
select {
case out := <-watches[testTypes[0]]:
snapshot2 := fixture.snapshot()
snapshot2.Resources[types.Endpoint] = cache.NewResources(fixture.version2, []types.Resource{})
assertResourceMapEqual(t, cache.IndexRawResourcesByName(out.(*cache.RawDeltaResponse).Resources), snapshot2.GetResources(rsrc.EndpointType))
assert.Empty(t, out.(*cache.RawDeltaResponse).Resources)
assert.Equal(t, []string{"otherCluster"}, out.(*cache.RawDeltaResponse).RemovedResources)
nextVersionMap := out.GetNextVersionMap()

// make sure the version maps are different since we no longer are tracking any endpoint resources
require.Equal(t, nextVersionMap, streams[testTypes[0]].GetKnownResources(), "versionMap for the endpoint resource type did not change")
assert.NotEqual(t, nextVersionMap, streams[testTypes[0]].GetKnownResources(), "versionMap for the endpoint resource type did not change")
case <-time.After(time.Second):
assert.Fail(t, "failed to receive snapshot response")
}
Expand Down
20 changes: 16 additions & 4 deletions pkg/cache/v3/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,22 @@ type logger struct {
t *testing.T
}

func (log logger) Debugf(format string, args ...interface{}) { log.t.Logf(format, args...) }
func (log logger) Infof(format string, args ...interface{}) { log.t.Logf(format, args...) }
func (log logger) Warnf(format string, args ...interface{}) { log.t.Logf(format, args...) }
func (log logger) Errorf(format string, args ...interface{}) { log.t.Logf(format, args...) }
func (log logger) Debugf(format string, args ...interface{}) {
log.t.Helper()
log.t.Logf(format, args...)
}
func (log logger) Infof(format string, args ...interface{}) {
log.t.Helper()
log.t.Logf(format, args...)
}
func (log logger) Warnf(format string, args ...interface{}) {
log.t.Helper()
log.t.Logf(format, args...)
}
func (log logger) Errorf(format string, args ...interface{}) {
log.t.Helper()
log.t.Logf(format, args...)
}

func TestSnapshotCacheWithTTL(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit af7a06d

Please sign in to comment.