2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-08-27 07:54:03 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-09-30 16:40:36 +02:00
|
|
|
"time"
|
2020-08-27 07:54:03 +02:00
|
|
|
|
2020-09-30 16:40:36 +02:00
|
|
|
abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
|
2020-08-27 07:54:03 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/abaputils"
|
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
2022-08-09 10:57:02 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2020-08-27 07:54:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-09-30 16:40:36 +02:00
|
|
|
func testSetup(client piperhttp.Sender, buildID string) abapbuild.Build {
|
|
|
|
conn := new(abapbuild.Connector)
|
2020-08-27 07:54:03 +02:00
|
|
|
conn.Client = client
|
2020-09-30 16:40:36 +02:00
|
|
|
conn.DownloadClient = &abapbuild.DownloadClientMock{}
|
2020-08-27 07:54:03 +02:00
|
|
|
conn.Header = make(map[string][]string)
|
2020-09-30 16:40:36 +02:00
|
|
|
b := abapbuild.Build{
|
|
|
|
Connector: *conn,
|
2020-08-27 07:54:03 +02:00
|
|
|
BuildID: buildID,
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckIfFailedAndPrintLogsWithError(t *testing.T) {
|
|
|
|
t.Run("checkIfFailedAndPrintLogs with failed build", func(t *testing.T) {
|
|
|
|
var repo abaputils.Repository
|
2020-09-30 16:40:36 +02:00
|
|
|
b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
|
|
|
|
b.RunState = abapbuild.Failed
|
2020-08-27 07:54:03 +02:00
|
|
|
var buildsWithRepo []buildWithRepository
|
|
|
|
bWR := buildWithRepository{
|
|
|
|
build: b,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
buildsWithRepo = append(buildsWithRepo, bWR)
|
|
|
|
err := checkIfFailedAndPrintLogs(buildsWithRepo)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("checkIfFailedAndPrintLogs", func(t *testing.T) {
|
|
|
|
var repo abaputils.Repository
|
2020-09-30 16:40:36 +02:00
|
|
|
b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
|
|
|
|
b.RunState = abapbuild.Finished
|
2020-08-27 07:54:03 +02:00
|
|
|
var buildsWithRepo []buildWithRepository
|
|
|
|
bWR := buildWithRepository{
|
|
|
|
build: b,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
buildsWithRepo = append(buildsWithRepo, bWR)
|
|
|
|
err := checkIfFailedAndPrintLogs(buildsWithRepo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartingInvalidInput(t *testing.T) {
|
2021-03-11 12:55:12 +02:00
|
|
|
t.Run("Run starting with Invalid Input", func(t *testing.T) {
|
2020-09-30 16:40:36 +02:00
|
|
|
client := &abapbuild.ClMock{
|
2020-08-27 07:54:03 +02:00
|
|
|
Token: "MyToken",
|
|
|
|
}
|
2020-09-30 16:40:36 +02:00
|
|
|
conn := new(abapbuild.Connector)
|
2020-08-27 07:54:03 +02:00
|
|
|
conn.Client = client
|
|
|
|
conn.Header = make(map[string][]string)
|
|
|
|
var repos []abaputils.Repository
|
|
|
|
repo := abaputils.Repository{
|
|
|
|
Name: "RepoA",
|
|
|
|
Status: "P",
|
|
|
|
}
|
|
|
|
repos = append(repos, repo)
|
2021-03-11 12:55:12 +02:00
|
|
|
builds, err := executeBuilds(repos, *conn, time.Duration(0*time.Second), time.Duration(1*time.Millisecond))
|
2020-08-27 07:54:03 +02:00
|
|
|
assert.NoError(t, err)
|
2021-03-11 12:55:12 +02:00
|
|
|
assert.Equal(t, 1, len(builds))
|
|
|
|
assert.Equal(t, abapbuild.Failed, builds[0].build.RunState)
|
2020-08-27 07:54:03 +02:00
|
|
|
})
|
|
|
|
}
|
2021-03-01 14:51:44 +02:00
|
|
|
|
|
|
|
func TestStep(t *testing.T) {
|
2021-03-11 12:55:12 +02:00
|
|
|
autils := &abaputils.AUtilsMock{
|
|
|
|
ReturnedConnectionDetailsHTTP: abaputils.ConnectionDetailsHTTP{
|
|
|
|
URL: `/sap/opu/odata/BUILD/CORE_SRV`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := abapbuild.GetBuildMockClient()
|
|
|
|
cpe := &abapEnvironmentAssemblePackagesCommonPipelineEnvironment{}
|
|
|
|
|
2021-03-01 14:51:44 +02:00
|
|
|
t.Run("abapEnvironmentAssemblePackages: nothing to do", func(t *testing.T) {
|
|
|
|
config := &abapEnvironmentAssemblePackagesOptions{
|
|
|
|
AddonDescriptor: cpeAbapAddonDescriptorPackageLocked,
|
|
|
|
MaxRuntimeInMinutes: 1,
|
|
|
|
PollIntervalsInMilliseconds: 1,
|
|
|
|
}
|
|
|
|
|
2022-08-09 10:57:02 +02:00
|
|
|
err := runAbapEnvironmentAssemblePackages(config, nil, autils, &mock.FilesMock{}, &client, cpe)
|
2021-03-01 14:51:44 +02:00
|
|
|
assert.NoError(t, err)
|
2021-10-28 11:01:16 +02:00
|
|
|
assert.Contains(t, cpe.abap.addonDescriptor, `"InBuildScope":false`)
|
2021-03-01 14:51:44 +02:00
|
|
|
})
|
|
|
|
t.Run("abapEnvironmentAssemblePackages: build", func(t *testing.T) {
|
|
|
|
config := &abapEnvironmentAssemblePackagesOptions{
|
|
|
|
AddonDescriptor: cpeAbapAddonDescriptorPackageReserved,
|
|
|
|
MaxRuntimeInMinutes: 1,
|
|
|
|
PollIntervalsInMilliseconds: 1,
|
|
|
|
}
|
|
|
|
|
2022-08-09 10:57:02 +02:00
|
|
|
err := runAbapEnvironmentAssemblePackages(config, nil, autils, &mock.FilesMock{}, &client, cpe)
|
2021-03-01 14:51:44 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, cpe.abap.addonDescriptor, `SAPK-001AAINITAPC1.SAR`)
|
2021-10-28 11:01:16 +02:00
|
|
|
assert.Contains(t, cpe.abap.addonDescriptor, `"InBuildScope":true`)
|
2021-03-01 14:51:44 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var cpeAbapAddonDescriptorPackageLocked = `{
|
|
|
|
"addonProduct":"/ITAPC1/I_CURRENCZPRODUCT",
|
|
|
|
"addonVersion":"1.0.0",
|
|
|
|
"addonVersionAAK":"0001",
|
|
|
|
"addonUniqueID":"myAddonId",
|
|
|
|
"customerID":"$ID",
|
|
|
|
"AddonSpsLevel":"0000",
|
|
|
|
"AddonPatchLevel":"0000",
|
|
|
|
"TargetVectorID":"",
|
|
|
|
"repositories":[
|
|
|
|
{ "name":"/ITAPC1/I_CURRENCZ",
|
|
|
|
"tag":"whatever",
|
|
|
|
"branch":"",
|
|
|
|
"commitID":"",
|
|
|
|
"version":"1.0.0",
|
|
|
|
"versionAAK":"0001",
|
|
|
|
"PackageName":"SAPK-002AAINITAPC1",
|
|
|
|
"PackageType":"AOI",
|
|
|
|
"SpLevel":"0000",
|
|
|
|
"PatchLevel":"0000",
|
|
|
|
"PredecessorCommitID":"",
|
|
|
|
"Status":"L",
|
|
|
|
"Namespace":"/ITAPC1/",
|
|
|
|
"SarXMLFilePath":".pipeline\\commonPipelineEnvironment\\abap\\SAPK-002AAINITAPC1.SAR"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
var cpeAbapAddonDescriptorPackageReserved = `{
|
|
|
|
"addonProduct":"/ITAPC1/I_CURRENCZPRODUCT",
|
|
|
|
"addonVersion":"1.0.0",
|
|
|
|
"addonVersionAAK":"0001",
|
|
|
|
"addonUniqueID":"myAddonId",
|
|
|
|
"customerID":"$ID",
|
|
|
|
"AddonSpsLevel":"0000",
|
|
|
|
"AddonPatchLevel":"0000",
|
|
|
|
"TargetVectorID":"",
|
|
|
|
"repositories":[
|
|
|
|
{ "name":"/ITAPC1/I_CURRENCZ",
|
|
|
|
"tag":"whatever",
|
|
|
|
"branch":"",
|
|
|
|
"commitID":"",
|
|
|
|
"version":"1.0.0",
|
|
|
|
"versionAAK":"0001",
|
|
|
|
"PackageName":"SAPK-002AAINITAPC1",
|
|
|
|
"PackageType":"AOI",
|
|
|
|
"SpLevel":"0000",
|
|
|
|
"PatchLevel":"0000",
|
|
|
|
"PredecessorCommitID":"",
|
|
|
|
"Status":"P",
|
|
|
|
"Namespace":"/ITAPC1/",
|
|
|
|
"SarXMLFilePath":""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|