debian-mirror-gitlab/spec/requests/api/ci/jobs_spec.rb

900 lines
30 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
require 'spec_helper'
2021-10-27 15:23:28 +05:30
RSpec.describe API::Ci::Jobs do
2021-04-17 20:07:23 +05:30
include HttpBasicAuthHelpers
include DependencyProxyHelpers
2021-02-22 17:27:13 +05:30
using RSpec::Parameterized::TableSyntax
2018-05-09 12:01:36 +05:30
include HttpIOHelpers
2020-03-13 15:44:24 +05:30
let_it_be(:project, reload: true) do
2017-08-17 22:00:37 +05:30
create(:project, :repository, public_builds: false)
end
2020-03-13 15:44:24 +05:30
let_it_be(:pipeline, reload: true) do
2020-07-28 23:09:34 +05:30
create(:ci_pipeline, project: project,
sha: project.commit.id,
ref: project.default_branch)
2017-08-17 22:00:37 +05:30
end
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(:reporter) { create(:project_member, :reporter, project: project).user }
let(:guest) { create(:project_member, :guest, project: project).user }
2021-04-17 20:07:23 +05:30
let(:running_job) do
create(:ci_build, :running, project: project,
user: user,
pipeline: pipeline,
artifacts_expire_at: 1.day.since)
end
let!(:job) do
2022-10-11 01:57:18 +05:30
create(:ci_build, :success, :tags, pipeline: pipeline, artifacts_expire_at: 1.day.since)
2021-04-17 20:07:23 +05:30
end
2017-08-17 22:00:37 +05:30
before do
project.add_developer(user)
end
2021-04-17 20:07:23 +05:30
shared_examples 'returns common pipeline data' do
it 'returns common pipeline data' do
expect(json_response['pipeline']).not_to be_empty
expect(json_response['pipeline']['id']).to eq jobx.pipeline.id
expect(json_response['pipeline']['project_id']).to eq jobx.pipeline.project_id
expect(json_response['pipeline']['ref']).to eq jobx.pipeline.ref
expect(json_response['pipeline']['sha']).to eq jobx.pipeline.sha
expect(json_response['pipeline']['status']).to eq jobx.pipeline.status
end
end
shared_examples 'returns common job data' do
it 'returns common job data' do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['id']).to eq(jobx.id)
expect(json_response['status']).to eq(jobx.status)
expect(json_response['stage']).to eq(jobx.stage)
expect(json_response['name']).to eq(jobx.name)
expect(json_response['ref']).to eq(jobx.ref)
expect(json_response['tag']).to eq(jobx.tag)
expect(json_response['coverage']).to eq(jobx.coverage)
expect(json_response['allow_failure']).to eq(jobx.allow_failure)
expect(Time.parse(json_response['created_at'])).to be_like_time(jobx.created_at)
expect(Time.parse(json_response['started_at'])).to be_like_time(jobx.started_at)
expect(Time.parse(json_response['artifacts_expire_at'])).to be_like_time(jobx.artifacts_expire_at)
expect(json_response['artifacts_file']).to be_nil
expect(json_response['artifacts']).to be_an Array
expect(json_response['artifacts']).to be_empty
expect(json_response['web_url']).to be_present
end
end
shared_examples 'returns unauthorized' do
it 'returns unauthorized' do
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
describe 'GET /job' do
shared_context 'with auth headers' do
let(:headers_with_token) { header }
let(:params_with_token) { {} }
end
shared_context 'with auth params' do
let(:headers_with_token) { {} }
let(:params_with_token) { param }
end
shared_context 'without auth' do
let(:headers_with_token) { {} }
let(:params_with_token) { {} }
end
2022-10-11 01:57:18 +05:30
def perform_request
get api('/job'), headers: headers_with_token, params: params_with_token
end
2021-04-17 20:07:23 +05:30
before do |example|
unless example.metadata[:skip_before_request]
2022-10-11 01:57:18 +05:30
perform_request
2021-04-17 20:07:23 +05:30
end
end
2021-04-29 21:17:54 +05:30
context 'when token is valid but not CI_JOB_TOKEN' do
let(:token) { create(:personal_access_token, user: user) }
include_context 'with auth headers' do
let(:header) { { 'Private-Token' => token.token } }
end
it 'returns not found' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
2021-04-17 20:07:23 +05:30
context 'with job token authentication header' do
include_context 'with auth headers' do
2021-10-27 15:23:28 +05:30
let(:header) { { API::Ci::Helpers::Runner::JOB_TOKEN_HEADER => running_job.token } }
2021-04-17 20:07:23 +05:30
end
it_behaves_like 'returns common job data' do
let(:jobx) { running_job }
end
it 'returns specific job data' do
expect(json_response['finished_at']).to be_nil
end
2022-10-11 01:57:18 +05:30
it 'avoids N+1 queries', :skip_before_request do
control_count = ActiveRecord::QueryRecorder.new { perform_request }.count
running_job = create(:ci_build, :running, project: project, user: user, pipeline: pipeline, artifacts_expire_at: 1.day.since)
running_job.save!
expect { perform_request }.not_to exceed_query_limit(control_count)
end
2021-04-17 20:07:23 +05:30
it_behaves_like 'returns common pipeline data' do
let(:jobx) { running_job }
end
end
context 'with job token authentication params' do
include_context 'with auth params' do
let(:param) { { job_token: running_job.token } }
end
it_behaves_like 'returns common job data' do
let(:jobx) { running_job }
end
it 'returns specific job data' do
expect(json_response['finished_at']).to be_nil
end
it_behaves_like 'returns common pipeline data' do
let(:jobx) { running_job }
end
end
context 'with non running job' do
include_context 'with auth headers' do
2021-10-27 15:23:28 +05:30
let(:header) { { API::Ci::Helpers::Runner::JOB_TOKEN_HEADER => job.token } }
2021-04-17 20:07:23 +05:30
end
it_behaves_like 'returns unauthorized'
end
context 'with basic auth header' do
let(:personal_access_token) { create(:personal_access_token, user: user) }
2022-08-27 11:52:29 +05:30
let(:token) { personal_access_token.token }
2021-04-17 20:07:23 +05:30
include_context 'with auth headers' do
let(:header) { { Gitlab::Auth::AuthFinders::PRIVATE_TOKEN_HEADER => token } }
end
it 'does not return a job' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'without authentication' do
include_context 'without auth'
it_behaves_like 'returns unauthorized'
end
end
2021-12-11 22:18:48 +05:30
describe 'GET /job/allowed_agents' do
let_it_be(:group) { create(:group) }
let_it_be(:group_agent) { create(:cluster_agent, project: create(:project, group: group)) }
let_it_be(:group_authorization) { create(:agent_group_authorization, agent: group_agent, group: group) }
let_it_be(:project_agent) { create(:cluster_agent, project: project) }
before(:all) do
project.update!(group: group_authorization.group)
end
let(:implicit_authorization) { Clusters::Agents::ImplicitAuthorization.new(agent: project_agent) }
let(:headers) { { API::Ci::Helpers::Runner::JOB_TOKEN_HEADER => job.token } }
let(:job) { create(:ci_build, :artifacts, pipeline: pipeline, user: api_user, status: job_status) }
let(:job_status) { 'running' }
let(:params) { {} }
subject do
get api('/job/allowed_agents'), headers: headers, params: params
end
before do
subject
end
context 'when token is valid and user is authorized' do
shared_examples_for 'valid allowed_agents request' do
it 'returns agent info', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response.dig('job', 'id')).to eq(job.id)
expect(json_response.dig('pipeline', 'id')).to eq(job.pipeline_id)
expect(json_response.dig('project', 'id')).to eq(job.project_id)
expect(json_response.dig('project', 'groups')).to match_array([{ 'id' => group_authorization.group.id }])
expect(json_response.dig('user', 'id')).to eq(api_user.id)
expect(json_response.dig('user', 'username')).to eq(api_user.username)
expect(json_response.dig('user', 'roles_in_project')).to match_array %w(guest reporter developer)
expect(json_response).not_to include('environment')
2022-11-25 23:54:43 +05:30
expect(json_response['allowed_agents']).to match_array(
[
{
'id' => implicit_authorization.agent_id,
'config_project' => hash_including('id' => implicit_authorization.agent.project_id),
'configuration' => implicit_authorization.config
},
{
'id' => group_authorization.agent_id,
'config_project' => hash_including('id' => group_authorization.agent.project_id),
'configuration' => group_authorization.config
}
])
2021-12-11 22:18:48 +05:30
end
end
it_behaves_like 'valid allowed_agents request'
context 'when deployment' do
let(:job) { create(:ci_build, :artifacts, :with_deployment, environment: 'production', pipeline: pipeline, user: api_user, status: job_status) }
it 'includes environment slug' do
expect(json_response.dig('environment', 'slug')).to eq('production')
end
2022-10-11 01:57:18 +05:30
it 'includes environment tier' do
expect(json_response.dig('environment', 'tier')).to eq('production')
end
2021-12-11 22:18:48 +05:30
end
2022-07-23 23:45:48 +05:30
context 'when non-deployment environment action' do
let(:job) do
create(:environment, name: 'review', project_id: project.id)
create(:ci_build, :artifacts, :stop_review_app, environment: 'review', pipeline: pipeline, user: api_user, status: job_status)
end
it 'includes environment slug' do
expect(json_response.dig('environment', 'slug')).to eq('review')
end
2022-10-11 01:57:18 +05:30
it 'includes environment tier' do
expect(json_response.dig('environment', 'tier')).to eq('development')
end
2022-07-23 23:45:48 +05:30
end
2021-12-11 22:18:48 +05:30
context 'when passing the token as params' do
let(:headers) { {} }
let(:params) { { job_token: job.token } }
it_behaves_like 'valid allowed_agents request'
end
end
context 'when user is anonymous' do
let(:api_user) { nil }
it 'returns unauthorized' do
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when token is invalid because job has finished' do
let(:job_status) { 'success' }
it 'returns unauthorized' do
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when token is invalid' do
let(:headers) { { API::Ci::Helpers::Runner::JOB_TOKEN_HEADER => 'bad_token' } }
it 'returns unauthorized' do
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when token is valid but not CI_JOB_TOKEN' do
let(:token) { create(:personal_access_token, user: user) }
let(:headers) { { 'Private-Token' => token.token } }
it 'returns not found' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET /projects/:id/jobs' do
2020-11-24 15:15:51 +05:30
let(:query) { {} }
2016-04-02 18:10:28 +05:30
2018-03-17 18:26:18 +05:30
before do |example|
unless example.metadata[:skip_before_request]
2019-02-15 15:39:39 +05:30
get api("/projects/#{project.id}/jobs", api_user), params: query
2018-03-17 18:26:18 +05:30
end
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 project jobs' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
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
2018-11-08 19:23:39 +05:30
expect(Time.parse(json_response.first['artifacts_expire_at'])).to be_like_time(job.artifacts_expire_at)
2021-03-11 19:13:27 +05:30
expect(json_response.first['tag_list'].sort).to eq job.tag_list.sort
2016-06-22 15:30:34 +05:30
end
2018-11-20 20:47:30 +05:30
context 'without artifacts and trace' do
it 'returns no artifacts nor trace data' do
json_job = json_response.first
2021-12-11 22:18:48 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-11-20 20:47:30 +05:30
expect(json_job['artifacts_file']).to be_nil
expect(json_job['artifacts']).to be_an Array
expect(json_job['artifacts']).to be_empty
end
end
it_behaves_like 'a job with artifacts and trace' do
let(:api_endpoint) { "/projects/#{project.id}/jobs" }
end
2016-11-03 12:29:30 +05:30
it 'returns pipeline data' do
2017-09-10 17:25:29 +05:30
json_job = json_response.first
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(json_job['pipeline']).not_to be_empty
expect(json_job['pipeline']['id']).to eq job.pipeline.id
expect(json_job['pipeline']['ref']).to eq job.pipeline.ref
expect(json_job['pipeline']['sha']).to eq job.pipeline.sha
expect(json_job['pipeline']['status']).to eq job.pipeline.status
2016-11-03 12:29:30 +05:30
end
2018-03-17 18:26:18 +05:30
it 'avoids N+1 queries', :skip_before_request do
2018-11-20 20:47:30 +05:30
first_build = create(:ci_build, :trace_artifact, :artifacts, :test_reports, pipeline: pipeline)
2018-03-17 18:26:18 +05:30
first_build.runner = create(:ci_runner)
first_build.user = create(:user)
2021-04-29 21:17:54 +05:30
first_build.save!
2018-03-17 18:26:18 +05:30
control_count = ActiveRecord::QueryRecorder.new { go }.count
second_pipeline = create(:ci_empty_pipeline, project: project, sha: project.commit.id, ref: project.default_branch)
2018-11-20 20:47:30 +05:30
second_build = create(:ci_build, :trace_artifact, :artifacts, :test_reports, pipeline: second_pipeline)
2018-03-17 18:26:18 +05:30
second_build.runner = create(:ci_runner)
second_build.user = create(:user)
2021-04-29 21:17:54 +05:30
second_build.save!
2018-03-17 18:26:18 +05:30
expect { go }.not_to exceed_query_limit(control_count)
end
2016-04-02 18:10:28 +05:30
context 'filter project with one scope element' do
2017-08-17 22:00:37 +05:30
let(:query) { { 'scope' => 'pending' } }
2016-04-02 18:10:28 +05:30
it do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
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 'filter project with array of scope elements' do
2017-08-17 22:00:37 +05:30
let(:query) { { scope: %w(pending running) } }
2016-04-02 18:10:28 +05:30
it do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
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
2017-08-17 22:00:37 +05:30
let(:query) { { scope: %w(unknown running) } }
2020-04-08 14:13:33 +05:30
it { expect(response).to have_gitlab_http_status(:bad_request) }
end
end
context 'unauthorized user' do
2019-01-03 12:48:30 +05:30
context 'when user is not logged in' do
let(:api_user) { nil }
2019-01-03 12:48:30 +05:30
it 'does not return project jobs' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2019-01-03 12:48:30 +05:30
end
end
context 'when user is guest' do
let(:api_user) { guest }
it 'does not return project jobs' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2019-01-03 12:48:30 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
def go
2019-02-15 15:39:39 +05:30
get api("/projects/#{project.id}/jobs", api_user), params: query
2018-03-17 18:26:18 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe 'GET /projects/:id/jobs/:job_id' do
2018-11-20 20:47:30 +05:30
before do |example|
unless example.metadata[:skip_before_request]
get api("/projects/#{project.id}/jobs/#{job.id}", api_user)
end
2016-09-29 09:46:39 +05:30
end
2016-04-02 18:10:28 +05:30
context 'authorized user' do
2021-04-17 20:07:23 +05:30
it_behaves_like 'returns common job data' do
let(:jobx) { job }
end
2017-08-17 22:00:37 +05:30
it 'returns specific job data' do
2018-03-17 18:26:18 +05:30
expect(Time.parse(json_response['finished_at'])).to be_like_time(job.finished_at)
expect(json_response['duration']).to eq(job.duration)
end
2016-11-03 12:29:30 +05:30
2018-11-20 20:47:30 +05:30
it_behaves_like 'a job with artifacts and trace', result_is_array: false do
let(:api_endpoint) { "/projects/#{project.id}/jobs/#{second_job.id}" }
end
2021-04-17 20:07:23 +05:30
it_behaves_like 'returns common pipeline data' do
let(:jobx) { job }
2016-11-03 12:29:30 +05:30
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
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
context 'when job succeeded' do
it 'does not return failure_reason' do
2021-12-11 22:18:48 +05:30
get api("/projects/#{project.id}/jobs/#{job.id}", api_user)
2022-01-26 12:08:38 +05:30
expect(json_response).not_to include('failure_reason')
2018-03-17 18:26:18 +05:30
end
2016-08-24 12:49:21 +05:30
end
2022-01-26 12:08:38 +05:30
context 'when job failed' do
let(:job) do
create(:ci_build, :failed, :tags, pipeline: pipeline)
2021-01-03 14:25:43 +05:30
end
2022-01-26 12:08:38 +05:30
it 'returns failure_reason' do
get api("/projects/#{project.id}/jobs/#{job.id}", api_user)
2021-01-03 14:25:43 +05:30
2022-01-26 12:08:38 +05:30
expect(json_response).to include('failure_reason')
2016-08-24 12:49:21 +05:30
end
2016-04-02 18:10:28 +05:30
end
2019-02-15 15:39:39 +05:30
2022-01-26 12:08:38 +05:30
context 'when trace artifact record exists with no stored file', :skip_before_request do
2019-02-15 15:39:39 +05:30
before do
2022-01-26 12:08:38 +05:30
create(:ci_job_artifact, :unarchived_trace_artifact, job: job, project: job.project)
2019-02-15 15:39:39 +05:30
end
2022-01-26 12:08:38 +05:30
it 'returns no artifacts nor trace data' do
get api("/projects/#{project.id}/jobs/#{job.id}", api_user)
2019-02-15 15:39:39 +05:30
2022-01-26 12:08:38 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['artifacts']).to be_an Array
expect(json_response['artifacts'].size).to eq(1)
expect(json_response['artifacts'][0]['file_type']).to eq('trace')
expect(json_response['artifacts'][0]['filename']).to eq('job.log')
2019-02-15 15:39:39 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET /projects/:id/jobs/:job_id/trace' do
2016-08-24 12:49:21 +05:30
before do
2017-09-10 17:25:29 +05:30
get api("/projects/#{project.id}/jobs/#{job.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
2022-07-16 23:28:13 +05:30
context 'when log is in ObjectStorage' do
2018-05-09 12:01:36 +05:30
let!(:job) { create(:ci_build, :trace_artifact, pipeline: pipeline) }
2018-11-18 11:00:15 +05:30
let(:url) { 'http://object-storage/trace' }
let(:file_path) { expand_fixture_path('trace/sample_trace') }
2018-05-09 12:01:36 +05:30
before do
2018-11-18 11:00:15 +05:30
stub_remote_url_206(url, file_path)
2020-01-01 13:55:28 +05:30
allow_next_instance_of(JobArtifactUploader) do |instance|
allow(instance).to receive(:file_storage?) { false }
allow(instance).to receive(:url) { url }
allow(instance).to receive(:size) { File.size(file_path) }
end
2018-05-09 12:01:36 +05:30
end
2022-07-16 23:28:13 +05:30
it 'returns specific job logs' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-05-09 12:01:36 +05:30
expect(response.body).to eq(job.trace.raw)
end
end
2022-07-16 23:28:13 +05:30
context 'when log is artifact' do
2018-03-17 18:26:18 +05:30
let(:job) { create(:ci_build, :trace_artifact, pipeline: pipeline) }
2022-07-16 23:28:13 +05:30
it 'returns specific job log' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-17 18:26:18 +05:30
expect(response.body).to eq(job.trace.raw)
end
end
2022-07-16 23:28:13 +05:30
context 'when incremental logging and uploadless log artifact' do
2021-12-11 22:18:48 +05:30
let(:job) { create(:ci_build, :trace_live, :unarchived_trace_artifact, pipeline: pipeline) }
2022-07-16 23:28:13 +05:30
it 'returns specific job log' do
2021-12-11 22:18:48 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq(job.trace.raw)
end
end
2022-07-16 23:28:13 +05:30
context 'when log is incremental' do
2018-03-17 18:26:18 +05:30
let(:job) { create(:ci_build, :trace_live, pipeline: pipeline) }
2022-07-16 23:28:13 +05:30
it 'returns specific job log' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-17 18:26:18 +05:30
expect(response.body).to eq(job.trace.raw)
end
end
2021-12-11 22:18:48 +05:30
2022-07-16 23:28:13 +05:30
context 'when no log' do
2021-12-11 22:18:48 +05:30
let(:job) { create(:ci_build, pipeline: pipeline) }
2022-07-16 23:28:13 +05:30
it 'returns empty log' do
2021-12-11 22:18:48 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to be_empty
end
end
2022-07-16 23:28:13 +05:30
context 'when log artifact record exists with no stored file' do
2021-12-11 22:18:48 +05:30
let(:job) { create(:ci_build, pipeline: pipeline) }
before do
create(:ci_job_artifact, :unarchived_trace_artifact, job: job, project: job.project)
end
it 'returns empty trace' do
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to be_empty
end
end
end
context 'unauthorized user' do
2016-04-02 18:10:28 +05:30
let(:api_user) { nil }
2022-07-16 23:28:13 +05:30
it 'does not return specific job log' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
2021-02-22 17:27:13 +05:30
context 'when ci_debug_trace is set to true' do
before_all do
create(:ci_instance_variable, key: 'CI_DEBUG_TRACE', value: true)
end
where(:public_builds, :user_project_role, :expected_status) do
true | 'developer' | :ok
true | 'guest' | :forbidden
false | 'developer' | :ok
false | 'guest' | :forbidden
end
with_them do
before do
project.update!(public_builds: public_builds)
project.add_role(user, user_project_role)
get api("/projects/#{project.id}/jobs/#{job.id}/trace", api_user)
end
it 'renders trace to authorized users' do
expect(response).to have_gitlab_http_status(expected_status)
end
end
end
2023-01-13 00:05:48 +05:30
context 'when ci_debug_services is set to true' do
before_all do
create(:ci_instance_variable, key: 'CI_DEBUG_SERVICES', value: true)
end
where(:public_builds, :user_project_role, :expected_status) do
true | 'developer' | :ok
true | 'guest' | :forbidden
false | 'developer' | :ok
false | 'guest' | :forbidden
end
with_them do
before do
project.update!(public_builds: public_builds)
project.add_role(user, user_project_role)
get api("/projects/#{project.id}/jobs/#{job.id}/trace", api_user)
end
it 'renders successfully to authorized users' do
expect(response).to have_gitlab_http_status(expected_status)
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:id/jobs/:job_id/cancel' do
2016-09-29 09:46:39 +05:30
before do
2017-09-10 17:25:29 +05:30
post api("/projects/#{project.id}/jobs/#{job.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
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2018-03-17 18:26:18 +05:30
expect(project.builds.first.status).to eq('success')
end
end
2016-04-02 18:10:28 +05:30
context 'user without :update_build permission' do
2017-08-17 22:00:37 +05:30
let(:api_user) { reporter }
2017-08-17 22:00:37 +05:30
it 'does not cancel job' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
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
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:id/jobs/:job_id/retry' do
2017-09-10 17:25:29 +05:30
let(:job) { 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-09-10 17:25:29 +05:30
post api("/projects/#{project.id}/jobs/#{job.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
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
expect(project.builds.first.status).to eq('canceled')
expect(json_response['status']).to eq('pending')
end
end
2022-06-21 17:19:12 +05:30
context 'when a build is not retryable' do
let(:job) { create(:ci_build, :created, pipeline: pipeline) }
it 'responds with unprocessable entity' do
expect(json_response['message']).to eq('403 Forbidden - Job is not retryable')
expect(response).to have_gitlab_http_status(:forbidden)
end
end
2016-04-02 18:10:28 +05:30
context 'user without :update_build permission' do
2017-08-17 22:00:37 +05:30
let(:api_user) { reporter }
2017-08-17 22:00:37 +05:30
it 'does not retry job' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
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
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:id/jobs/:job_id/erase' do
2018-11-18 11:00:15 +05:30
let(:role) { :maintainer }
2018-03-17 18:26:18 +05:30
2016-04-02 18:10:28 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_role(user, role)
2016-04-02 18:10:28 +05:30
end
2022-07-23 23:45:48 +05:30
context 'when project is not undergoing stats refresh' do
before do
post api("/projects/#{project.id}/jobs/#{job.id}/erase", user)
2016-04-02 18:10:28 +05:30
end
2021-12-11 22:18:48 +05:30
2022-07-23 23:45:48 +05:30
shared_examples_for 'erases job' do
it 'erases job content' do
expect(response).to have_gitlab_http_status(:created)
expect(job.job_artifacts.count).to eq(0)
expect(job.trace.exist?).to be_falsy
expect(job.artifacts_file.present?).to be_falsy
expect(job.artifacts_metadata.present?).to be_falsy
expect(job.has_job_artifacts?).to be_falsy
end
end
context 'job is erasable' do
let(:job) { create(:ci_build, :trace_artifact, :artifacts, :test_reports, :success, project: project, pipeline: pipeline) }
2021-12-11 22:18:48 +05:30
2022-07-23 23:45:48 +05:30
it_behaves_like 'erases job'
2016-04-02 18:10:28 +05:30
2022-07-23 23:45:48 +05:30
it 'updates job' do
job.reload
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect(job.erased_at).to be_truthy
expect(job.erased_by).to eq(user)
end
2016-04-02 18:10:28 +05:30
end
2022-07-23 23:45:48 +05:30
context 'when job has an unarchived trace artifact' do
let(:job) { create(:ci_build, :success, :trace_live, :unarchived_trace_artifact, project: project, pipeline: pipeline) }
2021-12-11 22:18:48 +05:30
2022-07-23 23:45:48 +05:30
it_behaves_like 'erases job'
end
2021-12-11 22:18:48 +05:30
2022-07-23 23:45:48 +05:30
context 'job is not erasable' do
let(:job) { create(:ci_build, :trace_live, project: project, pipeline: pipeline) }
2016-04-02 18:10:28 +05:30
2022-07-23 23:45:48 +05:30
it 'responds with forbidden' do
expect(response).to have_gitlab_http_status(:forbidden)
end
2018-03-17 18:26:18 +05:30
end
2022-07-23 23:45:48 +05:30
context 'when a developer erases a build' do
let(:role) { :developer }
let(:job) { create(:ci_build, :trace_artifact, :artifacts, :success, project: project, pipeline: pipeline, user: owner) }
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
context 'when the build was created by the developer' do
let(:owner) { user }
it { expect(response).to have_gitlab_http_status(:created) }
end
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
context 'when the build was created by another user' do
let(:owner) { create(:user) }
it { expect(response).to have_gitlab_http_status(:forbidden) }
end
2018-03-17 18:26:18 +05:30
end
2022-07-23 23:45:48 +05:30
end
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
context 'when project is undergoing stats refresh' do
let(:job) { create(:ci_build, :trace_artifact, :artifacts, :test_reports, :success, project: project, pipeline: pipeline) }
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
it_behaves_like 'preventing request because of ongoing project stats refresh' do
let(:make_request) { post api("/projects/#{project.id}/jobs/#{job.id}/erase", user) }
it 'does not delete artifacts' do
make_request
expect(job.reload.job_artifacts).not_to be_empty
end
2016-04-02 18:10:28 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:id/jobs/:job_id/play' do
2022-05-07 20:08:51 +05:30
let(:params) { {} }
2016-09-13 17:45:13 +05:30
before do
2022-05-07 20:08:51 +05:30
post api("/projects/#{project.id}/jobs/#{job.id}/play", api_user), params: params
2016-09-13 17:45:13 +05:30
end
2021-03-08 18:12:59 +05:30
context 'on a playable job' do
2022-05-07 20:08:51 +05:30
let_it_be(:job) { create(:ci_build, :manual, project: project, pipeline: pipeline) }
2021-03-08 18:12:59 +05:30
before do
project.add_developer(user)
end
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
context 'when user is authorized to trigger a manual action' do
2021-03-08 18:12:59 +05:30
context 'that is a bridge' do
2022-05-07 20:08:51 +05:30
let_it_be(:job) { create(:ci_bridge, :playable, pipeline: pipeline, downstream: project) }
2021-03-08 18:12:59 +05:30
it 'plays the job' do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['user']['id']).to eq(user.id)
expect(json_response['id']).to eq(job.id)
expect(job.reload).to be_pending
end
end
context 'that is a build' do
it 'plays the job' do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['user']['id']).to eq(user.id)
expect(json_response['id']).to eq(job.id)
expect(job.reload).to be_pending
end
2017-08-17 22:00:37 +05:30
end
2022-05-07 20:08:51 +05:30
context 'when the user provides valid custom variables' do
let(:params) { { job_variables_attributes: [{ key: 'TEST_VAR', value: 'test' }] } }
it 'applies the variables to the job' do
expect(response).to have_gitlab_http_status(:ok)
expect(job.reload).to be_pending
expect(job.job_variables.map(&:key)).to contain_exactly('TEST_VAR')
expect(job.job_variables.map(&:value)).to contain_exactly('test')
end
end
context 'when the user provides a variable without a key' do
let(:params) { { job_variables_attributes: [{ value: 'test' }] } }
it 'reports that the key is missing' do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eq('job_variables_attributes[0][key] is missing')
expect(job.reload).to be_manual
end
end
context 'when the user provides a variable without a value' do
let(:params) { { job_variables_attributes: [{ key: 'TEST_VAR' }] } }
it 'reports that the value is missing' do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eq('job_variables_attributes[0][value] is missing')
expect(job.reload).to be_manual
end
end
context 'when the user provides both valid and invalid variables' do
let(:params) { { job_variables_attributes: [{ key: 'TEST_VAR', value: 'test' }, { value: 'test2' }] } }
it 'reports the invalid variables and does not run the job' do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eq('job_variables_attributes[1][key] is missing')
expect(job.reload).to be_manual
end
end
2017-08-17 22:00:37 +05:30
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
2017-09-10 17:25:29 +05:30
expect(job.reload).to be_manual
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
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
2017-09-10 17:25:29 +05:30
expect(job.reload).to be_manual
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2017-08-17 22:00:37 +05:30
end
end
2016-09-13 17:45:13 +05:30
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
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
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