mirror of
https://github.com/hashicorp/vault.git
synced 2026-05-05 12:26:34 +02:00
Oss changes for activity log tests (#22231)
This commit is contained in:
parent
479519e6df
commit
4264c5a262
@ -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.
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user