Skip to content

Commit 3c37618

Browse files
authored
Improve logging around test results deployment errors (#201)
1 parent 5ae9364 commit 3c37618

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func collectFilesToDeploy(absDeployPth string, config Config, tmpDir string) (fi
342342
log.Warnf("Nothing to deploy at %s", absDeployPth)
343343
return
344344
}
345-
345+
346346
isDeployPathDir, err := pathutil.IsDirExists(absDeployPth)
347347
if err != nil {
348348
return nil, fmt.Errorf("failed to check if %s is a directory or a file: %s", absDeployPth, err)
@@ -400,7 +400,7 @@ func deployTestResults(config Config) {
400400
log.Infof("Collecting test results...")
401401
testResults, err := test.ParseTestResults(config.TestDeployDir)
402402
if err != nil {
403-
log.Warnf("error during parsing test results: ", err)
403+
log.Warnf("Failed to parse test results: %s", err)
404404
return
405405
}
406406
if len(testResults) == 0 {
@@ -412,13 +412,20 @@ func deployTestResults(config Config) {
412412
if i == 0 {
413413
log.Printf("List of test results:")
414414
}
415-
log.Printf("- %s (generated by the %s)", result.Name, stepNameWithIndex(result.StepInfo))
415+
if len(result.ImagePaths) > 0 {
416+
log.Printf("- %s (generated by the %s) with %d attachment(s):", result.Name, stepNameWithIndex(result.StepInfo), len(result.ImagePaths))
417+
for _, pth := range result.ImagePaths {
418+
log.Printf(" - %s", pth)
419+
}
420+
} else {
421+
log.Printf("- %s (generated by the %s)", result.Name, stepNameWithIndex(result.StepInfo))
422+
}
416423
}
417424

418425
fmt.Println()
419426
log.Infof("Deploying test results...")
420427
if err := testResults.Upload(config.AddonAPIToken, config.AddonAPIBaseURL, config.AppSlug, config.BuildSlug); err != nil {
421-
log.Warnf("Failed to upload test results: ", err)
428+
log.Warnf("Failed to deploy test results: %s", err)
422429
} else {
423430
log.Donef("Success")
424431
}

test/test.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ func httpCall(apiToken, method, url string, input io.Reader, output interface{})
8080
if resp.StatusCode < 200 || 299 < resp.StatusCode {
8181
bodyData, err := ioutil.ReadAll(resp.Body)
8282
if err != nil {
83-
return fmt.Errorf("unsuccessful response code: %d and failed to read body, error: %s", resp.StatusCode, err)
83+
log.Warnf("Failed to read response: %s", err)
84+
return fmt.Errorf("unsuccessful status code: %d", resp.StatusCode)
8485
}
85-
return fmt.Errorf("unsuccessful response code: %d\nbody:\n%s", resp.StatusCode, bodyData)
86+
return fmt.Errorf("unsuccessful status code: %d, response: %s", resp.StatusCode, bodyData)
8687
}
8788

8889
if output != nil {
@@ -103,7 +104,29 @@ func findImages(testDir string) (imageFilePaths []string) {
103104
return
104105
}
105106

106-
// ParseTestResults ...
107+
/*
108+
ParseTestResults walks through the Test Deploy directory and parses all the Steps' test results.
109+
110+
The Test Deploy directory has the following directory structure:
111+
112+
test_results ($BITRISE_TEST_DEPLOY_DIR)
113+
├── step_1_test_results ($BITRISE_TEST_RESULT_DIR)
114+
│ ├── step-info.json
115+
│ ├── test_run_1
116+
│ │ ├── UnitTest.xml
117+
│ │ └── test-info.json
118+
│ └── test_run_2
119+
│ ├── UITest.xml
120+
│ └── test-info.json
121+
└── step_2_test_results ($BITRISE_TEST_RESULT_DIR)
122+
├── step-info.json
123+
└── test_run
124+
├── results.xml
125+
├── screenshot_1.jpg
126+
├── screenshot_2.jpeg
127+
├── screenshot_3.png
128+
└── test-info.json
129+
*/
107130
func ParseTestResults(testsRootDir string) (results Results, err error) {
108131
// read dirs in base tests dir
109132
// <root_tests_dir>
@@ -219,15 +242,7 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
219242
// Upload ...
220243
func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug string) error {
221244
for _, result := range results {
222-
if len(result.ImagePaths) > 0 {
223-
log.Printf("Uploading: %s with attachments:", result.Name)
224-
for _, pth := range result.ImagePaths {
225-
log.Printf("- %s", pth)
226-
}
227-
log.Printf("")
228-
} else {
229-
log.Printf("Uploading: %s", result.Name)
230-
}
245+
log.Printf("Uploading: %s", result.Name)
231246

232247
uploadReq := UploadRequest{
233248
FileInfo: FileInfo{
@@ -240,7 +255,7 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
240255
for _, asset := range result.ImagePaths {
241256
fi, err := os.Stat(asset)
242257
if err != nil {
243-
return err
258+
return fmt.Errorf("failed to get file info for %s: %w", asset, err)
244259
}
245260
uploadReq.Assets = append(uploadReq.Assets, FileInfo{
246261
FileName: filepath.Base(asset),
@@ -250,30 +265,30 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
250265

251266
uploadRequestBodyData, err := json.Marshal(uploadReq)
252267
if err != nil {
253-
return err
268+
return fmt.Errorf("failed to json encode upload request: %w", err)
254269
}
255270

256271
var (
257272
uploadResponse UploadResponse
258273
uploadRequestURL = fmt.Sprintf("%s/apps/%s/builds/%s/test_reports", endpointBaseURL, appSlug, buildSlug)
259274
)
260275
if err := httpCall(apiToken, http.MethodPost, uploadRequestURL, bytes.NewReader(uploadRequestBodyData), &uploadResponse); err != nil {
261-
return err
276+
return fmt.Errorf("failed to initialise test result: %w", err)
262277
}
263278

264279
if err := httpCall("", http.MethodPut, uploadResponse.URL, bytes.NewReader(result.XMLContent), nil); err != nil {
265-
return err
280+
return fmt.Errorf("failed to upload test result xml: %w", err)
266281
}
267282

268283
for _, upload := range uploadResponse.Assets {
269284
for _, file := range result.ImagePaths {
270285
if filepath.Base(file) == upload.FileName {
271286
fi, err := os.Open(file)
272287
if err != nil {
273-
return err
288+
return fmt.Errorf("failed to open test result attachment (%s): %w", file, err)
274289
}
275290
if err := httpCall("", http.MethodPut, upload.URL, fi, nil); err != nil {
276-
return err
291+
return fmt.Errorf("failed to upload test result attachment (%s): %w", file, err)
277292
}
278293
break
279294
}
@@ -282,7 +297,7 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
282297

283298
var uploadPatchURL = fmt.Sprintf("%s/apps/%s/builds/%s/test_reports/%s", endpointBaseURL, appSlug, buildSlug, uploadResponse.ID)
284299
if err := httpCall(apiToken, http.MethodPatch, uploadPatchURL, strings.NewReader(`{"uploaded":true}`), nil); err != nil {
285-
return err
300+
return fmt.Errorf("failed to finalise test result: %w", err)
286301
}
287302
}
288303

0 commit comments

Comments
 (0)