From 4264c5a26214e7a1cdbde0eeaec76b9b8ff75a5d Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Mon, 28 Aug 2023 10:19:13 -0400 Subject: [PATCH] Oss changes for activity log tests (#22231) --- vault/activity_log.go | 2 ++ vault/activity_log_test.go | 44 ++++++++++++++---------------- vault/activity_log_testing_util.go | 31 +++++++++++++-------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/vault/activity_log.go b/vault/activity_log.go index c5214da19e..d206985cc0 100644 --- a/vault/activity_log.go +++ b/vault/activity_log.go @@ -209,6 +209,8 @@ type ActivityLogCoreConfig struct { // Clock holds a custom clock to modify time.Now, time.Ticker, time.Timer. // If nil, the default functions from the time package are used Clock timeutil.Clock + + DisableInvalidation bool } // NewActivityLog creates an activity log. diff --git a/vault/activity_log_test.go b/vault/activity_log_test.go index 226cc01a03..53f6fbfc2f 100644 --- a/vault/activity_log_test.go +++ b/vault/activity_log_test.go @@ -20,19 +20,17 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" - - "github.com/hashicorp/go-uuid" - "github.com/axiomhq/hyperloglog" "github.com/go-test/deep" "github.com/golang/protobuf/proto" + "github.com/hashicorp/go-uuid" "github.com/hashicorp/vault/helper/constants" "github.com/hashicorp/vault/helper/namespace" "github.com/hashicorp/vault/helper/timeutil" "github.com/hashicorp/vault/sdk/logical" "github.com/hashicorp/vault/vault/activity" "github.com/mitchellh/mapstructure" + "github.com/stretchr/testify/require" ) // TestActivityLog_Creation calls AddEntityToFragment and verifies that it appears correctly in a.fragment. @@ -1326,9 +1324,9 @@ func TestActivityLog_loadCurrentClientSegment(t *testing.T) { t.Errorf("bad data loaded. expected: %v, got: %v for path %q", tc.entities.Clients, currentEntities, tc.path) } - activeClients := core.GetActiveClients() - if !ActiveEntitiesEqual(activeClients, tc.entities.Clients) { - t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v for path %q", tc.entities.Clients, activeClients, tc.path) + activeClients := core.GetActiveClientsList() + if err := ActiveEntitiesEqual(activeClients, tc.entities.Clients); err != nil { + t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v for path %q: %v", tc.entities.Clients, activeClients, tc.path, err) } a.resetEntitiesInMemory(t) @@ -1421,9 +1419,9 @@ func TestActivityLog_loadPriorEntitySegment(t *testing.T) { t.Fatalf("got error loading data for %q: %v", tc.path, err) } - activeClients := core.GetActiveClients() - if !ActiveEntitiesEqual(activeClients, tc.entities.Clients) { - t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v for path %q", tc.entities.Clients, activeClients, tc.path) + activeClients := core.GetActiveClientsList() + if err := ActiveEntitiesEqual(activeClients, tc.entities.Clients); err != nil { + t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v for path %q: %v", tc.entities.Clients, activeClients, tc.path, err) } } } @@ -1647,10 +1645,10 @@ func TestActivityLog_refreshFromStoredLog(t *testing.T) { t.Errorf("bad activity token counts loaded. expected: %v got: %v", expectedTokenCounts, nsCount) } - activeClients := a.core.GetActiveClients() - if !ActiveEntitiesEqual(activeClients, expectedActive.Clients) { + activeClients := a.core.GetActiveClientsList() + if err := ActiveEntitiesEqual(activeClients, expectedActive.Clients); err != nil { // we expect activeClients to be loaded for the entire month - t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v", expectedActive.Clients, activeClients) + t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v: %v", expectedActive.Clients, activeClients, err) } } @@ -1691,10 +1689,10 @@ func TestActivityLog_refreshFromStoredLogWithBackgroundLoadingCancelled(t *testi t.Errorf("bad activity token counts loaded. expected: %v got: %v", expectedTokenCounts, nsCount) } - activeClients := a.core.GetActiveClients() - if !ActiveEntitiesEqual(activeClients, expected.Clients) { + activeClients := a.core.GetActiveClientsList() + if err := ActiveEntitiesEqual(activeClients, expected.Clients); err != nil { // we only expect activeClients to be loaded for the newest segment (for the current month) - t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v", expected.Clients, activeClients) + t.Error(err) } } @@ -1738,9 +1736,9 @@ func TestActivityLog_refreshFromStoredLogNoTokens(t *testing.T) { // we expect all segments for the current month to be loaded t.Errorf("bad activity entity logs loaded. expected: %v got: %v", expectedCurrent, currentEntities) } - activeClients := a.core.GetActiveClients() - if !ActiveEntitiesEqual(activeClients, expectedActive.Clients) { - t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v", expectedActive.Clients, activeClients) + activeClients := a.core.GetActiveClientsList() + if err := ActiveEntitiesEqual(activeClients, expectedActive.Clients); err != nil { + t.Error(err) } // we expect no tokens @@ -1773,7 +1771,7 @@ func TestActivityLog_refreshFromStoredLogNoEntities(t *testing.T) { if len(currentEntities.Clients) > 0 { t.Errorf("expected no current entity segment to be loaded. got: %v", currentEntities) } - activeClients := a.core.GetActiveClients() + activeClients := a.core.GetActiveClientsList() if len(activeClients) > 0 { t.Errorf("expected no active entity segment to be loaded. got: %v", activeClients) } @@ -1852,10 +1850,10 @@ func TestActivityLog_refreshFromStoredLogPreviousMonth(t *testing.T) { t.Errorf("bad activity token counts loaded. expected: %v got: %v", expectedTokenCounts, nsCount) } - activeClients := a.core.GetActiveClients() - if !ActiveEntitiesEqual(activeClients, expectedActive.Clients) { + activeClients := a.core.GetActiveClientsList() + if err := ActiveEntitiesEqual(activeClients, expectedActive.Clients); err != nil { // we expect activeClients to be loaded for the entire month - t.Errorf("bad data loaded into active entities. expected only set of EntityID from %v in %v", expectedActive.Clients, activeClients) + t.Error(err) } } diff --git a/vault/activity_log_testing_util.go b/vault/activity_log_testing_util.go index 1c4d6474fb..600da787c0 100644 --- a/vault/activity_log_testing_util.go +++ b/vault/activity_log_testing_util.go @@ -9,9 +9,12 @@ import ( "math/rand" "testing" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/vault/helper/constants" "github.com/hashicorp/vault/sdk/logical" "github.com/hashicorp/vault/vault/activity" + "google.golang.org/protobuf/testing/protocmp" ) // InjectActivityLogDataThisMonth populates the in-memory client store @@ -70,6 +73,16 @@ func (c *Core) GetActiveClients() map[string]*activity.EntityRecord { return out } +func (c *Core) GetActiveClientsList() []*activity.EntityRecord { + out := []*activity.EntityRecord{} + + for _, v := range c.GetActiveClients() { + out = append(out, v) + } + + return out +} + // GetCurrentEntities returns the current entity activity log func (a *ActivityLog) GetCurrentEntities() *activity.EntityActivityLog { a.l.RLock() @@ -175,18 +188,14 @@ func (a *ActivityLog) ExpectCurrentSegmentRefreshed(t *testing.T, expectedStart } // ActiveEntitiesEqual checks that only the set of `test` exists in `active` -func ActiveEntitiesEqual(active map[string]*activity.EntityRecord, test []*activity.EntityRecord) bool { - if len(active) != len(test) { - return false +func ActiveEntitiesEqual(active []*activity.EntityRecord, test []*activity.EntityRecord) error { + opts := []cmp.Option{protocmp.Transform(), cmpopts.SortSlices(func(x, y *activity.EntityRecord) bool { + return x.ClientID < y.ClientID + })} + if diff := cmp.Diff(active, test, opts...); len(diff) > 0 { + return fmt.Errorf("entity record mismatch: %v", diff) } - - for _, ent := range test { - if _, ok := active[ent.ClientID]; !ok { - return false - } - } - - return true + return nil } // GetStartTimestamp returns the start timestamp on an activity log