Skip to content

Commit

Permalink
refactor: job url propagation (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Plischke authored Oct 30, 2024
1 parent bda17a8 commit 66860b8
Show file tree
Hide file tree
Showing 24 changed files with 254 additions and 168 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/artifacts/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {
artifactSvc = ArtifactService{
JobService: saucecloud.JobService{
Resto: http.NewResto(
url, creds.Username, creds.AccessKey, restoTimeout,
reg, creds.Username, creds.AccessKey, restoTimeout,
),
RDC: http.NewRDCService(
url, creds.Username, creds.AccessKey, rdcTimeout,
reg, creds.Username, creds.AccessKey, rdcTimeout,
),
TestComposer: http.NewTestComposer(
url, creds, testComposerTimeout,
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/ini/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type initializer struct {
func newInitializer(stdio terminal.Stdio, creds iam.Credentials, cfg *initConfig) *initializer {
r := region.FromString(cfg.region)
tc := http.NewTestComposer(r.APIBaseURL(), creds, testComposerTimeout)
rc := http.NewRDCService(r.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rs := http.NewResto(r.APIBaseURL(), creds.Username, creds.AccessKey, restoTimeout)
rc := http.NewRDCService(r, creds.Username, creds.AccessKey, rdcTimeout)
rs := http.NewResto(r, creds.Username, creds.AccessKey, restoTimeout)
us := http.NewUserService(r.APIBaseURL(), creds, 5*time.Second)

return &initializer{
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func runApitest(cmd *cobra.Command, isCLIDriven bool) (int, error) {
creds := regio.Credentials()

apitestingClient := http.NewAPITester(regio.APIBaseURL(), creds.Username, creds.AccessKey, apitestingTimeout)
restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)

r := apitest.Runner{
Project: p,
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/cucumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func runCucumber(cmd *cobra.Command, isCLIDriven bool) (int, error) {

creds := regio.Credentials()

restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
jobService := saucecloud.JobService{
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/cypress.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func runCypress(cmd *cobra.Command, cflags cypressFlags, isCLIDriven bool) (int,

creds := regio.Credentials()

restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
jobService := saucecloud.JobService{
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/espresso.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ func runEspressoInCloud(p espresso.Project, regio region.Region) (int, error) {
log.Info().Msg("Running Espresso in Sauce Labs")

creds := regio.Credentials()
restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
jobService := saucecloud.JobService{
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/imagerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func runImageRunner(cmd *cobra.Command) (int, error) {
}

imageRunnerClient := http.NewImageRunner(regio.APIBaseURL(), creds, imgExecTimeout, asyncEventManager)
restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)

r := saucecloud.NewImgRunner(p, &imageRunnerClient, &restoClient, asyncEventManager,
reporters, gFlags.async)
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ func runPlaywright(cmd *cobra.Command, pf playwrightFlags, isCLIDriven bool) (in

creds := regio.Credentials()

restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
jobService := saucecloud.JobService{
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func runPuppeteerReplayInSauce(p replay.Project, regio region.Region) (int, erro
log.Info().Msg("Replaying chrome devtools recordings")

creds := regio.Credentials()
restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
buildService := http.NewBuildService(
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/testcafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ func runTestcafe(cmd *cobra.Command, tcFlags testcafeFlags, isCLIDriven bool) (i

creds := regio.Credentials()

restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
jobService := saucecloud.JobService{
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/run/xcuitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ func runXcuitestInCloud(p xcuitest.Project, regio region.Region) (int, error) {

creds := regio.Credentials()

restoClient := http.NewResto(regio.APIBaseURL(), creds.Username, creds.AccessKey, 0)
restoClient := http.NewResto(regio, creds.Username, creds.AccessKey, 0)
testcompClient := http.NewTestComposer(regio.APIBaseURL(), creds, testComposerTimeout)
webdriverClient := http.NewWebdriver(regio.WebDriverBaseURL(), creds, webdriverTimeout)
webdriverClient := http.NewWebdriver(regio, creds, webdriverTimeout)
appsClient := *http.NewAppStore(regio.APIBaseURL(), creds.Username, creds.AccessKey, gFlags.appStoreTimeout)
rdcClient := http.NewRDCService(regio.APIBaseURL(), creds.Username, creds.AccessKey, rdcTimeout)
rdcClient := http.NewRDCService(regio, creds.Username, creds.AccessKey, rdcTimeout)
insightsClient := http.NewInsightsService(regio.APIBaseURL(), creds, insightsTimeout)
iamClient := http.NewUserService(regio.APIBaseURL(), creds, iamTimeout)
jobService := saucecloud.JobService{
Expand Down
2 changes: 1 addition & 1 deletion internal/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewRetryableClient(timeout time.Duration) *retryablehttp.Client {
if e != nil {
return ok, e
}
if !ok && resp.StatusCode == http.StatusNotFound {
if !ok && resp != nil && resp.StatusCode == http.StatusNotFound {
return true, nil
}
return ok, e
Expand Down
37 changes: 25 additions & 12 deletions internal/http/rdcservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/saucelabs/saucectl/internal/region"
"github.com/saucelabs/saucectl/internal/slice"

"github.com/hashicorp/go-retryablehttp"
Expand All @@ -24,6 +25,7 @@ import (
// RDCService http client.
type RDCService struct {
Client *retryablehttp.Client
AppURL string
URL string
Username string
AccessKey string
Expand Down Expand Up @@ -83,17 +85,18 @@ type DeviceQuery struct {
}

// NewRDCService creates a new client.
func NewRDCService(url, username, accessKey string, timeout time.Duration) RDCService {
func NewRDCService(r region.Region, username, accessKey string, timeout time.Duration) RDCService {
return RDCService{
Client: NewRetryableClient(timeout),
URL: url,
AppURL: r.AppBaseURL(),
URL: r.APIBaseURL(),
Username: username,
AccessKey: accessKey,
}
}

// StartJob creates a new job in Sauce Labs.
func (c *RDCService) StartJob(ctx context.Context, opts job.StartOptions) (jobID string, err error) {
func (c *RDCService) StartJob(ctx context.Context, opts job.StartOptions) (job.Job, error) {
url := fmt.Sprintf("%s/v1/rdc/native-composer/tests", c.URL)

var frameworkName string
Expand Down Expand Up @@ -129,31 +132,31 @@ func (c *RDCService) StartJob(ctx context.Context, opts job.StartOptions) (jobID
}

var b bytes.Buffer
err = json.NewEncoder(&b).Encode(jobReq)
err := json.NewEncoder(&b).Encode(jobReq)
if err != nil {
return
return job.Job{}, err
}

req, err := NewRequestWithContext(ctx, http.MethodPost, url, &b)
if err != nil {
return
return job.Job{}, err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.Username, c.AccessKey)

resp, err := c.Client.HTTPClient.Do(req)
if err != nil {
return
return job.Job{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
return job.Job{}, err
}

if resp.StatusCode >= 300 {
err = fmt.Errorf("job start failed; unexpected response code:'%d', msg:'%v'", resp.StatusCode, strings.TrimSpace(string(body)))
return "", err
return job.Job{}, err
}

var sessionStart struct {
Expand All @@ -162,10 +165,15 @@ func (c *RDCService) StartJob(ctx context.Context, opts job.StartOptions) (jobID
} `json:"test_report"`
}
if err = json.Unmarshal(body, &sessionStart); err != nil {
return "", fmt.Errorf("job start status unknown: unable to parse server response: %w", err)
return job.Job{}, fmt.Errorf("job start status unknown: unable to parse server response: %w", err)
}

return sessionStart.TestReport.ID, nil
return job.Job{
ID: sessionStart.TestReport.ID,
IsRDC: true,
Status: job.StateQueued,
URL: fmt.Sprintf("%s/tests/%s", c.AppURL, sessionStart.TestReport.ID),
}, nil
}

func (c *RDCService) StopJob(ctx context.Context, id string, realDevice bool) (job.Job, error) {
Expand Down Expand Up @@ -201,7 +209,11 @@ func (c *RDCService) StopJob(ctx context.Context, id string, realDevice bool) (j
}

// RDC does not return any job details in the response.
return job.Job{}, nil
return job.Job{
ID: id,
Status: job.StateInProgress,
URL: fmt.Sprintf("%s/tests/%s", c.AppURL, id),
}, nil
}

// Job returns the job details.
Expand Down Expand Up @@ -485,5 +497,6 @@ func (c *RDCService) parseJob(body io.ReadCloser) (job.Job, error) {
OS: j.OS,
OSVersion: j.OSVersion,
IsRDC: true,
URL: fmt.Sprintf("%s/tests/%s", c.AppURL, j.ID),
}, err
}
Loading

0 comments on commit 66860b8

Please sign in to comment.