-
Notifications
You must be signed in to change notification settings - Fork 2
/
job_timeout_test.go
107 lines (85 loc) · 4.85 KB
/
job_timeout_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
package gocd_test
import (
"net/http"
"testing"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/stretchr/testify/assert"
)
func Test_client_GetDefaultJobTimeout(t *testing.T) {
t.Run("should be able fetch the default job timeout successfully", func(t *testing.T) {
server := mockServer([]byte(`{"default_job_timeout" : "0"}`), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetDefaultJobTimeout()
assert.NoError(t, err)
assert.Equal(t, map[string]string{"default_job_timeout": "0"}, actual)
})
t.Run("should error out while fetching default job timeout due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(`{"default_job_timeout" : "0"}`), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionTwo}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetDefaultJobTimeout()
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/admin/config/server/default_job_timeout\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, map[string]string(nil), actual)
})
t.Run("should error out while fetching default job timeout due to missing headers", func(t *testing.T) {
server := mockServer([]byte(`{"default_job_timeout" : "0"}`), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetDefaultJobTimeout()
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/admin/config/server/default_job_timeout\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, map[string]string(nil), actual)
})
t.Run("should error out while fetching default job timeout as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte(`default_job_timeout" : "0"}`), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetDefaultJobTimeout()
assert.EqualError(t, err, "reading response body errored with: invalid character 'd' looking for beginning of value")
assert.Equal(t, map[string]string(nil), actual)
})
t.Run("should error out while fetching default job timeout as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
actual, err := client.GetDefaultJobTimeout()
assert.EqualError(t, err, "call made to get default job timeout errored with: "+
"Get \"http://localhost:8156/go/api/admin/config/server/default_job_timeout\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, map[string]string(nil), actual)
})
}
func Test_client_UpdateDefaultJobTimeout(t *testing.T) {
t.Run("should be able update the default job timeout successfully", func(t *testing.T) {
server := mockServer([]byte(`{"default_job_timeout" : "10"}`), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.UpdateDefaultJobTimeout(10)
assert.NoError(t, err)
})
t.Run("should error out while updating default job timeout due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(`{"default_job_timeout" : "0"}`), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionTwo}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.UpdateDefaultJobTimeout(10)
assert.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/config/server/default_job_timeout\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
})
t.Run("should error out while updating default job timeout due to missing headers", func(t *testing.T) {
server := mockServer([]byte(`{"default_job_timeout" : "0"}`), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.UpdateDefaultJobTimeout(10)
assert.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/config/server/default_job_timeout\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
})
t.Run("should error out while updating default job timeout as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
err := client.UpdateDefaultJobTimeout(10)
assert.EqualError(t, err, "call made to update default job timeout errored with: "+
"Post \"http://localhost:8156/go/api/admin/config/server/default_job_timeout\": dial tcp [::1]:8156: connect: connection refused")
})
}