Skip to content

Commit

Permalink
Connect Uploader to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
hk21702 committed Feb 3, 2025
1 parent 4599876 commit cc1cbad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
34 changes: 34 additions & 0 deletions cmd/insights/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,37 @@ func TestConsent(t *testing.T) {
)
}
}

func TestUpload(t *testing.T) {
t.Parallel()
tests := map[string]struct {
lFiles map[string]testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, windows-2022)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, windows-2022)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-14)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-13)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-14)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-13)

undefined: testutils.ReportType

Check failure on line 111 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

undefined: testutils.ReportType
uFiles map[string]testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, windows-2022)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, windows-2022)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-14)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-13)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-14)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-13)

undefined: testutils.ReportType

Check failure on line 112 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

undefined: testutils.ReportType
source string
dummy bool

serverResponse int
serverOffline bool
url string
rmLocal bool
noPerms bool

consent string
minAge int64
dryRun bool
force bool

wantErr bool
skipContentCheck bool
}{
"No Permissions": {lFiles: map[string]testutils.ReportType{"1.json": testutils.Normal}, consent: "true", noPerms: true, wantErr: runtime.GOOS != "windows", skipContentCheck: true},

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, ubuntu-24.04)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, ubuntu-24.04)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, windows-2022)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, windows-2022)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, windows-2022)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, windows-2022)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-14)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-14)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-13)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-13)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-14)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-14)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-13)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-13)

undefined: testutils.Normal

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

undefined: testutils.ReportType

Check failure on line 130 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

undefined: testutils.Normal
}

for name, tc := range tests {

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, ubuntu-24.04)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, ubuntu-24.04)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, windows-2022)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

declared and not used: tc (typecheck)

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, windows-2022)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-14)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (coverage, macos-13)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-14)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

declared and not used: tc (typecheck)

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

declared and not used: tc (typecheck)

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Tests (race, macos-13)

declared and not used: tc

Check failure on line 133 in cmd/insights/commands/commands_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

declared and not used: tc
t.Run(name, func(t *testing.T) {
t.Parallel()

})

}
}
24 changes: 24 additions & 0 deletions cmd/insights/commands/upload.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package commands

import (
"fmt"
"log/slog"

"github.com/spf13/cobra"
"github.com/ubuntu/ubuntu-insights/internal/consent"
"github.com/ubuntu/ubuntu-insights/internal/constants"
"github.com/ubuntu/ubuntu-insights/internal/uploader"
)

type uploadConfig struct {
Expand Down Expand Up @@ -36,6 +39,9 @@ func installUploadCmd(app *App) {
app.uploadConfig.sources = args

slog.Info("Running upload command")
if err := app.uploadRun(); err != nil {
return fmt.Errorf("failed to run upload command: %v", err)
}

return nil
},
Expand All @@ -48,3 +54,21 @@ func installUploadCmd(app *App) {

app.cmd.AddCommand(uploadCmd)
}

func (a App) uploadRun() error {
cm := consent.New(a.rootConfig.ConsentDir)
opts := uploader.WithCachePath(a.rootConfig.InsightsDir)

for _, source := range a.uploadConfig.sources {
u, err := uploader.New(cm, source, a.uploadConfig.minAge, a.uploadConfig.dryRun, opts)
if err != nil {
return fmt.Errorf("failed to create uploader for source %s: %v", source, err)
}

if err := u.Upload(a.uploadConfig.force); err != nil {
return fmt.Errorf("failed to upload reports for source %s: %v", source, err)
}
}

return nil
}
7 changes: 1 addition & 6 deletions internal/uploader/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ func (m MockTimeProvider) Now() time.Time {
return time.Unix(m.CurrentTime, 0)
}

// WithCachePath sets the cache path for the uploader.
func WithCachePath(path string) Options {
return func(o *options) {
o.cachePath = path
}
}

Check failure on line 13 in internal/uploader/export_test.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

File is not properly formatted (gofmt)

// WithBaseServerURL sets the base server URL for the uploader.
func WithBaseServerURL(url string) Options {
Expand Down
7 changes: 7 additions & 0 deletions internal/uploader/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var (
ErrReportNotMature = errors.New("report is not mature enough to be uploaded")
)

// WithCachePath sets the cache path for the uploader.
func WithCachePath(path string) Options {
return func(o *options) {
o.cachePath = path
}
}

// Upload uploads the reports corresponding to the source to the configured server.
//
// It will only upload reports that are mature enough, and have not been uploaded before.
Expand Down

0 comments on commit cc1cbad

Please sign in to comment.