debian-mirror-gitlab/spec/services/projects/update_pages_service_spec.rb

271 lines
8.4 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require "spec_helper"
2020-07-28 23:09:34 +05:30
RSpec.describe Projects::UpdatePagesService do
2020-03-13 15:44:24 +05:30
let_it_be(:project, refind: true) { create(:project, :repository) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project, sha: project.commit('HEAD').sha) }
2020-02-01 01:16:34 +05:30
let(:build) { create(:ci_build, pipeline: pipeline, ref: 'HEAD') }
2018-11-08 19:23:39 +05:30
let(:invalid_file) { fixture_file_upload('spec/fixtures/dk.png') }
2018-03-17 18:26:18 +05:30
2019-02-02 18:00:53 +05:30
let(:file) { fixture_file_upload("spec/fixtures/pages.zip") }
let(:empty_file) { fixture_file_upload("spec/fixtures/pages_empty.zip") }
let(:metadata_filename) { "spec/fixtures/pages.zip.meta" }
let(:metadata) { fixture_file_upload(metadata_filename) if File.exist?(metadata_filename) }
2017-08-17 22:00:37 +05:30
subject { described_class.new(project, build) }
before do
project.remove_pages
end
2019-02-02 18:00:53 +05:30
context '::TMP_EXTRACT_PATH' do
subject { described_class::TMP_EXTRACT_PATH }
2018-03-17 18:26:18 +05:30
2019-02-02 18:00:53 +05:30
it { is_expected.not_to match(Gitlab::PathRegex.namespace_format_regex) }
end
2018-03-17 18:26:18 +05:30
context 'for new artifacts' do
context "for a valid job" do
2020-11-24 15:15:51 +05:30
let!(:artifacts_archive) { create(:ci_job_artifact, file: file, job: build) }
2017-08-17 22:00:37 +05:30
before do
2018-11-18 11:00:15 +05:30
create(:ci_job_artifact, file_type: :metadata, file_format: :gzip, file: metadata, job: build)
2018-03-17 18:26:18 +05:30
build.reload
2017-08-17 22:00:37 +05:30
end
describe 'pages artifacts' do
2018-10-15 14:42:47 +05:30
it "doesn't delete artifacts after deploying" do
expect(execute).to eq(:success)
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).to be_deployed
2018-10-15 14:42:47 +05:30
expect(build.artifacts?).to eq(true)
2017-08-17 22:00:37 +05:30
end
end
it 'succeeds' do
expect(project.pages_deployed?).to be_falsey
expect(execute).to eq(:success)
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).to be_deployed
2020-11-24 15:15:51 +05:30
expect(project.pages_metadatum.artifacts_archive).to eq(artifacts_archive)
2017-08-17 22:00:37 +05:30
expect(project.pages_deployed?).to be_truthy
2018-03-17 18:26:18 +05:30
# Check that all expected files are extracted
%w[index.html zero .hidden/file].each do |filename|
2020-10-24 23:57:45 +05:30
expect(File.exist?(File.join(project.pages_path, 'public', filename))).to be_truthy
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
end
2021-01-03 14:25:43 +05:30
it 'creates pages_deployment and saves it in the metadata' do
expect do
expect(execute).to eq(:success)
end.to change { project.pages_deployments.count }.by(1)
deployment = project.pages_deployments.last
expect(deployment.size).to eq(file.size)
expect(deployment.file).to be
expect(project.pages_metadatum.reload.pages_deployment_id).to eq(deployment.id)
end
it 'does not create deployment when zip_pages_deployments feature flag is disabled' do
stub_feature_flags(zip_pages_deployments: false)
expect do
expect(execute).to eq(:success)
end.not_to change { project.pages_deployments.count }
expect(project.pages_metadatum.reload.pages_deployment_id).to be_nil
end
2017-08-17 22:00:37 +05:30
it 'limits pages size' do
stub_application_setting(max_pages_size: 1)
expect(execute).not_to eq(:success)
end
it 'removes pages after destroy' do
expect(PagesWorker).to receive(:perform_in)
expect(project.pages_deployed?).to be_falsey
2020-10-24 23:57:45 +05:30
expect(Dir.exist?(File.join(project.pages_path))).to be_falsey
2019-12-21 20:55:43 +05:30
2017-08-17 22:00:37 +05:30
expect(execute).to eq(:success)
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).to be_deployed
2017-08-17 22:00:37 +05:30
expect(project.pages_deployed?).to be_truthy
2020-10-24 23:57:45 +05:30
expect(Dir.exist?(File.join(project.pages_path))).to be_truthy
2019-12-21 20:55:43 +05:30
2021-01-03 14:25:43 +05:30
project.destroy!
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
expect(Dir.exist?(File.join(project.pages_path))).to be_falsey
2019-12-21 20:55:43 +05:30
expect(ProjectPagesMetadatum.find_by_project_id(project)).to be_nil
2017-08-17 22:00:37 +05:30
end
it 'fails if sha on branch is not latest' do
2021-01-03 14:25:43 +05:30
build.update!(ref: 'feature')
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
expect(execute).not_to eq(:success)
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).not_to be_deployed
2020-04-08 14:13:33 +05:30
expect(deploy_status).to be_failed
expect(deploy_status.description).to eq('build SHA is outdated for this ref')
2017-08-17 22:00:37 +05:30
end
2018-05-09 12:01:36 +05:30
context 'when using empty file' do
let(:file) { empty_file }
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
it 'fails to extract' do
expect { execute }
.to raise_error(Projects::UpdatePagesService::FailedToExtractError)
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
2019-02-02 18:00:53 +05:30
context 'when using pages with non-writeable public' do
let(:file) { fixture_file_upload("spec/fixtures/pages_non_writeable.zip") }
context 'when using RubyZip' do
it 'succeeds to extract' do
expect(execute).to eq(:success)
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).to be_deployed
2019-02-02 18:00:53 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
context 'when timeout happens by DNS error' do
before do
2020-03-13 15:44:24 +05:30
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:extract_zip_archive!).and_raise(SocketError)
end
2018-03-17 18:26:18 +05:30
end
it 'raises an error' do
expect { execute }.to raise_error(SocketError)
build.reload
2018-05-01 15:08:00 +05:30
expect(deploy_status).to be_failed
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).not_to be_deployed
2018-05-01 15:08:00 +05:30
end
end
context 'when failed to extract zip artifacts' do
before do
2020-03-13 15:44:24 +05:30
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:extract_zip_archive!)
.and_raise(Projects::UpdatePagesService::FailedToExtractError)
end
2018-05-01 15:08:00 +05:30
end
it 'raises an error' do
expect { execute }
.to raise_error(Projects::UpdatePagesService::FailedToExtractError)
build.reload
expect(deploy_status).to be_failed
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).not_to be_deployed
2018-05-01 15:08:00 +05:30
end
end
context 'when missing artifacts metadata' do
before do
2018-10-15 14:42:47 +05:30
expect(build).to receive(:artifacts_metadata?).and_return(false)
2018-05-01 15:08:00 +05:30
end
2018-10-15 14:42:47 +05:30
it 'does not raise an error as failed job' do
2018-05-01 15:08:00 +05:30
execute
build.reload
expect(deploy_status).to be_failed
2019-12-21 20:55:43 +05:30
expect(project.pages_metadatum).not_to be_deployed
2018-03-17 18:26:18 +05:30
end
end
2020-06-23 00:09:42 +05:30
context 'with background jobs running', :sidekiq_inline do
2020-10-24 23:57:45 +05:30
it 'succeeds' do
expect(project.pages_deployed?).to be_falsey
expect(execute).to eq(:success)
2020-06-23 00:09:42 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
end
it 'fails to remove project pages when no pages is deployed' do
expect(PagesWorker).not_to receive(:perform_in)
expect(project.pages_deployed?).to be_falsey
2021-01-03 14:25:43 +05:30
project.destroy!
2017-08-17 22:00:37 +05:30
end
it 'fails if no artifacts' do
expect(execute).not_to eq(:success)
end
it 'fails for invalid archive' do
2019-09-04 21:01:54 +05:30
create(:ci_job_artifact, :archive, file: invalid_file, job: build)
2017-08-17 22:00:37 +05:30
expect(execute).not_to eq(:success)
end
2017-09-10 17:25:29 +05:30
describe 'maximum pages artifacts size' do
let(:metadata) { spy('metadata') }
before do
2018-11-08 19:23:39 +05:30
file = fixture_file_upload('spec/fixtures/pages.zip')
metafile = fixture_file_upload('spec/fixtures/pages.zip.meta')
2017-09-10 17:25:29 +05:30
2019-09-04 21:01:54 +05:30
create(:ci_job_artifact, :archive, file: file, job: build)
create(:ci_job_artifact, :metadata, file: metafile, job: build)
2017-09-10 17:25:29 +05:30
allow(build).to receive(:artifacts_metadata_entry)
.and_return(metadata)
end
context 'when maximum pages size is set to zero' do
before do
stub_application_setting(max_pages_size: 0)
end
2020-03-13 15:44:24 +05:30
it_behaves_like 'pages size limit is', ::Gitlab::Pages::MAX_SIZE
2017-09-10 17:25:29 +05:30
end
2020-03-13 15:44:24 +05:30
context 'when size is limited on the instance level' do
2017-09-10 17:25:29 +05:30
before do
2020-03-13 15:44:24 +05:30
stub_application_setting(max_pages_size: 100)
2017-09-10 17:25:29 +05:30
end
2020-03-13 15:44:24 +05:30
it_behaves_like 'pages size limit is', 100.megabytes
2017-09-10 17:25:29 +05:30
end
end
2020-02-01 01:16:34 +05:30
context 'when file size is spoofed' do
let(:metadata) { spy('metadata') }
include_context 'pages zip with spoofed size'
before do
file = fixture_file_upload(fake_zip_path, 'pages.zip')
metafile = fixture_file_upload('spec/fixtures/pages.zip.meta')
create(:ci_job_artifact, :archive, file: file, job: build)
create(:ci_job_artifact, :metadata, file: metafile, job: build)
allow(build).to receive(:artifacts_metadata_entry)
.and_return(metadata)
allow(metadata).to receive(:total_size).and_return(100)
end
it 'raises an error' do
expect do
subject.execute
end.to raise_error(Projects::UpdatePagesService::FailedToExtractError,
'Entry public/index.html should be 1B but is larger when inflated')
expect(deploy_status).to be_script_failure
end
end
2017-09-10 17:25:29 +05:30
def deploy_status
GenericCommitStatus.find_by(name: 'pages:deploy')
end
2017-08-17 22:00:37 +05:30
def execute
subject.execute[:status]
end
end