diff options
Diffstat (limited to 'spec/lib')
15 files changed, 442 insertions, 26 deletions
diff --git a/spec/lib/banzai/reference_parser/mentioned_user_parser_spec.rb b/spec/lib/banzai/reference_parser/mentioned_user_parser_spec.rb new file mode 100644 index 00000000000..1be279375bd --- /dev/null +++ b/spec/lib/banzai/reference_parser/mentioned_user_parser_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Banzai::ReferenceParser::MentionedUserParser do + include ReferenceParserHelpers + + let(:group) { create(:group, :private) } + let(:user) { create(:user) } + let(:new_user) { create(:user) } + let(:project) { create(:project, group: group, creator: user) } + let(:link) { empty_html_link } + + subject { described_class.new(Banzai::RenderContext.new(project, new_user)) } + + describe '#gather_references' do + context 'when the link has a data-group attribute' do + context 'using an existing group ID' do + before do + link['data-group'] = project.group.id.to_s + group.add_developer(new_user) + end + + it 'returns empty list of users' do + expect(subject.gather_references([link])).to eq([]) + end + end + end + + context 'when the link has a data-project attribute' do + context 'using an existing project ID' do + before do + link['data-project'] = project.id.to_s + project.add_developer(new_user) + end + + it 'returns empty list of users' do + expect(subject.gather_references([link])).to eq([]) + end + end + end + + context 'when the link has a data-user attribute' do + it 'returns an Array of users' do + link['data-user'] = user.id.to_s + + expect(subject.referenced_by([link])).to eq([user]) + end + end + end +end diff --git a/spec/lib/banzai/reference_parser/mentioned_users_by_group_parser_spec.rb b/spec/lib/banzai/reference_parser/mentioned_users_by_group_parser_spec.rb new file mode 100644 index 00000000000..99d607629eb --- /dev/null +++ b/spec/lib/banzai/reference_parser/mentioned_users_by_group_parser_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Banzai::ReferenceParser::MentionedUsersByGroupParser do + include ReferenceParserHelpers + + let(:group) { create(:group, :private) } + let(:user) { create(:user) } + let(:new_user) { create(:user) } + let(:project) { create(:project, group: group, creator: user) } + let(:link) { empty_html_link } + + subject { described_class.new(Banzai::RenderContext.new(project, new_user)) } + + describe '#gather_references' do + context 'when the link has a data-group attribute' do + context 'using an existing group ID where user does not have access' do + it 'returns empty array' do + link['data-group'] = project.group.id.to_s + + expect(subject.gather_references([link])).to eq([]) + end + end + + context 'using an existing group ID' do + before do + link['data-group'] = project.group.id.to_s + group.add_developer(new_user) + end + + it 'returns groups' do + expect(subject.gather_references([link])).to eq([group]) + end + end + + context 'using a non-existing group ID' do + it 'returns an empty Array' do + link['data-group'] = 'test-non-existing' + + expect(subject.gather_references([link])).to eq([]) + end + end + end + end +end diff --git a/spec/lib/banzai/reference_parser/mentioned_users_by_project_parser_spec.rb b/spec/lib/banzai/reference_parser/mentioned_users_by_project_parser_spec.rb new file mode 100644 index 00000000000..155f2189d9e --- /dev/null +++ b/spec/lib/banzai/reference_parser/mentioned_users_by_project_parser_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Banzai::ReferenceParser::MentionedUsersByProjectParser do + include ReferenceParserHelpers + + let(:group) { create(:group, :private) } + let(:user) { create(:user) } + let(:new_user) { create(:user) } + let(:project) { create(:project, group: group, creator: user) } + let(:link) { empty_html_link } + + subject { described_class.new(Banzai::RenderContext.new(project, new_user)) } + + describe '#gather_references' do + context 'when the link has a data-project attribute' do + context 'using an existing project ID where user does not have access' do + it 'returns empty Array' do + link['data-project'] = project.id.to_s + + expect(subject.gather_references([link])).to eq([]) + end + end + + context 'using an existing project ID' do + before do + link['data-project'] = project.id.to_s + project.add_developer(new_user) + end + + it 'returns an Array of referenced projects' do + expect(subject.gather_references([link])).to eq([project]) + end + end + + context 'using a non-existing project ID' do + it 'returns an empty Array' do + link['data-project'] = 'inexisting-project-id' + + expect(subject.gather_references([link])).to eq([]) + end + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/edge_stages_injector_spec.rb b/spec/lib/gitlab/ci/config/edge_stages_injector_spec.rb new file mode 100644 index 00000000000..042f9b591b6 --- /dev/null +++ b/spec/lib/gitlab/ci/config/edge_stages_injector_spec.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +describe Gitlab::Ci::Config::EdgeStagesInjector do + describe '#call' do + subject { described_class.new(config).to_hash } + + context 'without stages' do + let(:config) do + { + test: { script: 'test' } + } + end + + it { is_expected.to match config } + end + + context 'with values' do + let(:config) do + { + stages: %w[stage1 stage2], + test: { script: 'test' } + } + end + + let(:expected_stages) do + %w[.pre stage1 stage2 .post] + end + + it { is_expected.to match(config.merge(stages: expected_stages)) } + end + + context 'with bad values' do + let(:config) do + { + stages: 'stage1', + test: { script: 'test' } + } + end + + it { is_expected.to match(config) } + end + + context 'with collision values' do + let(:config) do + { + stages: %w[.post stage1 .pre .post stage2], + test: { script: 'test' } + } + end + + let(:expected_stages) do + %w[.pre stage1 stage2 .post] + end + + it { is_expected.to match(config.merge(stages: expected_stages)) } + end + + context 'with types' do + let(:config) do + { + types: %w[stage1 stage2], + test: { script: 'test' } + } + end + + let(:expected_config) do + { + types: %w[.pre stage1 stage2 .post], + test: { script: 'test' } + } + end + + it { is_expected.to match expected_config } + end + + context 'with types' do + let(:config) do + { + types: %w[.post stage1 .pre .post stage2], + test: { script: 'test' } + } + end + + let(:expected_config) do + { + types: %w[.pre stage1 stage2 .post], + test: { script: 'test' } + } + end + + it { is_expected.to match expected_config } + end + end + + describe '.wrap_stages' do + subject { described_class.wrap_stages(stages) } + + context 'with empty value' do + let(:stages) {} + + it { is_expected.to eq %w[.pre .post] } + end + + context 'with values' do + let(:stages) { %w[s1 .pre] } + + it { is_expected.to eq %w[.pre s1 .post] } + end + end +end diff --git a/spec/lib/gitlab/ci/config/entry/root_spec.rb b/spec/lib/gitlab/ci/config/entry/root_spec.rb index 968dbb9c7f2..7e1a80414d4 100644 --- a/spec/lib/gitlab/ci/config/entry/root_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/root_spec.rb @@ -215,7 +215,7 @@ describe Gitlab::Ci::Config::Entry::Root do describe '#stages_value' do it 'returns an array of root stages' do - expect(root.stages_value).to eq %w[build test deploy] + expect(root.stages_value).to eq %w[.pre build test deploy .post] end end diff --git a/spec/lib/gitlab/ci/config/entry/stages_spec.rb b/spec/lib/gitlab/ci/config/entry/stages_spec.rb index 97970522104..3e6ff8eca28 100644 --- a/spec/lib/gitlab/ci/config/entry/stages_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/stages_spec.rb @@ -42,7 +42,7 @@ describe Gitlab::Ci::Config::Entry::Stages do describe '.default' do it 'returns default stages' do - expect(described_class.default).to eq %w[build test deploy] + expect(described_class.default).to eq %w[.pre build test deploy .post] end end end diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb index 68c38644b5c..b254f9af2f1 100644 --- a/spec/lib/gitlab/ci/config_spec.rb +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -51,6 +51,54 @@ describe Gitlab::Ci::Config do end end end + + describe '#stages' do + subject(:subject) { config.stages } + + context 'with default stages' do + let(:default_stages) do + %w[.pre build test deploy .post] + end + + it { is_expected.to eq default_stages } + end + + context 'with custom stages' do + let(:yml) do + <<-EOS + stages: + - stage1 + - stage2 + job1: + stage: stage1 + script: + - ls + EOS + end + + it { is_expected.to eq %w[.pre stage1 stage2 .post] } + end + + context 'with feature disabled' do + before do + stub_feature_flags(ci_pre_post_pipeline_stages: false) + end + + let(:yml) do + <<-EOS + stages: + - stage1 + - stage2 + job1: + stage: stage1 + script: + - ls + EOS + end + + it { is_expected.to eq %w[stage1 stage2] } + end + end end context 'when using extendable hash' do diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index d43eb4e4b4a..cb5ebde16d7 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -26,7 +26,7 @@ module Gitlab it 'returns valid build attributes' do expect(subject).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "rspec", options: { before_script: ["pwd"], @@ -56,7 +56,7 @@ module Gitlab it 'returns valid build attributes' do expect(subject).to eq({ stage: 'test', - stage_idx: 1, + stage_idx: 2, name: 'rspec', options: { script: ['rspec'] }, rules: [ @@ -209,13 +209,16 @@ module Gitlab end let(:attributes) do - [{ name: "build", + [{ name: ".pre", index: 0, builds: [] }, - { name: "test", + { name: "build", index: 1, + builds: [] }, + { name: "test", + index: 2, builds: - [{ stage_idx: 1, + [{ stage_idx: 2, stage: "test", name: "rspec", allow_failure: false, @@ -225,9 +228,9 @@ module Gitlab only: { refs: ["branches"] }, except: {} }] }, { name: "deploy", - index: 2, + index: 3, builds: - [{ stage_idx: 2, + [{ stage_idx: 3, stage: "deploy", name: "prod", allow_failure: false, @@ -235,7 +238,10 @@ module Gitlab yaml_variables: [], options: { script: ["cap prod"] }, only: { refs: ["tags"] }, - except: {} }] }] + except: {} }] }, + { name: ".post", + index: 4, + builds: [] }] end it 'returns stages seed attributes' do @@ -425,7 +431,7 @@ module Gitlab expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").first).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "rspec", options: { before_script: ["pwd"], @@ -456,7 +462,7 @@ module Gitlab expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").first).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "rspec", options: { before_script: ["pwd"], @@ -485,7 +491,7 @@ module Gitlab expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").first).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "rspec", options: { before_script: ["pwd"], @@ -510,7 +516,7 @@ module Gitlab expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").first).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "rspec", options: { before_script: ["pwd"], @@ -977,7 +983,7 @@ module Gitlab expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").first).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "rspec", options: { before_script: ["pwd"], @@ -1272,7 +1278,7 @@ module Gitlab expect(subject.builds.size).to eq(5) expect(subject.builds[0]).to eq( stage: "build", - stage_idx: 0, + stage_idx: 1, name: "build1", options: { script: ["test"] @@ -1283,7 +1289,7 @@ module Gitlab ) expect(subject.builds[2]).to eq( stage: "test", - stage_idx: 1, + stage_idx: 2, name: "test1", options: { script: ["test"], @@ -1398,7 +1404,7 @@ module Gitlab expect(subject.size).to eq(1) expect(subject.first).to eq({ stage: "test", - stage_idx: 1, + stage_idx: 2, name: "normal_job", options: { script: ["test"] @@ -1442,7 +1448,7 @@ module Gitlab expect(subject.size).to eq(2) expect(subject.first).to eq({ stage: "build", - stage_idx: 0, + stage_idx: 1, name: "job1", options: { script: ["execute-script-for-job"] @@ -1453,7 +1459,7 @@ module Gitlab }) expect(subject.second).to eq({ stage: "build", - stage_idx: 0, + stage_idx: 1, name: "job2", options: { script: ["execute-script-for-job"] @@ -1665,14 +1671,14 @@ module Gitlab config = YAML.dump({ rspec: { script: "test", type: "acceptance" } }) expect do Gitlab::Ci::YamlProcessor.new(config) - end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy") + end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "rspec job: stage parameter should be .pre, build, test, deploy, .post") end it "returns errors if job stage is not a defined stage" do config = YAML.dump({ types: %w(build test), rspec: { script: "test", type: "acceptance" } }) expect do Gitlab::Ci::YamlProcessor.new(config) - end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "rspec job: stage parameter should be build, test") + end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "rspec job: stage parameter should be .pre, build, test, .post") end it "returns errors if stages is not an array" do diff --git a/spec/lib/gitlab/data_builder/push_spec.rb b/spec/lib/gitlab/data_builder/push_spec.rb index e8a9f0b06a8..58509b69463 100644 --- a/spec/lib/gitlab/data_builder/push_spec.rb +++ b/spec/lib/gitlab/data_builder/push_spec.rb @@ -90,4 +90,12 @@ describe Gitlab::DataBuilder::Push do .not_to raise_error end end + + describe '.build_bulk' do + subject do + described_class.build_bulk(action: :created, ref_type: :branch, changes: [double, double]) + end + + it { is_expected.to eq(action: :created, ref_count: 2, ref_type: :branch) } + end end diff --git a/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb b/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb index 6d614c6527a..8331f0b6bc7 100644 --- a/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb @@ -311,10 +311,11 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi end end - it 'creates the merge request diffs' do + it 'creates a merge request diff and sets it as the latest' do mr = insert_git_data expect(mr.merge_request_diffs.exists?).to eq(true) + expect(mr.reload.latest_merge_request_diff_id).to eq(mr.merge_request_diffs.first.id) end it 'creates the merge request diff commits' do diff --git a/spec/lib/gitlab/gpg/commit_spec.rb b/spec/lib/gitlab/gpg/commit_spec.rb index fa47cfd519b..8401b683fd5 100644 --- a/spec/lib/gitlab/gpg/commit_spec.rb +++ b/spec/lib/gitlab/gpg/commit_spec.rb @@ -370,5 +370,33 @@ describe Gitlab::Gpg::Commit do it_behaves_like 'returns the cached signature on second call' end + + context 'multiple commits with signatures' do + let(:first_signature) { create(:gpg_signature) } + + let(:gpg_key) { create(:gpg_key, key: GpgHelpers::User2.public_key) } + let(:second_signature) { create(:gpg_signature, gpg_key: gpg_key) } + + let!(:first_commit) { create(:commit, project: project, sha: first_signature.commit_sha) } + let!(:second_commit) { create(:commit, project: project, sha: second_signature.commit_sha) } + + let(:commits) do + [first_commit, second_commit].map do |commit| + gpg_commit = described_class.new(commit) + + allow(gpg_commit).to receive(:has_signature?).and_return(true) + + gpg_commit + end + end + + it 'does an aggregated sql request instead of 2 separate ones' do + recorder = ActiveRecord::QueryRecorder.new do + commits.each(&:signature) + end + + expect(recorder.count).to eq(1) + end + end end end diff --git a/spec/lib/gitlab/import/merge_request_creator_spec.rb b/spec/lib/gitlab/import/merge_request_creator_spec.rb index 7c73e9b39f7..ff2c3032dbf 100644 --- a/spec/lib/gitlab/import/merge_request_creator_spec.rb +++ b/spec/lib/gitlab/import/merge_request_creator_spec.rb @@ -21,8 +21,11 @@ describe Gitlab::Import::MergeRequestCreator do subject.execute(attributes) - expect(merge_request.reload.merge_request_diffs.count).to eq(1) - expect(merge_request.reload.merge_request_diffs.first.commits.count).to eq(commits_count) + merge_request.reload + + expect(merge_request.merge_request_diffs.count).to eq(1) + expect(merge_request.merge_request_diffs.first.commits.count).to eq(commits_count) + expect(merge_request.latest_merge_request_diff_id).to eq(merge_request.merge_request_diffs.first.id) end end diff --git a/spec/lib/gitlab/import_export/safe_model_attributes.yml b/spec/lib/gitlab/import_export/safe_model_attributes.yml index d3b51a53ede..ebc5d9d1f56 100644 --- a/spec/lib/gitlab/import_export/safe_model_attributes.yml +++ b/spec/lib/gitlab/import_export/safe_model_attributes.yml @@ -47,6 +47,7 @@ PushEventPayload: - commit_to - ref - commit_title +- ref_count Note: - id - note diff --git a/spec/lib/gitlab/metrics/exporter/sidekiq_exporter_spec.rb b/spec/lib/gitlab/metrics/exporter/sidekiq_exporter_spec.rb new file mode 100644 index 00000000000..a415b6407d5 --- /dev/null +++ b/spec/lib/gitlab/metrics/exporter/sidekiq_exporter_spec.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Metrics::Exporter::SidekiqExporter do + let(:exporter) { described_class.new } + + after do + exporter.stop + end + + context 'with valid config' do + before do + stub_config( + monitoring: { + sidekiq_exporter: { + enabled: true, + port: 0, + address: '127.0.0.1' + } + } + ) + end + + it 'does start thread' do + expect(exporter.start).not_to be_nil + end + end + + context 'when port is already taken' do + let(:first_exporter) { described_class.new } + + before do + stub_config( + monitoring: { + sidekiq_exporter: { + enabled: true, + port: 9992, + address: '127.0.0.1' + } + } + ) + + first_exporter.start + end + + after do + first_exporter.stop + end + + it 'does print error message' do + expect(Sidekiq.logger).to receive(:error) + .with( + class: described_class.to_s, + message: 'Cannot start sidekiq_exporter', + exception: anything) + + exporter.start + end + + it 'does not start thread' do + expect(exporter.start).to be_nil + end + end +end diff --git a/spec/lib/gitlab/reference_extractor_spec.rb b/spec/lib/gitlab/reference_extractor_spec.rb index 7513dbeeb6f..f6ace0d8bf5 100644 --- a/spec/lib/gitlab/reference_extractor_spec.rb +++ b/spec/lib/gitlab/reference_extractor_spec.rb @@ -265,7 +265,8 @@ describe Gitlab::ReferenceExtractor do describe 'referables prefixes' do def prefixes described_class::REFERABLES.each_with_object({}) do |referable, result| - klass = referable.to_s.camelize.constantize + class_name = referable.to_s.camelize + klass = class_name.constantize if Object.const_defined?(class_name) next unless klass.respond_to?(:reference_prefix) |