Skip to content

Commit

Permalink
feat: greatly improve cancellability (#991)
Browse files Browse the repository at this point in the history
* feat: cancellable artifacts command

* refactor: obsolete arg

* feat: ensure all commands are cancellable

* feat: cancellable tunnel validation

* feat: cancellable framework metadata retrieval
  • Loading branch information
alexplischke authored Dec 11, 2024
1 parent f443c9f commit 65e3c90
Show file tree
Hide file tree
Showing 41 changed files with 192 additions and 199 deletions.
3 changes: 2 additions & 1 deletion internal/apitest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ func FilterSuites(p *Project, suiteName string) error {
}

// RunProject runs the tests defined in apitest.Project
func (r *Runner) RunProject() (int, error) {
func (r *Runner) RunProject(ctx context.Context) (int, error) {
exitCode := 1
if err := tunnel.Validate(
ctx,
r.TunnelService,
r.Project.Sauce.Tunnel.Name,
r.Project.Sauce.Tunnel.Owner,
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/deletefile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -45,7 +44,7 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

confirmed, err := confirmDelete(name)
Expand All @@ -57,7 +56,7 @@ Use [--project] to specify the project by its name or run without [--project] to
return nil
}

err = apitesterClient.DeleteVaultFile(context.Background(), selectedProject.ID, []string{name})
err = apitesterClient.DeleteVaultFile(cmd.Context(), selectedProject.ID, []string{name})
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/apit/downloadfile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -47,9 +46,9 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
files, err := apitesterClient.ListVaultFiles(context.Background(), selectedProject.ID)
files, err := apitesterClient.ListVaultFiles(cmd.Context(), selectedProject.ID)
if err != nil {
return err
}
Expand All @@ -59,7 +58,7 @@ Use [--project] to specify the project by its name or run without [--project] to
return fmt.Errorf("project %q has no vault drive file with name %q", selectedProject.ProjectMeta.Name, name)
}

bodyReader, err := apitesterClient.GetVaultFileContent(context.Background(), selectedProject.ID, file.ID)
bodyReader, err := apitesterClient.GetVaultFileContent(cmd.Context(), selectedProject.ID, file.ID)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/get.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -38,8 +37,8 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, _ []string) error {
vault, err := apitesterClient.GetVault(context.Background(), selectedProject.Hooks[0].Identifier)
RunE: func(cmd *cobra.Command, _ []string) error {
vault, err := apitesterClient.GetVault(cmd.Context(), selectedProject.Hooks[0].Identifier)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/getsnippet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -43,9 +42,9 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
vault, err := apitesterClient.GetVault(context.Background(), selectedProject.Hooks[0].Identifier)
vault, err := apitesterClient.GetVault(cmd.Context(), selectedProject.Hooks[0].Identifier)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/getvariable.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -42,10 +41,10 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

vault, err := apitesterClient.GetVault(context.Background(), selectedProject.Hooks[0].Identifier)
vault, err := apitesterClient.GetVault(cmd.Context(), selectedProject.Hooks[0].Identifier)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/listfiles.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,8 +36,8 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, _ []string) error {
files, err := apitesterClient.ListVaultFiles(context.Background(), selectedProject.ID)
RunE: func(cmd *cobra.Command, _ []string) error {
files, err := apitesterClient.ListVaultFiles(cmd.Context(), selectedProject.ID)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/setsnippet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -54,7 +53,7 @@ cat snippet2.xml | saucectl apit vault set-snippet snip2 - --project "smoke test
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
fileName := args[1]

Expand All @@ -70,7 +69,7 @@ cat snippet2.xml | saucectl apit vault set-snippet snip2 - --project "smoke test
},
}

err = apitesterClient.PutVault(context.Background(), selectedProject.Hooks[0].Identifier, updateVault)
err = apitesterClient.PutVault(cmd.Context(), selectedProject.Hooks[0].Identifier, updateVault)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/setvariable.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -48,7 +47,7 @@ Use [--project] to specify a project by its name or run without [--project] to c
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
val := args[1]
updateVault := apitest.Vault{
Expand All @@ -62,7 +61,7 @@ Use [--project] to specify a project by its name or run without [--project] to c
Snippets: map[string]string{},
}

err := apitesterClient.PutVault(context.Background(), selectedProject.Hooks[0].Identifier, updateVault)
err := apitesterClient.PutVault(cmd.Context(), selectedProject.Hooks[0].Identifier, updateVault)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/apit/uploadfile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apit

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -45,14 +44,14 @@ Use [--project] to specify the project by its name or run without [--project] to
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

fd, err := os.Open(name)
if err != nil {
return err
}
_, err = apitesterClient.PutVaultFile(context.Background(), selectedProject.ID, name, fd)
_, err = apitesterClient.PutVaultFile(cmd.Context(), selectedProject.ID, name, fd)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/apit/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func VaultCommand(preRunE func(cmd *cobra.Command, args []string) error) *cobra.
return err
}
}
selectedProject, err = resolve(projectName)
selectedProject, err = resolve(cmd.Context(), projectName)
if err != nil {
return err
}
Expand Down Expand Up @@ -75,8 +75,8 @@ func isTerm(fd uintptr) bool {
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}

