-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathjob_spec.rb
725 lines (676 loc) · 25.6 KB
/
job_spec.rb
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#
# Specifying JenkinsApi::Client::Job class capabilities
# Author: Kannan Manickam <[email protected]>
#
require File.expand_path('../spec_helper', __FILE__)
require 'yaml'
describe JenkinsApi::Client::Job do
context "With properly initialized client" do
before(:all) do
@helper = JenkinsApiSpecHelper::Helper.new
@creds_file = '~/.jenkins_api_client/spec.yml'
@creds = YAML.load_file(File.expand_path(@creds_file, __FILE__))
@job_name_prefix = 'awesome_rspec_test_job'
@filter = "^#{@job_name_prefix}.*"
@job_name = ''
@valid_post_responses = [200, 201, 302]
begin
@client = JenkinsApi::Client.new(@creds)
rescue Exception => e
puts "WARNING: Credentials are not set properly."
puts e.message
end
# Creating 10 jobs to run the spec tests on
begin
10.times do |num|
xml = @helper.create_job_xml
job = "#{@job_name_prefix}_#{num}"
@job_name = job if num == 0
@client.job.create(job, xml).to_i.should == 200
end
rescue Exception => e
puts "WARNING: Can't create jobs for preparing to spec tests"
end
end
## This block is an expection how job creation under folders could work
## This can change as per the implementation
# before(:all) do
# @folder_path = '/job/folder1'
# @client.job.create(@folder_path, @helper.create_folder_xml)
# # Creating 10 jobs to run the spec tests on
# begin
# 10.times do |num|
# xml = @helper.create_job_xml
# job = "#{@job_name_prefix}_#{num}"
# @job_name = job if num == 0
# @client.job.create(job, xml, @folder_path).to_i.should == 200
# end
# rescue => e
# puts "WARNING: Can't create jobs for preparing to spec tests"
# end
# end
describe "InstanceMethods" do
describe "#initialize" do
it "Initializes without any exception" do
expect(
lambda { job = JenkinsApi::Client::Job.new(@client) }
).not_to raise_error
end
it "Raises an error if a reference of client is not passed" do
expect(
lambda { job = JenkinsApi::Client::Job.new() }
).to raise_error
end
end
describe "#create" do
it "Should be able to create a job by getting an xml" do
xml = @helper.create_job_xml
name = "qwerty_nonexistent_job"
@valid_post_responses.should include(
@client.job.create(name, xml).to_i
)
@client.job.list(name).include?(name).should be_truthy
end
it "Should raise proper exception when the job already exists" do
xml = @helper.create_job_xml
name = "the_duplicate_job"
@valid_post_responses.should include(
@client.job.create(name, xml).to_i
)
@client.job.list(name).include?(name).should be_truthy
expect(
lambda { @client.job.create(name, xml) }
).to raise_error(JenkinsApi::Exceptions::JobAlreadyExists)
@valid_post_responses.should include(
@client.job.delete(name).to_i
)
end
end
describe "#create_freestyle" do
def test_and_validate(name, params, config_line = nil)
@valid_post_responses.should include(
@client.job.create_freestyle(params).to_i
)
@client.job.list(name).include?(name).should be_truthy
# Test for the existense of the given line in the config.xml of the
# job created
unless config_line.nil?
config = @client.job.get_config(name)
config.should =~ /#{config_line}/
end
@valid_post_responses.should include(
@client.job.delete(name).to_i
)
@client.job.list(name).include?(name).should be_falsey
end
it "Should create a freestyle job with just name" do
name = "test_job_name_using_params"
params = {
:name => name
}
test_and_validate(name, params)
end
it "Should create a freestyle job with shell command" do
name = "test_job_using_params_shell"
params = {
:name => name,
:shell_command => "echo this is a free style project"
}
test_and_validate(
name,
params,
"<command>echo this is a free style project</command>"
)
end
it "Should create a freestyle job with Git SCM provider" do
name = "test_job_with_git_scm"
params = {
:name => name,
:scm_provider => "git",
:scm_url => "git://github.com./arangamani/jenkins_api_client.git",
:scm_branch => "master"
}
test_and_validate(
name,
params,
"<url>git://github.com./arangamani/jenkins_api_client.git</url>"
)
end
it "Should create a freestyle job with SVN SCM provider" do
name = "test_job_with_subversion_scm"
params = {
:name => name,
:scm_provider => "subversion",
:scm_url => "http://svn.freebsd.org/base/",
:scm_branch => "master"
}
test_and_validate(
name,
params,
"<remote>http://svn.freebsd.org/base/</remote>"
)
end
it "Should create a freestyle job with CVS SCM provider with branch" do
name = "test_job_with_cvs_scm_branch"
params = {
:name => name,
:scm_provider => "cvs",
:scm_url => "http://cvs.NetBSD.org",
:scm_module => "src",
:scm_branch => "MAIN"
}
test_and_validate(
name,
params,
"<cvsroot>http://cvs.NetBSD.org</cvsroot>"
)
end
it "Should create a freestyle job with CVS SCM provider with tag" do
name = "test_job_with_cvs_scm_tag"
params = {
:name => name,
:scm_provider => "cvs",
:scm_url => "http://cvs.NetBSD.org",
:scm_module => "src",
:scm_tag => "MAIN"
}
test_and_validate(
name,
params,
"<cvsroot>http://cvs.NetBSD.org</cvsroot>"
)
end
it "Should raise an error if unsupported SCM is specified" do
name = "test_job_unsupported_scm"
params = {
:name => name,
:scm_provider => "non-existent",
:scm_url => "http://non-existent.com/non-existent.non",
:scm_branch => "master"
}
expect(
lambda{ @client.job.create_freestyle(params) }
).to raise_error
end
it "Should create a freestyle job with restricted_node option" do
name = "test_job_restricted_node"
params = {
:name => name,
:restricted_node => "master"
}
test_and_validate(name, params)
end
it "Should create a freestyle job with" +
" block_build_when_downstream_building option" do
name = "test_job_block_build_when_downstream_building"
params = {
:name => name,
:block_build_when_downstream_building => true,
}
test_and_validate(name, params)
end
it "Should create a freestyle job with" +
" block_build_when_upstream_building option" do
name = "test_job_block_build_when_upstream_building"
params = {
:name => name,
:block_build_when_upstream_building => true
}
test_and_validate(name, params)
end
it "Should create a freestyle job with concurrent_build option" do
name = "test_job_concurrent_build"
params = {
:name => name,
:concurrent_build => true
}
test_and_validate(name, params)
end
it "Should create a freestyle job with timer option" do
name = "test_job_using_timer"
params = {
:name => name,
:timer => "* * * * *"
}
test_and_validate(name, params)
end
it "Should create a freestyle job with child projects option" do
name = "test_job_child_projects"
params = {
:name => name,
:child_projects => @job_name,
:child_threshold => "success"
}
test_and_validate(name, params)
end
it "Should create a freestyle job with notification_email option" do
name = "test_job_notification_email"
params = {
:name => name,
:notification_email => "[email protected]"
}
test_and_validate(name, params)
end
it "Should create a freestyle job with notification for" +
" individual skype targets" do
name = "test_job_with_individual_skype_targets"
params = {
:name => name,
:skype_targets => "testuser"
}
test_and_validate(name, params)
end
it "Should create a freestyle job with notification for" +
" group skype targets" do
name = "test_job_with_group_skype_targets"
params = {
:name => name,
:skype_targets => "*testgroup"
}
test_and_validate(name, params)
end
it "Should create a freestyle job with complex skype" +
" configuration" do
name = "test_job_with_complex_skype_configuration"
params = {
:name => name,
:skype_targets => "testuser *testgroup anotheruser *anothergroup",
:skype_strategy => "failure_and_fixed",
:skype_notify_on_build_start => true,
:skype_notify_suspects => true,
:skype_notify_culprits => true,
:skype_notify_fixers => true,
:skype_notify_upstream_committers => false,
:skype_message => "summary_and_scm_changes"
}
test_and_validate(name, params)
end
it "Should raise an error if the input parameters is not a Hash" do
expect(
lambda {
@client.job.create_freestyle("a_string")
}
).to raise_error(ArgumentError)
end
it "Should raise an error if the required name paremeter is missing" do
expect(
lambda {
@client.job.create_freestyle(:shell_command => "sleep 60")
}
).to raise_error(ArgumentError)
end
end
describe "#copy" do
it "accepts the from and to job name and copies the job" do
xml = @helper.create_job_xml
@client.job.create("from_job_copy_test", xml)
@client.job.copy("from_job_copy_test", "to_job_copy_test")
@client.job.list(".*_job_copy_test").should == [
"from_job_copy_test", "to_job_copy_test"
]
@client.job.delete("from_job_copy_test")
@client.job.delete("to_job_copy_test")
end
it "accepts the from job name and copies the from job to the" +
" copy_of_from job" do
xml = @helper.create_job_xml
@client.job.create("from_job_copy_test", xml)
@client.job.copy("from_job_copy_test")
@client.job.list(".*_job_copy_test").should == [
"copy_of_from_job_copy_test", "from_job_copy_test"
]
@client.job.delete("from_job_copy_test")
@client.job.delete("copy_of_from_job_copy_test")
end
end
describe "#add_email_notification" do
it "Should accept email address and add to existing job" do
name = "email_notification_test_job"
params = {:name => name}
@valid_post_responses.should include(
@client.job.create_freestyle(params).to_i
)
@valid_post_responses.should include(
@client.job.add_email_notification(
:name => name,
:notification_email => "[email protected]"
).to_i
)
@valid_post_responses.should include(
@client.job.delete(name).to_i
)
end
end
describe "#add_skype_notification" do
it "Should accept skype configuration and add to existing job" do
name = "skype_notification_test_job"
params = {
:name => name
}
@valid_post_responses.should include(
@client.job.create_freestyle(params).to_i
)
@valid_post_responses.should include(
@client.job.add_skype_notification(
:name => name,
:skype_targets => "testuser"
).to_i
)
@valid_post_responses.should include(
@client.job.delete(name).to_i
)
end
end
describe "#rename" do
it "Should accept new and old job names and rename the job" do
xml = @helper.create_job_xml
@client.job.create("old_job_rename_test", xml)
@client.job.rename("old_job_rename_test", "new_job_rename_test")
@client.job.list("old_job_rename_test").should == []
resp = @client.job.list("new_job_rename_test")
resp.size.should == 1
resp.first.should == "new_job_rename_test"
@client.job.delete("new_job_rename_test")
end
end
describe "#recreate" do
it "Should be able to re-create a job" do
@valid_post_responses.should include(
@client.job.recreate("qwerty_nonexistent_job").to_i
)
end
end
describe "#change_description" do
it "Should be able to change the description of a job" do
@valid_post_responses.should include(
@client.job.change_description("qwerty_nonexistent_job",
"The description has been changed by the spec test").to_i
)
end
end
describe "#delete" do
it "Should be able to delete a job" do
@valid_post_responses.should include(
@client.job.delete("qwerty_nonexistent_job").to_i
)
end
end
describe "#wipe_out_workspace" do
it "Should be able to wipe out the workspace of a job" do
xml = @helper.create_job_xml
@client.job.create("wipeout_job_test", xml)
@valid_post_responses.should include(
@client.job.wipe_out_workspace("wipeout_job_test").to_i
)
@client.job.delete("wipeout_job_test")
end
end
describe "#list_all" do
it "Should list all jobs" do
@client.job.list_all.class.should == Array
end
# This testcase is pending as currently there is no way to create a job within the folder
it 'Should list all jobs within the folder' # do
# @client.job.list_all(@folder_path).class.should == Array
# end
end
describe "#list" do
it "Should return job names based on the filter" do
names = @client.job.list(@filter)
names.class.should == Array
names.each { |name|
name.should match /#{@filter}/i
}
end
# This testcase is pending as currently there is no way to create a job within the folder
it 'Should return job names from a given folder based on the filter' # do
# names = @client.job.list(@filter, true, @folder_path)
# names.class.should == Array
# names.each do |name|
# name.should match /#{@filter}/i
# end
# end
end
describe "#list_by_status" do
it "Should be able to list jobs by status" do
names = @client.job.list_by_status('success')
names.class.should == Array
names.each do |name|
status = @client.job.get_current_build_status(name)
status.should == 'success'
end
end
# This testcase is pending as currently there is no way to create a job within the folder
it 'Should be able to jobs list from a folder by status' # do
# names = @client.job.list_by_status('success', [], @folder_path)
# names.class.should == Array
# names.each do |name|
# # The folder_path should also be implemented for Job#get_current_build_status method
# status = @client.job.get_current_build_status(name, @folder_path)
# status.should == 'success'
# end
# end
end
describe "#list_all_with_details" do
it "Should return all job names with details" do
@client.job.list_all_with_details.class.should == Array
end
# This testcase is pending as currently there is no way to create a job within the folder
it 'Should return all job names with details' # do
# @client.job.list_all_with_details(@folder_path).class.should == Array
# end
end
describe "#list_details" do
it "Should list details of a particular job" do
job_name = @client.job.list(@filter)[0]
job_name.class.should == String
@client.job.list_details(job_name).class.should == Hash
end
# This testcase is pending as currently there is no way to create a job within the folder
it 'Should list details of a particular job from a given folder' # do
# job_name = @client.job.list(@filter, true, @folder_path)[0]
# job_name.class.should == String
# @client.job.list_details(job_name, @folder_path).class.should == Hash
# end
end
describe "#get_upstream_projects" do
it "Should list upstream projects of the specified job" do
@client.job.get_upstream_projects(@job_name).class.should == Array
end
end
describe "#get_downstream_projects" do
it "Should list downstream projects of the specified job" do
@client.job.get_downstream_projects(@job_name).class.should == Array
end
end
describe "#get_builds" do
it "Should get builds of a specified job" do
@client.job.get_builds(@job_name).class.should == Array
end
end
describe "#get_current_build_status" do
it "Should obtain the current build status for the specified job" do
build_status = @client.job.get_current_build_status(@job_name)
build_status.class.should == String
valid_build_status = [
"not_run",
"aborted",
"success",
"failure",
"unstable",
"running"
]
valid_build_status.include?(build_status).should be_truthy
end
end
describe "#build" do
def wait_for_job_to_finish(job_name)
while @client.job.get_current_build_status(@job_name) == "running" do
# Waiting for this job to finish so it doesn't affect other tests
sleep 10
end
end
it "Should build the specified job" do
@client.job.get_current_build_status(
@job_name
).should_not == "running"
response = @client.job.build(@job_name)
# As of Jenkins version 1.519 the job build responds with a 201
# status code.
@valid_post_responses.should include(response.to_i)
# Sleep for 10 seconds so we don't hit the Jenkins quiet period (5
# seconds)
sleep 10
@client.job.get_current_build_status(@job_name).should == "running"
wait_for_job_to_finish(@job_name)
end
it "Should build the specified job (wait for start)" do
@client.job.get_current_build_status(
@job_name
).should_not == "running"
expected_build_id = (@client.job.get_current_build_number(@job_name) || 0) + 1
build_opts = {
'build_start_timeout' => 10,
'progress_proc' => lambda do |max_wait, curr_wait, poll_count|
puts "Waited #{curr_wait}s of #{max_wait}s max - poll count = #{poll_count}"
end,
'completion_proc' => lambda do |build_number, cancelled|
if build_number
puts "Wait over: build #{build_number} started"
else
puts "Wait over: build not started, build #{cancelled ? "" : "NOT "} cancelled"
end
end
}
build_id = @client.job.build(@job_name, {}, build_opts)
build_id.should_not be_nil
build_id.should eql(expected_build_id)
@client.job.get_current_build_status(@job_name).should == "running"
wait_for_job_to_finish(@job_name)
end
# This build doesn't start in time, but we don't cancel it, so it will run if
# Jenkins gets to it
it "Should build the specified job (wait for start - but not long enough)" do
@client.job.get_current_build_status(
@job_name
).should_not == "running"
build_opts = {
'build_start_timeout' => 1,
'progress_proc' => lambda do |max_wait, curr_wait, poll_count|
puts "Waited #{curr_wait}s of #{max_wait}s max - poll count = #{poll_count}"
end,
'completion_proc' => lambda do |build_number, cancelled|
if build_number
puts "Wait over: build #{build_number} started"
else
puts "Wait over: build not started, build #{cancelled ? "" : "NOT "}cancelled"
end
end
}
expect( lambda { @client.job.build(@job_name, {}, build_opts) } ).to raise_error(Timeout::Error)
# Sleep for 10 seconds so we don't hit the Jenkins quiet period (5
# seconds)
sleep 10
@client.job.get_current_build_status(@job_name).should == "running"
wait_for_job_to_finish(@job_name)
end
# This build doesn't start in time, and we will attempt to cancel it so it
# doesn't run
it "Should build the specified job (wait for start - but not long enough, cancelled)" do
@client.job.get_current_build_status(
@job_name
).should_not == "running"
build_opts = {
'build_start_timeout' => 1,
'cancel_on_build_start_timeout' => true,
'progress_proc' => lambda do |max_wait, curr_wait, poll_count|
puts "Waited #{curr_wait}s of #{max_wait}s max - poll count = #{poll_count}"
end,
'completion_proc' => lambda do |build_number, cancelled|
if build_number
puts "Wait over: build #{build_number} started"
else
puts "Wait over: build not started, build #{cancelled ? "" : "NOT "}cancelled"
end
end
}
expect( lambda { @client.job.build(@job_name, {}, build_opts) } ).to raise_error(Timeout::Error)
end
end
describe "#poll" do
it "Should poll the specified job for scm changes" do
response = @client.job.poll(@job_name)
@valid_post_responses.should include(response.to_i)
end
end
describe "#disable" do
it "Should disable the specified job and then enable it again" do
@client.job.list_details(@job_name)['buildable'].should == true
response = @client.job.disable(@job_name)
response.to_i.should == 302
sleep 3
@client.job.list_details(@job_name)['buildable'].should == false
response = @client.job.enable(@job_name)
response.to_i.should == 302
sleep 3
@client.job.list_details(@job_name)['buildable'].should == true
end
end
describe "#stop" do
it "Should be able to abort a recent build of a running job" do
@client.job.get_current_build_status(
@job_name
).should_not == "running"
@client.job.build(@job_name)
sleep 10
@client.job.get_current_build_status(@job_name).should == "running"
sleep 5
@valid_post_responses.should include(
@client.job.stop_build(@job_name).to_i
)
sleep 5
@client.job.get_current_build_status(@job_name).should == "aborted"
end
end
describe "#restrict_to_node" do
it "Should be able to restrict a job to a node" do
@valid_post_responses.should include(
@client.job.restrict_to_node(@job_name, 'master').to_i
)
# Run it again to make sure that the replace existing node works
@valid_post_responses.should include(
@client.job.restrict_to_node(@job_name, 'master').to_i
)
end
end
describe "#chain" do
it "Should be able to chain all jobs" do
# Filter jobs to be chained
jobs = @client.job.list(@filter)
jobs.class.should == Array
start_jobs = @client.job.chain(jobs, 'success', ["all"])
start_jobs.class.should == Array
start_jobs.length.should == 1
end
it "Should be able to chain jobs based on the specified criteria" do
jobs = @client.job.list(@filter)
jobs.class.should == Array
start_jobs = @client.job.chain(
jobs,
'failure',
["not_run", "aborted", 'failure'],
3
)
start_jobs.class.should == Array
start_jobs.length.should == 3
end
end
end
after(:all) do
job_names = @client.job.list(@filter)
job_names.each { |job_name|
@client.job.delete(job_name)
}
end
end
end