github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/transportrequest/solman/create_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package solman 5 6 import ( 7 "bytes" 8 "fmt" 9 "github.com/SAP/jenkins-library/pkg/mock" 10 "github.com/stretchr/testify/assert" 11 "testing" 12 ) 13 14 func TestSolmanCreateTransportRequest(t *testing.T) { 15 16 a := CreateAction{ 17 Connection: Connection{ 18 Endpoint: "https://example.org/solman", 19 User: "me", 20 Password: "******", 21 }, 22 ChangeDocumentID: "123", 23 DevelopmentSystemID: "XXX~EXT_SRV", 24 CMOpts: []string{"-Dprop1=abc", "-Dprop2=123"}, 25 } 26 27 t.Run("straight forward", func(t *testing.T) { 28 29 e := getExecMock() 30 e.StdoutReturn = map[string]string{"^cmclient.*": "ABCK123456"} 31 32 examinee := a 33 transportRequestId, err := examinee.Perform(e) 34 35 if assert.NoError(t, err) { 36 assert.Equal(t, []mock.ExecCall{mock.ExecCall{ 37 Exec: "cmclient", 38 Params: []string{ 39 "--endpoint", "https://example.org/solman", 40 "--user", "me", 41 "--password", "******", 42 "create-transport", 43 "-cID", "123", 44 "-dID", "XXX~EXT_SRV", 45 }, 46 }}, e.Calls) 47 assert.Equal(t, "ABCK123456", transportRequestId) 48 assert.Equal(t, []string{"CMCLIENT_OPTS=-Dprop1=abc -Dprop2=123"}, e.Env) 49 } 50 }) 51 52 t.Run("fail with error", func(t *testing.T) { 53 54 e := getExecMock() 55 e.ShouldFailOnCommand = map[string]error{"^cmclient.*": fmt.Errorf("creating transport request failed")} 56 57 examinee := a 58 _, err := examinee.Perform(e) 59 60 assert.EqualError(t, err, "cannot create transport request: creating transport request failed") 61 }) 62 63 t.Run("fail via return code", func(t *testing.T) { 64 65 e := getExecMock() 66 e.ExitCode = 42 67 68 examinee := a 69 _, err := examinee.Perform(e) 70 71 assert.EqualError(t, err, "cannot create transport request: create transport request command returned with exit code '42'") 72 }) 73 74 t.Run("input missing", func(t *testing.T) { 75 76 e := getExecMock() 77 78 examinee := a 79 examinee.Connection = Connection{} 80 examinee.ChangeDocumentID = "" 81 82 _, err := examinee.Perform(e) 83 84 if assert.Error(t, err) { 85 // I don't want to rely on the order of the parameters 86 assert.Contains(t, err.Error(), "cannot create transport request: the following parameters are not available") 87 assert.Contains(t, err.Error(), "Connection.Endpoint") 88 assert.Contains(t, err.Error(), "Connection.User") 89 assert.Contains(t, err.Error(), "Connection.Password") 90 assert.Contains(t, err.Error(), "ChangeDocumentID") 91 assert.Empty(t, e.Calls) 92 } 93 }) 94 } 95 96 func getExecMock() *mock.ExecMockRunner { 97 var out bytes.Buffer 98 e := &mock.ExecMockRunner{} 99 e.Stdout(&out) 100 return e 101 }