github.com/SAP/jenkins-library@v1.362.0/cmd/transportRequestUploadSOLMAN_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "fmt" 8 "github.com/SAP/jenkins-library/pkg/mock" 9 "github.com/SAP/jenkins-library/pkg/transportrequest/solman" 10 "github.com/stretchr/testify/assert" 11 "testing" 12 ) 13 14 type transportRequestUploadSOLMANMockUtils struct { 15 *mock.ExecMockRunner 16 *mock.FilesMock 17 } 18 19 func newTransportRequestUploadSOLMANTestsUtils(exitcode int) transportRequestUploadSOLMANMockUtils { 20 utils := transportRequestUploadSOLMANMockUtils{ 21 ExecMockRunner: &mock.ExecMockRunner{ExitCode: exitcode}, 22 FilesMock: &mock.FilesMock{}, 23 } 24 return utils 25 } 26 27 type ActionMock struct { 28 received solman.UploadAction 29 performCalled bool 30 failWith error 31 } 32 33 func (a *ActionMock) WithConnection(c solman.Connection) { 34 a.received.Connection = c 35 } 36 func (a *ActionMock) WithChangeDocumentID(id string) { 37 a.received.ChangeDocumentID = id 38 } 39 func (a *ActionMock) WithTransportRequestID(id string) { 40 a.received.TransportRequestID = id 41 } 42 func (a *ActionMock) WithApplicationID(id string) { 43 a.received.ApplicationID = id 44 } 45 func (a *ActionMock) WithFile(f string) { 46 a.received.File = f 47 } 48 func (a *ActionMock) WithCMOpts(opts []string) { 49 a.received.CMOpts = opts 50 } 51 func (a *ActionMock) Perform(fs solman.FileSystem, command solman.Exec) error { 52 a.performCalled = true 53 return a.failWith 54 } 55 56 type ConfigMock struct { 57 config *transportRequestUploadSOLMANOptions 58 } 59 60 func TestTrSolmanRunTransportRequestUpload(t *testing.T) { 61 t.Parallel() 62 63 t.Run("good", func(t *testing.T) { 64 t.Parallel() 65 66 t.Run("straight forward", func(t *testing.T) { 67 utilsMock := newTransportRequestUploadSOLMANTestsUtils(0) 68 configMock := newConfigMock() 69 actionMock := ActionMock{} 70 cpe := &transportRequestUploadSOLMANCommonPipelineEnvironment{} 71 72 err := runTransportRequestUploadSOLMAN(configMock.config, &actionMock, nil, utilsMock, cpe) 73 74 if assert.NoError(t, err) { 75 assert.Equal(t, actionMock.received, solman.UploadAction{ 76 Connection: solman.Connection{ 77 Endpoint: "https://example.org/solman", 78 User: "me", 79 Password: "********", 80 }, 81 ApplicationID: "XYZ", 82 ChangeDocumentID: "12345678", 83 TransportRequestID: "87654321", 84 File: "myApp.abc", 85 CMOpts: []string{"-Dtest=abc123"}, 86 }) 87 assert.True(t, actionMock.performCalled) 88 } 89 }) 90 }) 91 92 t.Run("bad", func(t *testing.T) { 93 t.Parallel() 94 95 t.Run("Error during deployment", func(t *testing.T) { 96 utilsMock := newTransportRequestUploadSOLMANTestsUtils(0) 97 configMock := newConfigMock() 98 actionMock := ActionMock{failWith: fmt.Errorf("upload failed")} 99 cpe := &transportRequestUploadSOLMANCommonPipelineEnvironment{} 100 101 err := runTransportRequestUploadSOLMAN(configMock.config, &actionMock, nil, utilsMock, cpe) 102 103 assert.Error(t, err, "upload failed") 104 }) 105 }) 106 } 107 108 func newConfigMock() *ConfigMock { 109 return &ConfigMock{ 110 config: &transportRequestUploadSOLMANOptions{ 111 Endpoint: "https://example.org/solman", 112 Username: "me", 113 Password: "********", 114 ApplicationID: "XYZ", 115 ChangeDocumentID: "12345678", 116 TransportRequestID: "87654321", 117 FilePath: "myApp.abc", 118 CmClientOpts: []string{"-Dtest=abc123"}, 119 }, 120 } 121 }