gitlab-org--gitlab-foss/spec/requests/api/jobs_spec.rb

514 lines
14 KiB
Ruby
Raw Normal View History

2015-12-29 17:12:36 -05:00
require 'spec_helper'
describe API::Jobs, :api do
let!(:project) do
create(:project, :repository, public_builds: false)
end
let!(:pipeline) do
create(:ci_empty_pipeline, project: project,
sha: project.commit.id,
ref: project.default_branch)
end
let!(:build) { create(:ci_build, pipeline: pipeline) }
2015-12-29 17:12:36 -05:00
let(:user) { create(:user) }
2016-02-19 12:06:53 -05:00
let(:api_user) { user }
let(:reporter) { create(:project_member, :reporter, project: project).user }
let(:guest) { create(:project_member, :guest, project: project).user }
before do
project.add_developer(user)
end
2015-12-29 17:12:36 -05:00
describe 'GET /projects/:id/jobs' do
let(:query) { Hash.new }
2016-02-19 12:06:53 -05:00
2016-07-14 12:53:27 -04:00
before do
get api("/projects/#{project.id}/jobs", api_user), query
2016-07-14 12:53:27 -04:00
end
2016-02-19 12:06:53 -05:00
2015-12-29 17:12:36 -05:00
context 'authorized user' do
it 'returns project jobs' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(200)
2017-01-24 15:49:10 -05:00
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
end
it 'returns correct values' do
expect(json_response).not_to be_empty
expect(json_response.first['commit']['id']).to eq project.commit.id
end
it 'returns pipeline data' do
json_build = json_response.first
expect(json_build['pipeline']).not_to be_empty
expect(json_build['pipeline']['id']).to eq build.pipeline.id
expect(json_build['pipeline']['ref']).to eq build.pipeline.ref
expect(json_build['pipeline']['sha']).to eq build.pipeline.sha
expect(json_build['pipeline']['status']).to eq build.pipeline.status
end
2016-02-19 12:06:53 -05:00
context 'filter project with one scope element' do
let(:query) { { 'scope' => 'pending' } }
2016-02-19 12:06:53 -05:00
it do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(200)
2016-02-19 12:06:53 -05:00
expect(json_response).to be_an Array
end
end
2016-02-19 12:06:53 -05:00
context 'filter project with array of scope elements' do
let(:query) { { scope: %w(pending running) } }
2016-02-19 12:06:53 -05:00
it do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(200)
2016-02-19 12:06:53 -05:00
expect(json_response).to be_an Array
end
2015-12-29 17:12:36 -05:00
end
2016-01-13 09:17:59 -05:00
2016-02-19 12:06:53 -05:00
context 'respond 400 when scope contains invalid state' do
let(:query) { { scope: %w(unknown running) } }
2016-01-13 09:17:59 -05:00
2016-06-27 14:10:42 -04:00
it { expect(response).to have_http_status(400) }
2016-01-13 09:17:59 -05:00
end
2015-12-29 17:12:36 -05:00
end
context 'unauthorized user' do
2016-02-19 12:06:53 -05:00
let(:api_user) { nil }
2015-12-29 17:12:36 -05:00
it 'does not return project builds' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(401)
2015-12-29 17:12:36 -05:00
end
end
end
describe 'GET /projects/:id/pipelines/:pipeline_id/jobs' do
let(:query) { Hash.new }
before do
get api("/projects/#{project.id}/pipelines/#{pipeline.id}/jobs", api_user), query
end
context 'authorized user' do
it 'returns pipeline jobs' do
expect(response).to have_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
end
it 'returns correct values' do
expect(json_response).not_to be_empty
expect(json_response.first['commit']['id']).to eq project.commit.id
end
it 'returns pipeline data' do
json_build = json_response.first
expect(json_build['pipeline']).not_to be_empty
expect(json_build['pipeline']['id']).to eq build.pipeline.id
expect(json_build['pipeline']['ref']).to eq build.pipeline.ref
expect(json_build['pipeline']['sha']).to eq build.pipeline.sha
expect(json_build['pipeline']['status']).to eq build.pipeline.status
end
context 'filter jobs with one scope element' do
let(:query) { { 'scope' => 'pending' } }
it do
expect(response).to have_http_status(200)
expect(json_response).to be_an Array
end
end
context 'filter jobs with array of scope elements' do
let(:query) { { scope: %w(pending running) } }
it do
expect(response).to have_http_status(200)
expect(json_response).to be_an Array
end
end
context 'respond 400 when scope contains invalid state' do
let(:query) { { scope: %w(unknown running) } }
it { expect(response).to have_http_status(400) }
end
context 'jobs in different pipelines' do
let!(:pipeline2) { create(:ci_empty_pipeline, project: project) }
let!(:build2) { create(:ci_build, pipeline: pipeline2) }
it 'excludes jobs from other pipelines' do
json_response.each { |job| expect(job['pipeline']['id']).to eq(pipeline.id) }
end
end
end
context 'unauthorized user' do
let(:api_user) { nil }
it 'does not return jobs' do
expect(response).to have_http_status(401)
end
end
end
describe 'GET /projects/:id/jobs/:job_id' do
2016-07-14 12:53:27 -04:00
before do
get api("/projects/#{project.id}/jobs/#{build.id}", api_user)
2016-07-14 12:53:27 -04:00
end
2016-02-19 12:06:53 -05:00
context 'authorized user' do
it 'returns specific job data' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(200)
expect(json_response['name']).to eq('test')
end
it 'returns pipeline data' do
json_build = json_response
expect(json_build['pipeline']).not_to be_empty
expect(json_build['pipeline']['id']).to eq build.pipeline.id
expect(json_build['pipeline']['ref']).to eq build.pipeline.ref
expect(json_build['pipeline']['sha']).to eq build.pipeline.sha
expect(json_build['pipeline']['status']).to eq build.pipeline.status
end
end
context 'unauthorized user' do
2016-02-19 12:06:53 -05:00
let(:api_user) { nil }
it 'does not return specific job data' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(401)
end
end
end
describe 'GET /projects/:id/jobs/:job_id/artifacts' do
2016-07-14 12:53:27 -04:00
before do
get api("/projects/#{project.id}/jobs/#{build.id}/artifacts", api_user)
2016-07-14 12:53:27 -04:00
end
context 'job with artifacts' do
let(:build) { create(:ci_build, :artifacts, pipeline: pipeline) }
2016-02-19 12:06:53 -05:00
context 'authorized user' do
let(:download_headers) do
{ 'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'attachment; filename=ci_build_artifacts.zip' }
2016-02-19 12:06:53 -05:00
end
it 'returns specific job artifacts' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(200)
2016-02-19 12:06:53 -05:00
expect(response.headers).to include(download_headers)
expect(response.body).to match_file(build.artifacts_file.file.file)
2016-02-19 12:06:53 -05:00
end
end
2016-02-19 12:06:53 -05:00
context 'unauthorized user' do
let(:api_user) { nil }
it 'does not return specific job artifacts' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(401)
2016-02-19 12:06:53 -05:00
end
end
end
2016-02-19 12:06:53 -05:00
it 'does not return job artifacts if not uploaded' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(404)
2016-02-19 12:06:53 -05:00
end
end
describe 'GET /projects/:id/artifacts/:ref_name/download?job=name' do
let(:api_user) { reporter }
let(:build) { create(:ci_build, :artifacts, pipeline: pipeline) }
before do
build.success
end
2016-07-14 13:07:41 -04:00
def get_for_ref(ref = pipeline.ref, job = build.name)
get api("/projects/#{project.id}/jobs/artifacts/#{ref}/download", api_user), job: job
2016-07-14 13:07:41 -04:00
end
context 'when not logged in' do
2016-07-19 13:24:59 -04:00
let(:api_user) { nil }
2016-07-14 13:07:41 -04:00
before do
get_for_ref
2016-07-14 13:07:41 -04:00
end
it 'gives 401' do
2016-07-14 13:07:41 -04:00
expect(response).to have_http_status(401)
end
end
context 'when logging as guest' do
let(:api_user) { guest }
before do
get_for_ref
end
it 'gives 403' do
expect(response).to have_http_status(403)
end
end
context 'non-existing job' do
shared_examples 'not found' do
it { expect(response).to have_http_status(:not_found) }
2016-07-14 13:07:41 -04:00
end
context 'has no such ref' do
before do
get_for_ref('TAIL')
end
it_behaves_like 'not found'
end
context 'has no such job' do
before do
get_for_ref(pipeline.ref, 'NOBUILD')
end
it_behaves_like 'not found'
end
2016-07-14 13:07:41 -04:00
end
context 'find proper job' do
shared_examples 'a valid file' do
let(:download_headers) do
2016-07-14 13:07:41 -04:00
{ 'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' =>
"attachment; filename=#{build.artifacts_file.filename}" }
end
2016-07-14 13:07:41 -04:00
it { expect(response).to have_http_status(200) }
it { expect(response.headers).to include(download_headers) }
end
context 'with regular branch' do
before do
2016-10-26 05:34:40 -04:00
pipeline.reload
pipeline.update(ref: 'master',
sha: project.commit('master').sha)
get_for_ref('master')
end
it_behaves_like 'a valid file'
2016-07-14 13:07:41 -04:00
end
context 'with branch name containing slash' do
before do
2016-10-26 05:34:40 -04:00
pipeline.reload
pipeline.update(ref: 'improve/awesome',
sha: project.commit('improve/awesome').sha)
end
before do
get_for_ref('improve/awesome')
end
it_behaves_like 'a valid file'
end
2016-07-14 13:07:41 -04:00
end
end
describe 'GET /projects/:id/jobs/:job_id/trace' do
let(:build) { create(:ci_build, :trace, pipeline: pipeline) }
2016-07-14 12:53:27 -04:00
before do
get api("/projects/#{project.id}/jobs/#{build.id}/trace", api_user)
2016-07-14 12:53:27 -04:00
end
2016-02-19 12:06:53 -05:00
context 'authorized user' do
it 'returns specific job trace' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(200)
expect(response.body).to eq(build.trace.raw)
end
end
context 'unauthorized user' do
2016-02-19 12:06:53 -05:00
let(:api_user) { nil }
it 'does not return specific job trace' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/jobs/:job_id/cancel' do
2016-07-14 12:53:27 -04:00
before do
post api("/projects/#{project.id}/jobs/#{build.id}/cancel", api_user)
2016-07-14 12:53:27 -04:00
end
2016-02-19 12:06:53 -05:00
context 'authorized user' do
context 'user with :update_build persmission' do
it 'cancels running or pending job' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(201)
expect(project.builds.first.status).to eq('canceled')
end
end
context 'user without :update_build permission' do
let(:api_user) { reporter }
it 'does not cancel job' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(403)
end
end
end
context 'unauthorized user' do
2016-02-19 12:06:53 -05:00
let(:api_user) { nil }
it 'does not cancel job' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/jobs/:job_id/retry' do
let(:build) { create(:ci_build, :canceled, pipeline: pipeline) }
2016-02-19 12:06:53 -05:00
2016-07-14 12:53:27 -04:00
before do
post api("/projects/#{project.id}/jobs/#{build.id}/retry", api_user)
2016-07-14 12:53:27 -04:00
end
2016-02-19 12:06:53 -05:00
context 'authorized user' do
2016-02-19 12:06:53 -05:00
context 'user with :update_build permission' do
it 'retries non-running job' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(201)
expect(project.builds.first.status).to eq('canceled')
expect(json_response['status']).to eq('pending')
end
end
context 'user without :update_build permission' do
let(:api_user) { reporter }
it 'does not retry job' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(403)
end
end
end
context 'unauthorized user' do
2016-02-19 12:06:53 -05:00
let(:api_user) { nil }
it 'does not retry job' do
2016-06-27 14:10:42 -04:00
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/jobs/:job_id/erase' do
before do
post api("/projects/#{project.id}/jobs/#{build.id}/erase", user)
end
context 'job is erasable' do
let(:build) { create(:ci_build, :trace, :artifacts, :success, project: project, pipeline: pipeline) }
it 'erases job content' do
expect(response).to have_http_status(201)
expect(build).not_to have_trace
expect(build.artifacts_file.exists?).to be_falsy
expect(build.artifacts_metadata.exists?).to be_falsy
end
it 'updates job' do
build.reload
expect(build.erased_at).to be_truthy
expect(build.erased_by).to eq(user)
end
end
context 'job is not erasable' do
let(:build) { create(:ci_build, :trace, project: project, pipeline: pipeline) }
it 'responds with forbidden' do
expect(response).to have_http_status(403)
end
end
end
2016-06-10 11:11:27 -04:00
describe 'POST /projects/:id/jobs/:build_id/artifacts/keep' do
2016-06-10 11:11:27 -04:00
before do
post api("/projects/#{project.id}/jobs/#{build.id}/artifacts/keep", user)
2016-06-10 11:11:27 -04:00
end
context 'artifacts did not expire' do
let(:build) do
create(:ci_build, :trace, :artifacts, :success,
project: project, pipeline: pipeline, artifacts_expire_at: Time.now + 7.days)
end
2016-06-10 15:45:06 -04:00
it 'keeps artifacts' do
expect(response).to have_http_status(200)
2016-06-10 15:45:06 -04:00
expect(build.reload.artifacts_expire_at).to be_nil
2016-06-10 11:11:27 -04:00
end
end
context 'no artifacts' do
let(:build) { create(:ci_build, project: project, pipeline: pipeline) }
2016-06-10 15:45:06 -04:00
it 'responds with not found' do
expect(response).to have_http_status(404)
2016-06-10 11:11:27 -04:00
end
end
end
2016-08-15 09:58:22 -04:00
describe 'POST /projects/:id/jobs/:job_id/play' do
2016-08-15 09:58:22 -04:00
before do
post api("/projects/#{project.id}/jobs/#{build.id}/play", api_user)
2016-08-15 09:58:22 -04:00
end
context 'on an playable job' do
2016-08-15 09:58:22 -04:00
let(:build) { create(:ci_build, :manual, project: project, pipeline: pipeline) }
context 'when user is authorized to trigger a manual action' do
it 'plays the job' do
expect(response).to have_http_status(200)
expect(json_response['user']['id']).to eq(user.id)
expect(json_response['id']).to eq(build.id)
expect(build.reload).to be_pending
end
end
context 'when user is not authorized to trigger a manual action' do
context 'when user does not have access to the project' do
let(:api_user) { create(:user) }
it 'does not trigger a manual action' do
expect(build.reload).to be_manual
expect(response).to have_http_status(404)
end
end
context 'when user is not allowed to trigger the manual action' do
let(:api_user) { reporter }
it 'does not trigger a manual action' do
expect(build.reload).to be_manual
expect(response).to have_http_status(403)
end
end
2016-08-15 09:58:22 -04:00
end
end
context 'on a non-playable job' do
2016-08-15 09:58:22 -04:00
it 'returns a status code 400, Bad Request' do
expect(response).to have_http_status 400
expect(response.body).to match("Unplayable Job")
2016-08-15 09:58:22 -04:00
end
end
end
2015-12-29 17:12:36 -05:00
end