2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ::Ci::DestroyPipelineService do
|
2021-06-08 01:23:25 +05:30
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
let!(:pipeline) { create(:ci_pipeline, :success, project: project, sha: project.commit.id) }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
subject { described_class.new(project, user).execute(pipeline) }
|
|
|
|
|
|
|
|
context 'user is owner' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:user) { project.first_owner }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
it 'destroys the pipeline' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect { pipeline.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'clears the cache', :use_clean_rails_redis_caching do
|
2019-07-07 11:18:12 +05:30
|
|
|
create(:commit_status, :success, pipeline: pipeline, ref: pipeline.ref)
|
|
|
|
|
|
|
|
expect(project.pipeline_status.has_status?).to be_truthy
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
# We need to reset lazy_latest_pipeline cache to simulate a new request
|
|
|
|
BatchLoader::Executor.clear_current
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
# Need to use find to avoid memoization
|
|
|
|
expect(Project.find(project.id).pipeline_status.has_status?).to be_falsey
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'does not log an audit event' do
|
2020-11-24 15:15:51 +05:30
|
|
|
expect { subject }.not_to change { AuditEvent.count }
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the pipeline has jobs' do
|
|
|
|
let!(:build) { create(:ci_build, project: project, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'destroys associated jobs' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect { build.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys associated stages' do
|
|
|
|
stages = pipeline.stages
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(stages).to all(raise_error(ActiveRecord::RecordNotFound))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when job has artifacts' do
|
|
|
|
let!(:artifact) { create(:ci_job_artifact, :archive, job: build) }
|
|
|
|
|
|
|
|
it 'destroys associated artifacts' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect { artifact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
it 'inserts deleted objects for object storage files' do
|
|
|
|
expect { subject }.to change { Ci::DeletedObject.count }
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
context 'when job has trace chunks' do
|
|
|
|
let(:connection_params) { Gitlab.config.artifacts.object_store.connection.symbolize_keys }
|
|
|
|
let(:connection) { ::Fog::Storage.new(connection_params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_object_storage(connection_params: connection_params, remote_directory: 'artifacts')
|
|
|
|
stub_artifacts_object_storage
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:trace_chunk) { create(:ci_build_trace_chunk, :fog_with_data, build: build) }
|
|
|
|
|
|
|
|
it 'destroys associated trace chunks' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect { trace_chunk.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes data from object store' do
|
|
|
|
expect { subject }.to change { Ci::BuildTraceChunks::Fog.new.data(trace_chunk) }
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
context 'when pipeline is in cancelable state' do
|
|
|
|
before do
|
|
|
|
allow(pipeline).to receive(:cancelable?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cancels the pipeline' do
|
|
|
|
expect(pipeline).to receive(:cancel_running)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is not owner' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'raises an exception' do
|
|
|
|
expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|