Skip to content

Commit 6ee302e

Browse files
authored
Fix uncaught error in ATC Step (#5276)
* Restructure ATC Step * Add test * Add build unit comment
1 parent ed6f6a5 commit 6ee302e

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

cmd/abapEnvironmentRunATCCheck.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ import (
2222
"github.com/pkg/errors"
2323
)
2424

25-
func abapEnvironmentRunATCCheck(options abapEnvironmentRunATCCheckOptions, telemetryData *telemetry.CustomData) {
25+
func abapEnvironmentRunATCCheck(options abapEnvironmentRunATCCheckOptions, _ *telemetry.CustomData) {
2626
// Mapping for options
27-
subOptions := convertATCOptions(&options)
2827

2928
c := &command.Command{}
3029
c.Stdout(log.Entry().Writer())
@@ -33,42 +32,51 @@ func abapEnvironmentRunATCCheck(options abapEnvironmentRunATCCheckOptions, telem
3332
autils := abaputils.AbapUtils{
3433
Exec: c,
3534
}
36-
var err error
3735

3836
client := piperhttp.Client{}
3937
fileUtils := piperutils.Files{}
38+
39+
err := runAbapEnvironmentRunATCCheck(autils, client, options, fileUtils)
40+
if err != nil {
41+
log.Entry().WithError(err).Fatal("step execution failed")
42+
}
43+
}
44+
45+
func runAbapEnvironmentRunATCCheck(autils abaputils.AbapUtils, client piperhttp.Client, options abapEnvironmentRunATCCheckOptions, fileUtils piperutils.Files) error {
46+
47+
var details abaputils.ConnectionDetailsHTTP
4048
cookieJar, _ := cookiejar.New(nil)
4149
clientOptions := piperhttp.ClientOptions{
4250
CookieJar: cookieJar,
4351
}
4452
client.SetOptions(clientOptions)
4553

46-
var details abaputils.ConnectionDetailsHTTP
47-
// If Host flag is empty read ABAP endpoint from Service Key instead. Otherwise take ABAP system endpoint from config instead
48-
if err == nil {
49-
details, err = autils.GetAbapCommunicationArrangementInfo(subOptions, "")
54+
subOptions := convertATCOptions(&options)
55+
details, err := autils.GetAbapCommunicationArrangementInfo(subOptions, "")
56+
if err != nil {
57+
return err
5058
}
51-
var resp *http.Response
52-
// Fetch Xcrsf-Token
53-
if err == nil {
54-
credentialsOptions := piperhttp.ClientOptions{
55-
Username: details.User,
56-
Password: details.Password,
57-
CookieJar: cookieJar,
58-
}
59-
client.SetOptions(credentialsOptions)
60-
details.XCsrfToken, err = fetchXcsrfToken("GET", details, nil, &client)
59+
60+
credentialsOptions := piperhttp.ClientOptions{
61+
Username: details.User,
62+
Password: details.Password,
63+
CookieJar: cookieJar,
6164
}
62-
if err == nil {
63-
resp, err = triggerATCRun(options, details, &client)
65+
client.SetOptions(credentialsOptions)
66+
details.XCsrfToken, err = fetchXcsrfToken("GET", details, nil, &client)
67+
if err != nil {
68+
return err
6469
}
65-
if err == nil {
66-
if err = fetchAndPersistATCResults(resp, details, &client, &fileUtils, options.AtcResultsFileName, options.GenerateHTML, options.FailOnSeverity); err != nil {
67-
log.Entry().WithError(err).Fatal("step execution failed")
68-
}
70+
resp, err := triggerATCRun(options, details, &client)
71+
if err != nil {
72+
return err
73+
}
74+
if err = fetchAndPersistATCResults(resp, details, &client, &fileUtils, options.AtcResultsFileName, options.GenerateHTML, options.FailOnSeverity); err != nil {
75+
return err
6976
}
7077

7178
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")
79+
return nil
7280
}
7381

7482
func fetchAndPersistATCResults(resp *http.Response, details abaputils.ConnectionDetailsHTTP, client piperhttp.Sender, utils piperutils.FileUtils, atcResultFileName string, generateHTML bool, failOnSeverityLevel string) error {

cmd/abapEnvironmentRunATCCheck_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,30 @@ import (
99
"testing"
1010

1111
"github.com/SAP/jenkins-library/pkg/abaputils"
12+
piperhttp "github.com/SAP/jenkins-library/pkg/http"
1213
"github.com/SAP/jenkins-library/pkg/mock"
14+
"github.com/SAP/jenkins-library/pkg/piperutils"
1315
"github.com/stretchr/testify/assert"
1416
)
1517

18+
func TestCLIFailure(t *testing.T) {
19+
t.Run("function error leads to pipeline error", func(t *testing.T) {
20+
21+
execRunner := &mock.ExecMockRunner{}
22+
autils := abaputils.AbapUtils{
23+
Exec: execRunner,
24+
}
25+
options := abapEnvironmentRunATCCheckOptions{}
26+
fileUtils := piperutils.Files{}
27+
client := piperhttp.Client{}
28+
29+
err := runAbapEnvironmentRunATCCheck(autils, client, options, fileUtils)
30+
31+
assert.Error(t, err)
32+
33+
})
34+
}
35+
1636
func TestHostConfig(t *testing.T) {
1737
t.Run("Check Host: ABAP Endpoint", func(t *testing.T) {
1838
config := abaputils.AbapEnvironmentOptions{

0 commit comments

Comments
 (0)