mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
ffc931aad1
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files. * Added unit build tag to all unit test files. * added to new unit test unit build tag * Update verify-go.yml * small fix --------- Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
//go:build unit
|
|
// +build unit
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
"github.com/SAP/jenkins-library/pkg/transportrequest/solman"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
type transportRequestUploadSOLMANMockUtils struct {
|
|
*mock.ExecMockRunner
|
|
*mock.FilesMock
|
|
}
|
|
|
|
func newTransportRequestUploadSOLMANTestsUtils(exitcode int) transportRequestUploadSOLMANMockUtils {
|
|
utils := transportRequestUploadSOLMANMockUtils{
|
|
ExecMockRunner: &mock.ExecMockRunner{ExitCode: exitcode},
|
|
FilesMock: &mock.FilesMock{},
|
|
}
|
|
return utils
|
|
}
|
|
|
|
type ActionMock struct {
|
|
received solman.UploadAction
|
|
performCalled bool
|
|
failWith error
|
|
}
|
|
|
|
func (a *ActionMock) WithConnection(c solman.Connection) {
|
|
a.received.Connection = c
|
|
}
|
|
func (a *ActionMock) WithChangeDocumentID(id string) {
|
|
a.received.ChangeDocumentID = id
|
|
}
|
|
func (a *ActionMock) WithTransportRequestID(id string) {
|
|
a.received.TransportRequestID = id
|
|
}
|
|
func (a *ActionMock) WithApplicationID(id string) {
|
|
a.received.ApplicationID = id
|
|
}
|
|
func (a *ActionMock) WithFile(f string) {
|
|
a.received.File = f
|
|
}
|
|
func (a *ActionMock) WithCMOpts(opts []string) {
|
|
a.received.CMOpts = opts
|
|
}
|
|
func (a *ActionMock) Perform(fs solman.FileSystem, command solman.Exec) error {
|
|
a.performCalled = true
|
|
return a.failWith
|
|
}
|
|
|
|
type ConfigMock struct {
|
|
config *transportRequestUploadSOLMANOptions
|
|
}
|
|
|
|
func TestTrSolmanRunTransportRequestUpload(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("straight forward", func(t *testing.T) {
|
|
utilsMock := newTransportRequestUploadSOLMANTestsUtils(0)
|
|
configMock := newConfigMock()
|
|
actionMock := ActionMock{}
|
|
cpe := &transportRequestUploadSOLMANCommonPipelineEnvironment{}
|
|
|
|
err := runTransportRequestUploadSOLMAN(configMock.config, &actionMock, nil, utilsMock, cpe)
|
|
|
|
if assert.NoError(t, err) {
|
|
assert.Equal(t, actionMock.received, solman.UploadAction{
|
|
Connection: solman.Connection{
|
|
Endpoint: "https://example.org/solman",
|
|
User: "me",
|
|
Password: "********",
|
|
},
|
|
ApplicationID: "XYZ",
|
|
ChangeDocumentID: "12345678",
|
|
TransportRequestID: "87654321",
|
|
File: "myApp.abc",
|
|
CMOpts: []string{"-Dtest=abc123"},
|
|
})
|
|
assert.True(t, actionMock.performCalled)
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("bad", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("Error during deployment", func(t *testing.T) {
|
|
utilsMock := newTransportRequestUploadSOLMANTestsUtils(0)
|
|
configMock := newConfigMock()
|
|
actionMock := ActionMock{failWith: fmt.Errorf("upload failed")}
|
|
cpe := &transportRequestUploadSOLMANCommonPipelineEnvironment{}
|
|
|
|
err := runTransportRequestUploadSOLMAN(configMock.config, &actionMock, nil, utilsMock, cpe)
|
|
|
|
assert.Error(t, err, "upload failed")
|
|
})
|
|
})
|
|
}
|
|
|
|
func newConfigMock() *ConfigMock {
|
|
return &ConfigMock{
|
|
config: &transportRequestUploadSOLMANOptions{
|
|
Endpoint: "https://example.org/solman",
|
|
Username: "me",
|
|
Password: "********",
|
|
ApplicationID: "XYZ",
|
|
ChangeDocumentID: "12345678",
|
|
TransportRequestID: "87654321",
|
|
FilePath: "myApp.abc",
|
|
CmClientOpts: []string{"-Dtest=abc123"},
|
|
},
|
|
}
|
|
}
|