func resolve(projectName string) (ResolvedProject, error) {
projects, err := apitesterClient.GetProjects(context.Background())
func resolve(ctx context.Context, projectName string) (ResolvedProject, error) {
projects, err := apitesterClient.GetProjects(ctx)
if projectName == "" {
if !isTerm(os.Stdin.Fd()) || !isTerm(os.Stdout.Fd()) {
return ResolvedProject{}, fmt.Errorf("no project specified, use --project to choose a project by name")
Expand All @@ -101,7 +101,7 @@ func resolve(projectName string) (ResolvedProject, error) {
return ResolvedProject{}, fmt.Errorf("could not find project named %s", projectName)
}

hooks, err := apitesterClient.GetHooks(context.Background(), project.ID)
hooks, err := apitesterClient.GetHooks(ctx, project.ID)
if err != nil {
return ResolvedProject{}, err
}
Expand Down
26 changes: 12 additions & 14 deletions internal/cmd/artifacts/artifactservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type ArtifactService struct {
}

// List returns an artifact list
func (s *ArtifactService) List(jobID string) (List, error) {
isRDC, err := s.isRDC(jobID)
func (s *ArtifactService) List(ctx context.Context, jobID string) (List, error) {
isRDC, err := s.isRDC(ctx, jobID)
if err != nil {
return List{}, err
}
items, err := s.JobService.ArtifactNames(
context.Background(), jobID, isRDC,
ctx, jobID, isRDC,
)
if err != nil {
return List{}, err
Expand All @@ -33,36 +33,34 @@ func (s *ArtifactService) List(jobID string) (List, error) {
}

// Download does download specified artifacts
func (s *ArtifactService) Download(jobID, filename string) ([]byte, error) {
isRDC, err := s.isRDC(jobID)
func (s *ArtifactService) Download(ctx context.Context, jobID, filename string) ([]byte, error) {
isRDC, err := s.isRDC(ctx, jobID)
if err != nil {
return nil, err
}

return s.JobService.Artifact(
context.Background(), jobID, filename, isRDC,
ctx, jobID, filename, isRDC,
)
}

// Upload does upload the specified artifact
func (s *ArtifactService) Upload(jobID, filename string, content []byte) error {
isRDC, err := s.isRDC(jobID)
func (s *ArtifactService) Upload(ctx context.Context, jobID, filename string, content []byte) error {
isRDC, err := s.isRDC(ctx, jobID)
if err != nil {
return err
}
if isRDC {
return errors.New("uploading file to Real Device job is not supported")
}

return s.JobService.UploadArtifact(
jobID, false, filename, http.DetectContentType(content), content,
)
return s.JobService.UploadArtifact(ctx, jobID, false, filename, http.DetectContentType(content), content)
}

func (s *ArtifactService) isRDC(jobID string) (bool, error) {
_, err := s.JobService.Job(context.Background(), jobID, false)
func (s *ArtifactService) isRDC(ctx context.Context, jobID string) (bool, error) {
_, err := s.JobService.Job(ctx, jobID, false)
if err != nil {
_, err = s.JobService.Job(context.Background(), jobID, true)
_, err = s.JobService.Job(ctx, jobID, true)
if err != nil {
return false, fmt.Errorf("failed to get the job: %w", err)
}
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/artifacts/download.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package artifacts

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -49,10 +50,10 @@ func DownloadCommand() *cobra.Command {
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
jobID := args[0]
filePattern := args[1]
return download(jobID, filePattern, targetDir, out)
return download(cmd.Context(), jobID, filePattern, targetDir, out)
},
}

Expand All @@ -63,8 +64,8 @@ func DownloadCommand() *cobra.Command {
return cmd
}

func download(jobID, filePattern, targetDir, outputFormat string) error {
lst, err := artifactSvc.List(jobID)
func download(ctx context.Context, jobID, filePattern, targetDir, outputFormat string) error {
lst, err := artifactSvc.List(ctx, jobID)
if err != nil {
return err
}
Expand All @@ -75,7 +76,7 @@ func download(jobID, filePattern, targetDir, outputFormat string) error {
bar := newDownloadProgressBar(outputFormat, len(files))
for _, f := range files {
_ = bar.Add(1)
body, err := artifactSvc.Download(jobID, f)
body, err := artifactSvc.Download(ctx, jobID, f)
if err != nil {
return fmt.Errorf("failed to get file: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/artifacts/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package artifacts

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -93,8 +94,8 @@ func ListCommand() *cobra.Command {
}()
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
return list(args[0], out)
RunE: func(cmd *cobra.Command, args []string) error {
return list(cmd.Context(), args[0], out)
},
}
flags := cmd.PersistentFlags()
Expand All @@ -103,8 +104,8 @@ func ListCommand() *cobra.Command {
return cmd
}

func list(jobID, outputFormat string) error {
lst, err := artifactSvc.List(jobID)
func list(ctx context.Context, jobID, outputFormat string) error {
lst, err := artifactSvc.List(ctx, jobID)
if err != nil {
return fmt.Errorf("failed to get artifacts list: %w", err)
}
Expand Down
Loading

0 comments on commit 65e3c90

Please sign in to comment.