Skip to content

Commit

Permalink
feat: Enable env vars for xcuitest (#834)
Browse files Browse the repository at this point in the history
* Add `Env` for xcuitest

* update schema

* Apply --env for xcuitest

* update --env usage notes

* Use same suite loop

* Add root level default env
  • Loading branch information
mhan83 authored Sep 14, 2023
1 parent c0ed293 commit b6f7481
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/saucectl.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,9 @@
},
"additionalProperties": false
},
"env": {
"$ref": "#/allOf/0/then/properties/env"
},
"xcuitest": {
"description": "Contains details specific to the XCUITest project.",
"type": "object",
Expand Down Expand Up @@ -2178,6 +2181,9 @@
"description": "A list of applications to be installed alongside the main app. Applications can be defined as a local path or a remote url. If a remote url is defined, the app will be downloaded to a local temp directory before uploading to the SauceLabs Mobile App Storage service. Supports environment variables as values. When targeting simulators, a maximum of 2 otherApps is supported.",
"type": "array"
},
"env": {
"$ref": "#/allOf/0/then/properties/env"
},
"testOptions": {
"description": "Allows you to control various details on how tests are executed.",
"type": "object",
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha/framework/xcuitest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
},
"additionalProperties": false
},
"env": {
"$ref": "../subschema/common.schema.json#/definitions/env"
},
"xcuitest": {
"description": "Contains details specific to the XCUITest project.",
"type": "object",
Expand Down Expand Up @@ -92,6 +95,9 @@
"description": "A list of applications to be installed alongside the main app. Applications can be defined as a local path or a remote url. If a remote url is defined, the app will be downloaded to a local temp directory before uploading to the SauceLabs Mobile App Storage service. Supports environment variables as values. When targeting simulators, a maximum of 2 otherApps is supported.",
"type": "array"
},
"env": {
"$ref": "../subschema/common.schema.json#/definitions/env"
},
"testOptions": {
"description": "Allows you to control various details on how tests are executed.",
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func Command() *cobra.Command {
cmd.PersistentFlags().BoolVar(&gFlags.failFast, "fail-fast", false, "Stops suites after the first failure")
cmd.PersistentFlags().DurationVar(&gFlags.appStoreTimeout, "uploadTimeout", 5*time.Minute, "Upload timeout that limits how long saucectl will wait for an upload to finish. Supports duration values like '10s' '30m' etc. (default: 5m)")
sc.StringP("region", "r", "sauce::region", "us-west-1", "The sauce labs region.")
sc.StringToStringP("env", "e", "env", map[string]string{}, "Set environment variables, e.g. -e foo=bar. Not supported when running espresso/xcuitest!")
sc.StringToStringP("env", "e", "env", map[string]string{}, "Set environment variables, e.g. -e foo=bar. Not supported for RDC or Espresso on virtual devices!")
sc.Bool("show-console-log", "showConsoleLog", false, "Shows suites console.log locally. By default console.log is only shown on failures.")
sc.Int("ccy", "sauce::concurrency", 2, "Concurrency specifies how many suites are run at the same time.")
sc.String("tunnel-id", "sauce::tunnel::id", "", "Sets the sauce-connect tunnel ID to be used for the run.")
Expand Down
19 changes: 19 additions & 0 deletions internal/http/webdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type SauceOpts struct {
Visibility string `json:"public,omitempty"`
}

type env struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}

// Batch represents capabilities for batch frameworks.
type Batch struct {
Framework string `json:"framework,omitempty"`
Expand All @@ -73,6 +78,7 @@ type Batch struct {
TestFile string `json:"testFile,omitempty"`
Args []map[string]string `json:"args,omitempty"`
VideoFPS int `json:"video_fps"`
Env []env `json:"env,omitempty"`
}

// sessionStartResponse represents the response body for starting a session.
Expand Down Expand Up @@ -132,6 +138,7 @@ func (c *Webdriver) StartJob(ctx context.Context, opts job.StartOptions) (jobID
TestFile: opts.Suite,
Args: c.formatTestOptions(opts.TestOptions),
VideoFPS: 13, // 13 is the sweet spot to minimize frame drops
Env: formatEnv(opts.Env),
},
IdleTimeout: 9999,
MaxDuration: 10800,
Expand Down Expand Up @@ -184,6 +191,18 @@ func (c *Webdriver) StartJob(ctx context.Context, opts job.StartOptions) (jobID
return sessionStart.SessionID, false, nil
}

func formatEnv(e map[string]string) []env {
var envs []env

for k, v := range e {
envs = append(envs, env{
Name: k,
Value: v,
})
}
return envs
}

// formatTestOptions adapts option shape to match chef expectations
func (c *Webdriver) formatTestOptions(options map[string]interface{}) []map[string]string {
var mappedOptions []map[string]string
Expand Down
1 change: 1 addition & 0 deletions internal/job/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type StartOptions struct {
RealDeviceKind string `json:"realDeviceKind,omitempty"`
TimeZone string `json:"timeZone,omitempty"`
Visibility string `json:"public,omitempty"`
Env map[string]string `json:"-"`
}

// AppSettings represents app settings for real device
Expand Down
3 changes: 3 additions & 0 deletions internal/saucecloud/xcuitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func (r *XcuitestRunner) startJob(jobOpts chan<- job.StartOptions, appFileID, te
DeviceType: d.deviceType,
DevicePrivateOnly: d.privateOnly,

// VMD specific settings
Env: s.Env,

// Overwrite device settings
RealDeviceKind: strings.ToLower(xcuitest.IOS),
AppSettings: job.AppSettings{
Expand Down
9 changes: 9 additions & 0 deletions internal/xcuitest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Project struct {
Artifacts config.Artifacts `yaml:"artifacts,omitempty" json:"artifacts"`
Reporters config.Reporters `yaml:"reporters,omitempty" json:"-"`
Notifications config.Notifications `yaml:"notifications,omitempty" json:"-"`
Env map[string]string `yaml:"env,omitempty" json:"-"`
}

// Xcuitest represents xcuitest apps configuration.
Expand Down Expand Up @@ -114,6 +115,7 @@ type Suite struct {
SmartRetry config.SmartRetry `yaml:"smartRetry,omitempty" json:"-"`
Shard string `yaml:"shard,omitempty" json:"-"`
TestListFile string `yaml:"testListFile,omitempty" json:"-"`
Env map[string]string `yaml:"env,omitempty" json:"-"`
}

// IOS constant
Expand Down Expand Up @@ -184,6 +186,13 @@ func SetDefaults(p *Project) {
if suite.PassThreshold < 1 {
suite.PassThreshold = 1
}

for k, v := range p.Env {
if suite.Env == nil {
suite.Env = map[string]string{}
}
suite.Env[k] = v
}
}
}

Expand Down

0 comments on commit b6f7481

Please sign in to comment.