-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_api.go
323 lines (283 loc) · 11.4 KB
/
test_api.go
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/bitrise-io/go-utils/log"
testing "google.golang.org/api/testing/v1"
)
// TestAsset describes a requested test asset
type TestAsset struct {
UploadURL string `json:"uploadUrl"`
GcsPath string `json:"gcsPath"`
Filename string `json:"filename"`
}
// TestAssetsAndroid describes requested Android test asset and as the returned test asset upload URLs
type TestAssetsAndroid struct {
isBundle bool
testApp *TestAsset
Apk TestAsset `json:"apk,omitempty"`
Aab TestAsset `json:"aab,omitmepty"`
TestApk TestAsset `json:"testApk,omitempty"`
RoboScript TestAsset `json:"roboScript,omitempty"`
ObbFiles []TestAsset `json:"obbFiles,omitempty"`
}
func uploadTestAssets(configs ConfigsModel) (TestAssetsAndroid, error) {
var testAssets TestAssetsAndroid
url := configs.APIBaseURL + "/assets/android/" + configs.AppSlug + "/" + configs.BuildSlug + "/" + configs.APIToken
if strings.ToLower(filepath.Ext(configs.AppPath)) == ".aab" {
testAssets.isBundle = true
}
log.Debugf("App path (%s), is bundle: %t", configs.AppPath, testAssets.isBundle)
var requestedAssets TestAssetsAndroid
if testAssets.isBundle {
requestedAssets.Aab = TestAsset{
Filename: filepath.Base(configs.AppPath),
}
} else {
requestedAssets.Apk = TestAsset{
Filename: filepath.Base(configs.AppPath),
}
}
if configs.TestType == testTypeInstrumentation {
requestedAssets.TestApk = TestAsset{
Filename: filepath.Base(configs.TestApkPath),
}
}
if configs.TestType == testTypeRobo && configs.RoboScenarioFile != "" {
requestedAssets.RoboScript = TestAsset{
Filename: filepath.Base(configs.RoboScenarioFile),
}
}
for _, obbFile := range configs.ObbFiles {
requestedAssets.ObbFiles = append(requestedAssets.ObbFiles, TestAsset{
Filename: filepath.Base(obbFile),
})
}
log.Debugf("Assets requested: %+v", requestedAssets)
data, err := json.Marshal(requestedAssets)
if err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to encode to json: %+v", requestedAssets)
}
req, err := http.NewRequest("POST", url, bytes.NewReader(data))
if err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to create http request, error: %s", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to get http response, error: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to read response body (status code: %d), error: %s", resp.StatusCode, err)
}
if resp.StatusCode != http.StatusOK {
return TestAssetsAndroid{}, fmt.Errorf("failed to start test: %d, error: %s", resp.StatusCode, string(body))
}
err = json.Unmarshal(body, &testAssets)
if err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to unmarshal response body, error: %s", err)
}
if testAssets.isBundle {
testAssets.testApp = &testAssets.Aab
} else {
testAssets.testApp = &testAssets.Apk
}
log.Debugf("Uploading file(%s) to (%s)", configs.AppPath, testAssets.testApp.GcsPath)
err = uploadFile(testAssets.testApp.UploadURL, configs.AppPath)
if err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to upload file(%s) to (%s), error: %s", configs.AppPath, testAssets.testApp.UploadURL, err)
}
if configs.TestType == testTypeInstrumentation {
if err := uploadFile(testAssets.TestApk.UploadURL, configs.TestApkPath); err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to upload file(%s) to (%s), error: %s", configs.TestApkPath, testAssets.TestApk.UploadURL, err)
}
}
if configs.TestType == testTypeRobo && configs.RoboScenarioFile != "" {
if err := uploadFile(testAssets.RoboScript.UploadURL, configs.RoboScenarioFile); err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to upload file(%s) to (%s), error: %s", configs.RoboScenarioFile, testAssets.RoboScript.UploadURL, err)
}
}
if len(testAssets.ObbFiles) != len(configs.ObbFiles) {
return TestAssetsAndroid{}, fmt.Errorf("invalid length of obb file upload URLs in response: %+v", testAssets)
}
for i, obbFile := range configs.ObbFiles {
if err := uploadFile(testAssets.ObbFiles[i].UploadURL, obbFile); err != nil {
return TestAssetsAndroid{}, fmt.Errorf("failed to upload obb file (%s) to (%s), error: %s", obbFile, testAssets.ObbFiles[i].UploadURL, err)
}
}
return testAssets, nil
}
func startTestRun(configs ConfigsModel, testAssets TestAssetsAndroid) error {
url := configs.APIBaseURL + "/" + configs.AppSlug + "/" + configs.BuildSlug + "/" + configs.APIToken
testModel := &testing.TestMatrix{}
testModel.EnvironmentMatrix = &testing.EnvironmentMatrix{AndroidDeviceList: &testing.AndroidDeviceList{}}
testModel.EnvironmentMatrix.AndroidDeviceList.AndroidDevices = configs.TestDevices
testModel.FlakyTestAttempts = int64(configs.FlakyTestAttempts)
// obb files to upload
var filesToPush []*testing.DeviceFile
for _, obbFile := range testAssets.ObbFiles {
filesToPush = append(filesToPush, &testing.DeviceFile{
ObbFile: &testing.ObbFile{
Obb: &testing.FileReference{
GcsPath: obbFile.GcsPath,
},
ObbFileName: obbFile.Filename,
},
})
}
// a nil account does not log in to test Google account before test is started
var account *testing.Account
if configs.AutoGoogleLogin {
account = &testing.Account{
GoogleAuto: &testing.GoogleAuto{},
}
}
testModel.TestSpecification = &testing.TestSpecification{
TestTimeout: fmt.Sprintf("%fs", configs.TestTimeout),
TestSetup: &testing.TestSetup{
EnvironmentVariables: configs.EnvironmentVariables,
FilesToPush: filesToPush,
DirectoriesToPull: configs.DirectoriesToPull,
Account: account,
},
}
switch configs.TestType {
case testTypeInstrumentation:
testModel.TestSpecification.AndroidInstrumentationTest = &testing.AndroidInstrumentationTest{}
if testAssets.isBundle {
testModel.TestSpecification.AndroidInstrumentationTest.AppBundle = &testing.AppBundle{
BundleLocation: &testing.FileReference{GcsPath: testAssets.testApp.GcsPath},
}
} else {
testModel.TestSpecification.AndroidInstrumentationTest.AppApk = &testing.FileReference{GcsPath: testAssets.testApp.GcsPath}
}
testModel.TestSpecification.AndroidInstrumentationTest.TestApk = &testing.FileReference{GcsPath: testAssets.TestApk.GcsPath}
if configs.AppPackageID != "" {
testModel.TestSpecification.AndroidInstrumentationTest.AppPackageId = configs.AppPackageID
}
if configs.InstTestPackageID != "" {
testModel.TestSpecification.AndroidInstrumentationTest.TestPackageId = configs.InstTestPackageID
}
if configs.InstTestRunnerClass != "" {
testModel.TestSpecification.AndroidInstrumentationTest.TestRunnerClass = configs.InstTestRunnerClass
}
if configs.InstTestTargets != "" {
targets := strings.Split(strings.TrimSpace(configs.InstTestTargets), ",")
testModel.TestSpecification.AndroidInstrumentationTest.TestTargets = targets
}
if configs.UseOrchestrator {
testModel.TestSpecification.AndroidInstrumentationTest.OrchestratorOption = "USE_ORCHESTRATOR"
} else {
testModel.TestSpecification.AndroidInstrumentationTest.OrchestratorOption = "DO_NOT_USE_ORCHESTRATOR"
}
log.Debugf("AndroidInstrumentationTest: %+v", testModel.TestSpecification.AndroidInstrumentationTest)
case testTypeRobo:
testModel.TestSpecification.AndroidRoboTest = &testing.AndroidRoboTest{}
if testAssets.isBundle {
testModel.TestSpecification.AndroidRoboTest.AppBundle = &testing.AppBundle{
BundleLocation: &testing.FileReference{GcsPath: testAssets.testApp.GcsPath},
}
} else {
testModel.TestSpecification.AndroidRoboTest.AppApk = &testing.FileReference{GcsPath: testAssets.testApp.GcsPath}
}
if configs.AppPackageID != "" {
testModel.TestSpecification.AndroidRoboTest.AppPackageId = configs.AppPackageID
}
if configs.RoboInitialActivity != "" {
testModel.TestSpecification.AndroidRoboTest.AppInitialActivity = configs.RoboInitialActivity
}
if configs.RoboMaxDepth != "" {
maxDepth, err := strconv.Atoi(configs.RoboMaxDepth)
if err != nil {
return fmt.Errorf("failed to parse string(%s) to integer, error: %s", configs.RoboMaxDepth, err)
}
testModel.TestSpecification.AndroidRoboTest.MaxDepth = int64(maxDepth)
}
if configs.RoboMaxSteps != "" {
maxSteps, err := strconv.Atoi(configs.RoboMaxSteps)
if err != nil {
return fmt.Errorf("failed to parse string(%s) to integer, error: %s", configs.RoboMaxSteps, err)
}
testModel.TestSpecification.AndroidRoboTest.MaxSteps = int64(maxSteps)
}
if configs.RoboDirectives != "" {
roboDirectives := []*testing.RoboDirective{}
scanner := bufio.NewScanner(strings.NewReader(configs.RoboDirectives))
for scanner.Scan() {
directive := scanner.Text()
directive = strings.TrimSpace(directive)
if directive == "" {
continue
}
directiveParams := strings.Split(directive, ",")
if len(directiveParams) != 3 {
return fmt.Errorf("invalid directive configuration: %s", directive)
}
roboDirectives = append(roboDirectives, &testing.RoboDirective{ResourceName: directiveParams[0], InputText: directiveParams[1], ActionType: directiveParams[2]})
}
testModel.TestSpecification.AndroidRoboTest.RoboDirectives = roboDirectives
}
if configs.RoboScenarioFile != "" {
log.Debugf("Robo scenario file: %s", testAssets.RoboScript.GcsPath)
testModel.TestSpecification.AndroidRoboTest.RoboScript = &testing.FileReference{
GcsPath: testAssets.RoboScript.GcsPath,
}
}
case "gameloop":
testModel.TestSpecification.AndroidTestLoop = &testing.AndroidTestLoop{}
if testAssets.isBundle {
testModel.TestSpecification.AndroidTestLoop.AppBundle = &testing.AppBundle{
BundleLocation: &testing.FileReference{GcsPath: testAssets.testApp.GcsPath},
}
} else {
testModel.TestSpecification.AndroidTestLoop.AppApk = &testing.FileReference{GcsPath: testAssets.testApp.GcsPath}
}
if configs.AppPackageID != "" {
testModel.TestSpecification.AndroidTestLoop.AppPackageId = configs.AppPackageID
}
if configs.LoopScenarios != "" {
loopScenarios := []int64{}
for _, scenarioStr := range strings.Split(strings.TrimSpace(configs.LoopScenarios), ",") {
scenario, err := strconv.Atoi(scenarioStr)
if err != nil {
return fmt.Errorf("failed to parse string(%s) to integer, error: %s", scenarioStr, err)
}
loopScenarios = append(loopScenarios, int64(scenario))
}
testModel.TestSpecification.AndroidTestLoop.Scenarios = loopScenarios
}
if configs.LoopScenarioLabels != "" {
scenarioLabels := strings.Split(strings.TrimSpace(configs.LoopScenarioLabels), ",")
testModel.TestSpecification.AndroidTestLoop.ScenarioLabels = scenarioLabels
}
}
jsonByte, err := json.Marshal(testModel)
if err != nil {
return fmt.Errorf("failed to marshal test model, error: %s", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonByte))
if err != nil {
return fmt.Errorf("failed to create http request, error: %s", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to get http response, error: %s", err)
}
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body, error: %s", err)
}
return fmt.Errorf("failed to start test: %d, error: %s", resp.StatusCode, string(body))
}
return nil
}