1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Add Execution Log (#3863)

* Add Execution Log

* Adapt Tests

* Remove log
This commit is contained in:
Daniel Mieg
2022-06-29 15:50:33 +02:00
committed by GitHub
parent cf617cb30e
commit 8923d1e583
5 changed files with 127 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -11,6 +12,25 @@ import (
"github.com/stretchr/testify/assert"
)
var executionLogStringCheckout string
func init() {
executionLog := abaputils.PullEntity{
ToExecutionLog: abaputils.AbapLogs{
Results: []abaputils.LogResults{
{
Index: "1",
Type: "LogEntry",
Description: "S",
Timestamp: "/Date(1644332299000+0000)/",
},
},
},
}
executionLogResponse, _ := json.Marshal(executionLog)
executionLogStringCheckout = string(executionLogResponse)
}
func TestCheckoutBranchStep(t *testing.T) {
t.Run("Run Step Successful - repositoryName and branchName config", func(t *testing.T) {
@@ -36,6 +56,7 @@ func TestCheckoutBranchStep(t *testing.T) {
logResultSuccess := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
`{"d" : ` + executionLogStringCheckout + `}`,
logResultSuccess,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "S" } }`,
@@ -64,6 +85,7 @@ func TestCheckoutBranchStep(t *testing.T) {
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
`{"d" : ` + executionLogStringCheckout + `}`,
logResultError,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
@@ -102,6 +124,7 @@ func TestCheckoutBranchStep(t *testing.T) {
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
`{"d" : ` + executionLogStringCheckout + `}`,
logResultError,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,

View File

@@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -10,6 +11,25 @@ import (
"github.com/stretchr/testify/assert"
)
var executionLogStringClone string
func init() {
executionLog := abaputils.PullEntity{
ToExecutionLog: abaputils.AbapLogs{
Results: []abaputils.LogResults{
{
Index: "1",
Type: "LogEntry",
Description: "S",
Timestamp: "/Date(1644332299000+0000)/",
},
},
},
}
executionLogResponse, _ := json.Marshal(executionLog)
executionLogStringClone = string(executionLogResponse)
}
func TestCloneStep(t *testing.T) {
t.Run("Run Step - Successful", func(t *testing.T) {
var autils = abaputils.AUtilsMock{}
@@ -61,18 +81,21 @@ repositories:
logResultSuccess := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
`{"d" : ` + executionLogStringClone + `}`,
logResultSuccess,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "S" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : ` + executionLogStringClone + `}`,
logResultSuccess,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "S" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : { "status" : "R" } }`,
`{"d" : ` + executionLogStringClone + `}`,
logResultSuccess,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "S" } }`,
@@ -172,6 +195,7 @@ repositories:
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
`{"d" : ` + executionLogStringClone + `}`,
logResultError,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,

View File

@@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -12,6 +13,27 @@ import (
"github.com/stretchr/testify/assert"
)
var executionLogStringPull string
var logResultErrorPull string
func init() {
executionLog := abaputils.PullEntity{
ToExecutionLog: abaputils.AbapLogs{
Results: []abaputils.LogResults{
{
Index: "1",
Type: "LogEntry",
Description: "S",
Timestamp: "/Date(1644332299000+0000)/",
},
},
},
}
executionLogResponse, _ := json.Marshal(executionLog)
executionLogStringPull = string(executionLogResponse)
logResultErrorPull = fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
}
func TestPullStep(t *testing.T) {
t.Run("Run Step Successful", func(t *testing.T) {
@@ -36,6 +58,7 @@ func TestPullStep(t *testing.T) {
logResultSuccess := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
`{"d" : ` + executionLogStringPull + `}`,
logResultSuccess,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "S" } }`,
@@ -170,11 +193,10 @@ repositories:
Password: "testPassword",
Repositories: "filename.yaml",
}
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
logResultError,
`{"d" : ` + executionLogStringPull + `}`,
logResultErrorPull,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,
@@ -232,10 +254,10 @@ repositories:
Repositories: "filename.yaml",
IgnoreCommit: true,
}
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
logResultError,
`{"d" : ` + executionLogStringPull + `}`,
logResultErrorPull,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,
@@ -271,10 +293,10 @@ repositories:
CommitID: "123456",
IgnoreCommit: false,
}
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
logResultError,
`{"d" : ` + executionLogStringPull + `}`,
logResultErrorPull,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,
@@ -309,10 +331,10 @@ repositories:
RepositoryName: "/DMO/SWC",
IgnoreCommit: false,
}
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &abaputils.ClientMock{
BodyList: []string{
logResultError,
`{"d" : ` + executionLogStringPull + `}`,
logResultErrorPull,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,

View File

@@ -30,17 +30,16 @@ func PollEntity(repositoryName string, connectionDetails ConnectionDetailsHTTP,
status = pullEntity.Status
log.Entry().WithField("StatusCode", responseStatus).Info("Pull Status: " + pullEntity.StatusDescription)
if pullEntity.Status != "R" {
printTransportLogs := true
if serviceContainsNewLogEntities(connectionDetails, client) {
PrintLogs(repositoryName, connectionDetails, client)
printTransportLogs = false
}
if pullEntity.Status == "E" {
log.SetErrorCategory(log.ErrorUndefined)
PrintLegacyLogs(repositoryName, connectionDetails, client, true, printTransportLogs)
} else {
// Fallback
if pullEntity.Status == "E" {
log.SetErrorCategory(log.ErrorUndefined)
PrintLegacyLogs(repositoryName, connectionDetails, client, true)
} else {
PrintLegacyLogs(repositoryName, connectionDetails, client, false)
}
PrintLegacyLogs(repositoryName, connectionDetails, client, false, printTransportLogs)
}
break
}
@@ -161,7 +160,7 @@ func printLog(logEntry LogResultsV2) {
}
// PrintLegacyLogs sorts and formats the received transport and execution log of an import; Deprecated with SAP BTP, ABAP Environment release 2205
func PrintLegacyLogs(repositoryName string, connectionDetails ConnectionDetailsHTTP, client piperhttp.Sender, errorOnSystem bool) {
func PrintLegacyLogs(repositoryName string, connectionDetails ConnectionDetailsHTTP, client piperhttp.Sender, errorOnSystem bool, includeTransportLog bool) {
connectionDetails.URL = connectionDetails.URL + "?$expand=to_Transport_log,to_Execution_log"
entity, _, err := GetStatus(failureMessageClonePull+repositoryName, connectionDetails, client)
@@ -179,12 +178,14 @@ func PrintLegacyLogs(repositoryName string, connectionDetails ConnectionDetailsH
// Show transport and execution log if either the action was erroenous on the system or the log level is set to "debug" (verbose = true)
if errorOnSystem {
log.Entry().Info("-------------------------")
log.Entry().Info("Transport Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
if includeTransportLog {
log.Entry().Info("-------------------------")
log.Entry().Info("Transport Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Info(logEntry.Description)
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Info(logEntry.Description)
}
}
log.Entry().Info("-------------------------")
@@ -195,12 +196,14 @@ func PrintLegacyLogs(repositoryName string, connectionDetails ConnectionDetailsH
}
log.Entry().Info("-------------------------")
} else {
log.Entry().Debug("-------------------------")
log.Entry().Debug("Transport Log")
log.Entry().Debug("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
if includeTransportLog {
log.Entry().Debug("-------------------------")
log.Entry().Debug("Transport Log")
log.Entry().Debug("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Debug(logEntry.Description)
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Debug(logEntry.Description)
}
}
log.Entry().Debug("-------------------------")
@@ -231,7 +234,13 @@ func GetStatus(failureMessage string, connectionDetails ConnectionDetailsHTTP, c
bodyText, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(bodyText, &abapResp)
if err != nil {
return body, resp.Status, errors.Wrap(err, "Could not read response")
}
json.Unmarshal(*abapResp["d"], &body)
if err != nil {
return body, resp.Status, errors.Wrap(err, "Could not read response")
}
if reflect.DeepEqual(PullEntity{}, body) {
log.Entry().WithField("StatusCode", resp.Status).Error(failureMessage)

View File

@@ -1,6 +1,7 @@
package abaputils
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -10,6 +11,24 @@ import (
"github.com/stretchr/testify/assert"
)
var executionLogString string
func init() {
executionLog := PullEntity{
ToExecutionLog: AbapLogs{
Results: []LogResults{
{
Index: "1",
Type: "LogEntry",
Description: "S",
Timestamp: "/Date(1644332299000+0000)/",
},
},
},
}
executionLogResponse, _ := json.Marshal(executionLog)
executionLogString = string(executionLogResponse)
}
func TestPollEntity(t *testing.T) {
t.Run("Test poll entity - success case", func(t *testing.T) {
@@ -17,6 +36,7 @@ func TestPollEntity(t *testing.T) {
logResultSuccess := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &ClientMock{
BodyList: []string{
`{"d" : ` + executionLogString + `}`,
logResultSuccess,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "S" } }`,
@@ -55,6 +75,7 @@ func TestPollEntity(t *testing.T) {
logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`)
client := &ClientMock{
BodyList: []string{
`{"d" : ` + executionLogString + `}`,
logResultError,
`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
`{"d" : { "status" : "E" } }`,