1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/abapEnvironmentAssemblePackages_test.go
Christian Luttenberger 7a028c4149
Refactor build framework steps (#2068)
* adding my steps

* messy step

* Update abapEnvironmentAssembly.go

* clean up

* change yaml

* corrections

* Update cloudFoundryDeploy.go

* update

* delete simulation step

* remove simulate

* Update PiperGoUtils.groovy

* Update PiperGoUtils.groovy

* Update CommonStepsTest.groovy

* add docu

* Update abapEnvironmentAssembly.md

* changes due to PR

* Update .gitignore

* b

* CV list

* Update abapEnvironmentAssembly.go

* testing with simulation

* Update abapEnvironmentAssembly.go

* remove simulation

* renaming

* Update mkdocs.yml

* moving service key to yaml and fixing code climate

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* change input

* Update abapEnvironmentAssemblePackages.go

* change json tag

* fixed error handling

* documentation

* Update abapEnvironmentAssemblePackages.md

* Update abapEnvironmentAssemblePackages.md

* fixing code climate issues

* fixing code climate issues

* Update abapEnvironmentAssemblePackages.yaml

* fixing code climate issues

* Update abapEnvironmentAssemblePackages.yaml

* adding unittests

* adding unittests and improved logging

* yaml -> json

* change scope of cfServiceKeyName

* correct indentation

* Update CommonStepsTest.groovy

* maintain correct step order

* Move Connector to connector.go

* Refactor bfw with unit tests

* remove spaces

* CodeClimate Fix for unexported type

* ABAP BF - Adding Error Handling Unmarshal

* Revert Unmarshal

Co-authored-by: rosemarieB <45030247+rosemarieB@users.noreply.github.com>
Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Koerner <tilo.koerner@sap.com>
Co-authored-by: tiloKo <70266685+tiloKo@users.noreply.github.com>
2020-09-30 16:40:36 +02:00

143 lines
4.0 KiB
Go

package cmd
import (
"path/filepath"
"testing"
"time"
abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
"github.com/SAP/jenkins-library/pkg/abaputils"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/stretchr/testify/assert"
)
func testSetup(client piperhttp.Sender, buildID string) abapbuild.Build {
conn := new(abapbuild.Connector)
conn.Client = client
conn.DownloadClient = &abapbuild.DownloadClientMock{}
conn.Header = make(map[string][]string)
b := abapbuild.Build{
Connector: *conn,
BuildID: buildID,
}
return b
}
func TestCheckIfFailedAndPrintLogsWithError(t *testing.T) {
t.Run("checkIfFailedAndPrintLogs with failed build", func(t *testing.T) {
var repo abaputils.Repository
b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
b.RunState = abapbuild.Failed
var buildsWithRepo []buildWithRepository
bWR := buildWithRepository{
build: b,
repo: repo,
}
buildsWithRepo = append(buildsWithRepo, bWR)
err := checkIfFailedAndPrintLogs(buildsWithRepo)
assert.Error(t, err)
})
}
func TestCheckIfFailedAndPrintLogs(t *testing.T) {
t.Run("checkIfFailedAndPrintLogs", func(t *testing.T) {
var repo abaputils.Repository
b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
b.RunState = abapbuild.Finished
var buildsWithRepo []buildWithRepository
bWR := buildWithRepository{
build: b,
repo: repo,
}
buildsWithRepo = append(buildsWithRepo, bWR)
err := checkIfFailedAndPrintLogs(buildsWithRepo)
assert.NoError(t, err)
})
}
func TestStarting(t *testing.T) {
t.Run("Run starting", func(t *testing.T) {
client := &abapbuild.ClMock{
Token: "MyToken",
}
conn := new(abapbuild.Connector)
conn.Client = client
conn.Header = make(map[string][]string)
var repos []abaputils.Repository
repo := abaputils.Repository{
Name: "RepoA",
Version: "0001",
PackageName: "Package",
PackageType: "AOI",
SpLevel: "0000",
PatchLevel: "0000",
Status: "P",
Namespace: "/DEMO/",
}
repos = append(repos, repo)
repo.Status = "R"
repos = append(repos, repo)
builds, buildsAlreadyReleased, err := starting(repos, *conn)
assert.NoError(t, err)
assert.Equal(t, 1, len(builds))
assert.Equal(t, 1, len(buildsAlreadyReleased))
assert.Equal(t, abapbuild.Accepted, builds[0].build.RunState)
assert.Equal(t, abapbuild.RunState(""), buildsAlreadyReleased[0].build.RunState)
})
}
func TestStartingInvalidInput(t *testing.T) {
t.Run("Run starting", func(t *testing.T) {
client := &abapbuild.ClMock{
Token: "MyToken",
}
conn := new(abapbuild.Connector)
conn.Client = client
conn.Header = make(map[string][]string)
var repos []abaputils.Repository
repo := abaputils.Repository{
Name: "RepoA",
Status: "P",
}
repos = append(repos, repo)
_, _, err := starting(repos, *conn)
assert.Error(t, err)
})
}
func TestPolling(t *testing.T) {
t.Run("Run polling", func(t *testing.T) {
var repo abaputils.Repository
b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
var buildsWithRepo []buildWithRepository
bWR := buildWithRepository{
build: b,
repo: repo,
}
buildsWithRepo = append(buildsWithRepo, bWR)
timeout := time.Duration(600 * time.Second)
pollInterval := time.Duration(1 * time.Second)
err := polling(buildsWithRepo, timeout, pollInterval)
assert.NoError(t, err)
assert.Equal(t, abapbuild.Finished, buildsWithRepo[0].build.RunState)
})
}
func TestDownloadSARXML(t *testing.T) {
t.Run("Run downloadSARXML", func(t *testing.T) {
var repo abaputils.Repository
b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
var buildsWithRepo []buildWithRepository
bWR := buildWithRepository{
build: b,
repo: repo,
}
buildsWithRepo = append(buildsWithRepo, bWR)
repos, err := downloadSARXML(buildsWithRepo)
assert.NoError(t, err)
downloadPath := filepath.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment", "abap", "SAPK-001AAINITAPC1.SAR")
assert.Equal(t, downloadPath, repos[0].SarXMLFilePath)
})
}