jenkins_api_client 0.12.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,7 @@ describe JenkinsApi::Client::Node do
10
10
  context "With properly initialized client" do
11
11
  before(:all) do
12
12
  @creds_file = '~/.jenkins_api_client/spec.yml'
13
+ @valid_post_responses = [200, 201, 302]
13
14
  @node_name = 'master'
14
15
  begin
15
16
  @client = JenkinsApi::Client.new(
@@ -46,9 +47,13 @@ describe JenkinsApi::Client::Node do
46
47
 
47
48
  def test_and_validate(params)
48
49
  name = params[:name]
49
- @client.node.create_dump_slave(params).to_i.should == 302
50
+ @valid_post_responses.should include(
51
+ @client.node.create_dump_slave(params).to_i
52
+ )
50
53
  @client.node.list(name).include?(name).should be_true
51
- @client.node.delete(params[:name]).to_i.should == 302
54
+ @valid_post_responses.should include(
55
+ @client.node.delete(params[:name]).to_i
56
+ )
52
57
  @client.node.list(name).include?(name).should be_false
53
58
  end
54
59
 
@@ -75,7 +80,7 @@ describe JenkinsApi::Client::Node do
75
80
  }
76
81
  expect(
77
82
  lambda{ @client.node.create_dump_slave(params) }
78
- ).to raise_error
83
+ ).to raise_error(ArgumentError)
79
84
  end
80
85
  it "fails if slave_host is missing" do
81
86
  params = {
@@ -84,7 +89,7 @@ describe JenkinsApi::Client::Node do
84
89
  }
85
90
  expect(
86
91
  lambda{ @client.node.create_dump_slave(params) }
87
- ).to raise_error
92
+ ).to raise_error(ArgumentError)
88
93
  end
89
94
  it "fails if private_key_file is missing" do
90
95
  params = {
@@ -93,7 +98,7 @@ describe JenkinsApi::Client::Node do
93
98
  }
94
99
  expect(
95
100
  lambda{ @client.node.create_dump_slave(params) }
96
- ).to raise_error
101
+ ).to raise_error(ArgumentError)
97
102
  end
98
103
  it "fails if the slave already exists in Jenkins" do
99
104
  params = {
@@ -101,11 +106,15 @@ describe JenkinsApi::Client::Node do
101
106
  :slave_host => "10.10.10.10",
102
107
  :private_key_file => "/root/.ssh/id_rsa"
103
108
  }
104
- @client.node.create_dump_slave(params).to_i.should == 302
109
+ @valid_post_responses.should include(
110
+ @client.node.create_dump_slave(params).to_i
111
+ )
105
112
  expect(
106
113
  lambda{ @client.node.create_dump_slave(params) }
107
- ).to raise_error
108
- @client.node.delete(params[:name]).to_i.should == 302
114
+ ).to raise_error(JenkinsApi::Exceptions::NodeAlreadyExists)
115
+ @valid_post_responses.should include(
116
+ @client.node.delete(params[:name]).to_i
117
+ )
109
118
  end
110
119
  end
111
120
 
@@ -116,8 +125,12 @@ describe JenkinsApi::Client::Node do
116
125
  :slave_host => "10.10.10.10",
117
126
  :private_key_file => "/root/.ssh/id_rsa"
118
127
  }
119
- @client.node.create_dump_slave(params).to_i.should == 302
120
- @client.node.delete(params[:name]).to_i.should == 302
128
+ @valid_post_responses.should include(
129
+ @client.node.create_dump_slave(params).to_i
130
+ )
131
+ @valid_post_responses.should include(
132
+ @client.node.delete(params[:name]).to_i
133
+ )
121
134
  end
122
135
  it "raises an error if the slave doesn't exist in Jenkins" do
