- include GitTestHelper
-
- test "submit a job" do
- authorize_with :active
- post :create, job: {
- script: "hash",
- script_version: "master",
- repository: "active/foo",
- script_parameters: {}
- }
- assert_response :success
- assert_not_nil assigns(:object)
- new_job = JSON.parse(@response.body)
- assert_not_nil new_job['uuid']
- assert_not_nil new_job['script_version'].match(/^[0-9a-f]{40}$/)
- assert_equal 0, new_job['priority']
- end
-
- test "normalize output and log uuids when creating job" do
- authorize_with :active
- post :create, job: {
- script: "hash",
- script_version: "master",
- script_parameters: {},
- repository: "active/foo",
- started_at: Time.now,
- finished_at: Time.now,
- running: false,
- success: true,
- output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
- log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
- }
- assert_response :success
- assert_not_nil assigns(:object)
- new_job = assigns(:object)
- assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['log']
- assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['output']
- version = new_job['script_version']
-
- # Make sure version doesn't get mangled by normalize
- assert_not_nil version.match(/^[0-9a-f]{40}$/)
- assert_equal 'master', json_response['supplied_script_version']
- end
-
- test "normalize output and log uuids when updating job" do
- authorize_with :active
-
- foobar_job = jobs(:foobar)
-
- new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
- new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
- put :update, {
- id: foobar_job['uuid'],
- job: {
- output: new_output,
- log: new_log
- }
- }
-
- updated_job = json_response
- assert_not_equal foobar_job['log'], updated_job['log']
- assert_not_equal new_log, updated_job['log'] # normalized during update
- assert_equal new_log[0,new_log.rindex('+')], updated_job['log']
- assert_not_equal foobar_job['output'], updated_job['output']
- assert_not_equal new_output, updated_job['output'] # normalized during update
- assert_equal new_output[0,new_output.rindex('+')], updated_job['output']
- end
-
- test "cancel a running job" do
- # We need to verify that "cancel" creates a trigger file, so first
- # let's make sure there is no stale trigger file.
- begin
- File.unlink(Rails.configuration.crunch_refresh_trigger)
- rescue Errno::ENOENT
- end
-
- authorize_with :active
- put :update, {
- id: jobs(:running).uuid,
- job: {
- cancelled_at: 4.day.ago
- }
- }
- assert_response :success
- assert_not_nil assigns(:object)
- job = JSON.parse(@response.body)
- assert_not_nil job['uuid']
- assert_not_nil job['cancelled_at']
- assert_not_nil job['cancelled_by_user_uuid']
- assert_not_nil job['cancelled_by_client_uuid']
- assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
- 'server should correct bogus cancelled_at ' +
- job['cancelled_at'])
- assert_equal(true,
- File.exists?(Rails.configuration.crunch_refresh_trigger),
- 'trigger file should be created when job is cancelled')
- end
-
- [
- [:put, :update, {job:{cancelled_at: Time.now}}, :success],
- [:put, :update, {job:{cancelled_at: nil}}, :unprocessable_entity],
- [:put, :update, {job:{state: 'Cancelled'}}, :success],
- [:put, :update, {job:{state: 'Queued'}}, :unprocessable_entity],
- [:put, :update, {job:{state: 'Running'}}, :unprocessable_entity],
- [:put, :update, {job:{state: 'Failed'}}, :unprocessable_entity],
- [:put, :update, {job:{state: 'Complete'}}, :unprocessable_entity],
- [:post, :cancel, {}, :success],
- ].each do |http_method, action, params, expected_response|
- test "cancelled job stays cancelled after #{[http_method, action, params].inspect}" do
- # We need to verify that "cancel" creates a trigger file, so first
- # let's make sure there is no stale trigger file.
- begin
- File.unlink(Rails.configuration.crunch_refresh_trigger)
- rescue Errno::ENOENT
- end
-
- authorize_with :active
- self.send http_method, action, { id: jobs(:cancelled).uuid }.merge(params)
- assert_response expected_response
- if expected_response == :success
- job = json_response
- assert_not_nil job['cancelled_at'], 'job cancelled again using #{attribute}=#{value} did not have cancelled_at value'
- assert_equal job['state'], 'Cancelled', 'cancelled again job state changed when updated using using #{attribute}=#{value}'
- end
- # Verify database record still says Cancelled
- assert_equal 'Cancelled', Job.find(jobs(:cancelled).id).state, 'job was un-cancelled'
- end
- end
-
- test "cancelled job updated to any other state change results in error" do
- # We need to verify that "cancel" creates a trigger file, so first
- # let's make sure there is no stale trigger file.
- begin
- File.unlink(Rails.configuration.crunch_refresh_trigger)
- rescue Errno::ENOENT
- end
-
- authorize_with :active
- put :update, {
- id: jobs(:running_cancelled).uuid,
- job: {
- cancelled_at: nil
- }
- }
- assert_response 422
- end
-
- ['abc.py', 'hash.py'].each do |script|
- test "update job script attribute to #{script} without failing script_version check" do
- authorize_with :admin
- put :update, {
- id: jobs(:uses_nonexistent_script_version).uuid,
- job: {
- script: script
- }
- }
- assert_response :success
- resp = assigns(:object)
- assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
- end
- end
-