github.com/SAP/jenkins-library@v1.362.0/cmd/isChangeInDevelopment_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/SAP/jenkins-library/pkg/mock" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 type isChangeInDevelopmentMockUtils struct { 15 *mock.ExecMockRunner 16 } 17 18 func newIsChangeInDevelopmentTestsUtils() isChangeInDevelopmentMockUtils { 19 utils := isChangeInDevelopmentMockUtils{ 20 ExecMockRunner: &mock.ExecMockRunner{}, 21 } 22 return utils 23 } 24 25 func TestRunIsChangeInDevelopment(t *testing.T) { 26 27 t.Parallel() 28 29 config := isChangeInDevelopmentOptions{ 30 Endpoint: "https://example.org/cm", 31 Username: "me", 32 Password: "****", 33 ChangeDocumentID: "12345678", 34 CmClientOpts: []string{"-Dabc=123", "-Ddef=456"}, 35 FailIfStatusIsNotInDevelopment: true, // this is the default 36 } 37 38 expectedShellCall := mock.ExecCall{ 39 Exec: "cmclient", 40 Params: []string{ 41 "--endpoint", "https://example.org/cm", 42 "--user", "me", 43 "--password", "****", 44 "is-change-in-development", 45 "--change-id", "12345678", 46 "--return-code", 47 }, 48 } 49 50 t.Run("change found and in status IN_DEVELOPMENT", func(t *testing.T) { 51 52 cmd := newIsChangeInDevelopmentTestsUtils() 53 cmd.ExitCode = 0 // this exit code represents a change in status IN_DEVELOPMENT 54 cpe := &isChangeInDevelopmentCommonPipelineEnvironment{} 55 56 err := runIsChangeInDevelopment(&config, nil, cmd, cpe) 57 58 assert.Equal(t, cpe.custom.isChangeInDevelopment, true) 59 if assert.NoError(t, err) { 60 assert.Equal(t, []string{"CMCLIENT_OPTS=-Dabc=123 -Ddef=456"}, cmd.Env) 61 assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls) 62 } 63 }) 64 65 t.Run("change found and not in status IN_DEVELOPMENT", func(t *testing.T) { 66 67 cmd := newIsChangeInDevelopmentTestsUtils() 68 cmd.ExitCode = 3 // this exit code represents a change which is not in status IN_DEVELOPMENT 69 cpe := &isChangeInDevelopmentCommonPipelineEnvironment{} 70 71 err := runIsChangeInDevelopment(&config, nil, cmd, cpe) 72 73 assert.Equal(t, cpe.custom.isChangeInDevelopment, false) 74 if assert.EqualError(t, err, "change '12345678' is not in status 'in development'") { 75 assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls) 76 } 77 }) 78 79 t.Run("change found and not in status IN_DEVELOPMENT, but we don't fail", func(t *testing.T) { 80 81 cmd := newIsChangeInDevelopmentTestsUtils() 82 cmd.ExitCode = 3 // this exit code represents a change which is not in status IN_DEVELOPMENT 83 84 myConfig := config 85 myConfig.FailIfStatusIsNotInDevelopment = false // needs to be explicitly configured 86 cpe := &isChangeInDevelopmentCommonPipelineEnvironment{} 87 88 err := runIsChangeInDevelopment(&myConfig, nil, cmd, cpe) 89 90 assert.Equal(t, cpe.custom.isChangeInDevelopment, false) 91 if assert.NoError(t, err) { 92 assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls) 93 } 94 }) 95 96 t.Run("invalid credentials", func(t *testing.T) { 97 98 cmd := newIsChangeInDevelopmentTestsUtils() 99 cmd.ExitCode = 2 // this exit code represents invalid credentials 100 cpe := &isChangeInDevelopmentCommonPipelineEnvironment{} 101 102 err := runIsChangeInDevelopment(&config, nil, cmd, cpe) 103 104 if assert.EqualError(t, err, "cannot retrieve change status: invalid credentials") { 105 assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls) 106 } 107 }) 108 109 t.Run("generic failure reported via exit code", func(t *testing.T) { 110 111 cmd := newIsChangeInDevelopmentTestsUtils() 112 cmd.ExitCode = 1 // this exit code indicates something went wrong 113 cpe := &isChangeInDevelopmentCommonPipelineEnvironment{} 114 115 err := runIsChangeInDevelopment(&config, nil, cmd, cpe) 116 117 if assert.EqualError(t, err, "cannot retrieve change status: check log for details") { 118 assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls) 119 } 120 }) 121 122 t.Run("generic failure reported via error", func(t *testing.T) { 123 124 cmd := newIsChangeInDevelopmentTestsUtils() 125 cmd.ExitCode = 1 // this exit code indicates something went wrong 126 cmd.ShouldFailOnCommand = map[string]error{"cm.*": fmt.Errorf("%v", "Something went wrong")} 127 cpe := &isChangeInDevelopmentCommonPipelineEnvironment{} 128 129 err := runIsChangeInDevelopment(&config, nil, cmd, cpe) 130 131 if assert.EqualError(t, err, "cannot retrieve change status: Something went wrong") { 132 assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls) 133 } 134 }) 135 }