2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-03 15:28:07 -05:00
|
|
|
require "spec_helper"
|
|
|
|
|
2016-01-15 06:21:52 -05:00
|
|
|
describe Projects::UpdatePagesService do
|
2017-11-24 03:23:56 -05:00
|
|
|
set(:project) { create(:project, :repository) }
|
|
|
|
set(:pipeline) { create(:ci_pipeline, project: project, sha: project.commit('HEAD').sha) }
|
|
|
|
set(:build) { create(:ci_build, pipeline: pipeline, ref: 'HEAD') }
|
2018-06-05 17:18:06 -04:00
|
|
|
let(:invalid_file) { fixture_file_upload('spec/fixtures/dk.png') }
|
2017-11-24 03:23:56 -05:00
|
|
|
|
2019-01-02 14:01:11 -05:00
|
|
|
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) }
|
2016-05-13 06:51:52 -04:00
|
|
|
|
2016-01-15 06:21:52 -05:00
|
|
|
subject { described_class.new(project, build) }
|
2015-11-03 15:28:07 -05:00
|
|
|
|
|
|
|
before do
|
2019-01-02 14:01:11 -05:00
|
|
|
stub_feature_flags(safezip_use_rubyzip: true)
|
|
|
|
|
2015-11-03 15:28:07 -05:00
|
|
|
project.remove_pages
|
|
|
|
end
|
|
|
|
|
2019-01-02 14:01:11 -05:00
|
|
|
context '::TMP_EXTRACT_PATH' do
|
|
|
|
subject { described_class::TMP_EXTRACT_PATH }
|
2017-11-24 03:23:56 -05:00
|
|
|
|
2019-01-02 14:01:11 -05:00
|
|
|
it { is_expected.not_to match(Gitlab::PathRegex.namespace_format_regex) }
|
|
|
|
end
|
|
|
|
|
2017-11-24 03:23:56 -05:00
|
|
|
context 'for new artifacts' do
|
|
|
|
context "for a valid job" do
|
2016-01-20 15:49:26 -05:00
|
|
|
before do
|
2017-11-24 03:23:56 -05:00
|
|
|
create(:ci_job_artifact, file: file, job: build)
|
2018-07-27 01:04:35 -04:00
|
|
|
create(:ci_job_artifact, file_type: :metadata, file_format: :gzip, file: metadata, job: build)
|
2017-11-24 03:23:56 -05:00
|
|
|
|
|
|
|
build.reload
|
2016-01-20 15:49:26 -05:00
|
|
|
end
|
2015-11-03 15:28:07 -05:00
|
|
|
|
2017-03-06 03:49:38 -05:00
|
|
|
describe 'pages artifacts' do
|
2018-04-27 13:30:52 -04:00
|
|
|
it "doesn't delete artifacts after deploying" do
|
|
|
|
expect(execute).to eq(:success)
|
2017-03-06 03:49:38 -05:00
|
|
|
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).to be_deployed
|
2018-04-27 13:30:52 -04:00
|
|
|
expect(build.artifacts?).to eq(true)
|
2017-03-05 17:14:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-20 15:49:26 -05:00
|
|
|
it 'succeeds' do
|
2016-02-19 14:12:56 -05:00
|
|
|
expect(project.pages_deployed?).to be_falsey
|
2016-01-20 15:49:26 -05:00
|
|
|
expect(execute).to eq(:success)
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).to be_deployed
|
2016-02-19 14:12:56 -05:00
|
|
|
expect(project.pages_deployed?).to be_truthy
|
2017-10-12 12:56:46 -04:00
|
|
|
|
|
|
|
# Check that all expected files are extracted
|
|
|
|
%w[index.html zero .hidden/file].each do |filename|
|
|
|
|
expect(File.exist?(File.join(project.public_pages_path, filename))).to be_truthy
|
|
|
|
end
|
2016-01-20 15:49:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits pages size' do
|
|
|
|
stub_application_setting(max_pages_size: 1)
|
2016-05-30 04:11:46 -04:00
|
|
|
expect(execute).not_to eq(:success)
|
2016-01-20 15:49:26 -05:00
|
|
|
end
|
2015-11-03 15:28:07 -05:00
|
|
|
|
2016-01-20 15:49:26 -05:00
|
|
|
it 'removes pages after destroy' do
|
|
|
|
expect(PagesWorker).to receive(:perform_in)
|
2016-02-19 14:12:56 -05:00
|
|
|
expect(project.pages_deployed?).to be_falsey
|
2019-09-25 05:06:04 -04:00
|
|
|
|
2016-01-20 15:49:26 -05:00
|
|
|
expect(execute).to eq(:success)
|
2019-09-25 05:06:04 -04:00
|
|
|
|
|
|
|
expect(project.pages_metadatum).to be_deployed
|
2016-02-19 14:12:56 -05:00
|
|
|
expect(project.pages_deployed?).to be_truthy
|
2019-09-25 05:06:04 -04:00
|
|
|
|
2016-01-20 15:49:26 -05:00
|
|
|
project.destroy
|
2019-09-25 05:06:04 -04:00
|
|
|
|
2016-02-19 14:12:56 -05:00
|
|
|
expect(project.pages_deployed?).to be_falsey
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(ProjectPagesMetadatum.find_by_project_id(project)).to be_nil
|
2016-01-20 15:49:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if sha on branch is not latest' do
|
2018-07-02 06:43:06 -04:00
|
|
|
build.update(ref: 'feature')
|
2017-11-24 03:23:56 -05:00
|
|
|
|
2016-05-30 04:11:46 -04:00
|
|
|
expect(execute).not_to eq(:success)
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).not_to be_deployed
|
2016-01-20 15:49:26 -05:00
|
|
|
end
|
|
|
|
|
2018-05-01 16:27:54 -04:00
|
|
|
context 'when using empty file' do
|
|
|
|
let(:file) { empty_file }
|
2017-11-24 03:23:56 -05:00
|
|
|
|
2018-05-01 16:27:54 -04:00
|
|
|
it 'fails to extract' do
|
|
|
|
expect { execute }
|
|
|
|
.to raise_error(Projects::UpdatePagesService::FailedToExtractError)
|
|
|
|
end
|
2016-01-20 15:49:26 -05:00
|
|
|
end
|
2018-03-05 14:37:09 -05:00
|
|
|
|
2019-01-02 14:01:11 -05:00
|
|
|
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
|
|
|
|
before do
|
|
|
|
stub_feature_flags(safezip_use_rubyzip: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds to extract' do
|
|
|
|
expect(execute).to eq(:success)
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).to be_deployed
|
2019-01-02 14:01:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-05 14:37:09 -05:00
|
|
|
context 'when timeout happens by DNS error' do
|
|
|
|
before do
|
2018-03-06 02:50:49 -05:00
|
|
|
allow_any_instance_of(described_class)
|
2018-03-05 14:37:09 -05:00
|
|
|
.to receive(:extract_zip_archive!).and_raise(SocketError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
2018-03-30 04:20:47 -04:00
|
|
|
expect { execute }.to raise_error(SocketError)
|
2018-03-06 04:17:37 -05:00
|
|
|
|
|
|
|
build.reload
|
2018-03-30 03:50:23 -04:00
|
|
|
expect(deploy_status).to be_failed
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).not_to be_deployed
|
2018-03-30 03:50:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when failed to extract zip artifacts' do
|
|
|
|
before do
|
2018-04-27 13:30:52 -04:00
|
|
|
expect_any_instance_of(described_class)
|
2018-03-30 03:50:23 -04:00
|
|
|
.to receive(:extract_zip_archive!)
|
|
|
|
.and_raise(Projects::UpdatePagesService::FailedToExtractError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
2018-03-30 04:20:47 -04:00
|
|
|
expect { execute }
|
|
|
|
.to raise_error(Projects::UpdatePagesService::FailedToExtractError)
|
2018-03-30 03:50:23 -04:00
|
|
|
|
|
|
|
build.reload
|
|
|
|
expect(deploy_status).to be_failed
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).not_to be_deployed
|
2018-03-30 03:50:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when missing artifacts metadata' do
|
|
|
|
before do
|
2018-04-27 13:30:52 -04:00
|
|
|
expect(build).to receive(:artifacts_metadata?).and_return(false)
|
2018-03-30 03:50:23 -04:00
|
|
|
end
|
|
|
|
|
2018-04-27 13:30:52 -04:00
|
|
|
it 'does not raise an error as failed job' do
|
2018-03-30 03:50:23 -04:00
|
|
|
execute
|
|
|
|
|
|
|
|
build.reload
|
|
|
|
expect(deploy_status).to be_failed
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).not_to be_deployed
|
2018-03-05 14:37:09 -05:00
|
|
|
end
|
|
|
|
end
|
2015-11-03 15:28:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-18 07:07:53 -05:00
|
|
|
it 'fails to remove project pages when no pages is deployed' do
|
2016-05-30 04:11:46 -04:00
|
|
|
expect(PagesWorker).not_to receive(:perform_in)
|
2016-02-19 14:12:56 -05:00
|
|
|
expect(project.pages_deployed?).to be_falsey
|
2015-12-18 07:07:53 -05:00
|
|
|
project.destroy
|
|
|
|
end
|
|
|
|
|
2015-11-03 15:28:07 -05:00
|
|
|
it 'fails if no artifacts' do
|
2016-05-30 04:11:46 -04:00
|
|
|
expect(execute).not_to eq(:success)
|
2015-11-03 15:28:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails for invalid archive' do
|
2019-03-22 08:38:45 -04:00
|
|
|
create(:ci_job_artifact, :archive, file: invalid_file, job: build)
|
2016-05-30 04:11:46 -04:00
|
|
|
expect(execute).not_to eq(:success)
|
2015-11-03 15:28:07 -05:00
|
|
|
end
|
2016-05-13 06:51:52 -04:00
|
|
|
|
2017-07-25 06:45:17 -04:00
|
|
|
describe 'maximum pages artifacts size' do
|
|
|
|
let(:metadata) { spy('metadata') }
|
|
|
|
|
|
|
|
before do
|
2018-06-05 17:18:06 -04:00
|
|
|
file = fixture_file_upload('spec/fixtures/pages.zip')
|
|
|
|
metafile = fixture_file_upload('spec/fixtures/pages.zip.meta')
|
2017-07-25 06:45:17 -04:00
|
|
|
|
2019-03-22 08:38:45 -04:00
|
|
|
create(:ci_job_artifact, :archive, file: file, job: build)
|
|
|
|
create(:ci_job_artifact, :metadata, file: metafile, job: build)
|
2017-07-25 06:45:17 -04:00
|
|
|
|
|
|
|
allow(build).to receive(:artifacts_metadata_entry)
|
|
|
|
.and_return(metadata)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'pages size limit exceeded' do
|
|
|
|
it 'limits the maximum size of gitlab pages' do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(deploy_status.description)
|
|
|
|
.to match(/artifacts for pages are too large/)
|
2017-09-05 02:10:34 -04:00
|
|
|
expect(deploy_status).to be_script_failure
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).not_to be_deployed
|
2017-07-25 06:45:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when maximum pages size is set to zero' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(max_pages_size: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page size does not exceed internal maximum' do
|
|
|
|
before do
|
|
|
|
allow(metadata).to receive(:total_size).and_return(200.megabytes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates pages correctly' do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(deploy_status.description).not_to be_present
|
2019-09-25 05:06:04 -04:00
|
|
|
expect(project.pages_metadatum).to be_deployed
|
2017-07-25 06:45:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pages size does exceed internal maximum' do
|
|
|
|
before do
|
|
|
|
allow(metadata).to receive(:total_size).and_return(2.terabytes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'pages size limit exceeded'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pages size is greater than max size setting' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(max_pages_size: 200)
|
|
|
|
allow(metadata).to receive(:total_size).and_return(201.megabytes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'pages size limit exceeded'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when max size setting is greater than internal max size' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(max_pages_size: 3.terabytes / 1.megabyte)
|
|
|
|
allow(metadata).to receive(:total_size).and_return(2.terabytes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'pages size limit exceeded'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_status
|
2017-07-25 07:47:03 -04:00
|
|
|
GenericCommitStatus.find_by(name: 'pages:deploy')
|
2017-07-25 06:45:17 -04:00
|
|
|
end
|
|
|
|
|
2016-01-15 06:21:52 -05:00
|
|
|
def execute
|
|
|
|
subject.execute[:status]
|
2015-11-03 15:28:07 -05:00
|
|
|
end
|
|
|
|
end
|