-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaintenance_test.go
120 lines (93 loc) · 5.32 KB
/
maintenance_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
package gocd_test
import (
_ "embed"
"net/http"
"testing"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/stretchr/testify/assert"
)
//go:embed internal/fixtures/maintenance.json
var maintenanceJSON string
func Test_client_EnableMaintenanceMode(t *testing.T) {
t.Run("should enable the maintenance mode successfully", func(t *testing.T) {
server := mockServer(nil, http.StatusNoContent, map[string]string{"Accept": gocd.HeaderVersionOne, "X-GoCD-Confirm": "true"}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.EnableMaintenanceMode()
assert.NoError(t, err)
})
t.Run("should error out while enabling maintenance mode as no valid headers set", func(t *testing.T) {
server := mockServer(nil, http.StatusNoContent, map[string]string{"Accept": gocd.HeaderVersionTwo, "X-GoCD-Confirm": "true"}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.EnableMaintenanceMode()
assert.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/maintenance_mode/enable\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
})
t.Run("should error out while making client call to enable maintenance mode", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
err := client.EnableMaintenanceMode()
assert.EqualError(t, err, "call made to enable maintenance mode errored with: "+
"Post \"http://localhost:8156/go/api/admin/maintenance_mode/enable\": dial tcp [::1]:8156: connect: connection refused")
})
}
func Test_client_DisableMaintenanceMode(t *testing.T) {
t.Run("should disable the maintenance mode successfully", func(t *testing.T) {
server := mockServer(nil, http.StatusNoContent, map[string]string{"Accept": gocd.HeaderVersionOne, "X-GoCD-Confirm": "true"}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.DisableMaintenanceMode()
assert.NoError(t, err)
})
t.Run("should error out while disabling maintenance mode as no valid headers set", func(t *testing.T) {
server := mockServer(nil, http.StatusNoContent, map[string]string{"Accept": gocd.HeaderVersionTwo, "X-GoCD-Confirm": "true"}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
err := client.DisableMaintenanceMode()
assert.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/maintenance_mode/disable\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
})
t.Run("should error out while making client call to disable maintenance mode", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
err := client.DisableMaintenanceMode()
assert.EqualError(t, err, "call made to disable maintenance mode errored with: "+
"Post \"http://localhost:8156/go/api/admin/maintenance_mode/disable\": dial tcp [::1]:8156: connect: connection refused")
})
}
func Test_client_GetMaintenanceModeInfo(t *testing.T) {
t.Run("should fetch the maintenance mode information successfully", func(t *testing.T) {
server := mockServer([]byte(maintenanceJSON), http.StatusOK, map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := &gocd.Maintenance{}
expected.MaintenanceInfo.Enabled = false
expected.MaintenanceInfo.Metadata.UpdatedBy = "admin"
expected.MaintenanceInfo.Metadata.UpdatedOn = "2019-01-02T04:18:28Z"
actual, err := client.GetMaintenanceModeInfo()
assert.NoError(t, err)
assert.Equal(t, actual, actual)
})
t.Run("should error out with 404 while fetching maintenance mode information due to wrong headers", func(t *testing.T) {
server := mockServer([]byte("maintenanceJSON"), http.StatusOK, map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetMaintenanceModeInfo()
assert.EqualError(t, err, "reading response body errored with: invalid character 'm' looking for beginning of value")
assert.Equal(t, gocd.Maintenance{}, actual)
})
t.Run("should error out with 404 while fetching maintenance mode information due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(maintenanceJSON), http.StatusOK, map[string]string{"Accept": gocd.HeaderVersionTwo}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetMaintenanceModeInfo()
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/admin/maintenance_mode/info\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, gocd.Maintenance{}, actual)
})
t.Run("should error out while fetching maintenance mode information 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.GetMaintenanceModeInfo()
assert.EqualError(t, err, "call made to get maintenance mode information errored with: "+
"Get \"http://localhost:8156/go/api/admin/maintenance_mode/info\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, gocd.Maintenance{}, actual)
})
}