diff options
author | Tomasz Maczukin <tomasz@maczukin.pl> | 2018-06-07 15:17:44 +0200 |
---|---|---|
committer | Tomasz Maczukin <tomasz@maczukin.pl> | 2018-06-07 15:17:44 +0200 |
commit | 435e661a2ec060c4b7349b26b506243c27e8cc30 (patch) | |
tree | 490f83a3dd46b18bead2bb82d96279f1ff6f5e66 | |
parent | df326d061e23a7d5eb2618e542c193d4aef79c1a (diff) | |
download | gitlab-ce-435e661a2ec060c4b7349b26b506243c27e8cc30.tar.gz |
Introduce new keep-alive API entrypoint for CI job
-rw-r--r-- | lib/api/runner.rb | 16 | ||||
-rw-r--r-- | spec/requests/api/runner_spec.rb | 62 |
2 files changed, 78 insertions, 0 deletions
diff --git a/lib/api/runner.rb b/lib/api/runner.rb index db502697a19..ac62b83ba4a 100644 --- a/lib/api/runner.rb +++ b/lib/api/runner.rb @@ -140,6 +140,22 @@ module API end end + desc 'Marks job as live' do + http_codes [[200, 'Request accepted']] + end + params do + requires :id, type: Integer, desc: %q(Job's ID) + optional :token, type: String, desc: %q(Job's authentication token) + end + post '/:id/keep-alive' do + job = authenticate_job! + + job.touch if job.running? && job.needs_touch? + + status 200 + header 'Job-Status', job.status + end + desc 'Appends a patch to the job trace' do http_codes [[202, 'Trace was patched'], [400, 'Missing Content-Range header'], diff --git a/spec/requests/api/runner_spec.rb b/spec/requests/api/runner_spec.rb index c981a10ac38..c93612d7ada 100644 --- a/spec/requests/api/runner_spec.rb +++ b/spec/requests/api/runner_spec.rb @@ -849,6 +849,68 @@ describe API::Runner, :clean_gitlab_redis_shared_state do end end + describe 'POST /api/v4/jobs/:id/keep-alive' do + let(:job) { create(:ci_build, :running, :trace_live, runner_id: runner.id, pipeline: pipeline) } + let(:headers) { { API::Helpers::Runner::JOB_TOKEN_HEADER => job.token, 'Content-Type' => 'text/plain' } } + let(:update_interval) { 30.seconds } + + it 'returns correct response' do + keep_alive_job + + expect(response.status).to eq 200 + expect(response.header).to have_key 'Job-Status' + end + + it 'updates updated_at value' do + expect { keep_alive_job }.to change { job.updated_at } + end + + context 'when project for the build has been deleted' do + let(:job) do + create(:ci_build, :running, :trace_live, runner_id: runner.id, pipeline: pipeline) do |job| + job.project.update(pending_delete: true) + end + end + + it 'responds with forbidden' do + keep_alive_job + + expect(response.status).to eq(403) + end + end + + context 'when job has been canceled' do + before do + job.cancel + end + + it 'returns job-status=canceled header' do + keep_alive_job + + expect(response.status).to eq 200 + expect(response.header['Job-Status']).to eq('canceled') + end + end + + context 'when job has been errased' do + let(:job) { create(:ci_build, runner_id: runner.id, erased_at: Time.now) } + + it 'rresponds with forbidden' do + keep_alive_job + + expect(response.status).to eq 403 + end + end + + def keep_alive_job(token = job.token, **params) + new_params = params.merge(token: token) + Timecop.travel(job.updated_at + update_interval) do + post api("/jobs/#{job.id}/keep-alive"), new_params + job.reload + end + end + end + describe 'PATCH /api/v4/jobs/:id/trace' do let(:job) { create(:ci_build, :running, :trace_live, runner_id: runner.id, pipeline: pipeline) } let(:headers) { { API::Helpers::Runner::JOB_TOKEN_HEADER => job.token, 'Content-Type' => 'text/plain' } } |