Skip to content

Commit

Permalink
Added spec test for setting ci address and list all j jobs name in je…
Browse files Browse the repository at this point in the history
…nkins.
  • Loading branch information
tuo committed Aug 30, 2011
1 parent b7ed1ef commit b3aa135
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
doc/*
1 change: 1 addition & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm --create use ree-1.8.7@jenkins-remote-api
2 changes: 2 additions & 0 deletions jenkins-remote-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.add_development_dependency 'rspec', '~> 2.6'
s.add_development_dependency 'mechanize', '~>2.0.1'
s.add_development_dependency 'libxml-ruby','~>2.2.2'
end
4 changes: 3 additions & 1 deletion lib/jenkins-remote-api.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Dir[File.dirname(__FILE__) + '/jenkins-remote-api/*.rb'].each {|file| require file }
require 'mechanize'
require 'libxml'
Dir[File.dirname(__FILE__) + '/jenkins-remote-api/api/*.rb'].each {|file| require file }
24 changes: 24 additions & 0 deletions lib/jenkins-remote-api/api/jenkins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Api
class Jenkins
include LibXML

def self.set_ci_addr(url)
@@ci_addr = url.end_with?("/") ? url : url + "/"
end

def self.ci_addr
@@ci_addr
end

def self.list_all_job_names
xml = ""
Mechanize.new.get(ci_addr + "api/xml") do |page|
xml = page.body
end
parser = LibXML::XML::Parser.string(xml)
doc = parser.parse
doc.find('//job').collect{|job| job.find_first('name').content }
end
end
end

8 changes: 0 additions & 8 deletions lib/jenkins-remote-api/jenkins.rb

This file was deleted.

46 changes: 42 additions & 4 deletions spec/jenkins_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe Jenkins do
it "should works as first spec" do
Jenkins.first_spec_status.should == "hello world"
describe Api::Jenkins do

it "should set Jenkins ci address" do
Api::Jenkins.set_ci_addr("http://some_ci_address:8080/")
Api::Jenkins.ci_addr.should == "http://some_ci_address:8080/"
end
end

it "should set Jenkins ci address end with slash" do
Api::Jenkins.set_ci_addr("http://some_ci_address:8080")
Api::Jenkins.ci_addr.should == "http://some_ci_address:8080/"
end

it "should get all job's names for specific ci" do
ci_addr = "http://deadlock.netbeans.org/hudson/"
Api::Jenkins.set_ci_addr(ci_addr)
mechanize = mock("Mechanize")
Mechanize.stub(:new).and_return(mechanize)
xml = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<hudson>
<assignedLabel/>
<mode>NORMAL</mode>
<nodeDescription>the master Hudson node</nodeDescription>
<job>
<name>analytics-server</name>
<url>
http://deadlock.netbeans.org/hudson/job/analytics-server/
</url>
<color>disabled</color>
</job>
<job>
<name>apitest</name>
<url>http://deadlock.netbeans.org/hudson/job/apitest/</url>
<color>blue</color>
</job>
</hudson>
EOF
result = mock("some xml ouput")
result.stub(:body).and_return(xml)
mechanize.should_receive(:get).with(ci_addr + "api/xml").and_yield(result)
Api::Jenkins.list_all_job_names.should == ["analytics-server", "apitest"]
end
end

0 comments on commit b3aa135

Please sign in to comment.