diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /spec/tooling | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) | |
download | gitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'spec/tooling')
-rw-r--r-- | spec/tooling/danger/changelog_spec.rb | 153 | ||||
-rw-r--r-- | spec/tooling/danger/project_helper_spec.rb | 38 | ||||
-rw-r--r-- | spec/tooling/lib/tooling/kubernetes_client_spec.rb | 10 | ||||
-rw-r--r-- | spec/tooling/merge_request_spec.rb | 31 | ||||
-rw-r--r-- | spec/tooling/quality/test_level_spec.rb | 225 |
5 files changed, 411 insertions, 46 deletions
diff --git a/spec/tooling/danger/changelog_spec.rb b/spec/tooling/danger/changelog_spec.rb index b74039b3cd1..7ea2288fd45 100644 --- a/spec/tooling/danger/changelog_spec.rb +++ b/spec/tooling/danger/changelog_spec.rb @@ -161,23 +161,42 @@ RSpec.describe Tooling::Danger::Changelog do describe '#modified_text' do subject { changelog.modified_text } - context "when title is not changed from sanitization", :aggregate_failures do - let(:mr_title) { 'Fake Title' } + context 'when in CI context' do + shared_examples 'changelog modified text' do |key| + specify do + expect(subject).to include('CHANGELOG.md was edited') + expect(subject).to include('bin/changelog -m 1234 "Fake Title"') + expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"') + end + end - specify do - expect(subject).to include('CHANGELOG.md was edited') - expect(subject).to include('bin/changelog -m 1234 "Fake Title"') - expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"') + before do + allow(fake_helper).to receive(:ci?).and_return(true) + end + + context "when title is not changed from sanitization", :aggregate_failures do + let(:mr_title) { 'Fake Title' } + + it_behaves_like 'changelog modified text' + end + + context "when title needs sanitization", :aggregate_failures do + let(:mr_title) { 'DRAFT: Fake Title' } + + it_behaves_like 'changelog modified text' end end - context "when title needs sanitization", :aggregate_failures do - let(:mr_title) { 'DRAFT: Fake Title' } + context 'when in local context' do + let(:mr_title) { 'Fake Title' } + + before do + allow(fake_helper).to receive(:ci?).and_return(false) + end specify do expect(subject).to include('CHANGELOG.md was edited') - expect(subject).to include('bin/changelog -m 1234 "Fake Title"') - expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"') + expect(subject).not_to include('bin/changelog') end end end @@ -187,56 +206,116 @@ RSpec.describe Tooling::Danger::Changelog do subject { changelog.required_texts } - shared_examples 'changelog required text' do |key| - specify do - expect(subject).to have_key(key) - expect(subject[key]).to include('CHANGELOG missing') - expect(subject[key]).to include('bin/changelog -m 1234 "Fake Title"') - expect(subject[key]).not_to include('--ee') + context 'when in CI context' do + before do + allow(fake_helper).to receive(:ci?).and_return(true) end - end - context 'with a new migration file' do - let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) } + shared_examples 'changelog required text' do |key| + specify do + expect(subject).to have_key(key) + expect(subject[key]).to include('CHANGELOG missing') + expect(subject[key]).to include('bin/changelog -m 1234 "Fake Title"') + expect(subject[key]).not_to include('--ee') + end + end - context "when title is not changed from sanitization", :aggregate_failures do - it_behaves_like 'changelog required text', :db_changes + context 'with a new migration file' do + let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) } + + context "when title is not changed from sanitization", :aggregate_failures do + it_behaves_like 'changelog required text', :db_changes + end + + context "when title needs sanitization", :aggregate_failures do + let(:mr_title) { 'DRAFT: Fake Title' } + + it_behaves_like 'changelog required text', :db_changes + end end - context "when title needs sanitization", :aggregate_failures do - let(:mr_title) { 'DRAFT: Fake Title' } + context 'with a removed feature flag file' do + let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) } - it_behaves_like 'changelog required text', :db_changes + it_behaves_like 'changelog required text', :feature_flag_removed end end - context 'with a removed feature flag file' do - let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) } + context 'when in local context' do + before do + allow(fake_helper).to receive(:ci?).and_return(false) + end + + shared_examples 'changelog required text' do |key| + specify do + expect(subject).to have_key(key) + expect(subject[key]).to include('CHANGELOG missing') + expect(subject[key]).not_to include('bin/changelog') + expect(subject[key]).not_to include('--ee') + end + end + + context 'with a new migration file' do + let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) } + + context "when title is not changed from sanitization", :aggregate_failures do + it_behaves_like 'changelog required text', :db_changes + end + + context "when title needs sanitization", :aggregate_failures do + let(:mr_title) { 'DRAFT: Fake Title' } - it_behaves_like 'changelog required text', :feature_flag_removed + it_behaves_like 'changelog required text', :db_changes + end + end + + context 'with a removed feature flag file' do + let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) } + + it_behaves_like 'changelog required text', :feature_flag_removed + end end end describe '#optional_text' do subject { changelog.optional_text } - context "when title is not changed from sanitization", :aggregate_failures do - let(:mr_title) { 'Fake Title' } + context 'when in CI context' do + shared_examples 'changelog optional text' do |key| + specify do + expect(subject).to include('CHANGELOG missing') + expect(subject).to include('bin/changelog -m 1234 "Fake Title"') + expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"') + end + end - specify do - expect(subject).to include('CHANGELOG missing') - expect(subject).to include('bin/changelog -m 1234 "Fake Title"') - expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"') + before do + allow(fake_helper).to receive(:ci?).and_return(true) + end + + context "when title is not changed from sanitization", :aggregate_failures do + let(:mr_title) { 'Fake Title' } + + it_behaves_like 'changelog optional text' + end + + context "when title needs sanitization", :aggregate_failures do + let(:mr_title) { 'DRAFT: Fake Title' } + + it_behaves_like 'changelog optional text' end end - context "when title needs sanitization", :aggregate_failures do - let(:mr_title) { 'DRAFT: Fake Title' } + context 'when in local context' do + let(:mr_title) { 'Fake Title' } + + before do + allow(fake_helper).to receive(:ci?).and_return(false) + end specify do expect(subject).to include('CHANGELOG missing') - expect(subject).to include('bin/changelog -m 1234 "Fake Title"') - expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"') + expect(subject).not_to include('bin/changelog') end end end diff --git a/spec/tooling/danger/project_helper_spec.rb b/spec/tooling/danger/project_helper_spec.rb index a8fda901b4a..5d106f08402 100644 --- a/spec/tooling/danger/project_helper_spec.rb +++ b/spec/tooling/danger/project_helper_spec.rb @@ -2,7 +2,8 @@ require 'rspec-parameterized' require 'gitlab-dangerfiles' -require 'danger/helper' +require 'danger' +require 'danger/plugins/helper' require 'gitlab/dangerfiles/spec_helper' require_relative '../../../danger/plugins/project_helper' @@ -43,7 +44,7 @@ RSpec.describe Tooling::Danger::ProjectHelper do end where(:path, :expected_categories) do - 'usage_data.rb' | [:database, :backend] + 'usage_data.rb' | [:database, :backend, :product_intelligence] 'doc/foo.md' | [:docs] 'CONTRIBUTING.md' | [:docs] 'LICENSE' | [:docs] @@ -140,6 +141,7 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'ee/db/geo/post_migrate/foo' | [:database, :migration] 'app/models/project_authorization.rb' | [:database] 'app/services/users/refresh_authorized_projects_service.rb' | [:database] + 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb' | [:database] 'lib/gitlab/background_migration.rb' | [:database] 'lib/gitlab/background_migration/foo' | [:database] 'ee/lib/gitlab/background_migration/foo' | [:database] @@ -157,6 +159,9 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'qa/foo' | [:qa] 'ee/qa/foo' | [:qa] + 'workhorse/main.go' | [:workhorse] + 'workhorse/internal/upload/upload.go' | [:workhorse] + 'changelogs/foo' | [:none] 'ee/changelogs/foo' | [:none] 'locale/gitlab.pot' | [:none] @@ -168,6 +173,21 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'foo/bar.js' | [:frontend] 'foo/bar.txt' | [:none] 'foo/bar.md' | [:none] + + 'ee/config/metrics/counts_7d/20210216174919_g_analytics_issues_weekly.yml' | [:product_intelligence] + 'lib/gitlab/usage_data_counters/aggregated_metrics/common.yml' | [:product_intelligence] + 'lib/gitlab/usage_data_counters/hll_redis_counter.rb' | [:backend, :product_intelligence] + 'doc/development/usage_ping/dictionary.md' | [:docs, :product_intelligence] + 'lib/gitlab/tracking.rb' | [:backend, :product_intelligence] + 'spec/lib/gitlab/tracking_spec.rb' | [:backend, :product_intelligence] + 'app/helpers/tracking_helper.rb' | [:backend, :product_intelligence] + 'spec/helpers/tracking_helper_spec.rb' | [:backend, :product_intelligence] + 'lib/generators/rails/usage_metric_definition_generator.rb' | [:backend, :product_intelligence] + 'spec/lib/generators/usage_metric_definition_generator_spec.rb' | [:backend, :product_intelligence] + 'config/metrics/schema.json' | [:product_intelligence] + 'app/assets/javascripts/tracking.js' | [:frontend, :product_intelligence] + 'spec/frontend/tracking_spec.js' | [:frontend, :product_intelligence] + 'lib/gitlab/usage_database/foo.rb' | [:backend] end with_them do @@ -178,12 +198,12 @@ RSpec.describe Tooling::Danger::ProjectHelper do context 'having specific changes' do where(:expected_categories, :patch, :changed_files) do - [:database, :backend] | '+ count(User.active)' | ['usage_data.rb', 'lib/gitlab/usage_data.rb', 'ee/lib/ee/gitlab/usage_data.rb'] - [:database, :backend] | '+ estimate_batch_distinct_count(User.active)' | ['usage_data.rb'] - [:backend] | '+ alt_usage_data(User.active)' | ['usage_data.rb'] - [:backend] | '+ count(User.active)' | ['user.rb'] - [:backend] | '+ count(User.active)' | ['usage_data/topology.rb'] - [:backend] | '+ foo_count(User.active)' | ['usage_data.rb'] + [:database, :backend, :product_intelligence] | '+ count(User.active)' | ['usage_data.rb', 'lib/gitlab/usage_data.rb', 'ee/lib/ee/gitlab/usage_data.rb'] + [:database, :backend, :product_intelligence] | '+ estimate_batch_distinct_count(User.active)' | ['usage_data.rb'] + [:backend, :product_intelligence] | '+ alt_usage_data(User.active)' | ['lib/gitlab/usage_data.rb'] + [:backend, :product_intelligence] | '+ count(User.active)' | ['lib/gitlab/usage_data/topology.rb'] + [:backend, :product_intelligence] | '+ foo_count(User.active)' | ['lib/gitlab/usage_data.rb'] + [:backend] | '+ count(User.active)' | ['user.rb'] end with_them do @@ -200,7 +220,7 @@ RSpec.describe Tooling::Danger::ProjectHelper do describe '.local_warning_message' do it 'returns an informational message with rules that can run' do - expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changes_size, commit_messages, database, documentation, duplicate_yarn_dependencies, eslint, karma, pajamas, pipeline, prettier, product_intelligence, utility_css') + expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changelog, changes_size, commit_messages, database, datateam, documentation, duplicate_yarn_dependencies, eslint, karma, pajamas, pipeline, prettier, product_intelligence, utility_css') end end diff --git a/spec/tooling/lib/tooling/kubernetes_client_spec.rb b/spec/tooling/lib/tooling/kubernetes_client_spec.rb index 4a84ec09b5c..636727401af 100644 --- a/spec/tooling/lib/tooling/kubernetes_client_spec.rb +++ b/spec/tooling/lib/tooling/kubernetes_client_spec.rb @@ -123,6 +123,16 @@ RSpec.describe Tooling::KubernetesClient do it_behaves_like 'a kubectl command to delete resources by older than given creation time' end + + context 'with no resources found' do + let(:resource_names) { [] } + + it 'does not call #delete_by_exact_names' do + expect(subject).not_to receive(:delete_by_exact_names) + + subject.cleanup_by_created_at(resource_type: resource_type, created_before: two_days_ago) + end + end end describe '#raw_resource_names' do diff --git a/spec/tooling/merge_request_spec.rb b/spec/tooling/merge_request_spec.rb new file mode 100644 index 00000000000..de6fd48ad9f --- /dev/null +++ b/spec/tooling/merge_request_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'webmock/rspec' + +require_relative '../../tooling/merge_request' +require_relative '../support/helpers/next_instance_of' + +RSpec.describe Tooling::MergeRequest do + let(:project_path) { 'gitlab-org/gitlab' } + let(:branch_name) { 'my-branch' } + let(:merge_request_iid) { 123 } + let(:merge_requests) { [{ 'iid' => merge_request_iid }] } + + describe '.for' do + let(:stub_api) do + stub_request(:get, "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/merge_requests") + .and_return(body: merge_requests) + end + + before do + stub_api.with(query: { source_branch: branch_name, order_by: 'updated_at', sort: 'desc' }) + end + + it 'fetches merge request for local branch in the given GitLab project path' do + merge_request = described_class.for(branch: branch_name, project_path: project_path) + + expect(merge_request.iid).to eq(merge_request_iid) + expect(stub_api).to have_been_requested.once + end + end +end diff --git a/spec/tooling/quality/test_level_spec.rb b/spec/tooling/quality/test_level_spec.rb new file mode 100644 index 00000000000..89abe337347 --- /dev/null +++ b/spec/tooling/quality/test_level_spec.rb @@ -0,0 +1,225 @@ +# frozen_string_literal: true + +require_relative '../../../tooling/quality/test_level' + +RSpec.describe Quality::TestLevel do + describe '#pattern' do + context 'when level is all' do + it 'returns a pattern' do + expect(subject.pattern(:all)) + .to eq("spec/**{,/**/}*_spec.rb") + end + end + + context 'when level is geo' do + it 'returns a pattern' do + expect(subject.pattern(:geo)) + .to eq("spec/**{,/**/}*_spec.rb") + end + end + + context 'when level is frontend_fixture' do + it 'returns a pattern' do + expect(subject.pattern(:frontend_fixture)) + .to eq("spec/{frontend/fixtures}{,/**/}*.rb") + end + end + + context 'when level is unit' do + it 'returns a pattern' do + expect(subject.pattern(:unit)) + .to eq("spec/{bin,channels,config,db,dependencies,elastic,elastic_integration,experiments,factories,finders,frontend,graphql,haml_lint,helpers,initializers,javascripts,lib,models,policies,presenters,rack_servers,replicators,routing,rubocop,serializers,services,sidekiq,spam,support_specs,tasks,uploaders,validators,views,workers,tooling}{,/**/}*_spec.rb") + end + end + + context 'when level is migration' do + it 'returns a pattern' do + expect(subject.pattern(:migration)) + .to eq("spec/{migrations,lib/gitlab/background_migration,lib/ee/gitlab/background_migration}{,/**/}*_spec.rb") + end + end + + context 'when level is background_migration' do + it 'returns a pattern' do + expect(subject.pattern(:background_migration)) + .to eq("spec/{lib/gitlab/background_migration,lib/ee/gitlab/background_migration}{,/**/}*_spec.rb") + end + end + + context 'when level is integration' do + it 'returns a pattern' do + expect(subject.pattern(:integration)) + .to eq("spec/{controllers,mailers,requests}{,/**/}*_spec.rb") + end + end + + context 'when level is system' do + it 'returns a pattern' do + expect(subject.pattern(:system)) + .to eq("spec/{features}{,/**/}*_spec.rb") + end + end + + context 'with a prefix' do + it 'returns a pattern' do + expect(described_class.new('ee/').pattern(:system)) + .to eq("ee/spec/{features}{,/**/}*_spec.rb") + end + end + + describe 'performance' do + it 'memoizes the pattern for a given level' do + expect(subject.pattern(:system).object_id).to eq(subject.pattern(:system).object_id) + end + + it 'freezes the pattern for a given level' do + expect(subject.pattern(:system)).to be_frozen + end + end + end + + describe '#regexp' do + context 'when level is all' do + it 'returns a regexp' do + expect(subject.regexp(:all)) + .to eq(%r{spec/}) + end + end + + context 'when level is geo' do + it 'returns a regexp' do + expect(subject.regexp(:geo)) + .to eq(%r{spec/}) + end + end + + context 'when level is frontend_fixture' do + it 'returns a regexp' do + expect(subject.regexp(:frontend_fixture)) + .to eq(%r{spec/(frontend/fixtures)}) + end + end + + context 'when level is unit' do + it 'returns a regexp' do + expect(subject.regexp(:unit)) + .to eq(%r{spec/(bin|channels|config|db|dependencies|elastic|elastic_integration|experiments|factories|finders|frontend|graphql|haml_lint|helpers|initializers|javascripts|lib|models|policies|presenters|rack_servers|replicators|routing|rubocop|serializers|services|sidekiq|spam|support_specs|tasks|uploaders|validators|views|workers|tooling)}) + end + end + + context 'when level is migration' do + it 'returns a regexp' do + expect(subject.regexp(:migration)) + .to eq(%r{spec/(migrations|lib/gitlab/background_migration|lib/ee/gitlab/background_migration)}) + end + end + + context 'when level is background_migration' do + it 'returns a regexp' do + expect(subject.regexp(:background_migration)) + .to eq(%r{spec/(lib/gitlab/background_migration|lib/ee/gitlab/background_migration)}) + end + end + + context 'when level is integration' do + it 'returns a regexp' do + expect(subject.regexp(:integration)) + .to eq(%r{spec/(controllers|mailers|requests)}) + end + end + + context 'when level is system' do + it 'returns a regexp' do + expect(subject.regexp(:system)) + .to eq(%r{spec/(features)}) + end + end + + context 'with a prefix' do + it 'returns a regexp' do + expect(described_class.new('ee/').regexp(:system)) + .to eq(%r{ee/spec/(features)}) + end + end + + describe 'performance' do + it 'memoizes the regexp for a given level' do + expect(subject.regexp(:system).object_id).to eq(subject.regexp(:system).object_id) + end + + it 'freezes the regexp for a given level' do + expect(subject.regexp(:system)).to be_frozen + end + end + end + + describe '#level_for' do + it 'returns the correct level for a unit test' do + expect(subject.level_for('spec/models/abuse_report_spec.rb')).to eq(:unit) + end + + it 'returns the correct level for a frontend fixture test' do + expect(subject.level_for('spec/frontend/fixtures/pipelines.rb')).to eq(:frontend_fixture) + end + + it 'returns the correct level for a tooling test' do + expect(subject.level_for('spec/tooling/lib/tooling/test_file_finder_spec.rb')).to eq(:unit) + end + + it 'returns the correct level for a migration test' do + expect(subject.level_for('spec/migrations/add_default_and_free_plans_spec.rb')).to eq(:migration) + end + + it 'returns the correct level for a background migration test' do + expect(subject.level_for('spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb')).to eq(:migration) + end + + it 'returns the correct level for an EE file without passing a prefix' do + expect(subject.level_for('ee/spec/migrations/geo/migrate_ci_job_artifacts_to_separate_registry_spec.rb')).to eq(:migration) + end + + it 'returns the correct level for a geo migration test' do + expect(described_class.new('ee/').level_for('ee/spec/migrations/geo/migrate_ci_job_artifacts_to_separate_registry_spec.rb')).to eq(:migration) + end + + it 'returns the correct level for a EE-namespaced background migration test' do + expect(described_class.new('ee/').level_for('ee/spec/lib/ee/gitlab/background_migration/prune_orphaned_geo_events_spec.rb')).to eq(:migration) + end + + it 'returns the correct level for an integration test' do + expect(subject.level_for('spec/mailers/abuse_report_mailer_spec.rb')).to eq(:integration) + end + + it 'returns the correct level for a system test' do + expect(subject.level_for('spec/features/abuse_report_spec.rb')).to eq(:system) + end + + it 'raises an error for an unknown level' do + expect { subject.level_for('spec/unknown/foo_spec.rb') } + .to raise_error(described_class::UnknownTestLevelError, + %r{Test level for spec/unknown/foo_spec.rb couldn't be set. Please rename the file properly or change the test level detection regexes in .+/tooling/quality/test_level.rb.}) + end + end + + describe '#background_migration?' do + it 'returns false for a unit test' do + expect(subject.background_migration?('spec/models/abuse_report_spec.rb')).to be(false) + end + + it 'returns true for a migration test' do + expect(subject.background_migration?('spec/migrations/add_default_and_free_plans_spec.rb')).to be(false) + end + + it 'returns true for a background migration test' do + expect(subject.background_migration?('spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb')).to be(true) + end + + it 'returns true for a geo migration test' do + expect(described_class.new('ee/').background_migration?('ee/spec/migrations/geo/migrate_ci_job_artifacts_to_separate_registry_spec.rb')).to be(false) + end + + it 'returns true for a EE-namespaced background migration test' do + expect(described_class.new('ee/').background_migration?('ee/spec/lib/ee/gitlab/background_migration/prune_orphaned_geo_events_spec.rb')).to be(true) + end + end +end |