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

272 lines
7.7 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require "spec_helper"
describe Projects::UpdatePagesService do
2018-03-17 18:26:18 +05:30
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-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
let(:extension) { 'zip' }
2018-11-08 19:23:39 +05:30
let(:file) { fixture_file_upload("spec/fixtures/pages.#{extension}") }
let(:empty_file) { fixture_file_upload("spec/fixtures/pages_empty.#{extension}") }
2018-03-17 18:26:18 +05:30
let(:metadata) do
2018-11-08 19:23:39 +05:30
filename = "spec/fixtures/pages.#{extension}.meta"
2018-03-17 18:26:18 +05:30
fixture_file_upload(filename) if File.exist?(filename)
end
2017-08-17 22:00:37 +05:30
subject { described_class.new(project, build) }
before do
project.remove_pages
end
2018-03-17 18:26:18 +05:30
context 'legacy artifacts' do
2018-05-09 12:01:36 +05:30
let(:extension) { 'zip' }
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
before do
build.update_attributes(legacy_artifacts_file: file)
build.update_attributes(legacy_artifacts_metadata: metadata)
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)
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
expect(build.reload.artifacts?).to eq(true)
2018-05-09 12:01:36 +05:30
end
end
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
it 'succeeds' do
expect(project.pages_deployed?).to be_falsey
expect(execute).to eq(:success)
expect(project.pages_deployed?).to be_truthy
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
# 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
end
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
it 'limits pages size' do
stub_application_setting(max_pages_size: 1)
expect(execute).not_to eq(:success)
end
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
it 'removes pages after destroy' do
expect(PagesWorker).to receive(:perform_in)
expect(project.pages_deployed?).to be_falsey
expect(execute).to eq(:success)
expect(project.pages_deployed?).to be_truthy
project.destroy
expect(project.pages_deployed?).to be_falsey
end
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
it 'fails if sha on branch is not latest' do
build.update_attributes(ref: 'feature')
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
expect(execute).not_to eq(:success)
end
it 'fails for empty file fails' do
build.update_attributes(legacy_artifacts_file: empty_file)
expect { execute }
.to raise_error(Projects::UpdatePagesService::FailedToExtractError)
2018-03-17 18:26:18 +05:30
end
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
context 'for new artifacts' do
context "for a valid job" do
2017-08-17 22:00:37 +05:30
before do
2018-03-17 18:26:18 +05:30
create(:ci_job_artifact, file: file, job: build)
create(:ci_job_artifact, file_type: :metadata, file: metadata, job: build)
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
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)
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|
expect(File.exist?(File.join(project.public_pages_path, filename))).to be_truthy
end
2017-08-17 22:00:37 +05:30
end
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
expect(execute).to eq(:success)
expect(project.pages_deployed?).to be_truthy
project.destroy
expect(project.pages_deployed?).to be_falsey
end
it 'fails if sha on branch is not latest' do
2018-03-17 18:26:18 +05:30
build.update_attributes(ref: 'feature')
2017-08-17 22:00:37 +05:30
expect(execute).not_to eq(:success)
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
context 'when timeout happens by DNS error' do
before do
allow_any_instance_of(described_class)
.to receive(:extract_zip_archive!).and_raise(SocketError)
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
end
end
context 'when failed to extract zip artifacts' do
before do
2018-10-15 14:42:47 +05:30
expect_any_instance_of(described_class)
2018-05-01 15:08:00 +05:30
.to receive(:extract_zip_archive!)
.and_raise(Projects::UpdatePagesService::FailedToExtractError)
end
it 'raises an error' do
expect { execute }
.to raise_error(Projects::UpdatePagesService::FailedToExtractError)
build.reload
expect(deploy_status).to be_failed
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
2018-03-17 18:26:18 +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
project.destroy
end
it 'fails if no artifacts' do
expect(execute).not_to eq(:success)
end
it 'fails for invalid archive' do
2018-03-17 18:26:18 +05:30
build.update_attributes(legacy_artifacts_file: invalid_file)
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
2018-03-17 18:26:18 +05:30
build.update_attributes(legacy_artifacts_file: file)
build.update_attributes(legacy_artifacts_metadata: metafile)
2017-09-10 17:25:29 +05:30
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/)
2018-03-17 18:26:18 +05:30
expect(deploy_status).to be_script_failure
2017-09-10 17:25:29 +05:30
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
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
GenericCommitStatus.find_by(name: 'pages:deploy')
end
2017-08-17 22:00:37 +05:30
def execute
subject.execute[:status]
end
end