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