Skip to content

Commit

Permalink
Retry test results deployment http calls (#202)
Browse files Browse the repository at this point in the history
* Retry test results deployment http calls

* Wire in v2 logger for test results deployment

* Fix missing v2 loggers

* Fix master merge
  • Loading branch information
godrei authored Jun 5, 2024
1 parent 8831a88 commit 45842c8
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 131 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/bitrise-io/go-steputils v1.0.5
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.20
github.com/bitrise-io/go-utils v1.0.12
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.20
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.22
github.com/bitrise-io/go-xcode v1.0.18
github.com/bitrise-io/go-xcode/v2 v2.0.0-alpha.44
github.com/google/go-cmp v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/bitrise-io/go-utils v1.0.2/go.mod h1:ZY1DI+fEpZuFpO9szgDeICM4QbqoWVt0
github.com/bitrise-io/go-utils v1.0.12 h1:iJV1ZpyvSA0NCte/N6x+aIQ9TrNr5sIBlcJBf0dn1dE=
github.com/bitrise-io/go-utils v1.0.12/go.mod h1:ZY1DI+fEpZuFpO9szgDeICM4QbqoWVt0RSY3tRI1heY=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.10/go.mod h1:Ta/ards3Ih/3Q6X8tBtcj6zTHcNf1hRSXv1E8lPgIYk=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.20 h1:R+xJRWsuHhF/Pnx0gjI1+HH4Y0YSFVI+U/CbLpSx4sU=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.20/go.mod h1:Laih4ji980SQkRgdnMCH0g4u2GZI/5nnbqmYT9UfKFQ=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.22 h1:/SD9xE4LlX/Ju9YZ+n/yW/uDs7hXMdFlXg4Nxlb7678=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.22/go.mod h1:Laih4ji980SQkRgdnMCH0g4u2GZI/5nnbqmYT9UfKFQ=
github.com/bitrise-io/go-xcode v1.0.18 h1:guFywV/AwcZuexqIQkL1ixc3QThpbJvA4voa9MqvPto=
github.com/bitrise-io/go-xcode v1.0.18/go.mod h1:9OwsvrhZ4A2JxHVoEY7CPcABAKA+OE7FQqFfBfvbFuY=
github.com/bitrise-io/go-xcode/v2 v2.0.0-alpha.44 h1:TTPF1W/9RcwIXdT2GY3xRj8f4h22JZUwJNLpqQYJHoQ=
Expand Down
186 changes: 93 additions & 93 deletions main.go

Large diffs are not rendered by default.

43 changes: 23 additions & 20 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (

"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
logV2 "github.com/bitrise-io/go-utils/v2/log"
"github.com/bitrise-io/go-utils/v2/retryhttp"
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/test/converters"
"github.com/hashicorp/go-retryablehttp"
)

// FileInfo ...
Expand Down Expand Up @@ -57,30 +59,31 @@ type Result struct {
// Results ...
type Results []Result

func httpCall(apiToken, method, url string, input io.Reader, output interface{}) error {
func httpCall(apiToken, method, url string, input io.Reader, output interface{}, logger logV2.Logger) error {
if apiToken != "" {
url = url + "/" + apiToken
}
req, err := http.NewRequest(method, url, input)
req, err := retryablehttp.NewRequest(method, url, input)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
client := retryhttp.NewClient(logger)
resp, err := client.Do(req)
if err != nil {
return err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.Warnf("Failed to close body: %s", err)
logger.Warnf("Failed to close body: %s", err)
}
}()

if resp.StatusCode < 200 || 299 < resp.StatusCode {
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warnf("Failed to read response: %s", err)
logger.Warnf("Failed to read response: %s", err)
return fmt.Errorf("unsuccessful status code: %d", resp.StatusCode)
}
return fmt.Errorf("unsuccessful status code: %d, response: %s", resp.StatusCode, bodyData)
Expand Down Expand Up @@ -127,7 +130,7 @@ The Test Deploy directory has the following directory structure:
├── screenshot_3.png
└── test-info.json
*/
func ParseTestResults(testsRootDir string) (results Results, err error) {
func ParseTestResults(testsRootDir string, logger logV2.Logger) (results Results, err error) {
// read dirs in base tests dir
// <root_tests_dir>

Expand All @@ -142,7 +145,7 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
testDirPath := filepath.Join(testsRootDir, testDir.Name())

if !testDir.IsDir() {
log.Debugf("%s is not a directory", testDirPath)
logger.Debugf("%s is not a directory", testDirPath)
continue
}

Expand All @@ -157,7 +160,7 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {

stepInfoPth := filepath.Join(testDirPath, "step-info.json")
if isExists, err := pathutil.IsPathExists(stepInfoPth); err != nil {
log.Warnf("Failed to check if step-info.json file exists in dir: %s: %s", testDirPath, err)
logger.Warnf("Failed to check if step-info.json file exists in dir: %s: %s", testDirPath, err)
continue
} else if !isExists {
continue
Expand All @@ -166,12 +169,12 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
var stepInfo *models.TestResultStepInfo
stepInfoFileContent, err := fileutil.ReadBytesFromFile(stepInfoPth)
if err != nil {
log.Warnf("Failed to read step-info.json file in dir: %s, error: %s", testDirPath, err)
logger.Warnf("Failed to read step-info.json file in dir: %s, error: %s", testDirPath, err)
continue
}

if err := json.Unmarshal(stepInfoFileContent, &stepInfo); err != nil {
log.Warnf("Failed to parse step-info.json file in dir: %s, error: %s", testDirPath, err)
logger.Warnf("Failed to parse step-info.json file in dir: %s, error: %s", testDirPath, err)
continue
}

Expand All @@ -190,12 +193,12 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {

// get the converter that can manage test type contained in the dir
for _, converter := range converters.List() {
log.Debugf("Running converter: %T", converter)
logger.Debugf("Running converter: %T", converter)

// skip if couldn't find converter for content type
detected := converter.Detect(testFiles)

log.Debugf("known test result detected: %v", detected)
logger.Debugf("known test result detected: %v", detected)

if detected {
// test-info.json file is required
Expand Down Expand Up @@ -224,7 +227,7 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
// so here I will have image paths, xml data, and step info per test dir in a bundle info
images := findImages(testPhaseDirPath)

log.Debugf("found images: %d", len(images))
logger.Debugf("found images: %d", len(images))

results = append(results, Result{
Name: testInfo.Name,
Expand All @@ -240,9 +243,9 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
}

// Upload ...
func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug string) error {
func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug string, logger logV2.Logger) error {
for _, result := range results {
log.Printf("Uploading: %s", result.Name)
logger.Printf("Uploading: %s", result.Name)

uploadReq := UploadRequest{
FileInfo: FileInfo{
Expand Down Expand Up @@ -272,11 +275,11 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
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 {
if err := httpCall(apiToken, http.MethodPost, uploadRequestURL, bytes.NewReader(uploadRequestBodyData), &uploadResponse, logger); err != nil {
return fmt.Errorf("failed to initialise test result: %w", err)
}

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

Expand All @@ -287,7 +290,7 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
if err != nil {
return fmt.Errorf("failed to open test result attachment (%s): %w", file, err)
}
if err := httpCall("", http.MethodPut, upload.URL, fi, nil); err != nil {
if err := httpCall("", http.MethodPut, upload.URL, fi, nil, logger); err != nil {
return fmt.Errorf("failed to upload test result attachment (%s): %w", file, err)
}
break
Expand All @@ -296,7 +299,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 {
if err := httpCall(apiToken, http.MethodPatch, uploadPatchURL, strings.NewReader(`{"uploaded":true}`), nil, logger); err != nil {
return fmt.Errorf("failed to finalise test result: %w", err)
}
}
Expand Down
20 changes: 10 additions & 10 deletions test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"testing"
"time"

"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/env"
logV2 "github.com/bitrise-io/go-utils/v2/log"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func createDummyFilesInDirWithContent(dir, content string, fileNames []string) error {
Expand Down Expand Up @@ -157,7 +157,7 @@ func Test_Upload(t *testing.T) {

time.Sleep(time.Second)

if err := results.Upload("access-token", "http://localhost:8893/test", "test-app-slug", "test-build-slug"); err != nil {
if err := results.Upload("access-token", "http://localhost:8893/test", "test-app-slug", "test-build-slug", logV2.NewLogger()); err != nil {
t.Fatalf("%v", errors.WithStack(err))
return
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func Test_ParseXctestResults(t *testing.T) {
t.Fatal("failed to create temp dir, error:", err)
}

bundle, err := ParseTestResults(testsDir)
bundle, err := ParseTestResults(testsDir, logV2.NewLogger())
if err != nil {
t.Fatal("failed to get bundle, error:", err)
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func Test_ParseXctestResults(t *testing.T) {
t.Fatal("failed to create dummy files in dir, error:", err)
}

bundle, err := ParseTestResults(testsDir)
bundle, err := ParseTestResults(testsDir, logV2.NewLogger())
if err != nil {
t.Fatal("failed to get bundle, error:", err)
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func Test_ParseXctestResults(t *testing.T) {
t.Fatal("failed to create dummy files in dir, error:", err)
}

bundle, err := ParseTestResults(testsDir)
bundle, err := ParseTestResults(testsDir, logV2.NewLogger())
if err != nil {
t.Fatal("failed to get bundle, error:", err)
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func Test_ParseXctest3Results(t *testing.T) {
err = copyCmd.Run()
require.NoError(t, err)

bundle, err := ParseTestResults(testDir)
bundle, err := ParseTestResults(testDir, logV2.NewLogger())
require.NoError(t, err)

want, err := fileutil.ReadStringFromFile(filepath.Join("testdata", "ios_device_config_xml_output.golden"))
Expand Down
9 changes: 6 additions & 3 deletions vendor/github.com/bitrise-io/go-utils/v2/command/command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions vendor/github.com/bitrise-io/go-utils/v2/command/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/bitrise-io/go-utils/v2/retryhttp/retryhttp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ github.com/bitrise-io/go-utils/retry
github.com/bitrise-io/go-utils/sliceutil
github.com/bitrise-io/go-utils/urlutil
github.com/bitrise-io/go-utils/ziputil
# github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.20
# github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.22
## explicit; go 1.17
github.com/bitrise-io/go-utils/v2/command
github.com/bitrise-io/go-utils/v2/env
Expand All @@ -50,6 +50,7 @@ github.com/bitrise-io/go-utils/v2/log
github.com/bitrise-io/go-utils/v2/log/colorstring
github.com/bitrise-io/go-utils/v2/pathutil
github.com/bitrise-io/go-utils/v2/redactwriter
github.com/bitrise-io/go-utils/v2/retryhttp
# github.com/bitrise-io/go-xcode v1.0.18
## explicit; go 1.20
github.com/bitrise-io/go-xcode/certificateutil
Expand Down

0 comments on commit 45842c8

Please sign in to comment.