Merge remote-tracking branch 'origin/master' into 1971-show-image-thumbnails
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::JobsControllerTest < ActionController::TestCase
4
5   test "submit a job" do
6     authorize_with :active
7     post :create, job: {
8       script: "hash",
9       script_version: "master",
10       script_parameters: {}
11     }
12     assert_response :success
13     assert_not_nil assigns(:object)
14     new_job = JSON.parse(@response.body)
15     assert_not_nil new_job['uuid']
16     assert_not_nil new_job['script_version'].match(/^[0-9a-f]{40}$/)
17   end
18
19   test "normalize output and log uuids when creating job" do
20     authorize_with :active
21     post :create, job: {
22       script: "hash",
23       script_version: "master",
24       script_parameters: {},
25       started_at: Time.now,
26       finished_at: Time.now,
27       running: false,
28       success: true,
29       output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
30       log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
31     }
32     assert_response :success
33     assert_not_nil assigns(:object)
34     new_job = JSON.parse(@response.body)
35     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['log']
36     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['output']
37     version = new_job['script_version']
38
39     # Make sure version doesn't get mangled by normalize
40     assert_not_nil version.match(/^[0-9a-f]{40}$/)
41     put :update, {
42       id: new_job['uuid'],
43       job: {
44         log: new_job['log']
45       }
46     }
47     assert_equal version, JSON.parse(@response.body)['script_version']
48   end
49
50   test "cancel a running job" do
51     # We need to verify that "cancel" creates a trigger file, so first
52     # let's make sure there is no stale trigger file.
53     begin
54       File.unlink(Rails.configuration.crunch_refresh_trigger)
55     rescue Errno::ENOENT
56     end
57
58     authorize_with :active
59     put :update, {
60       id: jobs(:running).uuid,
61       job: {
62         cancelled_at: 4.day.ago
63       }
64     }
65     assert_response :success
66     assert_not_nil assigns(:object)
67     job = JSON.parse(@response.body)
68     assert_not_nil job['uuid']
69     assert_not_nil job['cancelled_at']
70     assert_not_nil job['cancelled_by_user_uuid']
71     assert_not_nil job['cancelled_by_client_uuid']
72     assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
73                  'server should correct bogus cancelled_at ' +
74                  job['cancelled_at'])
75     assert_equal(true,
76                  File.exists?(Rails.configuration.crunch_refresh_trigger),
77                  'trigger file should be created when job is cancelled')
78
79     put :update, {
80       id: jobs(:running).uuid,
81       job: {
82         cancelled_at: nil
83       }
84     }
85     job = JSON.parse(@response.body)
86     assert_not_nil job['cancelled_at'], 'un-cancelled job stays cancelled'
87   end
88
89   test "update a job without failing script_version check" do
90     authorize_with :admin
91     put :update, {
92       id: jobs(:uses_nonexistent_script_version).uuid,
93       job: {
94         owner_uuid: users(:admin).uuid
95       }
96     }
97     assert_response :success
98     put :update, {
99       id: jobs(:uses_nonexistent_script_version).uuid,
100       job: {
101         owner_uuid: users(:active).uuid
102       }
103     }
104     assert_response :success
105   end
106 end