Skip to content

Commit

Permalink
Merge branch 'udenG-5706-ubuntu-insights-uploader' into consent-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hk21702 committed Feb 3, 2025
2 parents 757a821 + f1c6c53 commit 4599876
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 23 deletions.
9 changes: 4 additions & 5 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,23 @@ func getReportTime(path string) (int64, error) {
}

// GetPeriodStart returns the start of the period window for a given period in seconds.
func GetPeriodStart(period int) (int64, error) {
func GetPeriodStart(period int, t time.Time) (int64, error) {
if period <= 0 {
return 0, ErrInvalidPeriod
}
utcTime := time.Now().UTC().Unix()
return utcTime - (utcTime % int64(period)), nil
return t.Unix() - (t.Unix() % int64(period)), nil
}

// GetForPeriod returns the most recent report within a period window for a given directory.
// Not inclusive of the period end (periodStart + period).
//
// For example, given reports 1 and 7, with time 2 and period 7, the function will return the path for report 1.
func GetForPeriod(dir string, time time.Time, period int) (Report, error) {
func GetForPeriod(dir string, t time.Time, period int) (Report, error) {
if period <= 0 {
return Report{}, ErrInvalidPeriod
}

periodStart := time.Unix() - (time.Unix() % int64(period))
periodStart := t.Unix() - (t.Unix() % int64(period))
periodEnd := periodStart + int64(period)

// Reports names are utc timestamps. Get the most recent report within the period window.
Expand Down
10 changes: 7 additions & 3 deletions internal/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ func TestGetPeriodStart(t *testing.T) {

tests := map[string]struct {
period int
time int64

wantErr error
}{
"Valid Period": {period: 500},
"Valid Period": {period: 500, time: 100000},
"Negative Time:": {period: 500, time: -100000},

"Invalid Negative Period": {period: -500, wantErr: report.ErrInvalidPeriod},
"Invalid Zero Period": {period: 0, wantErr: report.ErrInvalidPeriod},
Expand All @@ -30,14 +32,16 @@ func TestGetPeriodStart(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, err := report.GetPeriodStart(tc.period)
got, err := report.GetPeriodStart(tc.period, time.Unix(tc.time, 0))
if tc.wantErr != nil {
require.ErrorIs(t, err, tc.wantErr)
return
}
require.NoError(t, err, "got an unexpected error")

require.IsType(t, int64(0), got)
want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
require.Equal(t, want, got, "GetPeriodStart should return the expect start of the period window")
})
}
}
Expand Down Expand Up @@ -233,7 +237,7 @@ func TestGetAll(t *testing.T) {
}
require.NoError(t, err, "got an unexpected error")

got := make([]report.Report, len(reports))
got := make([]report.Report, 0, len(reports))
for _, r := range reports {
got = append(got, sanitizeReportPath(t, r, dir))
}
Expand Down
6 changes: 0 additions & 6 deletions internal/report/testdata/TestGetAll/golden/files_in_subdir
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
- path: ""
name: ""
timestamp: 0
- path: ""
name: ""
timestamp: 0
- path: 1.json
name: 1.json
timestamp: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
- path: ""
name: ""
timestamp: 0
- path: ""
name: ""
timestamp: 0
- path: ""
name: ""
timestamp: 0
- path: 1.json
name: 1.json
timestamp: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-100000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100000

0 comments on commit 4599876

Please sign in to comment.