1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-03 15:02:35 +02:00

Include Transport & Execution Log in abapEnvironmentPullGitRepo() (#1347)

* Add logs and ABAP error messages

* Improve functions and tests

* Add improvements

* Further improvements

* Check for empty string for repositoryName

* Small adaptions

* Fix tests

* Add comments

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Daniel Mieg 2020-04-08 12:43:41 +02:00 committed by GitHub
parent a3c18d403f
commit bbaaef97ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 211 additions and 49 deletions

View File

@ -6,6 +6,9 @@ import (
"io/ioutil"
"net/http"
"net/http/cookiejar"
"reflect"
"sort"
"strconv"
"strings"
"time"
@ -17,14 +20,20 @@ import (
)
func abapEnvironmentPullGitRepo(config abapEnvironmentPullGitRepoOptions, telemetryData *telemetry.CustomData) error {
// Determine the host, user and password, either via the input parameters or via a cloud foundry service key
c := command.Command{}
var connectionDetails, error = getAbapCommunicationArrangementInfo(config, &c)
if error != nil {
log.Entry().WithError(error).Fatal("Parameters for the ABAP Connection not available")
connectionDetails, errorGetInfo := getAbapCommunicationArrangementInfo(config, &c)
if errorGetInfo != nil {
log.Entry().WithError(errorGetInfo).Fatal("Parameters for the ABAP Connection not available")
}
// Configuring the HTTP Client and CookieJar
client := piperhttp.Client{}
cookieJar, _ := cookiejar.New(nil)
cookieJar, errorCookieJar := cookiejar.New(nil)
if errorCookieJar != nil {
log.Entry().WithError(errorCookieJar).Fatal("Could not create a Cookie Jar")
}
clientOptions := piperhttp.ClientOptions{
MaxRequestDuration: 60 * time.Second,
CookieJar: cookieJar,
@ -33,14 +42,17 @@ func abapEnvironmentPullGitRepo(config abapEnvironmentPullGitRepoOptions, teleme
}
client.SetOptions(clientOptions)
var uriConnectionDetails, err = triggerPull(config, connectionDetails, &client)
if err != nil {
log.Entry().WithError(err).Fatal("Pull failed on the ABAP System")
// Triggering the Pull of the repository into the ABAP Environment system
uriConnectionDetails, errorTriggerPull := triggerPull(config, connectionDetails, &client)
if errorTriggerPull != nil {
log.Entry().WithError(errorTriggerPull).Fatal("Pull failed on the ABAP System")
}
var status, er = pollEntity(config, uriConnectionDetails, &client, 10*time.Second)
if er != nil {
log.Entry().WithError(er).Fatal("Pull failed on the ABAP System")
// Polling the status of the repository import on the ABAP Environment system
pollIntervall := 10 * time.Second
status, errorPollEntity := pollEntity(config, uriConnectionDetails, &client, pollIntervall)
if errorPollEntity != nil {
log.Entry().WithError(errorPollEntity).Fatal("Pull failed on the ABAP System")
}
if status == "E" {
log.Entry().Fatal("Pull failed on the ABAP System")
@ -56,9 +68,9 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
pullConnectionDetails.XCsrfToken = "fetch"
// Loging into the ABAP System - getting the x-csrf-token and cookies
var resp, err = getHTTPResponse("HEAD", pullConnectionDetails, nil, client)
resp, err := getHTTPResponse("HEAD", pullConnectionDetails, nil, client)
if err != nil {
handleHTTPError(resp, err, "Authentication on the ABAP system failed", pullConnectionDetails)
err = handleHTTPError(resp, err, "Authentication on the ABAP system failed", pullConnectionDetails)
return uriConnectionDetails, err
}
defer resp.Body.Close()
@ -67,10 +79,13 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
pullConnectionDetails.XCsrfToken = uriConnectionDetails.XCsrfToken
// Trigger the Pull of a Repository
var jsonBody = []byte(`{"sc_name":"` + config.RepositoryName + `"}`)
if config.RepositoryName == "" {
return uriConnectionDetails, errors.New("An empty string was passed for the parameter 'repositoryName'")
}
jsonBody := []byte(`{"sc_name":"` + config.RepositoryName + `"}`)
resp, err = getHTTPResponse("POST", pullConnectionDetails, jsonBody, client)
if err != nil {
handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, uriConnectionDetails)
err = handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, uriConnectionDetails)
return uriConnectionDetails, err
}
defer resp.Body.Close()
@ -79,15 +94,20 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
// Parse Response
var body abapEntity
var abapResp map[string]*json.RawMessage
bodyText, err := ioutil.ReadAll(resp.Body)
json.Unmarshal(bodyText, &abapResp)
json.Unmarshal(*abapResp["d"], &body)
if body == (abapEntity{}) {
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
var err = errors.New("Request to ABAP System not successful")
bodyText, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
return uriConnectionDetails, err
}
uriConnectionDetails.URL = body.Metadata.URI
json.Unmarshal(bodyText, &abapResp)
json.Unmarshal(*abapResp["d"], &body)
if reflect.DeepEqual(abapEntity{}, body) {
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
err := errors.New("Request to ABAP System not successful")
return uriConnectionDetails, err
}
expandLog := "?$expand=to_Execution_log,to_Transport_log"
uriConnectionDetails.URL = body.Metadata.URI + expandLog
return uriConnectionDetails, nil
}
@ -99,7 +119,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
for {
var resp, err = getHTTPResponse("GET", connectionDetails, nil, client)
if err != nil {
handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, connectionDetails)
err = handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, connectionDetails)
return "", err
}
defer resp.Body.Close()
@ -110,7 +130,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
var abapResp map[string]*json.RawMessage
json.Unmarshal(bodyText, &abapResp)
json.Unmarshal(*abapResp["d"], &body)
if body == (abapEntity{}) {
if reflect.DeepEqual(abapEntity{}, body) {
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
var err = errors.New("Request to ABAP System not successful")
return "", err
@ -118,6 +138,7 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
status = body.Status
log.Entry().WithField("StatusCode", resp.Status).Info("Pull Status: " + body.StatusDescr)
if body.Status != "R" {
printLogs(body)
break
}
time.Sleep(pollIntervall)
@ -162,31 +183,31 @@ func readCfServiceKey(config abapEnvironmentPullGitRepoOptions, c execRunner) (s
// Logging into the Cloud Foundry via CF CLI
log.Entry().WithField("cfApiEndpoint", config.CfAPIEndpoint).WithField("cfSpace", config.CfSpace).WithField("cfOrg", config.CfOrg).WithField("User", config.Username).Info("Cloud Foundry parameters: ")
cfLoginSlice := []string{"login", "-a", config.CfAPIEndpoint, "-u", config.Username, "-p", config.Password, "-o", config.CfOrg, "-s", config.CfSpace}
error := c.RunExecutable("cf", cfLoginSlice...)
if error != nil {
errorRunExecutable := c.RunExecutable("cf", cfLoginSlice...)
if errorRunExecutable != nil {
log.Entry().Error("Login at cloud foundry failed.")
return abapServiceKey, error
return abapServiceKey, errorRunExecutable
}
// Reading the Service Key via CF CLI
var serviceKeyBytes bytes.Buffer
c.Stdout(&serviceKeyBytes)
cfReadServiceKeySlice := []string{"service-key", config.CfServiceInstance, config.CfServiceKey}
error = c.RunExecutable("cf", cfReadServiceKeySlice...)
errorRunExecutable = c.RunExecutable("cf", cfReadServiceKeySlice...)
var serviceKeyJSON string
if len(serviceKeyBytes.String()) > 0 {
var lines []string = strings.Split(serviceKeyBytes.String(), "\n")
serviceKeyJSON = strings.Join(lines[2:], "")
}
if error != nil {
return abapServiceKey, error
if errorRunExecutable != nil {
return abapServiceKey, errorRunExecutable
}
log.Entry().WithField("cfServiceInstance", config.CfServiceInstance).WithField("cfServiceKey", config.CfServiceKey).Info("Read service key for service instance")
json.Unmarshal([]byte(serviceKeyJSON), &abapServiceKey)
if abapServiceKey == (serviceKey{}) {
return abapServiceKey, errors.New("Parsing the service key failed")
}
return abapServiceKey, error
return abapServiceKey, errorRunExecutable
}
func getHTTPResponse(requestType string, connectionDetails connectionDetailsHTTP, body []byte, client piperhttp.Sender) (*http.Response, error) {
@ -200,13 +221,70 @@ func getHTTPResponse(requestType string, connectionDetails connectionDetailsHTTP
return req, err
}
func handleHTTPError(resp *http.Response, err error, message string, connectionDetails connectionDetailsHTTP) {
func handleHTTPError(resp *http.Response, err error, message string, connectionDetails connectionDetailsHTTP) error {
if resp == nil {
// Response is nil in case of a timeout
log.Entry().WithError(err).WithField("ABAP Endpoint", connectionDetails.URL).Error("Request failed")
} else {
log.Entry().WithField("StatusCode", resp.Status).Error(message)
// Include the error message of the ABAP Environment system, if available
var abapErrorResponse abapError
bodyText, readError := ioutil.ReadAll(resp.Body)
if readError != nil {
return readError
}
var abapResp map[string]*json.RawMessage
json.Unmarshal(bodyText, &abapResp)
json.Unmarshal(*abapResp["error"], &abapErrorResponse)
if (abapError{}) != abapErrorResponse {
log.Entry().WithField("ErrorCode", abapErrorResponse.Code).Error(abapErrorResponse.Message.Value)
abapError := errors.New(abapErrorResponse.Code + " - " + abapErrorResponse.Message.Value)
err = errors.Wrap(abapError, err.Error())
}
resp.Body.Close()
}
return err
}
func printLogs(entity abapEntity) {
// Sort logs
sort.SliceStable(entity.ToExecutionLog.Results, func(i, j int) bool {
return entity.ToExecutionLog.Results[i].Index < entity.ToExecutionLog.Results[j].Index
})
sort.SliceStable(entity.ToTransportLog.Results, func(i, j int) bool {
return entity.ToTransportLog.Results[i].Index < entity.ToTransportLog.Results[j].Index
})
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().Info("-------------------------")
log.Entry().Info("Execution Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToExecutionLog.Results {
log.Entry().WithField("Timestamp", convertTime(logEntry.Timestamp)).Info(logEntry.Description)
}
log.Entry().Info("-------------------------")
}
func convertTime(logTimeStamp string) time.Time {
// The ABAP Environment system returns the date in the following format: /Date(1585576807000+0000)/
seconds := strings.TrimPrefix(strings.TrimSuffix(logTimeStamp, "000+0000)/"), "/Date(")
n, error := strconv.ParseInt(seconds, 10, 64)
if error != nil {
return time.Unix(0, 0).UTC()
}
t := time.Unix(n, 0).UTC()
return t
}
type abapEntity struct {
@ -216,14 +294,25 @@ type abapEntity struct {
Namespace string `json:"namepsace"`
Status string `json:"status"`
StatusDescr string `json:"status_descr"`
ToExecutionLog deferred `json:"to_Execution_log"`
ToTransportLog deferred `json:"to_Transport_log"`
ToExecutionLog abapLogs `json:"to_Execution_log"`
ToTransportLog abapLogs `json:"to_Transport_log"`
}
type abapMetadata struct {
URI string `json:"uri"`
}
type abapLogs struct {
Results []logResults `json:"results"`
}
type logResults struct {
Index string `json:"index_no"`
Type string `json:"type"`
Description string `json:"descr"`
Timestamp string `json:"timestamp"`
}
type serviceKey struct {
Abap abapConenction `json:"abap"`
Binding abapBinding `json:"binding"`
@ -256,3 +345,13 @@ type connectionDetailsHTTP struct {
URL string `json:"url"`
XCsrfToken string `json:"xcsrftoken"`
}
type abapError struct {
Code string `json:"code"`
Message abapErrorMessage `json:"message"`
}
type abapErrorMessage struct {
Lang string `json:"lang"`
Value string `json:"value"`
}

View File

@ -2,12 +2,14 @@ package cmd
import (
"bytes"
"github.com/SAP/jenkins-library/pkg/mock"
"io"
"io/ioutil"
"net/http"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/pkg/errors"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/stretchr/testify/assert"
)
@ -16,12 +18,49 @@ func TestTriggerPull(t *testing.T) {
t.Run("Test trigger pull: success case", func(t *testing.T) {
uriExpected := "example.com"
receivedURI := "example.com/Entity"
uriExpected := receivedURI + "?$expand=to_Execution_log,to_Transport_log"
tokenExpected := "myToken"
client := &clientMock{
Body: `{"d" : { "__metadata" : { "uri" : "` + uriExpected + `" } } }`,
Token: tokenExpected,
Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`,
Token: tokenExpected,
StatusCode: 200,
}
config := abapEnvironmentPullGitRepoOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
CfServiceInstance: "testInstance",
CfServiceKey: "testServiceKey",
Username: "testUser",
Password: "testPassword",
RepositoryName: "testRepo",
}
con := connectionDetailsHTTP{
User: "MY_USER",
Password: "MY_PW",
URL: "https://api.endpoint.com/Entity/",
}
entityConnection, err := triggerPull(config, con, client)
assert.Nil(t, err)
assert.Equal(t, uriExpected, entityConnection.URL)
assert.Equal(t, tokenExpected, entityConnection.XCsrfToken)
})
t.Run("Test trigger pull: ABAP Error", func(t *testing.T) {
errorMessage := "ABAP Error Message"
errorCode := "ERROR/001"
HTTPErrorMessage := "HTTP Error Message"
combinedErrorMessage := "HTTP Error Message: ERROR/001 - ABAP Error Message"
client := &clientMock{
Body: `{"error" : { "code" : "` + errorCode + `", "message" : { "lang" : "en", "value" : "` + errorMessage + `" } } }`,
Token: "myToken",
StatusCode: 400,
Error: errors.New(HTTPErrorMessage),
}
config := abapEnvironmentPullGitRepoOptions{
CfAPIEndpoint: "https://api.endpoint.com",
@ -38,9 +77,8 @@ func TestTriggerPull(t *testing.T) {
Password: "MY_PW",
URL: "https://api.endpoint.com/Entity/",
}
entityConnection, _ := triggerPull(config, con, client)
assert.Equal(t, uriExpected, entityConnection.URL)
assert.Equal(t, tokenExpected, entityConnection.XCsrfToken)
_, err := triggerPull(config, con, client)
assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected")
})
}
@ -54,7 +92,8 @@ func TestPollEntity(t *testing.T) {
`{"d" : { "status" : "S" } }`,
`{"d" : { "status" : "R" } }`,
},
Token: "myToken",
Token: "myToken",
StatusCode: 200,
}
config := abapEnvironmentPullGitRepoOptions{
CfAPIEndpoint: "https://api.endpoint.com",
@ -83,7 +122,8 @@ func TestPollEntity(t *testing.T) {
`{"d" : { "status" : "E" } }`,
`{"d" : { "status" : "R" } }`,
},
Token: "myToken",
Token: "myToken",
StatusCode: 200,
}
config := abapEnvironmentPullGitRepoOptions{
CfAPIEndpoint: "https://api.endpoint.com",
@ -142,7 +182,7 @@ func TestGetAbapCommunicationArrangementInfo(t *testing.T) {
execRunner := mock.ExecMockRunner{}
var _, err = getAbapCommunicationArrangementInfo(config, &execRunner)
assert.Equal(t, "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510", err.Error(), "Expected error message")
assert.Equal(t, "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510", err.Error(), "Different error message expected")
})
t.Run("Test cf cli command: params missing", func(t *testing.T) {
@ -155,15 +195,38 @@ func TestGetAbapCommunicationArrangementInfo(t *testing.T) {
execRunner := mock.ExecMockRunner{}
var _, err = getAbapCommunicationArrangementInfo(config, &execRunner)
assert.Equal(t, "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510", err.Error(), "Expected error message")
assert.Equal(t, "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510", err.Error(), "Different error message expected")
})
}
func TestTimeConverter(t *testing.T) {
t.Run("Test example time", func(t *testing.T) {
inputDate := "/Date(1585576809000+0000)/"
expectedDate := "2020-03-30 14:00:09 +0000 UTC"
result := convertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test Unix time", func(t *testing.T) {
inputDate := "/Date(0000000000000+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := convertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test unexpected format", func(t *testing.T) {
inputDate := "/Date(0012300000001+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := convertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
}
type clientMock struct {
Token string
Body string
BodyList []string
Token string
Body string
BodyList []string
StatusCode int
Error error
}
func (c *clientMock) SetOptions(opts piperhttp.ClientOptions) {}
@ -181,8 +244,8 @@ func (c *clientMock) SendRequest(method, url string, bdy io.Reader, hdr http.Hea
header := http.Header{}
header.Set("X-Csrf-Token", c.Token)
return &http.Response{
StatusCode: 200,
StatusCode: c.StatusCode,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(body)),
}, nil
}, c.Error
}