-
Notifications
You must be signed in to change notification settings - Fork 2
/
gojenkins_test.go
262 lines (220 loc) · 5.34 KB
/
gojenkins_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package gojenkins
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"testing"
)
var jenkins *Jenkins
var testJob string
type Config struct {
Baseurl string
Username string
Password string
TestJob string
}
func Init() {
jsonConfig, err := ioutil.ReadFile("test.json")
if err != nil {
log.Fatalf("Error reading test config. Provide one based on test.json.example: %s", err)
}
var config Config
err = json.Unmarshal(jsonConfig, &config)
if err != nil {
log.Fatal(err)
}
jenkins = &Jenkins{
Baseurl: config.Baseurl,
}
jenkins.SetAuth(config.Username, config.Password)
testJob = config.TestJob
}
func TestAuth(t *testing.T) {
Init()
_, err := jenkins.Get("")
if err != nil {
t.Errorf("Expected proper authentication but instead got:\n%s", err.Error())
}
}
func TestBadAuth(t *testing.T) {
Init()
jenkins.SetAuth("bad", "auth")
_, err := jenkins.Get("")
if err == nil {
t.Error("Incorrect authorization should return an error")
}
}
func TestParseError(t *testing.T) {
jenkins = &Jenkins{
Baseurl: "http://example.com",
}
_, err := jenkins.Get("")
if err == nil {
t.Errorf("Expected a parsing error because the target is not a jenkins instance")
}
}
func TestBadURL(t *testing.T) {
jenkins = &Jenkins{
Baseurl: "htt://example.com",
}
_, err := jenkins.Get("")
if err == nil {
t.Errorf("Expected a net/http error for incorrect URL")
}
}
func TestJobsParseError(t *testing.T) {
jenkins = &Jenkins{
Baseurl: "http://example.com",
}
defer func() {
if r := recover(); r != nil {
t.Error("jenkins.Jobs() on an incorrect url should not panic")
}
}()
_, err := jenkins.Jobs()
if err == nil {
t.Error("Expected to receive an error from calling jenkins.Jobs() on an incorrect url")
}
}
func TestJobs(t *testing.T) {
Init()
jobs, _ := jenkins.Jobs()
for _, job := range jobs {
t.Log(job.URL)
}
}
func TestArtifacts(t *testing.T) {
Init()
jobs, err := jenkins.Jobs()
if err != nil {
t.Error(err)
}
artifacts, _ := jenkins.Artifacts(jobs[testJob], "lastSuccessfulBuild")
for _, artifact := range artifacts {
t.Log(artifact.FileName)
}
}
func TestArtifactsParseError(t *testing.T) {
jenkins = &Jenkins{
Baseurl: "http://example.com",
}
defer func() {
if r := recover(); r != nil {
t.Error("jenkins.Artifacts() on an incorrect url/data should not panic")
}
}()
_, err := jenkins.Artifacts(Job{"bad-job", "bad-job", "bad-color"}, "bad-build")
if err == nil {
t.Error("Expected to receive an error from calling jenkins.Artifacts() on an incorrect url/data")
}
t.Log(err)
}
func TestArtifactsBadJobBuild(t *testing.T) {
Init()
defer func() {
if r := recover(); r != nil {
t.Error("jenkins.Artifacts() on an incorrect job should not panic")
}
}()
_, err := jenkins.Artifacts(Job{"bad-job", "bad-job", "bad-color"}, "bad-build")
if err == nil {
t.Error("Expected to receive an error from calling jenkins.Artifacts() on an incorrect url/data")
}
t.Log(err)
}
func TestArtifactsBadBuild(t *testing.T) {
Init()
defer func() {
if r := recover(); r != nil {
t.Error("jenkins.Artifacts() on an incorrect job should not panic")
}
}()
jobs, err := jenkins.Jobs()
if err != nil {
t.Errorf("Did not expect an error from listing jobs but got: ", err.Error())
}
for _, job := range jobs {
_, err = jenkins.Artifacts(job, "bad-build")
if err == nil {
t.Error("Expected to receive an error from calling jenkins.Artifacts() on an incorrect url/data")
}
t.Log(err)
}
}
func TestDownloadNoAuth(t *testing.T) {
Init()
jenkins.SetAuth("bad", "auth")
_, err := jenkins.Download(Job{"bad-job", "bad-job", "bad-color"}, "lastSuccessfulBuild", Artifact{"bad-path", "bad-file", "bad-relative-path"})
if err == nil {
t.Error("test jenkins.Download: Incorrect authorization should return an error")
}
}
func TestDownloadBadBuild(t *testing.T) {
Init()
jobs, err := jenkins.Jobs()
if err != nil {
t.Error(err)
}
_, err = jenkins.Download(jobs[testJob], "bad-build", Artifact{"bad-path", "bad-file", "bad-relative-path"})
if err == nil {
t.Error("Asking for a download from a bad build should be an error")
}
}
func TestDownloadBadFile(t *testing.T) {
Init()
jobs, err := jenkins.Jobs()
if err != nil {
t.Error(err)
}
_, err = jenkins.Download(jobs[testJob], "lastSuccessfulBuild", Artifact{"bad-path", "bad-file", "bad-relative-path"})
if err == nil {
t.Error("Asking for a download for a bad file should be an error")
}
}
func TestDownloadArtifactsLatest(t *testing.T) {
Init()
jobs, err := jenkins.Jobs()
if err != nil {
t.Error(err)
}
err = os.Mkdir("downloads", 0700)
if err != nil {
t.Error(err)
}
pwd, err := os.Getwd()
if err != nil {
t.Error(err)
}
err = os.Chdir("downloads")
if err != nil {
t.Error(err)
}
artifacts, _ := jenkins.Artifacts(jobs[testJob], "lastSuccessfulBuild")
for _, artifact := range artifacts {
t.Logf("Downloading file: %s\n", artifact.FileName)
out, err := os.Create(artifact.FileName)
if err != nil {
t.Error(err)
}
defer out.Close()
a, err := jenkins.Download(jobs[testJob], "lastSuccessfulBuild", artifact)
if err != nil {
t.Error("Downloading a proper artifact should not result in an error")
}
defer a.Close()
_, err = io.Copy(out, a)
if err != nil {
t.Error(err)
}
}
err = os.Chdir(pwd)
if err != nil {
t.Error(err)
}
err = os.RemoveAll("downloads")
if err != nil {
t.Error(err)
}
}