-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathespresso.go
More file actions
288 lines (259 loc) · 8.26 KB
/
Copy pathespresso.go
File metadata and controls
288 lines (259 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package saucecloud
import (
"context"
"fmt"
"strings"
"github.com/rs/zerolog/log"
"github.com/saucelabs/saucectl/internal/config"
"github.com/saucelabs/saucectl/internal/espresso"
"github.com/saucelabs/saucectl/internal/job"
"github.com/saucelabs/saucectl/internal/msg"
"github.com/saucelabs/saucectl/internal/storage"
)
// deviceConfig represent the configuration for a specific device.
type deviceConfig struct {
ID string
name string
platformName string
platformVersion string
orientation string
isRealDevice bool
hasCarrier bool
deviceType string
privateOnly bool
armRequired bool
}
// EspressoRunner represents the Sauce Labs cloud implementation for cypress.
type EspressoRunner struct {
CloudRunner
Project espresso.Project
}
// RunProject runs the tests defined in cypress.Project.
func (r *EspressoRunner) RunProject(ctx context.Context) (int, error) {
exitCode := 1
if err := r.validateTunnel(
ctx,
r.Project.Sauce.Tunnel.Name,
r.Project.Sauce.Tunnel.Owner,
r.Project.DryRun,
r.Project.Sauce.Tunnel.Timeout,
); err != nil {
return 1, err
}
var err error
r.Project.Espresso.App, err = r.uploadArchive(
ctx,
storage.FileInfo{Name: r.Project.Espresso.App, Description: r.Project.Espresso.AppDescription},
appUpload,
r.Project.DryRun,
)
if err != nil {
return exitCode, err
}
r.Project.Espresso.OtherApps, err = r.uploadArchives(ctx, r.Project.Espresso.OtherApps, otherAppsUpload, r.Project.DryRun)
if err != nil {
return exitCode, err
}
cache := map[string]string{}
for i, suite := range r.Project.Suites {
if val, ok := cache[suite.TestApp]; ok {
r.Project.Suites[i].TestApp = val
continue
}
testAppURL, err := r.uploadArchive(
ctx,
storage.FileInfo{Name: suite.TestApp, Description: suite.TestAppDescription},
testAppUpload,
r.Project.DryRun,
)
if err != nil {
return exitCode, err
}
r.Project.Suites[i].TestApp = testAppURL
cache[suite.TestApp] = testAppURL
}
if r.Project.DryRun {
r.dryRun()
return 0, nil
}
passed := r.runSuites(ctx)
if passed {
exitCode = 0
}
return exitCode, nil
}
func (r *EspressoRunner) runSuites(ctx context.Context) bool {
jobOpts, results := r.createWorkerPool(
ctx, r.Project.Sauce.Concurrency, r.Project.Sauce.Retries,
)
defer close(results)
suites := r.Project.Suites
if r.Project.Sauce.LaunchOrder != "" {
history, err := r.getHistory(ctx, r.Project.Sauce.LaunchOrder)
if err != nil {
log.Warn().Err(err).Msg(msg.RetrieveJobHistoryError)
} else {
suites = espresso.SortByHistory(suites, history)
}
}
var startOptions []job.StartOptions
for _, s := range suites {
shardCfg := s.ShardConfig()
// Automatically apply ShardIndex if numShards is defined
if shardCfg.Shards > 0 {
for i := 0; i < shardCfg.Shards; i++ {
// Enforce copy of the map to ensure it is not shared.
testOptions := map[string]interface{}{}
for k, v := range s.TestOptions {
testOptions[k] = v
}
s.TestOptions = testOptions
s.TestOptions["shardIndex"] = i
for _, deviceCfg := range enumerateDevices(
s.Devices, s.Emulators,
) {
startOptions = append(
startOptions, r.newStartOptions(
s, r.Project.Espresso.App, s.TestApp,
r.Project.Espresso.OtherApps, deviceCfg,
),
)
}
}
} else {
for _, deviceCfg := range enumerateDevices(s.Devices, s.Emulators) {
startOptions = append(
startOptions, r.newStartOptions(
s, r.Project.Espresso.App, s.TestApp,
r.Project.Espresso.OtherApps, deviceCfg,
),
)
}
}
}
go func() {
for _, opt := range startOptions {
jobOpts <- opt
}
}()
return r.collectResults(ctx, results, len(startOptions))
}
func (r *EspressoRunner) dryRun() {
fmt.Println("\nThe following test suites would have run:")
for _, s := range r.Project.Suites {
fmt.Printf(" - %s\n", s.Name)
for _, c := range enumerateDevices(s.Devices, s.Emulators) {
fmt.Printf(" - on %s %s %s\n", c.name, c.platformName, c.platformVersion)
}
}
fmt.Println()
}
// enumerateDevices returns a list of emulators and devices targeted by the current suite.
func enumerateDevices(devices []config.Device, virtualDevices []config.VirtualDevice) []deviceConfig {
var configs []deviceConfig
for _, e := range virtualDevices {
for _, p := range e.PlatformVersions {
configs = append(configs, deviceConfig{
name: e.Name,
platformName: e.PlatformName,
platformVersion: p,
orientation: e.Orientation,
armRequired: e.ARMRequired,
})
}
}
for _, d := range devices {
configs = append(configs, deviceConfig{
ID: d.ID,
name: d.Name,
platformName: d.PlatformName,
platformVersion: d.PlatformVersion,
isRealDevice: true,
hasCarrier: d.Options.CarrierConnectivity,
deviceType: d.Options.DeviceType,
privateOnly: d.Options.Private,
})
}
return configs
}
// newStartOptions add the job to the list for the workers.
func (r *EspressoRunner) newStartOptions(
s espresso.Suite, appFileURI, testAppFileURI string, otherAppsURIs []string,
d deviceConfig,
) job.StartOptions {
displayName := s.Name
shardCfg := s.ShardConfig()
if shardCfg.Shards > 0 {
displayName = fmt.Sprintf(
"%s (shard %d/%d)", displayName, shardCfg.Index+1, shardCfg.Shards,
)
}
return job.StartOptions{
DisplayName: displayName,
Timeout: s.Timeout,
ConfigFilePath: r.Project.ConfigFilePath,
CLIFlags: r.Project.CLIFlags,
App: appFileURI,
TestApp: testAppFileURI,
Suite: testAppFileURI,
OtherApps: otherAppsURIs,
Framework: "espresso",
FrameworkVersion: "1.0.0-stable",
PlatformName: d.platformName,
PlatformVersion: d.platformVersion,
DeviceID: d.ID,
DeviceName: d.name,
DeviceOrientation: d.orientation,
Name: displayName,
Build: r.Project.Sauce.Metadata.Build,
Tags: r.Project.Sauce.Metadata.Tags,
Tunnel: job.TunnelOptions{
Name: r.Project.Sauce.Tunnel.Name,
Owner: r.Project.Sauce.Tunnel.Owner,
},
Experiments: r.Project.Sauce.Experiments,
TestOptions: s.TestOptions,
Attempt: 0,
Retries: r.Project.Sauce.Retries,
Visibility: r.Project.Sauce.Visibility,
PassThreshold: s.PassThreshold,
SmartRetry: job.SmartRetry{
FailedOnly: s.SmartRetry.IsRetryFailedOnly(),
},
// Network throttling
NetworkProfile: s.NetworkProfile,
NetworkConditions: configToJobNetworkConditions(s.NetworkConditions),
// RDC Specific flags
RealDevice: d.isRealDevice,
DeviceHasCarrier: d.hasCarrier,
DeviceType: d.deviceType,
DevicePrivateOnly: d.privateOnly,
// VMD specific settings
ARMRequired: d.armRequired,
// Configure device settings
RealDeviceKind: strings.ToLower(espresso.Android),
AppSettings: job.AppSettings{
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The key name mismatch here between left and right hand side is intentional.
//
// Traditionally, saucectl made no distinction between instrumentation and resigning
// while our composer API backend always used specific key names per platform,
// either Instrumentation for Android and Resigning for iOS.
//
// This created a situation where app settings were sporadically ignored in Android RDC sessions.
//
// Here, we choose the lesser evil and keep supporting `ResignerEnabled` in the saucectl config yaml
// for backward compatibility while also mapping it to the correct API parameter for composer to evaluate.
InstrumentationEnabled: s.AppSettings.ResigningEnabled,
////////////////////////////////////////////////////////////////////////////////////////////////////////////
AudioCapture: s.AppSettings.AudioCapture,
Instrumentation: job.Instrumentation{
ImageInjection: s.AppSettings.Instrumentation.ImageInjection,
BypassScreenshotRestriction: s.AppSettings.Instrumentation.BypassScreenshotRestriction,
Vitals: s.AppSettings.Instrumentation.Vitals,
NetworkCapture: s.AppSettings.Instrumentation.NetworkCapture,
BiometricsInterception: s.AppSettings.Instrumentation.Biometrics,
},
},
}
}