debian-mirror-gitlab/spec/requests/api/v3/builds_spec.rb

488 lines
14 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2017-08-17 22:00:37 +05:30
describe API::V3::Builds do
let(:user) { create(:user) }
2016-04-02 18:10:28 +05:30
let(:api_user) { user }
2017-08-17 22:00:37 +05:30
let!(:project) { create(:project, :repository, creator: user, public_builds: false) }
2016-06-02 11:05:42 +05:30
let!(:developer) { create(:project_member, :developer, user: user, project: project) }
2016-08-24 12:49:21 +05:30
let(:reporter) { create(:project_member, :reporter, project: project) }
let(:guest) { create(:project_member, :guest, project: project) }
2016-09-13 17:45:13 +05:30
let!(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.id, ref: project.default_branch) }
2016-06-22 15:30:34 +05:30
let!(:build) { create(:ci_build, pipeline: pipeline) }
describe 'GET /projects/:id/builds ' do
2016-04-02 18:10:28 +05:30
let(:query) { '' }
2016-09-29 09:46:39 +05:30
before do
2017-08-17 22:00:37 +05:30
create(:ci_build, :skipped, pipeline: pipeline)
get v3_api("/projects/#{project.id}/builds?#{query}", api_user)
2016-09-29 09:46:39 +05:30
end
2016-04-02 18:10:28 +05:30
context 'authorized user' do
2016-09-13 17:45:13 +05:30
it 'returns project builds' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
end
2016-06-22 15:30:34 +05:30
it 'returns correct values' do
expect(json_response).not_to be_empty
expect(json_response.first['commit']['id']).to eq project.commit.id
end
2016-11-03 12:29:30 +05:30
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-04-02 18:10:28 +05:30
context 'filter project with one scope element' do
let(:query) { 'scope=pending' }
2016-04-02 18:10:28 +05:30
it do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-04-02 18:10:28 +05:30
expect(json_response).to be_an Array
end
end
2017-08-17 22:00:37 +05:30
context 'filter project with scope skipped' do
let(:query) { 'scope=skipped' }
let(:json_build) { json_response.first }
it 'return builds with status skipped' do
expect(response).to have_http_status 200
expect(json_response).to be_an Array
expect(json_response.length).to eq 1
expect(json_build['status']).to eq 'skipped'
end
end
2016-04-02 18:10:28 +05:30
context 'filter project with array of scope elements' do
let(:query) { 'scope[0]=pending&scope[1]=running' }
2016-04-02 18:10:28 +05:30
it do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-04-02 18:10:28 +05:30
expect(json_response).to be_an Array
end
end
2016-04-02 18:10:28 +05:30
context 'respond 400 when scope contains invalid state' do
let(:query) { 'scope[0]=pending&scope[1]=unknown_status' }
2016-08-24 12:49:21 +05:30
it { expect(response).to have_http_status(400) }
end
end
context 'unauthorized user' do
2016-04-02 18:10:28 +05:30
let(:api_user) { nil }
2017-08-17 22:00:37 +05:30
it 'does not return project builds' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
describe 'GET /projects/:id/repository/commits/:sha/builds' do
2016-08-24 12:49:21 +05:30
context 'when commit does not exist in repository' do
before do
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/repository/commits/1a271fd1/builds", api_user)
2016-08-24 12:49:21 +05:30
end
2016-04-02 18:10:28 +05:30
2016-08-24 12:49:21 +05:30
it 'responds with 404' do
expect(response).to have_http_status(404)
end
end
2016-08-24 12:49:21 +05:30
context 'when commit exists in repository' do
context 'when user is authorized' do
2017-08-17 22:00:37 +05:30
context 'when pipeline has jobs' do
2016-08-24 12:49:21 +05:30
before do
create(:ci_pipeline, project: project, sha: project.commit.id)
create(:ci_build, pipeline: pipeline)
create(:ci_build)
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", api_user)
2016-08-24 12:49:21 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns project jobs for specific commit' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2016-08-24 12:49:21 +05:30
expect(json_response).to be_an Array
expect(json_response.size).to eq 2
end
2016-11-03 12:29:30 +05:30
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-08-24 12:49:21 +05:30
end
2017-08-17 22:00:37 +05:30
context 'when pipeline has no jobs' do
2016-08-24 12:49:21 +05:30
before do
branch_head = project.commit('feature').id
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/repository/commits/#{branch_head}/builds", api_user)
2016-08-24 12:49:21 +05:30
end
it 'returns an empty array' do
expect(response).to have_http_status(200)
expect(json_response).to be_an Array
expect(json_response).to be_empty
end
end
end
context 'when user is not authorized' do
before do
create(:ci_pipeline, project: project, sha: project.commit.id)
create(:ci_build, pipeline: pipeline)
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", nil)
2016-08-24 12:49:21 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not return project jobs' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
expect(json_response.except('message')).to be_empty
end
end
end
end
describe 'GET /projects/:id/builds/:build_id' do
2016-09-29 09:46:39 +05:30
before do
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/builds/#{build.id}", api_user)
2016-09-29 09:46:39 +05:30
end
2016-04-02 18:10:28 +05:30
context 'authorized user' do
2017-08-17 22:00:37 +05:30
it 'returns specific job data' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
expect(json_response['name']).to eq('test')
end
2016-11-03 12:29:30 +05:30
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-04-02 18:10:28 +05:30
let(:api_user) { nil }
2017-08-17 22:00:37 +05:30
it 'does not return specific job data' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
2016-04-02 18:10:28 +05:30
describe 'GET /projects/:id/builds/:build_id/artifacts' do
2016-09-29 09:46:39 +05:30
before do
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/builds/#{build.id}/artifacts", api_user)
2016-09-29 09:46:39 +05:30
end
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
context 'job with artifacts' do
let(:build) { create(:ci_build, :artifacts, pipeline: pipeline) }
2016-04-02 18:10:28 +05:30
context 'authorized user' do
let(:download_headers) do
2016-06-02 11:05:42 +05:30
{ 'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'attachment; filename=ci_build_artifacts.zip' }
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns specific job artifacts' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-04-02 18:10:28 +05:30
expect(response.headers).to include(download_headers)
2017-08-17 22:00:37 +05:30
expect(response.body).to match_file(build.artifacts_file.file.file)
2016-04-02 18:10:28 +05:30
end
end
context 'unauthorized user' do
let(:api_user) { nil }
2017-08-17 22:00:37 +05:30
it 'does not return specific job artifacts' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
2016-04-02 18:10:28 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
it 'does not return job artifacts if not uploaded' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
end
end
describe 'GET /projects/:id/artifacts/:ref_name/download?job=name' do
let(:api_user) { reporter.user }
2016-09-13 17:45:13 +05:30
let(:build) { create(:ci_build, :artifacts, pipeline: pipeline) }
before do
build.success
end
2016-08-24 12:49:21 +05:30
def path_for_ref(ref = pipeline.ref, job = build.name)
2017-08-17 22:00:37 +05:30
v3_api("/projects/#{project.id}/builds/artifacts/#{ref}/download?job=#{job}", api_user)
2016-08-24 12:49:21 +05:30
end
context 'when not logged in' do
let(:api_user) { nil }
before do
get path_for_ref
end
it 'gives 401' do
expect(response).to have_http_status(401)
end
end
context 'when logging as guest' do
let(:api_user) { guest.user }
before do
get path_for_ref
end
it 'gives 403' do
expect(response).to have_http_status(403)
end
end
2017-08-17 22:00:37 +05:30
context 'non-existing job' do
2016-08-24 12:49:21 +05:30
shared_examples 'not found' do
it { expect(response).to have_http_status(:not_found) }
end
context 'has no such ref' do
before do
get path_for_ref('TAIL', build.name)
end
it_behaves_like 'not found'
end
2017-08-17 22:00:37 +05:30
context 'has no such job' do
2016-08-24 12:49:21 +05:30
before do
get path_for_ref(pipeline.ref, 'NOBUILD')
end
it_behaves_like 'not found'
end
end
2017-08-17 22:00:37 +05:30
context 'find proper job' do
2016-08-24 12:49:21 +05:30
shared_examples 'a valid file' do
let(:download_headers) do
{ 'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' =>
"attachment; filename=#{build.artifacts_file.filename}" }
end
it { expect(response).to have_http_status(200) }
it { expect(response.headers).to include(download_headers) }
end
context 'with regular branch' do
before do
2017-08-17 22:00:37 +05:30
pipeline.reload
2016-08-24 12:49:21 +05:30
pipeline.update(ref: 'master',
sha: project.commit('master').sha)
get path_for_ref('master')
end
it_behaves_like 'a valid file'
end
context 'with branch name containing slash' do
before do
2017-08-17 22:00:37 +05:30
pipeline.reload
2016-08-24 12:49:21 +05:30
pipeline.update(ref: 'improve/awesome',
sha: project.commit('improve/awesome').sha)
end
before do
get path_for_ref('improve/awesome')
end
it_behaves_like 'a valid file'
end
2016-04-02 18:10:28 +05:30
end
end
describe 'GET /projects/:id/builds/:build_id/trace' do
let(:build) { create(:ci_build, :trace, pipeline: pipeline) }
2016-06-22 15:30:34 +05:30
2016-08-24 12:49:21 +05:30
before do
2017-08-17 22:00:37 +05:30
get v3_api("/projects/#{project.id}/builds/#{build.id}/trace", api_user)
2016-08-24 12:49:21 +05:30
end
2016-04-02 18:10:28 +05:30
context 'authorized user' do
2017-08-17 22:00:37 +05:30
it 'returns specific job trace' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2017-08-17 22:00:37 +05:30
expect(response.body).to eq(build.trace.raw)
end
end
context 'unauthorized user' do
2016-04-02 18:10:28 +05:30
let(:api_user) { nil }
2017-08-17 22:00:37 +05:30
it 'does not return specific job trace' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/builds/:build_id/cancel' do
2016-09-29 09:46:39 +05:30
before do
2017-08-17 22:00:37 +05:30
post v3_api("/projects/#{project.id}/builds/#{build.id}/cancel", api_user)
2016-09-29 09:46:39 +05:30
end
2016-04-02 18:10:28 +05:30
context 'authorized user' do
2016-04-02 18:10:28 +05:30
context 'user with :update_build persmission' do
2017-08-17 22:00:37 +05:30
it 'cancels running or pending job' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
expect(project.builds.first.status).to eq('canceled')
end
end
2016-04-02 18:10:28 +05:30
context 'user without :update_build permission' do
2016-08-24 12:49:21 +05:30
let(:api_user) { reporter.user }
2017-08-17 22:00:37 +05:30
it 'does not cancel job' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
end
end
end
context 'unauthorized user' do
2016-04-02 18:10:28 +05:30
let(:api_user) { nil }
2017-08-17 22:00:37 +05:30
it 'does not cancel job' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/builds/:build_id/retry' do
let(:build) { create(:ci_build, :canceled, pipeline: pipeline) }
2016-04-02 18:10:28 +05:30
2016-09-29 09:46:39 +05:30
before do
2017-08-17 22:00:37 +05:30
post v3_api("/projects/#{project.id}/builds/#{build.id}/retry", api_user)
2016-09-29 09:46:39 +05:30
end
2016-04-02 18:10:28 +05:30
context 'authorized user' do
2016-04-02 18:10:28 +05:30
context 'user with :update_build permission' do
2017-08-17 22:00:37 +05:30
it 'retries non-running job' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
expect(project.builds.first.status).to eq('canceled')
expect(json_response['status']).to eq('pending')
end
end
2016-04-02 18:10:28 +05:30
context 'user without :update_build permission' do
2016-08-24 12:49:21 +05:30
let(:api_user) { reporter.user }
2017-08-17 22:00:37 +05:30
it 'does not retry job' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
end
end
end
context 'unauthorized user' do
2016-04-02 18:10:28 +05:30
let(:api_user) { nil }
2017-08-17 22:00:37 +05:30
it 'does not retry job' do
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
2016-04-02 18:10:28 +05:30
describe 'POST /projects/:id/builds/:build_id/erase' do
before do
2017-08-17 22:00:37 +05:30
post v3_api("/projects/#{project.id}/builds/#{build.id}/erase", user)
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
context 'job is erasable' do
let(:build) { create(:ci_build, :trace, :artifacts, :success, project: project, pipeline: pipeline) }
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
it 'erases job content' do
2016-04-02 18:10:28 +05:30
expect(response.status).to eq 201
2017-08-17 22:00:37 +05:30
expect(build).not_to have_trace
2016-04-02 18:10:28 +05:30
expect(build.artifacts_file.exists?).to be_falsy
expect(build.artifacts_metadata.exists?).to be_falsy
end
2017-08-17 22:00:37 +05:30
it 'updates job' do
2016-04-02 18:10:28 +05:30
expect(build.reload.erased_at).to be_truthy
expect(build.reload.erased_by).to eq user
end
end
2017-08-17 22:00:37 +05:30
context 'job is not erasable' do
let(:build) { create(:ci_build, :trace, project: project, pipeline: pipeline) }
2016-04-02 18:10:28 +05:30
2016-09-13 17:45:13 +05:30
it 'responds with forbidden' do
2016-04-02 18:10:28 +05:30
expect(response.status).to eq 403
end
end
end
describe 'POST /projects/:id/builds/:build_id/artifacts/keep' do
before do
2017-08-17 22:00:37 +05:30
post v3_api("/projects/#{project.id}/builds/#{build.id}/artifacts/keep", user)
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
it 'keeps artifacts' do
expect(response.status).to eq 200
expect(build.reload.artifacts_expire_at).to be_nil
end
end
context 'no artifacts' do
let(:build) { create(:ci_build, project: project, pipeline: pipeline) }
it 'responds with not found' do
expect(response.status).to eq 404
end
end
end
2016-09-13 17:45:13 +05:30
describe 'POST /projects/:id/builds/:build_id/play' do
before do
2017-08-17 22:00:37 +05:30
post v3_api("/projects/#{project.id}/builds/#{build.id}/play", user)
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
context 'on an playable job' do
2016-09-13 17:45:13 +05:30
let(:build) { create(:ci_build, :manual, project: project, pipeline: pipeline) }
2017-08-17 22:00:37 +05:30
it 'plays the job' do
2016-09-13 17:45:13 +05:30
expect(response).to have_http_status 200
expect(json_response['user']['id']).to eq(user.id)
expect(json_response['id']).to eq(build.id)
end
end
2017-08-17 22:00:37 +05:30
context 'on a non-playable job' do
2016-09-13 17:45:13 +05:30
it 'returns a status code 400, Bad Request' do
expect(response).to have_http_status 400
2017-08-17 22:00:37 +05:30
expect(response.body).to match("Unplayable Job")
2016-09-13 17:45:13 +05:30
end
end
end
end