2021-02-19 14:07:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:58:20 +02:00
|
|
|
func newTransportRequestUploadSOLMANTestsUtils(exitcode int) transportRequestUploadSOLMANMockUtils {
|
2021-02-19 14:07:18 +02:00
|
|
|
utils := transportRequestUploadSOLMANMockUtils{
|
2021-03-19 09:58:20 +02:00
|
|
|
ExecMockRunner: &mock.ExecMockRunner{ExitCode: exitcode},
|
2021-02-19 14:07:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:58:20 +02:00
|
|
|
type ConfigMock struct {
|
|
|
|
config *transportRequestUploadSOLMANOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrSolmanRunTransportRequestUpload(t *testing.T) {
|
2021-02-19 14:07:18 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-05-21 08:54:43 +02:00
|
|
|
t.Run("good", func(t *testing.T) {
|
2021-02-19 14:07:18 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("straight forward", func(t *testing.T) {
|
2021-03-19 09:58:20 +02:00
|
|
|
utilsMock := newTransportRequestUploadSOLMANTestsUtils(0)
|
|
|
|
configMock := newConfigMock()
|
|
|
|
actionMock := ActionMock{}
|
|
|
|
cpe := &transportRequestUploadSOLMANCommonPipelineEnvironment{}
|
2021-02-19 14:07:18 +02:00
|
|
|
|
2021-03-19 09:58:20 +02:00
|
|
|
err := runTransportRequestUploadSOLMAN(configMock.config, &actionMock, nil, utilsMock, cpe)
|
2021-02-19 14:07:18 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
2021-03-19 09:58:20 +02:00
|
|
|
assert.Equal(t, actionMock.received, solman.UploadAction{
|
2021-02-19 14:07:18 +02:00
|
|
|
Connection: solman.Connection{
|
|
|
|
Endpoint: "https://example.org/solman",
|
|
|
|
User: "me",
|
|
|
|
Password: "********",
|
|
|
|
},
|
|
|
|
ApplicationID: "XYZ",
|
|
|
|
ChangeDocumentID: "12345678",
|
|
|
|
TransportRequestID: "87654321",
|
2021-03-19 09:58:20 +02:00
|
|
|
File: "myApp.abc",
|
2021-02-19 14:07:18 +02:00
|
|
|
CMOpts: []string{"-Dtest=abc123"},
|
|
|
|
})
|
2021-03-19 09:58:20 +02:00
|
|
|
assert.True(t, actionMock.performCalled)
|
2021-02-19 14:07:18 +02:00
|
|
|
}
|
|
|
|
})
|
2021-05-21 08:54:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-02-19 14:07:18 +02:00
|
|
|
|
|
|
|
t.Run("Error during deployment", func(t *testing.T) {
|
2021-03-19 09:58:20 +02:00
|
|
|
utilsMock := newTransportRequestUploadSOLMANTestsUtils(0)
|
|
|
|
configMock := newConfigMock()
|
|
|
|
actionMock := ActionMock{failWith: fmt.Errorf("upload failed")}
|
|
|
|
cpe := &transportRequestUploadSOLMANCommonPipelineEnvironment{}
|
2021-02-19 14:07:18 +02:00
|
|
|
|
2021-03-19 09:58:20 +02:00
|
|
|
err := runTransportRequestUploadSOLMAN(configMock.config, &actionMock, nil, utilsMock, cpe)
|
2021-02-19 14:07:18 +02:00
|
|
|
|
|
|
|
assert.Error(t, err, "upload failed")
|
|
|
|
})
|
2021-03-19 09:58:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigMock() *ConfigMock {
|
|
|
|
return &ConfigMock{
|
|
|
|
config: &transportRequestUploadSOLMANOptions{
|
2021-05-21 08:54:43 +02:00
|
|
|
Endpoint: "https://example.org/solman",
|
|
|
|
Username: "me",
|
|
|
|
Password: "********",
|
|
|
|
ApplicationID: "XYZ",
|
|
|
|
ChangeDocumentID: "12345678",
|
|
|
|
TransportRequestID: "87654321",
|
|
|
|
FilePath: "myApp.abc",
|
|
|
|
CmClientOpts: []string{"-Dtest=abc123"},
|
2021-03-19 09:58:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|