123
136
  expect(
@@ -167,8 +180,12 @@ describe JenkinsApi::Client::Node do
167
180
 
168
181
  describe "#change_mode" do
169
182
  it "changes the mode of the given slave to the given mode" do
170
- @client.node.change_mode("slave", "exclusive").to_i.should == 200
171
- @client.node.change_mode("slave", "normal").to_i.should == 200
183
+ @valid_post_responses.should include(
184
+ @client.node.change_mode("slave", "exclusive").to_i
185
+ )
186
+ @valid_post_responses.should include(
187
+ @client.node.change_mode("slave", "normal").to_i
188
+ )
172
189
  end
173
190
  end
174
191
 
@@ -10,13 +10,8 @@ require 'pp'
10
10
  require 'yaml'
11
11
  require 'nokogiri'
12
12
 
13
- #RSpec.configure do |config|
14
- # config.mock_with :flexmock
15
- #end
16
-
17
13
  module JenkinsApiSpecHelper
18
14
  class Helper
19
-
20
15
  def create_job_xml
21
16
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') { |xml|
22
17
  xml.project {
@@ -33,7 +28,7 @@ module JenkinsApiSpecHelper
33
28
  xml.concurrentBuild "false"
34
29
  xml.builders {
35
30
  xml.send("hudson.tasks.Shell") {
36
- xml.command "\necho 'going to take a nice nap'\nsleep 10\necho 'took a nice nap'"
31
+ xml.command "\necho 'going to take a nice nap'\nsleep 15\necho 'took a nice nap'"
37
32
  }
38
33
  }
39
34
  xml.publishers
@@ -42,6 +37,5 @@ module JenkinsApiSpecHelper
42
37
  }
43
38
  builder.to_xml
44
39
  end
45
-
46
40
  end
47
41
  end
@@ -10,8 +10,11 @@ describe JenkinsApi::Client::System do
10
10
  context "With properly initialized client" do
11
11
  before(:all) do
12
12
  @creds_file = '~/.jenkins_api_client/spec.yml'
13
+ @valid_post_responses = [200, 201, 302]
13
14
  begin
14
- @client = JenkinsApi::Client.new(YAML.load_file(File.expand_path(@creds_file, __FILE__)))
15
+ @client = JenkinsApi::Client.new(
16
+ YAML.load_file(File.expand_path(@creds_file, __FILE__))
17
+ )
15
18
  rescue Exception => e
16
19
  puts "WARNING: Credentials are not set properly."
17
20
  puts e.message
@@ -22,19 +25,25 @@ describe JenkinsApi::Client::System do
22
25
 
23
26
  describe "#quiet_down" do
24
27
  it "Should be able to quiet down a Jenkins server" do
25
- @client.system.quiet_down.to_i.should == 302
28
+ @valid_post_responses.should include(
29
+ @client.system.quiet_down.to_i
30
+ )
26
31
  end
27
32
  end
28
33
 
29
34
  describe "#cancel_quiet_down" do
30
35
  it "Should be able to cancel the quiet down a Jenkins server" do
31
- @client.system.cancel_quiet_down.to_i.should == 302
36
+ @valid_post_responses.should include(
37
+ @client.system.cancel_quiet_down.to_i
38
+ )
32
39
  end
33
40
  end
34
41
 
35
42
  describe "#restart" do
36
43
  it "Should be able to restart a Jenkins server safely" do
37
- @client.system.restart.to_i.should == 302
44
+ @valid_post_responses.should include(
45
+ @client.system.restart.to_i
46
+ )
38
47
  end
39
48
 
40
49
  it "Should be able to wait after a safe restart" do
@@ -42,7 +51,9 @@ describe JenkinsApi::Client::System do
42
51
  end
43
52
 
44
53
  it "Should be able to force restart a Jenkins server" do
45
- @client.system.restart(true).to_i.should == 302
54
+ @valid_post_responses.should include(
55
+ @client.system.restart(true).to_i
56
+ )
46
57
  end
47
58
 
48
59
  it "Should be able to wait after a force restart" do
@@ -52,13 +63,21 @@ describe JenkinsApi::Client::System do
52
63
 
53
64
  describe "#reload" do
54
65
  it "Should be able to reload a Jenkins server" do
55
- @client.system.reload.to_i.should == 302
66
+ @valid_post_responses.should include(
67
+ @client.system.reload.to_i
68
+ )
56
69
  end
57
70
  it "Should be able to wait after a force restart" do
58
71
  @client.system.wait_for_ready.should == true
59
72
  end
60
73
  end
61
74
 
75
+ describe "#list_users" do
76
+ it "Should be able to get a list of users" do
77
+ @client.system.list_users.size == 1
78
+ end
79
+ end
80
+
62
81
  end
63
82
 
64
83
  end
@@ -10,6 +10,7 @@ describe JenkinsApi::Client::View do
10
10
  context "With properly initialized client" do
11
11
  before(:all) do
12
12
  @creds_file = '~/.jenkins_api_client/spec.yml'
13
+ @valid_post_responses = [200, 201, 302]
13
14
  @node_name = 'master'
14
15
  begin
15
16
  @client = JenkinsApi::Client.new(
@@ -21,7 +22,9 @@ describe JenkinsApi::Client::View do
21
22
  end
22
23
 
23
24
  # Create a view that can be used for tests
24
- @client.view.create("general_purpose_view").to_i.should == 302
25
+ @valid_post_responses.should include(
26
+ @client.view.create("general_purpose_view").to_i
27
+ )
25
28
  end
26
29
 
27
30
  describe "InstanceMethods" do
@@ -35,42 +38,75 @@ describe JenkinsApi::Client::View do
35
38
  describe "#create" do
36
39
  it "accepts the name of the view and creates the view" do
37
40
  name = "test_view"
38
- @client.view.create(name).to_i.should == 302
41
+ @valid_post_responses.should include(
42
+ @client.view.create(name).to_i
43
+ )
39
44
  @client.view.list(name).include?(name).should be_true
40
- @client.view.delete(name).to_i.should == 302
45
+ @valid_post_responses.should include(
46
+ @client.view.delete(name).to_i
47
+ )
41
48
  end
42
49
  it "accepts spaces and other characters in the view name" do
43
50
  name = "test view with spaces and {special characters}"
44
- @client.view.create(name).to_i.should == 302
51
+ @valid_post_responses.should include(
52
+ @client.view.create(name).to_i
53
+ )
45
54
  @client.view.list(name).include?(name).should be_true
46
- @client.view.delete(name).to_i.should == 302
55
+ @valid_post_responses.should include(
56
+ @client.view.delete(name).to_i
57
+ )
47
58
  end
48
59
  it "accepts the name of view and creates a listview" do
49
60
  name = "test_view"
50
- @client.view.create(name, "listview").to_i.should == 302
61
+ @valid_post_responses.should include(
62
+ @client.view.create(name, "listview").to_i
63
+ )
51
64
  @client.view.list(name).include?(name).should be_true
52
- @client.view.delete(name).to_i.should == 302
65
+ @valid_post_responses.should include(
66
+ @client.view.delete(name).to_i
67
+ )
53
68
  end
54
69
  it "accepts the name of view and creates a myview" do
55
70
  name = "test_view"
56
- @client.view.create(name, "myview").to_i.should == 302
71
+ @valid_post_responses.should include(
72
+ @client.view.create(name, "myview").to_i
73
+ )
57
74
  @client.view.list(name).include?(name).should be_true
58
- @client.view.delete(name).to_i.should == 302
75
+ @valid_post_responses.should include(
76
+ @client.view.delete(name).to_i
77
+ )
59
78
  end
60
79
  it "raises an error when unsupported view type is specified" do
61
80
  expect(
62
81
  lambda { @client.view.create(name, "awesomeview") }
63
82
  ).to raise_error
64
83
  end
84
+ it "raises proper error if the view already exists" do
85
+ name = "duplicate_view"
86
+ @valid_post_responses.should include(
87
+ @client.view.create(name, "listview").to_i
88
+ )
89
+ @client.view.list(name).include?(name).should be_true
90
+ expect(
91
+ lambda { @client.view.create(name, "listview") }
92
+ ).to raise_error(JenkinsApi::Exceptions::ViewAlreadyExists)
93
+ @valid_post_responses.should include(
94
+ @client.view.delete(name).to_i
95
+ )
96
+ end
65
97
  end
66
98
 
67
99
  describe "#create_list_view" do
68
100
 
69
101
  def test_and_validate(params)
70
102
  name = params[:name]
71
- @client.view.create_list_view(params).to_i.should == 302
103
+ @valid_post_responses.should include(
104
+ @client.view.create_list_view(params).to_i
105
+ )
72
106
  @client.view.list(name).include?(name).should be_true
73
- @client.view.delete(name).to_i.should == 302
107
+ @valid_post_responses.should include(
108
+ @client.view.delete(name).to_i
109
+ )
74
110
  @client.view.list(name).include?(name).should be_false
75
111
  end
76
112
 
@@ -112,16 +148,34 @@ describe JenkinsApi::Client::View do
112
148
  }
113
149
  test_and_validate(params)
114
150
  end
151
+ it "raises an error when the input parameters is not a Hash" do
152
+ expect(
153
+ lambda {
154
+ @client.view.create_list_view("a_string")
155
+ }
156
+ ).to raise_error(ArgumentError)
157
+ end
158
+ it "raises an error when the required name paremeter is missing" do
159
+ expect(
160
+ lambda {
161
+ @client.view.create_list_view(:description => "awesomeview")
162
+ }
163
+ ).to raise_error(ArgumentError)
164
+ end
115
165
  end
116
166
 
117
167
  describe "#delete" do
118
168
  name = "test_view_to_delete"
119
169
  before(:all) do
120
- @client.view.create(name).to_i.should == 302
170
+ @valid_post_responses.should include(
171
+ @client.view.create(name).to_i
172
+ )
121
173
  end
122
174
  it "accepts the name of the view and deletes from Jenkins" do
123
175
  @client.view.list(name).include?(name).should be_true
124
- @client.view.delete(name).to_i.should == 302
176
+ @valid_post_responses.should include(
177
+ @client.view.delete(name).to_i
178
+ )
125
179
  @client.view.list(name).include?(name).should be_false
126
180
  end
127
181
  end
@@ -140,15 +194,19 @@ describe JenkinsApi::Client::View do
140
194
 
141
195
  describe "#add_job" do
142
196
  before(:all) do
143
- @client.job.create_freestyle(
144
- :name => "test_job_for_view"
145
- ).to_i.should == 200
197
+ @valid_post_responses.should include(
198
+ @client.job.create_freestyle(
199
+ :name => "test_job_for_view"
200
+ ).to_i
201
+ )
146
202
  end
147
203
  it "accepts the job and and adds it to the specified view" do
148
- @client.view.add_job(
149
- "general_purpose_view",
150
- "test_job_for_view"
151
- ).to_i.should == 200
204
+ @valid_post_responses.should include(
205
+ @client.view.add_job(
206
+ "general_purpose_view",
207
+ "test_job_for_view"
208
+ ).to_i
209
+ )
152
210
  @client.view.list_jobs(
153
211
  "general_purpose_view"
154
212
  ).include?("test_job_for_view").should be_true
@@ -158,23 +216,29 @@ describe JenkinsApi::Client::View do
158
216
  describe "#remove_job" do
159
217
  before(:all) do
160
218
  unless @client.job.exists?("test_job_for_view")
161
- @client.job.create_freestyle(
162
- :name => "test_job_for_view"
163
- ).to_i.should == 200
219
+ @valid_post_responses.should include(
220
+ @client.job.create_freestyle(
221
+ :name => "test_job_for_view"
222
+ ).to_i
223
+ )
164
224
  end
165
225
  unless @client.view.list_jobs(
166
226
  "general_purpose_view").include?("test_job_for_view")
167
- @client.view.add_job(
168
- "general_purpose_job",
169
- "test_job_for_view"
170
- ).to_i.should == 200
227
+ @valid_post_responses.should include(
228
+ @client.view.add_job(
229
+ "general_purpose_job",
230
+ "test_job_for_view"
231
+ ).to_i
232
+ )
171
233
  end
172
234
  end
173
235
  it "accepts the job name and removes it from the specified view" do
174
- @client.view.remove_job(
175
- "general_purpose_view",
176
- "test_job_for_view"
177
- ).to_i.should == 200
236
+ @valid_post_responses.should include(
237
+ @client.view.remove_job(
238
+ "general_purpose_view",
239
+ "test_job_for_view"
240
+ ).to_i
241
+ )
178
242
  end
179
243
  end
180
244
 
@@ -199,11 +263,14 @@ describe JenkinsApi::Client::View do
199
263
  end
200
264
 
201
265
  after(:all) do
202
- @client.view.delete("general_purpose_view").to_i.should == 302
266
+ @valid_post_responses.should include(
267
+ @client.view.delete("general_purpose_view").to_i
268
+ )
203
269
  if @client.job.exists?("test_job_for_view")
204
- @client.job.delete("test_job_for_view").to_i.should == 302
270
+ @valid_post_responses.should include(
271
+ @client.job.delete("test_job_for_view").to_i
272
+ )
205
273
  end
206
274
  end
207
-
208
275
  end
209
276
  end
@@ -4,6 +4,8 @@ describe JenkinsApi::Client::BuildQueue do
4
4
  context "With properly initialized Client" do
5
5
  before do
6
6
  @client = mock
7
+ mock_logger = Logger.new "/dev/null"
8
+ @client.should_receive(:logger).and_return(mock_logger)
7
9
  @queue = JenkinsApi::Client::BuildQueue.new(@client)
8
10
  @sample_queue_json = {
9
11
  "items" => [
@@ -38,6 +40,8 @@ describe JenkinsApi::Client::BuildQueue do
38
40
  describe "InstanceMethods" do
39
41
  describe "#initialize" do
40
42
  it "initializes by receiving an instance of client object" do
43
+ mock_logger = Logger.new "/dev/null"
44
+ @client.should_receive(:logger).and_return(mock_logger)
41
45
  expect(
42
46
  lambda{ JenkinsApi::Client::BuildQueue.new(@client) }
43
47
  ).not_to raise_error
@@ -7,7 +7,8 @@ describe JenkinsApi::Client do
7
7
  :server_ip => '127.0.0.1',
8
8
  :server_port => 8080,
9
9
  :username => 'username',
10
- :password => 'password'
10
+ :password => 'password',
11
+ :log_location => '/dev/null'
11
12
  )
12
13
  end
13
14
 
@@ -141,41 +142,6 @@ describe JenkinsApi::Client do
141
142
  end
142
143
 
143
144
  describe "InstanceMethods" do
144
- describe "#debug" do
145
- it "The default for debug should be false" do
146
- client = JenkinsApi::Client.new(
147
- :server_ip => '127.0.0.1',
148
- :server_port => 8080,
149
- :username => 'username',
150
- :password => 'password'
151
- )
152
- client.debug.should == false
153
- end
154
-
155
- it "Should be able to set the debug value" do
156
- client = JenkinsApi::Client.new(
157
- :server_ip => '127.0.0.1',
158
- :server_port => 8080,
159
- :username => 'username',
160
- :password => 'password',
161
- :debug => true
162
- )
163
- client.debug.should == true
164
- end
165
-
166
- it "Should be able to toggle the debug value" do
167
- client = JenkinsApi::Client.new(
168
- :server_ip => '127.0.0.1',
169
- :server_port => 8080,
170
- :username => 'username',
171
- :password => 'password',
172
- :debug => true
173
- )
174
- client.toggle_debug
175
- client.debug.should == false
176
- end
177
- end
178
-
179
145
  describe "#getroot" do
180
146
  it "is defined with no parameters" do
181
147
  expect(