mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-30 05:59:39 +02:00
Adds retry mechanism for whitesource in case the download of the unified agent or JRE fails (#2961)
This commit is contained in:
parent
62bf70015c
commit
e9d8175c9b
@ -9,6 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/SAP/jenkins-library/pkg/log"
|
"github.com/SAP/jenkins-library/pkg/log"
|
||||||
@ -176,6 +177,17 @@ func downloadAgent(config *ScanOptions, utils Utils) error {
|
|||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
err := utils.DownloadFile(config.AgentDownloadURL, agentFile, nil, nil)
|
err := utils.DownloadFile(config.AgentDownloadURL, agentFile, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
// we check if the copy error occurs and retry the download
|
||||||
|
// if the copy error did not happen, we rerun the whole download mechanism once
|
||||||
|
if strings.Contains(err.Error(), "unable to copy content from url to file") {
|
||||||
|
// retry the download once again
|
||||||
|
log.Entry().Warnf("Previous Download failed due to %v", err)
|
||||||
|
err = nil // reset error to nil
|
||||||
|
err = utils.DownloadFile(config.AgentDownloadURL, agentFile, nil, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to download unified agent from URL '%s' to file '%s'", config.AgentDownloadURL, agentFile)
|
return errors.Wrapf(err, "failed to download unified agent from URL '%s' to file '%s'", config.AgentDownloadURL, agentFile)
|
||||||
}
|
}
|
||||||
@ -193,7 +205,18 @@ func downloadJre(config *ScanOptions, utils Utils) (string, error) {
|
|||||||
javaPath := "java"
|
javaPath := "java"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Entry().Infof("No Java installation found, downloading JVM from %v", config.JreDownloadURL)
|
log.Entry().Infof("No Java installation found, downloading JVM from %v", config.JreDownloadURL)
|
||||||
err := utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil)
|
err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
// we check if the copy error occurs and retry the download
|
||||||
|
// if the copy error did not happen, we rerun the whole download mechanism once
|
||||||
|
if strings.Contains(err.Error(), "unable to copy content from url to file") {
|
||||||
|
// retry the download once again
|
||||||
|
log.Entry().Warnf("Previous Download failed due to %v", err)
|
||||||
|
err = nil
|
||||||
|
err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrapf(err, "failed to download jre from URL '%s'", config.JreDownloadURL)
|
return "", errors.Wrapf(err, "failed to download jre from URL '%s'", config.JreDownloadURL)
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,11 @@ package whitesource
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/SAP/jenkins-library/pkg/log"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/SAP/jenkins-library/pkg/log"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExecuteUAScan(t *testing.T) {
|
func TestExecuteUAScan(t *testing.T) {
|
||||||
@ -291,6 +290,17 @@ func TestDownloadAgent(t *testing.T) {
|
|||||||
err := downloadAgent(&config, utilsMock)
|
err := downloadAgent(&config, utilsMock)
|
||||||
assert.Contains(t, fmt.Sprint(err), "failed to download unified agent from URL")
|
assert.Contains(t, fmt.Sprint(err), "failed to download unified agent from URL")
|
||||||
})
|
})
|
||||||
|
t.Run("error - download with retry", func(t *testing.T) {
|
||||||
|
config := ScanOptions{
|
||||||
|
AgentDownloadURL: "errorCopyFile", // Misusing this ScanOptions to tell DownloadFile Mock to raise an error
|
||||||
|
AgentFileName: "unified-agent.jar",
|
||||||
|
}
|
||||||
|
utilsMock := NewScanUtilsMock()
|
||||||
|
utilsMock.DownloadError = map[string]error{"https://download.ua.org/agent.jar": fmt.Errorf("unable to copy content from url to file")}
|
||||||
|
|
||||||
|
err := downloadAgent(&config, utilsMock)
|
||||||
|
assert.Contains(t, fmt.Sprint(err), "unable to copy content from url to file")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownloadJre(t *testing.T) {
|
func TestDownloadJre(t *testing.T) {
|
||||||
@ -350,6 +360,18 @@ func TestDownloadJre(t *testing.T) {
|
|||||||
assert.Contains(t, fmt.Sprint(err), "failed to download jre from URL")
|
assert.Contains(t, fmt.Sprint(err), "failed to download jre from URL")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("error - download with retry", func(t *testing.T) {
|
||||||
|
config := ScanOptions{
|
||||||
|
JreDownloadURL: "errorCopyFile",
|
||||||
|
}
|
||||||
|
utilsMock := NewScanUtilsMock()
|
||||||
|
utilsMock.ShouldFailOnCommand = map[string]error{"java": fmt.Errorf("failed to run java")}
|
||||||
|
//utilsMock.DownloadError = map[string]error{"https://download.jre.org/jvm.jar": fmt.Errorf("failed to download file")}
|
||||||
|
|
||||||
|
_, err := downloadJre(&config, utilsMock)
|
||||||
|
assert.Contains(t, fmt.Sprint(err), "unable to copy content from url to file")
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("error - tar execution", func(t *testing.T) {
|
t.Run("error - tar execution", func(t *testing.T) {
|
||||||
config := ScanOptions{
|
config := ScanOptions{
|
||||||
JreDownloadURL: "https://download.jre.org/jvm.jar",
|
JreDownloadURL: "https://download.jre.org/jvm.jar",
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/SAP/jenkins-library/pkg/mock"
|
"github.com/SAP/jenkins-library/pkg/mock"
|
||||||
"github.com/SAP/jenkins-library/pkg/piperutils"
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newTestScan(config *ScanOptions) *Scan {
|
func newTestScan(config *ScanOptions) *Scan {
|
||||||
@ -67,6 +68,9 @@ func (m *ScanUtilsMock) InstallAllNPMDependencies(_ *ScanOptions, packageJSONs [
|
|||||||
|
|
||||||
// DownloadFile mimics http.Downloader and records the downloaded file.
|
// DownloadFile mimics http.Downloader and records the downloaded file.
|
||||||
func (m *ScanUtilsMock) DownloadFile(url, filename string, _ http.Header, _ []*http.Cookie) error {
|
func (m *ScanUtilsMock) DownloadFile(url, filename string, _ http.Header, _ []*http.Cookie) error {
|
||||||
|
if url == "errorCopyFile" {
|
||||||
|
return errors.New("unable to copy content from url to file")
|
||||||
|
}
|
||||||
if m.DownloadError[url] != nil {
|
if m.DownloadError[url] != nil {
|
||||||
return m.DownloadError[url]
|
return m.DownloadError[url]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user