Skip to content

Commit

Permalink
Fix uncaught error in ATC Step (#5276)
Browse files Browse the repository at this point in the history
* Restructure ATC Step

* Add test

* Add build unit comment
  • Loading branch information
DanielMieg authored Feb 20, 2025
1 parent ed6f6a5 commit 6ee302e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
54 changes: 31 additions & 23 deletions cmd/abapEnvironmentRunATCCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
"github.com/pkg/errors"
)

func abapEnvironmentRunATCCheck(options abapEnvironmentRunATCCheckOptions, telemetryData *telemetry.CustomData) {
func abapEnvironmentRunATCCheck(options abapEnvironmentRunATCCheckOptions, _ *telemetry.CustomData) {
// Mapping for options
subOptions := convertATCOptions(&options)

c := &command.Command{}
c.Stdout(log.Entry().Writer())
Expand All @@ -33,42 +32,51 @@ func abapEnvironmentRunATCCheck(options abapEnvironmentRunATCCheckOptions, telem
autils := abaputils.AbapUtils{
Exec: c,
}
var err error

client := piperhttp.Client{}
fileUtils := piperutils.Files{}

err := runAbapEnvironmentRunATCCheck(autils, client, options, fileUtils)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}

func runAbapEnvironmentRunATCCheck(autils abaputils.AbapUtils, client piperhttp.Client, options abapEnvironmentRunATCCheckOptions, fileUtils piperutils.Files) error {

var details abaputils.ConnectionDetailsHTTP
cookieJar, _ := cookiejar.New(nil)
clientOptions := piperhttp.ClientOptions{
CookieJar: cookieJar,
}
client.SetOptions(clientOptions)

var details abaputils.ConnectionDetailsHTTP
// If Host flag is empty read ABAP endpoint from Service Key instead. Otherwise take ABAP system endpoint from config instead
if err == nil {
details, err = autils.GetAbapCommunicationArrangementInfo(subOptions, "")
subOptions := convertATCOptions(&options)
details, err := autils.GetAbapCommunicationArrangementInfo(subOptions, "")
if err != nil {
return err
}
var resp *http.Response
// Fetch Xcrsf-Token
if err == nil {
credentialsOptions := piperhttp.ClientOptions{
Username: details.User,
Password: details.Password,
CookieJar: cookieJar,
}
client.SetOptions(credentialsOptions)
details.XCsrfToken, err = fetchXcsrfToken("GET", details, nil, &client)

credentialsOptions := piperhttp.ClientOptions{
Username: details.User,
Password: details.Password,
CookieJar: cookieJar,
}
if err == nil {
resp, err = triggerATCRun(options, details, &client)
client.SetOptions(credentialsOptions)
details.XCsrfToken, err = fetchXcsrfToken("GET", details, nil, &client)
if err != nil {
return err
}
if err == nil {
if err = fetchAndPersistATCResults(resp, details, &client, &fileUtils, options.AtcResultsFileName, options.GenerateHTML, options.FailOnSeverity); err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
resp, err := triggerATCRun(options, details, &client)
if err != nil {
return err
}
if err = fetchAndPersistATCResults(resp, details, &client, &fileUtils, options.AtcResultsFileName, options.GenerateHTML, options.FailOnSeverity); err != nil {
return err
}

log.Entry().Info("ATC run completed successfully. If there are any results from the respective run they will be listed in the logs above as well as being saved in the output .xml file")
return nil
}

func fetchAndPersistATCResults(resp *http.Response, details abaputils.ConnectionDetailsHTTP, client piperhttp.Sender, utils piperutils.FileUtils, atcResultFileName string, generateHTML bool, failOnSeverityLevel string) error {
Expand Down
20 changes: 20 additions & 0 deletions cmd/abapEnvironmentRunATCCheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ import (
"testing"

"github.com/SAP/jenkins-library/pkg/abaputils"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/stretchr/testify/assert"
)

func TestCLIFailure(t *testing.T) {
t.Run("function error leads to pipeline error", func(t *testing.T) {

execRunner := &mock.ExecMockRunner{}
autils := abaputils.AbapUtils{
Exec: execRunner,
}
options := abapEnvironmentRunATCCheckOptions{}
fileUtils := piperutils.Files{}
client := piperhttp.Client{}

err := runAbapEnvironmentRunATCCheck(autils, client, options, fileUtils)

assert.Error(t, err)

})
}

func TestHostConfig(t *testing.T) {
t.Run("Check Host: ABAP Endpoint", func(t *testing.T) {
config := abaputils.AbapEnvironmentOptions{
Expand Down

0 comments on commit 6ee302e

Please sign in to comment.