1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/abap/build/bfw_mock.go
rosemarieB e90856d5bf
Generic build step (#3323)
* new step abapEnvironmentBuild

* Update piper.go

* Update abapEnvironmentBuild.go

* update yaml file

* Logging for debugging

* Update abaputils.go

* Update connector.go

* assigning connector

* delete debugging logging

* Update abapEnvironmentBuild.go

* certificate to yaml

* Update abapEnvironmentBuild.go

* add scope

* Update abapEnvironmentBuild.go

* Update abapEnvironmentBuild.yaml

* change certificate name in yaml

* test my new gitscript

* logging for debugging

* debugging...

* adding options to client.

* skip verification

* debugging

* debugging...

* switch of transportskipverification

* changing connector return

* deleting additional set options

* fixed timeout error

* adding certificate

* testing without certificate set

* testing with certificate set

* download, publish and value logic

* write values to cpe

* logging

* adding condition on string length

* change publishmethod and some logging

* change download method -> using references

* evaluation of parameter for download

* add case for empty string

* adding unittests

* Update mockClient.go

* make abapEnvironmentBuildUtilsBundle powerful

* refactor abapEnvironmentBuild into pieces

* check error message

* check error message 2

* check error message 3

* check error message 4

* remove check error message

* cleanup

* adding unittests

* unittests and docu

* docu

* docu

* Update abapEnvironmentBuild.md

* removing trailing spaces and adding empty lines in docu

* Update abapEnvironmentBuild.md

* fixing unittest and PR recommen

* Update abapEnvironmentPipelineStageBuild.groovy

* Update abapEnvironmentPipelineStageBuild.groovy

* Update abapEnvironmentPipelineStageBuild.groovy

* Update abapEnvironmentPipelineStageBuild.groovy

* changes derived from pull request

Co-authored-by: tiloKo <70266685+tiloKo@users.noreply.github.com>
2021-12-06 14:43:37 +01:00

387 lines
13 KiB
Go

package build
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"strings"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
)
// DownloadClientMock : Mock for Download Client used for artefact test
type DownloadClientMock struct{}
// DownloadFile : Empty file download
func (dc *DownloadClientMock) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
return nil
}
// SetOptions : Download Client options
func (dc *DownloadClientMock) SetOptions(opts piperhttp.ClientOptions) {}
// ClMock : Mock for Build Framework Client used for BF test
type ClMock struct {
Token string
StatusCode int
Error error
}
// SetOptions : BF Client options
func (c *ClMock) SetOptions(opts piperhttp.ClientOptions) {}
// SendRequest : BF Send Fake request
func (c *ClMock) SendRequest(method string, url string, bdy io.Reader, hdr http.Header, cookies []*http.Cookie) (*http.Response, error) {
if method == "GET" || method == "POST" {
var body []byte
body = []byte(fakeResponse(method, url))
return &http.Response{
StatusCode: c.StatusCode,
Body: ioutil.NopCloser(bytes.NewReader(body)),
}, c.Error
} else if method == "HEAD" {
var body []byte
header := http.Header{}
header.Set("X-Csrf-Token", c.Token)
body = []byte("")
return &http.Response{
StatusCode: c.StatusCode,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(body)),
}, c.Error
} else {
return nil, c.Error
}
}
func fakeResponse(method string, url string) string {
if method == "POST" {
return responsePOST
}
if strings.HasSuffix(url, "/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')") {
return responseGetBuild
} else if strings.HasSuffix(url, "/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')/tasks") {
return responseGetTasks
} else if strings.HasSuffix(url, "/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0)/logs") {
return responseGetLog0
} else if strings.HasSuffix(url, "/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1)/logs") {
return responseGetLog1
} else if strings.HasSuffix(url, "/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')/values") {
return responseGetValues
} else if strings.HasSuffix(url, "tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0)/results") {
return responseGetResults0
} else if strings.HasSuffix(url, "tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1)/results") {
return responseGetResults1
}
return ""
}
var responseGetBuild = `{
"d": {
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_BUILDSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"run_state": "FINISHED",
"result_state": "SUCCESSFUL",
"phase": "test1",
"entitytype": "P",
"startedby": "BENTELER",
"started_at": "/Date(1591718108103+0000)/",
"finished_at": "/Date(1591718129432+0000)/",
"tasks": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')/tasks"
}
},
"values": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPOVMXK4DNPBDRW2M')/values"
}
}
}
}`
var responsePOST = `{
"d": {
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPNVMOUQL2LHUFAUA')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPNVMOUQL2LHUFAUA')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_BUILDSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"run_state": "ACCEPTED",
"result_state": "",
"phase": "test1",
"entitytype": "",
"startedby": "BENTELER",
"started_at": null,
"finished_at": null,
"tasks": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/builds('ABIFNLDCSQPNVMOUQL2LHUFAUA')/tasks"
}
},
"values": {
"results": []
}
}
}`
var responseGetTasks = `{
"d": {
"results": [
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1)",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1)",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_TASKSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 1,
"log_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M_1",
"plugin_class": "/BUILD/CL_TEST_PLUGIN_OK",
"started_at": "/Date(1591718128730+0000)/",
"finished_at": "/Date(1591718129369+0000)/",
"result_state": "SUCCESSFUL",
"logs": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1)/logs"
}
},
"results": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1)/results"
}
}
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0)",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0)",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_TASKSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 0,
"log_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M_0",
"plugin_class": "",
"started_at": "/Date(1591718128728+0000)/",
"finished_at": "/Date(1591718129462+0000)/",
"result_state": "SUCCESSFUL",
"logs": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0)/logs"
}
},
"results": {
"__deferred": {
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/tasks(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0)/results"
}
}
}
]
}
}`
var responseGetLog0 = `{
"d": {
"results": [
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_0')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_0')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_LOGSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 0,
"log_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M_0",
"msgty": "I",
"detlevel": "3",
"log_line": "I:/BUILD/LOG:000 ABAP Build Framework",
"TIME_STMP": "20200721133523"
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_0')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=0,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_0')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_LOGSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 0,
"log_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M_0",
"msgty": "I",
"detlevel": "3",
"log_line": "I:/BUILD/LOG:000 ... Build Execution finished SUCCESSFUL",
"TIME_STMP": "20200721133528"
}
]
}
}`
var responseGetLog1 = `{
"d": {
"results": [
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_1')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_1')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_LOGSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 1,
"log_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M_1",
"msgty": "I",
"detlevel": "1",
"log_line": "I:/BUILD/LOG:000 Hello Packages [1]: , /BUILD/CORE, here is your lovely test_ok plugin!",
"TIME_STMP": "20200721133528"
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_1')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/logs(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,log_id='ABIFNLDCSQPOVMXK4DNPBDRW2M_1')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_LOGSType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 1,
"log_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M_1",
"msgty": "W",
"detlevel": "3",
"log_line": "W:/BUILD/LOG:000 We can even have warnings!",
"TIME_STMP": "20200721133528"
}
]
}
}`
var responseGetResults0 = `{
"d": {
"results": []
}
}`
var responseGetResults1 = `{
"d": {
"results": [
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/results(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,name='HT-6111.JPG')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/results(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,name='HT-6111.JPG')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_RESULTSType",
"content_type": "image/jpeg",
"media_src": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/results(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,name='HT-6111.JPG')/$value"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 1,
"name": "HT-6111.JPG",
"additional_info": "",
"mimetype": "image/jpeg"
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/results(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,name='2times_hello')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/results(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,name='2times_hello')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_RESULTSType",
"content_type": "text/plain",
"media_src": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/results(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',task_id=1,name='2times_hello')/$value"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"task_id": 1,
"name": "SAR_XML",
"additional_info": "/usr/sap/trans/tmp/SAPK-001AAINITAPC1.SAR",
"mimetype": "application/octet-stream"
}
]
}
}`
var responseGetValues = `{
"d": {
"results": [
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='PHASE')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='PHASE')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_VALUESType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"value_id": "PHASE",
"value": "test1"
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='PACKAGES')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='PACKAGES')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_VALUESType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"value_id": "PACKAGES",
"value": "/BUILD/CORE"
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='season')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='season')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_VALUESType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"value_id": "season",
"value": "winter"
},
{
"__metadata": {
"id": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='SUN')",
"uri": "https://ldai3yi3.wdf.sap.corp:44334/sap/opu/odata/BUILD/CORE_SRV/values(build_id='ABIFNLDCSQPOVMXK4DNPBDRW2M',value_id='SUN')",
"type": "BUILD.CORE_SRV.xBUILDxVIEW_VALUESType"
},
"build_id": "ABIFNLDCSQPOVMXK4DNPBDRW2M",
"value_id": "SUN",
"value": "FLOWER"
}
]
}
}`
func GetMockBuildTestDownloadPublish() Build {
conn := new(Connector)
conn.DownloadClient = &DownloadClientMock{}
results0 := []Result{
{
connector: *conn,
Name: dummyResultName,
},
}
results1 := []Result{
{
connector: *conn,
Name: "File1",
},
{
connector: *conn,
Name: "File2",
},
{
connector: *conn,
Name: "File3",
},
}
build := Build{
BuildID: "123",
Tasks: []task{
{
TaskID: 0,
Results: results0,
},
{
TaskID: 1,
Results: results1,
},
},
}
return build
}