mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-21 22:51:09 +02:00
Add auto-roll billing start date changes CE changes (#27656)
* add NormalizeToYear function and test * add ent changelog * test name typo
This commit is contained in:
parent
c5c185ff71
commit
01f78f59b1
3
changelog/27656.txt
Normal file
3
changelog/27656.txt
Normal file
@ -0,0 +1,3 @@
|
||||
```release-note:improvement
|
||||
license utilization reporting (enterprise): Auto-roll billing start date.
|
||||
```
|
@ -179,3 +179,13 @@ func (_ DefaultClock) NewTicker(d time.Duration) *time.Ticker {
|
||||
func (_ DefaultClock) NewTimer(d time.Duration) *time.Timer {
|
||||
return time.NewTimer(d)
|
||||
}
|
||||
|
||||
// NormalizeToYear returns date normalized to the latest date
|
||||
// within one year of normal. Assumes the date argument is
|
||||
// some date before normal.
|
||||
func NormalizeToYear(date, normal time.Time) time.Time {
|
||||
for date.AddDate(1, 0, 0).Compare(normal) <= 0 {
|
||||
date = date.AddDate(1, 0, 0)
|
||||
}
|
||||
return date
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTimeutil_StartOfPreviousMonth(t *testing.T) {
|
||||
@ -367,3 +369,94 @@ func TestTimeUtil_ParseTimeFromPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestTimeUtil_NormalizeToYear tests NormalizeToYear function which returns the normalized input date wrt to the normal.
|
||||
func TestTimeUtil_NormalizeToYear(t *testing.T) {
|
||||
testCases := []struct {
|
||||
inputDate time.Time
|
||||
normalDate time.Time
|
||||
expectedNormalizedDate time.Time
|
||||
}{
|
||||
{
|
||||
inputDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 10, 1, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2025, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2025, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2025, 10, 1, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2025, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
// inputDate more than 2 years prior to normal date
|
||||
{
|
||||
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2023, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 9, 28, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2023, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 9, 30, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2020, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 12, 1, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
// leap year test cases
|
||||
{
|
||||
inputDate: time.Date(2020, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2024, 9, 28, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2023, 9, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2025, 2, 28, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2025, 3, 2, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2028, 2, 28, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2028, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
|
||||
normalDate: time.Date(2028, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
expectedNormalizedDate: time.Date(2028, 3, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
normalizedDate := NormalizeToYear(tc.inputDate, tc.normalDate)
|
||||
require.Equal(t, tc.expectedNormalizedDate, normalizedDate)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user