-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstages_test.go
115 lines (93 loc) · 4.63 KB
/
stages_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
package gocd_test
import (
"net/http"
"testing"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_client_RunStage(t *testing.T) {
correctJobsHeader := map[string]string{"Accept": gocd.HeaderVersionTwo, gocd.HeaderConfirm: "true"}
t.Run("should error out while running stage from server", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
actual, err := client.RunStage(gocd.Stage{})
require.EqualError(t, err, "call made to run stage errored with: "+
"Post \"http://localhost:8156/go/api/stages/run\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, "", actual)
})
t.Run("should error out while running stage from a pipeline as server returned non 200 status code", func(t *testing.T) {
server := mockServer([]byte("scheduledJobJSON"), http.StatusBadGateway, correctJobsHeader, true, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.RunStage(gocd.Stage{})
require.EqualError(t, err, "got 502 from GoCD while making POST call for "+server.URL+
"/api/stages/run\nwith BODY:scheduledJobJSON")
assert.Equal(t, "", actual)
})
t.Run("should error out while running stage from a pipeline as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte(`{"email_on_failure"`), http.StatusAccepted, correctJobsHeader, true, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.RunStage(gocd.Stage{})
require.EqualError(t, err, "reading response body errored with: unexpected end of JSON input")
assert.Equal(t, "", actual)
})
t.Run("should be able to run stage from a pipeline present in GoCD", func(t *testing.T) {
runFailedJobResponse := `{"message": "Request to schedule stage pipeline1/2/myStage accepted"}`
server := mockServer([]byte(runFailedJobResponse), http.StatusAccepted, correctJobsHeader, true, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
stage := gocd.Stage{
Pipeline: "pipeline1",
PipelineInstance: "2",
Name: "myStage",
Jobs: nil,
}
expected := "Request to schedule stage pipeline1/2/myStage accepted"
actual, err := client.RunStage(stage)
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
}
func Test_client_CancelStage(t *testing.T) {
correctJobsHeader := map[string]string{"Accept": gocd.HeaderVersionThree, gocd.HeaderConfirm: "true"}
t.Run("should error out while cancelling stage from server", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
actual, err := client.CancelStage(gocd.Stage{})
require.EqualError(t, err, "call made to cancel stage errored with: "+
"Post \"http://localhost:8156/go/api/stages/cancel\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, "", actual)
})
t.Run("should error out while cancelling stage from a pipeline as server returned non 200 status code", func(t *testing.T) {
server := mockServer([]byte("scheduledJobJSON"), http.StatusBadGateway, correctJobsHeader, true, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.CancelStage(gocd.Stage{})
require.EqualError(t, err, "got 502 from GoCD while making POST call for "+server.URL+
"/api/stages/cancel\nwith BODY:scheduledJobJSON")
assert.Equal(t, "", actual)
})
t.Run("should error out while cancelling stage from a pipeline as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte(`{"email_on_failure"`), http.StatusOK, correctJobsHeader, true, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.CancelStage(gocd.Stage{})
require.EqualError(t, err, "reading response body errored with: unexpected end of JSON input")
assert.Equal(t, "", actual)
})
t.Run("should be able to cancel stage from a pipeline present in GoCD", func(t *testing.T) {
runFailedJobResponse := `{"message": "Request to schedule stage pipeline1/2/myStage accepted"}`
server := mockServer([]byte(runFailedJobResponse), http.StatusOK, correctJobsHeader, true, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
stage := gocd.Stage{
Pipeline: "pipeline1",
PipelineInstance: "2",
Name: "myStage",
StageCounter: "2",
Jobs: nil,
}
expected := "Request to schedule stage pipeline1/2/myStage accepted"
actual, err := client.CancelStage(stage)
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
}