File: format_checking_spec.rb

package info (click to toggle)
oar 2.5.4-2
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 11,064 kB
  • sloc: perl: 28,891; ruby: 5,813; sh: 5,208; ml: 3,408; sql: 3,255; cpp: 2,277; ansic: 702; makefile: 389; php: 99; exp: 23
file content (730 lines) | stat: -rw-r--r-- 21,990 bytes parent folder | download | duplicates (5)
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
726
727
728
729
730
# Rspec tests for OAR REST API format checking
# Please, run this test on a kameleon appliance
# If this is the first time, run it twice and do not care about the first time.

$LOAD_PATH << '.'

USER=ENV['USER']
require 'oarrestapi_lib'
require 'shared_examples'

$jobid=""
$rjobid=""

# Oar API specs
describe OarApi do
  before :all do
    @api = OarApi.new(APIURI)
  end

  #############################################################
  # Tests environment initialisation
  #############################################################
 
  describe "INITIAL QUEUE" do
 
    it "should have an 'items' array " do
      @api.get_hash('jobs')
      @api.value['items'].should_not == nil
    end

    it "should submit some jobs to populate the database" do
      jhash = { 'resource' => "/nodes=1/core=1" , 'script' => "id" , 'array' => '15' }
      @api.submit_job(jhash)
      $jobid = @api.jobstatus['id']
      $jobid.should be_a(Integer)
      $jobid.should > 0
      # Now, we wait for the 15th job to be terminated
      # It is necessary for the rest of the tests to pass
      timeout=180
      t=0
      c=0
      while t<timeout and c == 0
        @api.specific_job_details($jobid+14)
        if @api.specificjobdetails["state"] == "Terminated"
          c=1
        else
          printf "."
          $stdout.flush
        end
        sleep 1
        t+=1
      end
      t.should < timeout
    end

    it "should have an empty queue before running this tests " do
      @api.value['items'].should be_empty
    end

    it "should have an array of 10 test jobs just submitted" do
      jhash = { 'resource' => "/nodes=1/core=2" , 
                'property' => "network_address like 'node%'", 
                'script' => "ls;pwd;whoami;sleep 60" , 
                'array' => '10' }
      @api.submit_job(jhash)
      $jobid = @api.jobstatus['id']
      $jobid.should be_a(Integer)
      $jobid.should > 0
      # Now, we wait for the 4th job to be running
      # It is necessary for the rest of the tests to pass
      timeout=60
      t=0
      c=0
      while t<timeout and c == 0
        @api.specific_job_details($jobid+3)
        if @api.specificjobdetails["state"] == "Running"
          c=1
        else
          printf "."
          $stdout.flush
        end
        sleep 1
        t+=1
      end
      t.should < timeout
    end

    it "should have a reservation job just submitted" do
      jhash = { 'resource' => "/nodes=1/core=1" , 'script' => "ls;pwd;whoami;sleep 60" , 'reservation' => '2037-01-01 01:00:00' }
      @api.submit_job(jhash)
      $rjobid = @api.jobstatus['id']
      $rjobid.should be_a(Integer)
      $rjobid.should > 0
    end

  end

  #############################################################
  # URIs basic structure format checking
  #############################################################

  uris=[
         "jobs","jobs/details","jobs/table",
         "jobs?state=Running,Waiting,Launching","jobs?state=Terminated&limit=10",
         "jobs?state=Running,Terminated&limit=10","jobs?from=0&to=2147483647&limit=10",
         "resources","resources/full"
       ]
  uris.each do |uri|
    describe "#{uri} basic data structure" do
      before(:all) do
        @api = OarApi.new(APIURI)
        @api.get_hash(uri)
      end
      it_should_behave_like "All list structures"
    end
  end

  #############################################################
  # Specific URI tests
  #############################################################

  describe "JOBS CHECKINGS: /jobs data structure" do
    before(:all) do
      @api = OarApi.new(APIURI)
    end
    
    context "(using state=Running,Launching,Waiting&limit=2)" do
      before(:all) do
        $uri="jobs?state=Running,Launching,Waiting&limit=2"
        @api.get_hash($uri)
      end
      it "should correctly limit the number of results" do
        @api.value['items'].length.should == 2
      end
      it "should return 2 links" do
        @api.value['links'].length.should == 2
      end
      it "should return a total of 11" do
        @api.value['total'].to_i.should == 11
      end
      it "should return an offset of 0" do
        @api.value['offset'].to_i.should == 0
      end
      it "should return a correct self link" do
        @api.get_self_link_href.should == "#{APIPATH}jobs?state=Running%2CLaunching%2CWaiting&limit=2&offset=0"
      end
      it "should return a correct next link" do
        @api.get_next_link_href.should == "#{APIPATH}jobs?state=Running%2CLaunching%2CWaiting&limit=2&offset=2"
      end
    end

    context "(using /jobs?state=Running&limit=2&offset=3)" do
      before(:all) do
        $uri="jobs?state=Running,Waiting,Launching&limit=2&offset=3"
        @api.get_hash($uri)
      end
      it "should correctly limit the number of results" do
        @api.value['items'].length.should == 2
      end
      it "should return 3 links" do
        @api.value['links'].length.should == 3
      end
      it "should return a total of 11" do
        @api.value['total'].to_i.should == 11
      end
      it "should return an offset of 3" do
        @api.value['offset'].to_i.should == 3
      end
      it "should return a correct self link" do
        @api.get_self_link_href.should == "#{APIPATH}jobs?state=Running%2CWaiting%2CLaunching&limit=2&offset=3"
      end
      it "should return a correct previous link" do
        @api.get_previous_link_href.should == "#{APIPATH}jobs?state=Running%2CWaiting%2CLaunching&limit=2&offset=1"
      end
      it "should return a correct next link" do
        @api.get_next_link_href.should == "#{APIPATH}jobs?state=Running%2CWaiting%2CLaunching&limit=2&offset=5"
      end
    end

    context "(using same as above plus Terminated jobs)" do
      before(:all) do
        $uri="jobs?state=Running,Waiting,Launching,Terminated&limit=15"
        @api.get_hash($uri)
      end
      it "should return a total >= 20" do
        @api.value['total'].to_i.should >= 20
      end
    end

    context "(with state=Terminated)" do
      before(:all) do
        @api.get_hash("jobs?state=Terminated")
      end
      it "should return only terminated jobs" do
        @api.value['items'].each do |item|
          item['state'].should == "Terminated"
        end
      end
    end

    context "(with state=Running [note: add a sleep if test jobs are not yet running])" do
      before(:all) do
        @api.get_hash("jobs?state=Running&user=#{USER}")
      end
      it "should return a few running jobs" do
        @api.value['items'].length.should > 0
      end
      it "should return only running jobs" do
        @api.value['items'].each do |item|
          item['state'].should == "Running"
        end
      end
      it "should return jobs having a correct self link" do
        links=@api.value['items'][0]['links']
        id=@api.value['items'][0]['id']
        @api.get_link_href_from_array_by_rel(links,"self").should == "#{APIPATH}jobs/#{id}"
      end
      it "should return jobs having a correct resources link" do
        links=@api.value['items'][0]['links']
        id=@api.value['items'][0]['id']
        @api.get_link_href_from_array(links,"resources").should == "#{APIPATH}jobs/#{id}/resources"
      end
      it "should return jobs owned by the #{USER} user" do
        @api.value['items'][0]['owner'].should == "#{USER}"
      end
    end

    context "(with non-existent obiwankenobi user)" do
      before(:all) do
        @api.get_hash("jobs?state=Terminated&user=obiwankenobi")
      end
      it "should return no jobs" do
        @api.value['items'].length.should == 0
      end
    end
 
    context "(inside job)" do
      before(:all) do
        @api.get_hash("jobs?state=Running")
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "Job"
    end

    context "(with limit=1)" do
      before(:all) do
        @api.get_hash("jobs?limit=1")
      end
      it_should_behave_like "All list structures"
    end

    context "(with limit=1, insider job)" do
      before(:all) do
        @api.get_hash("jobs?limit=1")
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "Job"
    end
 
  end

  describe "JOB INFOS CHECKING: /jobs/<id> data structure" do
    context "(with normal job)" do
      before(:all) do
        @api = OarApi.new(APIURI)
        @api.get_hash("jobs/#{$jobid}")
      end
      it_should_behave_like "Job"
      it "should be owned by the #{USER} user" do
        @api.value['owner'].should == "#{USER}"
      end
    end
    context "(with non-existent job)" do
      before(:all) do
        @api = OarApi.new(APIURI)
      end

      it "should raise an exception" do
        lambda {
            @api.get_hash("jobs/00")
        }.should raise_exception
      end

      it "should return a 404 error" do
        begin
          @api.get_hash("jobs/00")
        rescue => e
          e.should respond_to('http_code')
          e.http_code.should == 404
        end 
      end
    end
    context "(with finished job)" do
      before(:all) do
        @api = OarApi.new(APIURI)
        @api.get_hash("jobs/1")
      end
      it_should_behave_like "Job"
    end

  end


  describe "JOB DETAILS CHECKING: /jobs/<id>/details data structure" do
    context "(with normal job)" do
      before(:all) do
        @api = OarApi.new(APIURI)
        @api.get_hash("jobs/#{$jobid}/details")
        @value=@api.value
      end
      it_should_behave_like "Job"
      it "should be owned by the #{USER} user" do
        @api.value['owner'].should == "#{USER}"
      end
      it "should have resources and nodes details" do
        @api.value['resources'].should be_an(Array)
        @api.value['nodes'].should be_an(Array)
      end
      context "should have resources behaving correctly" do
        before(:all) do
          @api.value=@value['resources'][0]
        end
        it_should_behave_like "ResourceId"
      end
      context "should have nodes behaving correctly" do
        before(:all) do
          @api.value=@value['nodes'][0]
        end
        it_should_behave_like "Node"
        it "should have a status" do
          @api.value.should have_key('status')
        end
      end
    end
    context "(with non-existent job)" do
      before(:all) do
        @api = OarApi.new(APIURI)
      end

      it "should raise an exception" do
        lambda {
            @api.get_hash("jobs/00/details")
        }.should raise_exception
      end

      it "should return a 404 error" do
        begin
          @api.get_hash("jobs/00/details")
        rescue => e
          e.should respond_to('http_code')
          e.http_code.should == 404
        end 
      end
    end
    context "(with finished job)" do
      before(:all) do
        @api = OarApi.new(APIURI)
        @api.get_hash("jobs/1/details")
      end
      it_should_behave_like "Job"
    end
  end

  describe "JOBS LIST DETAILS CHECKING: /jobs/details data structure" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("jobs/details")
      @values=@api.value
    end
    context "(basic structure)" do
      it_should_behave_like "All list structures"
    end
    context "(insider job)" do
      before(:all) do
        @api.value=@api.value["items"][0]
      end
      it_should_behave_like "Job"
      it "should have resources and nodes details" do
        @api.value['resources'].should be_an(Array) 
        @api.value['nodes'].should be_an(Array)
      end
      context "should have resources behaving correctly" do
        before(:all) do
          @api.value=@values["items"][0]['resources'][0]
        end
        it_should_behave_like "ResourceId"
      end
      context "should have nodes behaving correctly" do
        before(:all) do
          @api.value=@values["items"][0]['nodes'][0]
        end
        it_should_behave_like "Node"
        it "should have a status" do
          @api.value.should have_key('status')
        end
      end
    end
  end

  describe "JOB RESOURCES: /jobs/<id>/resources" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("jobs/#{$jobid}/resources")
    end
    context "(basic data structure)" do
      it_should_behave_like "All list structures"
    end
    context "(inside resource)" do
      before(:all) do
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "ResourceId"
      it "should have a status field" do
        @api.value['status'].should_not be_nil
      end
      it "should have a valid status field (reserved or assigned)" do
        @api.value['status'].should match(/(reserved|assigned)/)
      end
    end
  end

  describe "JOB NODES: /jobs/<id>/nodes" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("jobs/#{$jobid}/nodes")
    end
    context "(basic data structure)" do
      it_should_behave_like "All list structures"
    end
    context "(inside node)" do
      before(:all) do
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "Node"
    end
  end

  describe "FUTUR JOBS CHECKING: /jobs" do
    before(:all) do
      @api = OarApi.new(APIURI)
    end
    context "(from 2036 to 2038)" do
      before(:all) do
        from=Time.local(2036,"jan",1,1,1,1).to_i
        to=Time.local(2038,"jan",1,1,1,1).to_i
        @api.get_hash("jobs?from=#{from}&to=#{to}")
      end
      it "should return only one job" do
        @api.value["items"].length.should == 1
      end
      it "should return the previously submitted reservation" do
        @api.value["items"][0]["id"].should == $rjobid
      end
    end
  end
    
  describe "RESOURCES CHECKING: /resources" do
    before(:all) do
      @api = OarApi.new(APIURI)
    end

    context "(with limit=2)" do
      before(:all) do
        @api.get_hash("resources?limit=2")
      end
      it "should correctly limit the number of results" do
        @api.value['items'].length.should == 2
      end
      it "should return 2 links" do
        @api.value['links'].length.should == 2
      end
      it "should return a total > 4" do
        @api.value['total'].to_i.should > 4
      end
      it "should return an offset of 0" do
        @api.value['offset'].to_i.should == 0
      end
      it "should return a correct self link" do
        @api.get_self_link_href.should == "#{APIPATH}resources?limit=2&offset=0"
      end
      it "should return a correct next link" do
        @api.get_next_link_href.should == "#{APIPATH}resources?limit=2&offset=2"
      end
    end

    context "(with limit=2&offset=2)" do
      before(:all) do
        @api.get_hash("resources?limit=2&offset=2")
      end
      it "should correctly limit the number of results" do
        @api.value['items'].length.should == 2
      end
      it "should return 3 links" do
        @api.value['links'].length.should == 3
      end
      it "should return a total > 4" do
        @api.value['total'].to_i.should > 4
      end
      it "should return an offset of 2" do
        @api.value['offset'].to_i.should == 2
      end
      it "should return a correct self link" do
        @api.get_self_link_href.should == "#{APIPATH}resources?limit=2&offset=2"
      end
      it "should return a correct next link" do
        @api.get_next_link_href.should == "#{APIPATH}resources?limit=2&offset=4"
      end
      it "should return a correct previous link" do
        @api.get_previous_link_href.should == "#{APIPATH}resources?limit=2&offset=0"
      end
    end
    
    context "(inside resource)" do
      before(:all) do
        @api.get_hash("resources?limit=2&offset=1")
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "Resource"
    end

    context "(with limit=1)" do
      before(:all) do
        @api.get_hash("resources?limit=1")
      end
      it_should_behave_like "All list structures"
    end

    context "(with limit=1, insider job)" do
      before(:all) do
        @api.get_hash("resources?limit=1")
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "Resource"
    end
  end

  describe "RESOURCES DETAILS CHECKING: /resources/3 data structure" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("resources/3")
    end
    it_should_behave_like "Resource"
  end

  describe "NON-EXISTENT RESOURCE" do
    before(:all) do
      @api = OarApi.new(APIURI)
    end
    it "should raise an exception" do
      lambda {
          @api.get(@api.api,"resources/00")
      }.should raise_exception
    end

    it "should return a 404 error" do
      begin
        @api.get(@api.api,"resources/00")
      rescue => e
        e.should respond_to('http_code')
        e.http_code.should == 404
      end 
    end
  end

  describe "NODE RESOURCES: /resources/nodes/<node>" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("resources?limit=1")
      node_link=@api.get_link_href_from_array(@api.value["items"][0]["links"],"node")
      @api.get_hash(node_link)
    end
    context "(basic data structure)" do
      it_should_behave_like "All list structures"
    end
    context "(insider resource)" do
      before(:all) do
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "ResourceId"
    end
  end

  describe "RESOURCES JOBS: /resources/3/jobs" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("resources/3/jobs")
    end
    context "(basic data structure)" do
      it_should_behave_like "All list structures"
    end
    context "(inside job)" do
      before(:all) do
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "JobId"
    end
  end   

  describe "NODE JOBS: /resources/nodes/<node>/jobs" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("resources?limit=1")
      node_link=@api.get_link_href_from_array(@api.value["items"][0]["links"],"node")
      @api.get_hash(node_link+"/jobs")
    end
    context "(basic data structure)" do
      it_should_behave_like "All list structures"
    end
    context "(inside job)" do
      before(:all) do
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "JobId"
    end
  end   

  describe "NODE JOBS: /resources/nodes/<node>/jobs with state=Running filter" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("resources?limit=1")
      node_link=@api.get_link_href_from_array(@api.value["items"][0]["links"],"node")
      @api.get_hash(node_link+"/jobs?state=Running")
    end
    context "(basic data structure)" do
      it_should_behave_like "All list structures"
    end
    context "(inside job)" do
      before(:all) do
        @api.value=@api.value['items'][0]
      end
      it_should_behave_like "JobId"
    end
  end   

  describe "NODE JOBS: /resources/nodes/<node>/jobs with state=Obiwan filter" do
    before(:all) do
      @api = OarApi.new(APIURI)
      @api.get_hash("resources?limit=1")
      node_link=@api.get_link_href_from_array(@api.value["items"][0]["links"],"node")
      @api.get_hash(node_link+"/jobs?state=Obiwan")
    end
    it "should return an empty list of jobs" do
      @api.value=@api.value['items'].length.should==0
    end
  end   

  #############################################################
  # Job submission responses checks
  #############################################################
  describe "Job submission" do
    it "should return a self link" do
      jhash = { 'resource' => "/nodes=1/core=1" , 'script' => "ls;pwd;whoami;sleep 60"}
      @api.submit_job(jhash)
      $ljobid = @api.jobstatus['id']
      @api.value= @api.jobstatus
      @api.get_self_link_href.should == "#{APIPATH}jobs/#{$ljobid}"
    end
    it "should return a 400 error on bad reservation date" do
      jhash = { 'resource' => "/nodes=1/core=1" , 'script' => "ls;pwd;whoami;sleep 60",
                'reservation' => '1973-06-03 18:00:00' }
      begin
        @api.submit_job(jhash)
      rescue => e
        #puts e.response.body
        e.should respond_to('http_code')
        e.http_code.should == 400
      end
    end
  end

  #############################################################
  # Cleaning
  #############################################################

  describe "Cleaning queue" do
    it "should delete the test jobs" do
      @api.del_array_job($jobid)
      @api.deletestatus['status'].should == "Delete request registered"
    end
    it "should delete the test reservation" do
      @api.del_array_job($rjobid)
      @api.deletestatus['status'].should == "Delete request registered"
    end
    it "should delete the test job" do
      @api.del_array_job($ljobid)
      @api.deletestatus['status'].should == "Delete request registered"
    end
  end

  #############################################################
  # Resubmission
  #############################################################
  describe "Re-submission of a terminated job" do
    before(:all) do
      # Wait for the 2nd job to be terminated
      timeout=180
      t=0
      c=0
      while t<timeout and c == 0
        @api.specific_job_details($jobid+1)
        if @api.specificjobdetails["state"] == "Terminated" || @api.specificjobdetails["state"] == "Error"
          c=1
        else
          printf "."
          $stdout.flush
        end
        sleep 1
        t+=1
      end
      t.should < timeout
      begin
        @api.value=@api.post(@api.api,"jobs/#{$jobid+1}/resubmissions/new",nil)
      rescue => e
        puts e.response.body
      end
    end
    it "should have a self link" do
      @api.get_self_link_href.should == "#{APIPATH}jobs/#{@api.value['id']}"
    end
    it "should have a parent link" do
      @api.get_link_href_by_rel('parent').should == "#{APIPATH}jobs/#{$jobid+1}"
    end
    it "should delete the test job (cleaning)" do
      @api.del_job(@api.value['id'])
      @api.deletestatus['status'].should == "Delete request registered"
    end
  end

end