|
28 | 28 | end
|
29 | 29 |
|
30 | 30 | describe "InstanceMethods" do
|
| 31 | + script_text = <<'EOS' |
| 32 | + def Map findJobs(Object obj, String namespace = null) { |
| 33 | + def found = [:] |
| 34 | +
|
| 35 | + // groovy apparently can't #collect on a list and return a map? |
| 36 | + obj.items.each { job -> |
| 37 | + // a possibly better approach would be to walk the parent chain from // |
| 38 | + // each job |
| 39 | + def path = job.getName() |
| 40 | + if (namespace) { |
| 41 | + path = "${namespace}/" + path |
| 42 | + } |
| 43 | + found[path] = job |
| 44 | + // intentionally not using `instanceof` here so we don't blow up if the |
| 45 | + // cloudbees-folder plugin is not installed |
| 46 | + if (job.getClass().getName() == 'com.cloudbees.hudson.plugins.folder.Folder') { |
| 47 | + found << findJobs(job, path) |
| 48 | + } |
| 49 | + } |
| 50 | +
|
| 51 | + found |
| 52 | + } |
| 53 | +
|
| 54 | + void job_list_json() { |
| 55 | + def jobs = findJobs(Jenkins.getInstance()) |
| 56 | +
|
| 57 | + def allInfo = jobs.collect { path, job -> |
| 58 | + // at least these job classes do not respond to respond to #isDisabled: |
| 59 | + // - org.jenkinsci.plugins.workflow.job.WorkflowJob |
| 60 | + // - com.cloudbees.hudson.plugins.folder.Folder |
| 61 | + def enabled = false |
| 62 | + if (job.metaClass.respondsTo(job, 'isDisabled')) { |
| 63 | + enabled = !job.isDisabled() |
| 64 | + } |
| 65 | +
|
| 66 | + [ |
| 67 | + _class: job.getClass().getCanonicalName().toString(), |
| 68 | + name: path, |
| 69 | + url: Hudson.getInstance().getRootUrl().toString() + job.getUrl().toString(), |
| 70 | + ] |
| 71 | + } |
| 72 | +
|
| 73 | + def builder = new groovy.json.JsonBuilder(allInfo) |
| 74 | + out.println(builder.toString()) |
| 75 | + } |
| 76 | +
|
| 77 | + job_list_json() |
| 78 | +EOS |
31 | 79 |
|
32 | 80 | describe "#create_job" do
|
33 | 81 | it "accepts job_name and xml and creates the job" do
|
|
45 | 93 |
|
46 | 94 | mock_job_list_response = { "jobs" => [] } # job response w/ 0 jobs
|
47 | 95 |
|
48 |
| - @client.should_receive(:api_get_request).with('').and_return(mock_job_list_response) |
| 96 | + @client.should_receive(:api_post_request).with('/scriptText', {'script' => script_text}, true).and_return(FakeResponse.new(200,mock_job_list_response)) |
49 | 97 | @job.should_receive(:create).with(job_name, xml).and_return(nil)
|
50 | 98 |
|
51 | 99 | @job.create_or_update(job_name, xml)
|
|
55 | 103 | job_name = 'test_job'
|
56 | 104 | xml = '<name>somename</name>'
|
57 | 105 |
|
58 |
| - mock_job_list_response = { "jobs" => [ { "name" => job_name } ] } # job response w/ 1 job |
| 106 | + mock_job_list_response = { "jobs" => [ { "_class": "hudson.model.FreeStyleProject", "name": job_name,"url": "https://jenkins.example.com/job/#{job_name}/"} ] } |
59 | 107 |
|
60 |
| - @client.should_receive(:api_get_request).with('').and_return(mock_job_list_response) |
| 108 | + @client.should_receive(:api_post_request).with('/scriptText', {'script' => script_text}, true).and_return(FakeResponse.new(200,mock_job_list_response)) |
61 | 109 | @job.should_receive(:update).with(job_name, xml).and_return(nil)
|
62 | 110 |
|
63 | 111 | @job.create_or_update(job_name, xml)
|
|
275 | 323 |
|
276 | 324 | describe "#exists?" do
|
277 | 325 | it "accepts a job name and returns true if the job exists" do
|
278 |
| - @client.should_receive(:api_get_request).and_return( |
279 |
| - @sample_json_response) |
280 |
| - @job.exists?("test_job").should == true |
| 326 | + job_name = 'test_job' |
| 327 | + mock_job_list_response = { "jobs" => [ { "_class": "hudson.model.FreeStyleProject", "name": job_name,"url": "https://jenkins.example.com/job/#{job_name}/"} ] } |
| 328 | + @client.should_receive(:api_post_request).with('/scriptText', {'script' => script_text}, true).and_return(FakeResponse.new(200,mock_job_list_response)) |
| 329 | + @job.exists?(job_name).should == true |
| 330 | + end |
| 331 | + |
| 332 | + it "does not match folder names" do |
| 333 | + job_name = 'test_job' |
| 334 | + mock_job_list_response = { "jobs" => [ { "_class": "com.cloudbees.hudson.plugins.folder.Folder", "name": job_name,"url": "https://jenkins.example.com/job/#{job_name}/"} ] } |
| 335 | + @client.should_receive(:api_post_request).with('/scriptText', {'script' => script_text}, true).and_return(FakeResponse.new(200,mock_job_list_response)) |
| 336 | + |
| 337 | + @job.exists?(job_name).should == false |
281 | 338 | end
|
282 | 339 | end
|
283 | 340 |
|
|
296 | 353 |
|
297 | 354 | describe "#list" do
|
298 | 355 | it "accepts a filter and returns all jobs matching the filter" do
|
299 |
| - @client.should_receive(:api_get_request).and_return( |
300 |
| - "jobs" => ["test_job"]) |
| 356 | + job_name = 'test_job' |
| 357 | + mock_job_list_response = { "jobs" => [ { "_class": "com.cloudbees.hudson.plugins.folder.Folder", "name": job_name,"url": "https://jenkins.example.com/job/#{job_name}/"} ] } |
| 358 | + @client.should_receive(:api_post_request).with('/scriptText', {'script' => script_text}, true).and_return(FakeResponse.new(200,mock_job_list_response)) |
| 359 | + |
301 | 360 | @job.list("filter").class.should == Array
|
302 | 361 | end
|
303 | 362 | end
|
|
0 commit comments