Skip to content

Commit

Permalink
Improve logging around test results deployment errors (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
godrei authored Jun 3, 2024
1 parent 5ae9364 commit 3c37618
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func collectFilesToDeploy(absDeployPth string, config Config, tmpDir string) (fi
log.Warnf("Nothing to deploy at %s", absDeployPth)
return
}

isDeployPathDir, err := pathutil.IsDirExists(absDeployPth)
if err != nil {
return nil, fmt.Errorf("failed to check if %s is a directory or a file: %s", absDeployPth, err)
Expand Down Expand Up @@ -400,7 +400,7 @@ func deployTestResults(config Config) {
log.Infof("Collecting test results...")
testResults, err := test.ParseTestResults(config.TestDeployDir)
if err != nil {
log.Warnf("error during parsing test results: ", err)
log.Warnf("Failed to parse test results: %s", err)
return
}
if len(testResults) == 0 {
Expand All @@ -412,13 +412,20 @@ func deployTestResults(config Config) {
if i == 0 {
log.Printf("List of test results:")
}
log.Printf("- %s (generated by the %s)", result.Name, stepNameWithIndex(result.StepInfo))
if len(result.ImagePaths) > 0 {
log.Printf("- %s (generated by the %s) with %d attachment(s):", result.Name, stepNameWithIndex(result.StepInfo), len(result.ImagePaths))
for _, pth := range result.ImagePaths {
log.Printf(" - %s", pth)
}
} else {
log.Printf("- %s (generated by the %s)", result.Name, stepNameWithIndex(result.StepInfo))
}
}

fmt.Println()
log.Infof("Deploying test results...")
if err := testResults.Upload(config.AddonAPIToken, config.AddonAPIBaseURL, config.AppSlug, config.BuildSlug); err != nil {
log.Warnf("Failed to upload test results: ", err)
log.Warnf("Failed to deploy test results: %s", err)
} else {
log.Donef("Success")
}
Expand Down
53 changes: 34 additions & 19 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ func httpCall(apiToken, method, url string, input io.Reader, output interface{})
if resp.StatusCode < 200 || 299 < resp.StatusCode {
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unsuccessful response code: %d and failed to read body, error: %s", resp.StatusCode, err)
log.Warnf("Failed to read response: %s", err)
return fmt.Errorf("unsuccessful status code: %d", resp.StatusCode)
}
return fmt.Errorf("unsuccessful response code: %d\nbody:\n%s", resp.StatusCode, bodyData)
return fmt.Errorf("unsuccessful status code: %d, response: %s", resp.StatusCode, bodyData)
}

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

// ParseTestResults ...
/*
ParseTestResults walks through the Test Deploy directory and parses all the Steps' test results.
The Test Deploy directory has the following directory structure:
test_results ($BITRISE_TEST_DEPLOY_DIR)
├── step_1_test_results ($BITRISE_TEST_RESULT_DIR)
│ ├── step-info.json
│ ├── test_run_1
│ │ ├── UnitTest.xml
│ │ └── test-info.json
│ └── test_run_2
│ ├── UITest.xml
│ └── test-info.json
└── step_2_test_results ($BITRISE_TEST_RESULT_DIR)
├── step-info.json
└── test_run
├── results.xml
├── screenshot_1.jpg
├── screenshot_2.jpeg
├── screenshot_3.png
└── test-info.json
*/
func ParseTestResults(testsRootDir string) (results Results, err error) {
// read dirs in base tests dir
// <root_tests_dir>
Expand Down Expand Up @@ -219,15 +242,7 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
// Upload ...
func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug string) error {
for _, result := range results {
if len(result.ImagePaths) > 0 {
log.Printf("Uploading: %s with attachments:", result.Name)
for _, pth := range result.ImagePaths {
log.Printf("- %s", pth)
}
log.Printf("")
} else {
log.Printf("Uploading: %s", result.Name)
}
log.Printf("Uploading: %s", result.Name)

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

uploadRequestBodyData, err := json.Marshal(uploadReq)
if err != nil {
return err
return fmt.Errorf("failed to json encode upload request: %w", err)
}

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

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

for _, upload := range uploadResponse.Assets {
for _, file := range result.ImagePaths {
if filepath.Base(file) == upload.FileName {
fi, err := os.Open(file)
if err != nil {
return err
return fmt.Errorf("failed to open test result attachment (%s): %w", file, err)
}
if err := httpCall("", http.MethodPut, upload.URL, fi, nil); err != nil {
return err
return fmt.Errorf("failed to upload test result attachment (%s): %w", file, err)
}
break
}
Expand All @@ -282,7 +297,7 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri

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

Expand Down

0 comments on commit 3c37618

Please sign in to comment.