diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-09-04 09:07:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-09-04 09:07:20 +0300 |
commit | 4e31f79b57eb8d5af7e2f9522b271a42bcf3a635 (patch) | |
tree | a9c4d4b4cfb0f8a0275d43fbd0dc5bb1d84aa2e5 /spec | |
parent | 03a2dce94a42ba978e60d02aeb0ee9909a0e947e (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/job_spec.rb | 18 | ||||
-rw-r--r-- | spec/lib/gitlab/pages_spec.rb | 40 | ||||
-rw-r--r-- | spec/models/ci/build_spec.rb | 81 | ||||
-rw-r--r-- | spec/services/projects/update_pages_service_spec.rb | 105 | ||||
-rw-r--r-- | spec/support/shared_examples/services/pages_size_limit_shared_examples.rb | 32 |
5 files changed, 155 insertions, 121 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/job_spec.rb b/spec/lib/gitlab/ci/config/entry/job_spec.rb index 49999b1070d..1a78d929871 100644 --- a/spec/lib/gitlab/ci/config/entry/job_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/job_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_composition do + using RSpec::Parameterized::TableSyntax + let(:entry) { described_class.new(config, name: :rspec) } it_behaves_like 'with inheritable CI config' do @@ -696,8 +698,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo end context 'with workflow rules' do - using RSpec::Parameterized::TableSyntax - where(:name, :has_workflow_rules?, :only, :rules, :result) do "uses default only" | false | nil | nil | { refs: %w[branches tags] } "uses user only" | false | %w[branches] | nil | { refs: %w[branches] } @@ -739,6 +739,20 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo end end + describe '#pages_job?', :aggregate_failures, feature_category: :pages do + where(:name, :result) do + :pages | true + :'pages:staging' | false + :'something:pages:else' | false + end + + with_them do + subject { described_class.new({}, name: name).pages_job? } + + it { is_expected.to eq(result) } + end + end + context 'when composed' do before do entry.compose! diff --git a/spec/lib/gitlab/pages_spec.rb b/spec/lib/gitlab/pages_spec.rb index 9f85efd56e6..20b7821f4d2 100644 --- a/spec/lib/gitlab/pages_spec.rb +++ b/spec/lib/gitlab/pages_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Pages do +RSpec.describe Gitlab::Pages, feature_category: :pages do using RSpec::Parameterized::TableSyntax let(:pages_secret) { SecureRandom.random_bytes(Gitlab::Pages::SECRET_LENGTH) } @@ -48,4 +48,42 @@ RSpec.describe Gitlab::Pages do it { is_expected.to eq(result) } end end + + describe '.multiple_versions_enabled_for?' do + context 'when project is nil' do + it 'returns false' do + expect(described_class.multiple_versions_enabled_for?(nil)).to eq(false) + end + end + + context 'when a project is given' do + let_it_be(:project) { create(:project) } + + where(:setting, :feature_flag, :license, :result) do + false | false | false | false + false | false | true | false + false | true | false | false + false | true | true | false + true | false | false | false + true | false | true | false + true | true | false | false + true | true | true | true + end + + with_them do + let_it_be(:project) { create(:project) } + + subject { described_class.multiple_versions_enabled_for?(project) } + + before do + stub_licensed_features(pages_multiple_versions: license) + stub_feature_flags(pages_multiple_versions_setting: feature_flag) + project.project_setting.update!(pages_multiple_versions_enabled: setting) + end + + # this feature is only available in EE + it { is_expected.to eq(result && Gitlab.ee?) } + end + end + end end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index f17cbd821c5..b95dec7195f 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' RSpec.describe Ci::Build, feature_category: :continuous_integration, factory_default: :keep do + using RSpec::Parameterized::TableSyntax include Ci::TemplateHelpers include AfterNextHelpers @@ -1493,8 +1494,6 @@ RSpec.describe Ci::Build, feature_category: :continuous_integration, factory_def end describe 'state transition metrics' do - using RSpec::Parameterized::TableSyntax - subject { build.send(event) } where(:state, :report_count, :trait) do @@ -2129,8 +2128,6 @@ RSpec.describe Ci::Build, feature_category: :continuous_integration, factory_def end describe '#ref_slug' do - using RSpec::Parameterized::TableSyntax - where(:ref, :slug) do 'master' | 'master' '1-foo' | '1-foo' @@ -4025,73 +4022,61 @@ RSpec.describe Ci::Build, feature_category: :continuous_integration, factory_def end end - describe 'pages deployments' do - let_it_be(:build, reload: true) { create(:ci_build, pipeline: pipeline, user: user) } + describe '#pages_generator?', feature_category: :pages do + where(:name, :enabled, :result) do + 'foo' | false | false + 'pages' | false | false + 'pages:preview' | true | false + 'pages' | true | true + end - context 'when job is "pages"' do + with_them do before do - build.name = 'pages' + stub_pages_setting(enabled: enabled) + build.update!(name: name) end - context 'when pages are enabled' do - before do - allow(Gitlab.config.pages).to receive_messages(enabled: true) - end - - it 'is marked as pages generator' do - expect(build).to be_pages_generator - end - - context 'job succeeds' do - it "calls pages worker" do - expect(PagesWorker).to receive(:perform_async).with(:deploy, build.id) + subject { build.pages_generator? } - build.success! - end - end + it { is_expected.to eq(result) } + end + end - context 'job fails' do - it "does not call pages worker" do - expect(PagesWorker).not_to receive(:perform_async) + describe 'pages deployments', feature_category: :pages do + let_it_be(:build, reload: true) { create(:ci_build, name: 'pages', pipeline: pipeline, user: user) } - build.drop! - end - end + context 'when pages are enabled' do + before do + stub_pages_setting(enabled: true) end - context 'when pages are disabled' do - before do - allow(Gitlab.config.pages).to receive_messages(enabled: false) - end + context 'and job succeeds' do + it "calls pages worker" do + expect(PagesWorker).to receive(:perform_async).with(:deploy, build.id) - it 'is not marked as pages generator' do - expect(build).not_to be_pages_generator + build.success! end + end - context 'job succeeds' do - it "does not call pages worker" do - expect(PagesWorker).not_to receive(:perform_async) + context 'and job fails' do + it "does not call pages worker" do + expect(PagesWorker).not_to receive(:perform_async) - build.success! - end + build.drop! end end end - context 'when job is not "pages"' do + context 'when pages are disabled' do before do - build.name = 'other-job' - end - - it 'is not marked as pages generator' do - expect(build).not_to be_pages_generator + stub_pages_setting(enabled: false) end - context 'job succeeds' do + context 'and job succeeds' do it "does not call pages worker" do expect(PagesWorker).not_to receive(:perform_async) - build.success + build.success! end end end diff --git a/spec/services/projects/update_pages_service_spec.rb b/spec/services/projects/update_pages_service_spec.rb index a113f3506e1..a250be0c94f 100644 --- a/spec/services/projects/update_pages_service_spec.rb +++ b/spec/services/projects/update_pages_service_spec.rb @@ -20,13 +20,45 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do let(:custom_root_file_metadata) { "spec/fixtures/pages_with_custom_root.zip.meta" } let(:metadata) { fixture_file_upload(metadata_filename) if File.exist?(metadata_filename) } - subject { described_class.new(project, build) } + subject(:service) { described_class.new(project, build) } + + RSpec.shared_examples 'pages size limit is' do |size_limit| + context "when size is below the limit" do + before do + allow(metadata).to receive(:total_size).and_return(size_limit - 1.megabyte) + allow(metadata).to receive(:entries).and_return([]) + end + + it 'updates pages correctly' do + subject.execute + + deploy_status = GenericCommitStatus.last + expect(deploy_status.description).not_to be_present + expect(project.pages_metadatum).to be_deployed + end + end + + context "when size is above the limit" do + before do + allow(metadata).to receive(:total_size).and_return(size_limit + 1.megabyte) + allow(metadata).to receive(:entries).and_return([]) + end + + it 'limits the maximum size of gitlab pages' do + subject.execute + + deploy_status = GenericCommitStatus.last + expect(deploy_status.description).to match(/artifacts for pages are too large/) + expect(deploy_status).to be_script_failure + end + end + end context 'when a deploy stage already exists', :aggregate_failures do let!(:stage) { create(:ci_stage, name: 'deploy', pipeline: pipeline) } it 'assigns the deploy stage' do - expect { subject.execute } + expect { service.execute } .to change(GenericCommitStatus, :count).by(1) .and change(Ci::Stage.where(name: 'deploy'), :count).by(0) @@ -41,7 +73,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do context 'when a deploy stage does not exists' do it 'assigns the deploy stage' do - expect { subject.execute } + expect { service.execute } .to change(GenericCommitStatus, :count).by(1) .and change(Ci::Stage.where(name: 'deploy'), :count).by(1) @@ -64,7 +96,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do end it "doesn't delete artifacts after deploying" do - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) expect(project.pages_metadatum).to be_deployed expect(build.artifacts?).to eq(true) @@ -72,7 +104,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do it 'succeeds' do expect(project.pages_deployed?).to be_falsey - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) expect(project.pages_metadatum).to be_deployed expect(project.pages_deployed?).to be_truthy end @@ -84,12 +116,12 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do root_namespace_id: project.root_namespace.id } - expect { subject.execute }.to publish_event(Pages::PageDeployedEvent).with(expected_data) + expect { service.execute }.to publish_event(Pages::PageDeployedEvent).with(expected_data) end it 'creates pages_deployment and saves it in the metadata' do expect do - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) end.to change { project.pages_deployments.count }.by(1) deployment = project.pages_deployments.last @@ -108,7 +140,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do project.reload expect do - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) end.to change { project.pages_deployments.count }.by(1) expect(project.pages_metadatum.reload.pages_deployment).to eq(project.pages_deployments.last) @@ -127,12 +159,12 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do ) ) - execute + service.execute end it 'removes older deployments', :sidekiq_inline do expect do - execute + service.execute end.not_to change { PagesDeployment.count } # it creates one and deletes one expect(PagesDeployment.find_by_id(old_deployment.id)).to be_nil @@ -144,7 +176,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do let(:metadata_filename) { empty_metadata_filename } it 'returns an error' do - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) expect(GenericCommitStatus.last.description).to eq("Error: You need to either include a `public/` folder in your artifacts, or specify which one to use for Pages using `publish` in `.gitlab-ci.yml`") end @@ -158,7 +190,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do let(:options) { { publish: 'foo' } } it 'creates pages_deployment and saves it in the metadata' do - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) deployment = project.pages_deployments.last expect(deployment.root_directory).to eq(options[:publish]) @@ -169,7 +201,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do let(:options) { { publish: 'bar' } } it 'returns an error' do - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) expect(GenericCommitStatus.last.description).to eq("Error: You need to either include a `public/` folder in your artifacts, or specify which one to use for Pages using `publish` in `.gitlab-ci.yml`") end @@ -181,7 +213,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do let(:metadata_filename) { "spec/fixtures/pages.zip.meta" } it 'returns an error' do - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) expect(GenericCommitStatus.last.description).to eq("Error: You need to either include a `public/` folder in your artifacts, or specify which one to use for Pages using `publish` in `.gitlab-ci.yml`") end @@ -190,13 +222,13 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do it 'limits pages size' do stub_application_setting(max_pages_size: 1) - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) end it 'limits pages file count' do create(:plan_limits, :default_plan, pages_file_entries: 2) - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) expect(GenericCommitStatus.last.description).to eq("pages site contains 3 file entries, while limit is set to 2") end @@ -209,9 +241,11 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do end it 'raises an error' do - expect { execute }.to raise_error(SocketError) + expect { service.execute }.to raise_error(SocketError) build.reload + + deploy_status = GenericCommitStatus.last expect(deploy_status).to be_failed expect(project.pages_metadatum).not_to be_deployed end @@ -223,9 +257,11 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do end it 'does not raise an error as failed job' do - execute + service.execute build.reload + + deploy_status = GenericCommitStatus.last expect(deploy_status).to be_failed expect(project.pages_metadatum).not_to be_deployed end @@ -234,7 +270,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do context 'with background jobs running', :sidekiq_inline do it 'succeeds' do expect(project.pages_deployed?).to be_falsey - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) end end @@ -249,7 +285,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do shared_examples 'successfully deploys' do it 'succeeds' do expect do - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) end.to change { project.pages_deployments.count }.by(1) deployment = project.pages_deployments.last @@ -261,7 +297,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do context 'when old deployment present' do before do - old_build = create(:ci_build, pipeline: old_pipeline, ref: 'HEAD') + old_build = create(:ci_build, name: 'pages', pipeline: old_pipeline, ref: 'HEAD') old_deployment = create(:pages_deployment, ci_build: old_build, project: project) project.update_pages_deployment!(old_deployment) end @@ -272,15 +308,16 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do context 'when newer deployment present' do before do new_pipeline = create(:ci_pipeline, project: project, sha: project.commit('HEAD').sha) - new_build = create(:ci_build, pipeline: new_pipeline, ref: 'HEAD') + new_build = create(:ci_build, name: 'pages', pipeline: new_pipeline, ref: 'HEAD') new_deployment = create(:pages_deployment, ci_build: new_build, project: project) project.update_pages_deployment!(new_deployment) end it 'fails with outdated reference message' do - expect(execute).to eq(:error) + expect(service.execute[:status]).to eq(:error) expect(project.reload.pages_metadatum).not_to be_deployed + deploy_status = GenericCommitStatus.last expect(deploy_status).to be_failed expect(deploy_status.description).to eq('build SHA is outdated for this ref') end @@ -294,7 +331,7 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do .and_return(file.size + 1) end - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) expect(GenericCommitStatus.last.description).to eq('The uploaded artifact size does not match the expected value') project.pages_metadatum.reload @@ -318,18 +355,18 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do it 'fails with exception raised' do expect do - execute + service.execute end.to raise_error("Validation failed: File sha256 can't be blank") end end it 'fails if no artifacts' do - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) end it 'fails for invalid archive' do create(:ci_job_artifact, :archive, file: invalid_file, job: build) - expect(execute).not_to eq(:success) + expect(service.execute[:status]).not_to eq(:success) end describe 'maximum pages artifacts size' do @@ -383,21 +420,13 @@ RSpec.describe Projects::UpdatePagesService, feature_category: :pages do end it 'marks older pages:deploy jobs retried' do - expect(execute).to eq(:success) + expect(service.execute[:status]).to eq(:success) expect(older_deploy_job.reload).to be_retried + + deploy_status = GenericCommitStatus.last expect(deploy_status.ci_stage).to eq(stage) expect(deploy_status.stage_idx).to eq(stage.position) end end - - private - - def deploy_status - GenericCommitStatus.where(name: 'pages:deploy').last - end - - def execute - subject.execute[:status] - end end diff --git a/spec/support/shared_examples/services/pages_size_limit_shared_examples.rb b/spec/support/shared_examples/services/pages_size_limit_shared_examples.rb deleted file mode 100644 index d9e906ebb75..00000000000 --- a/spec/support/shared_examples/services/pages_size_limit_shared_examples.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_examples 'pages size limit is' do |size_limit| - context "when size is below the limit" do - before do - allow(metadata).to receive(:total_size).and_return(size_limit - 1.megabyte) - allow(metadata).to receive(:entries).and_return([]) - end - - it 'updates pages correctly' do - subject.execute - - expect(deploy_status.description).not_to be_present - expect(project.pages_metadatum).to be_deployed - end - end - - context "when size is above the limit" do - before do - allow(metadata).to receive(:total_size).and_return(size_limit + 1.megabyte) - allow(metadata).to receive(:entries).and_return([]) - end - - it 'limits the maximum size of gitlab pages' do - subject.execute - - expect(deploy_status.description) - .to match(/artifacts for pages are too large/) - expect(deploy_status).to be_script_failure - end - end -end |