1
0

backport of commit 0e227bf0d75bc680439ea3b8a080c0cccc700015 (#25064)

Co-authored-by: akshya96 <87045294+akshya96@users.noreply.github.com>
This commit is contained in:
hc-github-team-secure-vault-core 2024-01-24 19:34:34 -05:00 committed by GitHub
parent ac786547c2
commit 28fa6d0fd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 10 deletions

View File

@ -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())

View File

@ -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

View File

@ -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) }

View File

@ -2462,7 +2462,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)
}
}