-
Notifications
You must be signed in to change notification settings - Fork 787
/
fake_jenkins.go
299 lines (249 loc) · 7.24 KB
/
fake_jenkins.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package fake
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/jenkins-x/golang-jenkins"
)
// FakeJenkins contains the state of the fake JenkinsClient
type FakeJenkins struct {
baseURL string
Jobs []gojenkins.Job
JobMap map[string]*gojenkins.Job
}
// NewFakeJenkins creates a fake JenkinsClient that can be used in tests
func NewFakeJenkins() *FakeJenkins {
return &FakeJenkins{
JobMap: map[string]*gojenkins.Job{},
}
}
// GetJobs returns the jobs
func (j *FakeJenkins) GetJobs() ([]gojenkins.Job, error) {
return j.Jobs, nil
}
// GetJob gets a job by name
func (j *FakeJenkins) GetJob(name string) (gojenkins.Job, error) {
for _, job := range j.Jobs {
if job.Name == name {
return job, nil
}
}
return gojenkins.Job{}, j.notFoundf("job: %s", name)
}
// CreateJobWithXML create a job from XML
func (j *FakeJenkins) CreateJobWithXML(jobXml string, folder string) error {
folderJob := j.getOrCreateFolderJob(folder)
if folderJob != nil {
return nil
}
return j.notFoundf("job: %s", folder)
}
// CreateFolderJobWithXML creates a folder based job from XML
func (j *FakeJenkins) CreateFolderJobWithXML(jobXml string, folder string, jobName string) error {
folderJob := j.getOrCreateFolderJob(folder)
for _, job := range folderJob.Jobs {
if job.Name == jobName {
return nil
}
}
folderJob.Jobs = append(folderJob.Jobs, gojenkins.Job{
Name: jobName,
Url: folderJob.Url + "/job/" + folder,
})
return nil
}
func (j *FakeJenkins) getOrCreateFolderJob(folder string) *gojenkins.Job {
var folderJob *gojenkins.Job
for i, job := range j.Jobs {
if job.Name == folder {
folderJob = &j.Jobs[i]
break
}
}
if folderJob == nil {
j.Jobs = append(j.Jobs, gojenkins.Job{
Name: folder,
Url: "/job/" + folder,
})
folderJob = &j.Jobs[len(j.Jobs)-1]
}
return folderJob
}
// GetJobURLPath gets the job URL patjh
func (j *FakeJenkins) GetJobURLPath(name string) string {
return gojenkins.FullPath(name)
}
// IsErrNotFound returns true if the error is not found
func (j *FakeJenkins) IsErrNotFound(err error) bool {
te, ok := err.(gojenkins.APIError)
return ok && te.StatusCode == 404
}
// BaseURL returns the server base URL
func (j *FakeJenkins) BaseURL() string {
return j.baseURL
}
// SetHTTPClient sets the http client
func (j *FakeJenkins) SetHTTPClient(*http.Client) {
}
// Post posts an object
func (j *FakeJenkins) Post(string, url.Values, interface{}) (err error) {
return nil
}
// GetJobConfig gets the job config for the given name
func (j *FakeJenkins) GetJobConfig(name string) (gojenkins.JobItem, error) {
return gojenkins.JobItem{}, j.notImplemented()
}
// GetBuild gets the build for a specific job and build number
func (j *FakeJenkins) GetBuild(gojenkins.Job, int) (gojenkins.Build, error) {
return gojenkins.Build{}, j.notImplemented()
}
// GetLastBuild returns the last build of the job
func (j *FakeJenkins) GetLastBuild(gojenkins.Job) (gojenkins.Build, error) {
return gojenkins.Build{}, j.notImplemented()
}
// StopBuild stops the build
func (j *FakeJenkins) StopBuild(gojenkins.Job, int) error {
return nil
}
// GetMultiBranchJob gets a multi branch job of the given name
func (j *FakeJenkins) GetMultiBranchJob(string, string, string) (gojenkins.Job, error) {
return gojenkins.Job{}, j.notImplemented()
}
// GetJobByPath fake
func (j *FakeJenkins) GetJobByPath(names ...string) (gojenkins.Job, error) {
jobs := j.Jobs
lastIdx := len(names) - 1
for idx, name := range names {
found := false
for _, job := range jobs {
if job.Name == name {
if idx >= lastIdx {
return job, nil
}
jobs = job.Jobs
found = true
break
}
}
if !found {
break
}
}
return gojenkins.Job{}, j.notFoundf("job for path %s", strings.Join(names, "/"))
}
// GetOrganizationScanResult returns the organisation scan result
func (j *FakeJenkins) GetOrganizationScanResult(int, gojenkins.Job) (string, error) {
return "", j.notImplemented()
}
// CreateJob creates a job
func (j *FakeJenkins) CreateJob(gojenkins.JobItem, string) error {
return nil
}
// Reload reloads the fake server
func (j *FakeJenkins) Reload() error {
return nil
}
// Restart restarts the fake server
func (j *FakeJenkins) Restart() error {
return nil
}
// SafeRestart safely restarts the fake server
func (j *FakeJenkins) SafeRestart() error {
return nil
}
// QuietDown quiets down
func (j *FakeJenkins) QuietDown() error {
return nil
}
// GetCredential get the credential of the given name
func (j *FakeJenkins) GetCredential(string) (*gojenkins.Credentials, error) {
return nil, j.notImplemented()
}
// CreateCredential creates a credential
func (j *FakeJenkins) CreateCredential(string, string, string) error {
return nil
}
// DeleteJob deletes a job
func (j *FakeJenkins) DeleteJob(gojenkins.Job) error {
return nil
}
// UpdateJob updates a job
func (j *FakeJenkins) UpdateJob(gojenkins.JobItem, string) error {
return nil
}
// RemoveJob removes a job
func (j *FakeJenkins) RemoveJob(string) error {
return nil
}
// AddJobToView adds a job to the view
func (j *FakeJenkins) AddJobToView(string, gojenkins.Job) error {
return nil
}
// CreateView creates a view
func (j *FakeJenkins) CreateView(gojenkins.ListView) error {
return nil
}
// Build triggers a build
func (j *FakeJenkins) Build(gojenkins.Job, url.Values) error {
return nil
}
// GetBuildConsoleOutput get the console output
func (j *FakeJenkins) GetBuildConsoleOutput(gojenkins.Build) ([]byte, error) {
return nil, j.notImplemented()
}
// GetQueue gets the build queue
func (j *FakeJenkins) GetQueue() (gojenkins.Queue, error) {
return gojenkins.Queue{}, j.notImplemented()
}
// GetArtifact gets an artifact
func (j *FakeJenkins) GetArtifact(gojenkins.Build, gojenkins.Artifact) ([]byte, error) {
return nil, j.notImplemented()
}
// SetBuildDescription sets the build description
func (j *FakeJenkins) SetBuildDescription(gojenkins.Build, string) error {
return nil
}
// GetComputerObject gets the computer
func (j *FakeJenkins) GetComputerObject() (gojenkins.ComputerObject, error) {
return gojenkins.ComputerObject{}, j.notImplemented()
}
// GetComputers gets the computers
func (j *FakeJenkins) GetComputers() ([]gojenkins.Computer, error) {
return nil, j.notImplemented()
}
// GetComputer gets the computer
func (j *FakeJenkins) GetComputer(string) (gojenkins.Computer, error) {
return gojenkins.Computer{}, j.notImplemented()
}
// GetBuildURL gets the build URL
func (j *FakeJenkins) GetBuildURL(gojenkins.Job, int) string {
return ""
}
// GetLogFromURL gets the log from a URL
func (j *FakeJenkins) GetLogFromURL(string, int64, *gojenkins.LogData) error {
return nil
}
// TailLog tails the log
func (j *FakeJenkins) TailLog(string, io.Writer, time.Duration, time.Duration) error {
return nil
}
// TailLogFunc tails the log function
func (j *FakeJenkins) TailLogFunc(string, io.Writer) gojenkins.ConditionFunc {
return nil
}
// NewLogPoller creates a new log poller
func (j *FakeJenkins) NewLogPoller(string, io.Writer) *gojenkins.LogPoller {
return nil
}
func (j *FakeJenkins) notImplemented() error {
return fmt.Errorf("not implemented")
}
func (j *FakeJenkins) notFound(message string) error {
return fmt.Errorf("not found: %s", message)
}
func (j *FakeJenkins) notFoundf(message string, arguments ...interface{}) error {
return j.notFound(fmt.Sprintf(message, arguments...))
}