Skip to content

Commit

Permalink
backport of commit 0e227bf (#25065)
Browse files Browse the repository at this point in the history
Co-authored-by: akshya96 <87045294+akshya96@users.noreply.github.com>
  • Loading branch information
1 parent e77923f commit 41badae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
14 changes: 14 additions & 0 deletions helper/timeutil/timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ func StartOfPreviousMonth(t time.Time) time.Time {
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location()).AddDate(0, -1, 0)
}

func StartOfDay(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}

// IsCurrentDay checks if :t: is in the current day, as defined by :compare:
// generally, pass in time.Now().UTC() as :compare:
func IsCurrentDay(t, compare time.Time) bool {
thisDayStart := StartOfDay(compare)
queryDayStart := StartOfDay(t)

return queryDayStart.Equal(thisDayStart)
}

func StartOfMonth(t time.Time) time.Time {
year, month, _ := t.Date()
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
Expand Down
41 changes: 41 additions & 0 deletions helper/timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,47 @@ func TestTimeutil_IsCurrentMonth(t *testing.T) {
}
}

// TestTimeutil_IsCurrentDay checks if the test times equals the current day or not.
func TestTimeutil_IsCurrentDay(t *testing.T) {
now := time.Now()
testCases := []struct {
input time.Time
expected bool
}{
{
input: now,
expected: true,
},
{
input: StartOfDay(now).AddDate(0, 0, -1),
expected: false,
},
{
input: StartOfDay(now).AddDate(-1, 0, 0),
expected: false,
},
{
input: StartOfDay(now).Add(1 * time.Second),
expected: true,
},
{
input: StartOfDay(now).Add(-1 * time.Second),
expected: false,
},
{
input: StartOfDay(now).Add(86400), // a day is 86400 seconds
expected: true,
},
}

for _, tc := range testCases {
result := IsCurrentDay(tc.input, now)
if result != tc.expected {
t.Errorf("invalid result. expected %t for %v", tc.expected, tc.input)
}
}
}

func TestTimeUtil_ContiguousMonths(t *testing.T) {
testCases := []struct {
input []time.Time
Expand Down
19 changes: 10 additions & 9 deletions vault/census.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import "time"
// CensusAgent is a stub for OSS
type CensusReporter interface{}

func (c *Core) setupCensusManager() error { return nil }
func (c *Core) BillingStart() time.Time { return time.Time{} }
func (c *Core) AutomatedLicenseReportingEnabled() bool { return false }
func (c *Core) CensusAgent() CensusReporter { return nil }
func (c *Core) ReloadCensus() error { return nil }
func (c *Core) teardownCensusManager() error { return nil }
func (c *Core) StartManualCensusSnapshots() {}
func (c *Core) ManualLicenseReportingEnabled() bool { return false }
func (c *Core) ManualCensusSnapshotInterval() time.Duration { return time.Duration(0) }
func (c *Core) setupCensusManager() error { return nil }
func (c *Core) BillingStart() time.Time { return time.Time{} }
func (c *Core) AutomatedLicenseReportingEnabled() bool { return false }
func (c *Core) CensusAgent() CensusReporter { return nil }
func (c *Core) ReloadCensus() error { return nil }
func (c *Core) teardownCensusManager() error { return nil }
func (c *Core) StartManualCensusSnapshots() {}
func (c *Core) ManualLicenseReportingEnabled() bool { return false }
func (c *Core) ManualCensusSnapshotInterval() time.Duration { return time.Duration(0) }
func (c *Core) ManualCensusSnapshotRetentionTime() time.Duration { return time.Duration(0) }
2 changes: 1 addition & 1 deletion vault/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2463,7 +2463,7 @@ func (s standardUnsealStrategy) unseal(ctx context.Context, logger log.Logger, c

if !c.perfStandby {
if err := c.setupCensusManager(); err != nil {
logger.Error("skipping license reporting for nil agent", "error", err)
logger.Error("failed to instantiate the license reporting agent", "error", err)
}
}

Expand Down

0 comments on commit 41badae

Please sign in to comment.