diff options
Diffstat (limited to 'spec/support/helpers')
-rw-r--r-- | spec/support/helpers/expect_offense.rb | 22 | ||||
-rw-r--r-- | spec/support/helpers/fast_rails_root.rb | 10 | ||||
-rw-r--r-- | spec/support/helpers/filtered_search_helpers.rb | 5 | ||||
-rw-r--r-- | spec/support/helpers/jira_service_helper.rb | 3 | ||||
-rw-r--r-- | spec/support/helpers/metrics_dashboard_helpers.rb | 14 | ||||
-rw-r--r-- | spec/support/helpers/notification_helpers.rb | 12 | ||||
-rw-r--r-- | spec/support/helpers/packages_manager_api_spec_helper.rb | 46 | ||||
-rw-r--r-- | spec/support/helpers/partitioning_helpers.rb | 46 | ||||
-rw-r--r-- | spec/support/helpers/rack_attack_spec_helpers.rb | 12 | ||||
-rw-r--r-- | spec/support/helpers/reference_parser_helpers.rb | 4 | ||||
-rw-r--r-- | spec/support/helpers/snippet_helpers.rb | 14 | ||||
-rw-r--r-- | spec/support/helpers/stub_configuration.rb | 8 | ||||
-rw-r--r-- | spec/support/helpers/stub_object_storage.rb | 26 | ||||
-rw-r--r-- | spec/support/helpers/test_env.rb | 30 | ||||
-rw-r--r-- | spec/support/helpers/trigger_helpers.rb | 5 | ||||
-rw-r--r-- | spec/support/helpers/usage_data_helpers.rb | 12 |
16 files changed, 215 insertions, 54 deletions
diff --git a/spec/support/helpers/expect_offense.rb b/spec/support/helpers/expect_offense.rb deleted file mode 100644 index 76301fe19ff..00000000000 --- a/spec/support/helpers/expect_offense.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -require 'rubocop/rspec/support' - -# https://github.com/backus/rubocop-rspec/blob/master/spec/support/expect_offense.rb -# rubocop-rspec gem extension of RuboCop's ExpectOffense module. -# -# This mixin is the same as rubocop's ExpectOffense except the default -# filename ends with `_spec.rb` -module ExpectOffense - include RuboCop::RSpec::ExpectOffense - - DEFAULT_FILENAME = 'example_spec.rb'.freeze - - def expect_offense(source, filename = DEFAULT_FILENAME) - super - end - - def expect_no_offenses(source, filename = DEFAULT_FILENAME) - super - end -end diff --git a/spec/support/helpers/fast_rails_root.rb b/spec/support/helpers/fast_rails_root.rb new file mode 100644 index 00000000000..1510fe0825c --- /dev/null +++ b/spec/support/helpers/fast_rails_root.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# For specs which don't load Rails, provide a path to Rails root +module FastRailsRoot + RAILS_ROOT = File.absolute_path("#{__dir__}/../../..") + + def rails_root_join(*args) + File.join(RAILS_ROOT, *args) + end +end diff --git a/spec/support/helpers/filtered_search_helpers.rb b/spec/support/helpers/filtered_search_helpers.rb index 99a5e043825..1847a8f8a06 100644 --- a/spec/support/helpers/filtered_search_helpers.rb +++ b/spec/support/helpers/filtered_search_helpers.rb @@ -45,9 +45,8 @@ module FilteredSearchHelpers all_count = open_count + closed_count expect(page).to have_issuable_counts(open: open_count, closed: closed_count, all: all_count) - page.within '.issues-list' do - expect(page).to have_selector('.issue', count: open_count) - end + + expect(page).to have_selector('.issue', count: open_count) end # Enables input to be added character by character diff --git a/spec/support/helpers/jira_service_helper.rb b/spec/support/helpers/jira_service_helper.rb index 198bedfe3bc..9072c41fe66 100644 --- a/spec/support/helpers/jira_service_helper.rb +++ b/spec/support/helpers/jira_service_helper.rb @@ -5,14 +5,13 @@ module JiraServiceHelper JIRA_API = JIRA_URL + "/rest/api/2" def jira_service_settings - title = "Jira tracker" url = JIRA_URL username = 'jira-user' password = 'my-secret-password' jira_issue_transition_id = '1' jira_tracker.update( - title: title, url: url, username: username, password: password, + url: url, username: username, password: password, jira_issue_transition_id: jira_issue_transition_id, active: true ) end diff --git a/spec/support/helpers/metrics_dashboard_helpers.rb b/spec/support/helpers/metrics_dashboard_helpers.rb index b8a641d5911..b2dd8ead7dd 100644 --- a/spec/support/helpers/metrics_dashboard_helpers.rb +++ b/spec/support/helpers/metrics_dashboard_helpers.rb @@ -7,6 +7,12 @@ module MetricsDashboardHelpers create(:project, :custom_repo, files: { dashboard_path => dashboard_yml }) end + def project_with_dashboard_namespace(dashboard_path, dashboard_yml = nil) + dashboard_yml ||= fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') + + create(:project, :custom_repo, namespace: namespace, path: 'monitor-project', files: { dashboard_path => dashboard_yml }) + end + def delete_project_dashboard(project, user, dashboard_path) project.repository.delete_file( user, @@ -18,6 +24,14 @@ module MetricsDashboardHelpers project.repository.refresh_method_caches([:metrics_dashboard]) end + def load_sample_dashboard + load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')) + end + + def load_dashboard_yaml(data) + ::Gitlab::Config::Loader::Yaml.new(data).load_raw! + end + def system_dashboard_path Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH end diff --git a/spec/support/helpers/notification_helpers.rb b/spec/support/helpers/notification_helpers.rb index b3e0e7d811b..887d68de4e1 100644 --- a/spec/support/helpers/notification_helpers.rb +++ b/spec/support/helpers/notification_helpers.rb @@ -38,26 +38,26 @@ module NotificationHelpers end def expect_delivery_jobs_count(count) - expect(ActionMailer::DeliveryJob).to have_been_enqueued.exactly(count).times + expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.exactly(count).times end def expect_no_delivery_jobs - expect(ActionMailer::DeliveryJob).not_to have_been_enqueued + expect(ActionMailer::MailDeliveryJob).not_to have_been_enqueued end def expect_any_delivery_jobs - expect(ActionMailer::DeliveryJob).to have_been_enqueued.at_least(:once) + expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.at_least(:once) end def have_enqueued_email(*args, mailer: "Notify", mail: "", delivery: "deliver_now") - have_enqueued_job(ActionMailer::DeliveryJob).with(mailer, mail, delivery, *args) + have_enqueued_job(ActionMailer::MailDeliveryJob).with(mailer, mail, delivery, args: args) end def expect_enqueud_email(*args, mailer: "Notify", mail: "", delivery: "deliver_now") - expect(ActionMailer::DeliveryJob).to have_been_enqueued.with(mailer, mail, delivery, *args) + expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.with(mailer, mail, delivery, args: args) end def expect_not_enqueud_email(*args, mailer: "Notify", mail: "") - expect(ActionMailer::DeliveryJob).not_to have_been_enqueued.with(mailer, mail, *args, any_args) + expect(ActionMailer::MailDeliveryJob).not_to have_been_enqueued.with(mailer, mail, args: any_args) end end diff --git a/spec/support/helpers/packages_manager_api_spec_helper.rb b/spec/support/helpers/packages_manager_api_spec_helper.rb new file mode 100644 index 00000000000..e5a690e1680 --- /dev/null +++ b/spec/support/helpers/packages_manager_api_spec_helper.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module PackagesManagerApiSpecHelpers + def build_auth_headers(value) + { 'HTTP_AUTHORIZATION' => value } + end + + def build_basic_auth_header(username, password) + build_auth_headers(ActionController::HttpAuthentication::Basic.encode_credentials(username, password)) + end + + def build_token_auth_header(token) + build_auth_headers("Bearer #{token}") + end + + def build_jwt(personal_access_token, secret: jwt_secret, user_id: nil) + JSONWebToken::HMACToken.new(secret).tap do |jwt| + jwt['access_token'] = personal_access_token.id + jwt['user_id'] = user_id || personal_access_token.user_id + end + end + + def build_jwt_from_job(job, secret: jwt_secret) + JSONWebToken::HMACToken.new(secret).tap do |jwt| + jwt['access_token'] = job.token + jwt['user_id'] = job.user.id + end + end + + def build_jwt_from_deploy_token(deploy_token, secret: jwt_secret) + JSONWebToken::HMACToken.new(secret).tap do |jwt| + jwt['access_token'] = deploy_token.token + jwt['user_id'] = deploy_token.username + end + end + + def temp_file(package_tmp) + upload_path = ::Packages::PackageFileUploader.workhorse_local_upload_path + file_path = "#{upload_path}/#{package_tmp}" + + FileUtils.mkdir_p(upload_path) + File.write(file_path, 'test') + + UploadedFile.new(file_path, filename: File.basename(file_path)) + end +end diff --git a/spec/support/helpers/partitioning_helpers.rb b/spec/support/helpers/partitioning_helpers.rb index 98a13915d76..8981fea04d5 100644 --- a/spec/support/helpers/partitioning_helpers.rb +++ b/spec/support/helpers/partitioning_helpers.rb @@ -9,13 +9,36 @@ module PartitioningHelpers end def expect_range_partition_of(partition_name, table_name, min_value, max_value) - definition = find_partition_definition(partition_name) + definition = find_partition_definition(partition_name, schema: Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA) expect(definition).not_to be_nil expect(definition['base_table']).to eq(table_name.to_s) expect(definition['condition']).to eq("FOR VALUES FROM (#{min_value}) TO (#{max_value})") end + def expect_total_partitions(table_name, count, schema: Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA) + partitions = find_partitions(table_name, schema: schema) + + expect(partitions.size).to eq(count) + end + + def expect_range_partitions_for(table_name, partitions) + partitions.each do |suffix, (min_value, max_value)| + partition_name = "#{table_name}_#{suffix}" + expect_range_partition_of(partition_name, table_name, min_value, max_value) + end + + expect_total_partitions(table_name, partitions.size, schema: Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA) + end + + def expect_hash_partition_of(partition_name, table_name, modulus, remainder) + definition = find_partition_definition(partition_name, schema: Gitlab::Database::STATIC_PARTITIONS_SCHEMA) + + expect(definition).not_to be_nil + expect(definition['base_table']).to eq(table_name.to_s) + expect(definition['condition']).to eq("FOR VALUES WITH (modulus #{modulus}, remainder #{remainder})") + end + private def find_partitioned_columns(table) @@ -40,7 +63,7 @@ module PartitioningHelpers SQL end - def find_partition_definition(partition) + def find_partition_definition(partition, schema: ) connection.select_one(<<~SQL) select parent_class.relname as base_table, @@ -48,7 +71,24 @@ module PartitioningHelpers from pg_class inner join pg_inherits i on pg_class.oid = inhrelid inner join pg_class parent_class on parent_class.oid = inhparent - where pg_class.relname = '#{partition}' and pg_class.relispartition; + inner join pg_namespace ON pg_namespace.oid = pg_class.relnamespace + where pg_namespace.nspname = '#{schema}' + and pg_class.relname = '#{partition}' + and pg_class.relispartition + SQL + end + + def find_partitions(partition, schema: Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA) + connection.select_rows(<<~SQL) + select + pg_class.relname + from pg_class + inner join pg_inherits i on pg_class.oid = inhrelid + inner join pg_class parent_class on parent_class.oid = inhparent + inner join pg_namespace ON pg_namespace.oid = pg_class.relnamespace + where pg_namespace.nspname = '#{schema}' + and parent_class.relname = '#{partition}' + and pg_class.relispartition SQL end end diff --git a/spec/support/helpers/rack_attack_spec_helpers.rb b/spec/support/helpers/rack_attack_spec_helpers.rb index e0cedb5a57b..65082ec690f 100644 --- a/spec/support/helpers/rack_attack_spec_helpers.rb +++ b/spec/support/helpers/rack_attack_spec_helpers.rb @@ -30,4 +30,16 @@ module RackAttackSpecHelpers expect(response).to have_gitlab_http_status(:too_many_requests) end + + def expect_ok(&block) + yield + + expect(response).to have_gitlab_http_status(:ok) + end + + def random_next_ip + allow_next_instance_of(Rack::Attack::Request) do |instance| + allow(instance).to receive(:ip).and_return(FFaker::Internet.ip_v4_address) + end + end end diff --git a/spec/support/helpers/reference_parser_helpers.rb b/spec/support/helpers/reference_parser_helpers.rb index 9084265b587..e65cb8c96db 100644 --- a/spec/support/helpers/reference_parser_helpers.rb +++ b/spec/support/helpers/reference_parser_helpers.rb @@ -10,7 +10,7 @@ module ReferenceParserHelpers expect(result[:not_visible].count).to eq(not_visible_count) end - shared_examples 'no project N+1 queries' do + RSpec.shared_examples 'no project N+1 queries' do it 'avoids N+1 queries in #nodes_visible_to_user', :request_store do context = Banzai::RenderContext.new(project, user) @@ -28,7 +28,7 @@ module ReferenceParserHelpers end end - shared_examples 'no N+1 queries' do + RSpec.shared_examples 'no N+1 queries' do it_behaves_like 'no project N+1 queries' it 'avoids N+1 queries in #records_for_nodes', :request_store do diff --git a/spec/support/helpers/snippet_helpers.rb b/spec/support/helpers/snippet_helpers.rb new file mode 100644 index 00000000000..de64ad7d3e2 --- /dev/null +++ b/spec/support/helpers/snippet_helpers.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module SnippetHelpers + def sign_in_as(user) + sign_in(public_send(user)) if user + end + + def snippet_blob_file(blob) + { + "path" => blob.path, + "raw_url" => gitlab_raw_snippet_blob_url(blob.container, blob.path) + } + end +end diff --git a/spec/support/helpers/stub_configuration.rb b/spec/support/helpers/stub_configuration.rb index 6a832ca97d1..e19f230d8df 100644 --- a/spec/support/helpers/stub_configuration.rb +++ b/spec/support/helpers/stub_configuration.rb @@ -113,6 +113,14 @@ module StubConfiguration allow(Gitlab.config.rack_attack.git_basic_auth).to receive_messages(to_settings(messages)) end + def stub_service_desk_email_setting(messages) + allow(::Gitlab.config.service_desk_email).to receive_messages(to_settings(messages)) + end + + def stub_packages_setting(messages) + allow(::Gitlab.config.packages).to receive_messages(to_settings(messages)) + end + private # Modifies stubbed messages to also stub possible predicate versions diff --git a/spec/support/helpers/stub_object_storage.rb b/spec/support/helpers/stub_object_storage.rb index b473cdaefc1..6056359d026 100644 --- a/spec/support/helpers/stub_object_storage.rb +++ b/spec/support/helpers/stub_object_storage.rb @@ -1,6 +1,25 @@ # frozen_string_literal: true module StubObjectStorage + def stub_packages_object_storage(**params) + stub_object_storage_uploader(config: ::Gitlab.config.packages.object_store, + uploader: ::Packages::PackageFileUploader, + remote_directory: 'packages', + **params) + end + + def stub_dependency_proxy_object_storage(**params) + stub_object_storage_uploader(config: ::Gitlab.config.dependency_proxy.object_store, + uploader: ::DependencyProxy::FileUploader, + remote_directory: 'dependency_proxy', + **params) + end + + def stub_object_storage_pseudonymizer + stub_object_storage(connection_params: Pseudonymizer::Uploader.object_store_credentials, + remote_directory: Pseudonymizer::Uploader.remote_directory) + end + def stub_object_storage_uploader( config:, uploader:, @@ -73,7 +92,7 @@ module StubObjectStorage def stub_terraform_state_object_storage(uploader = described_class, **params) stub_object_storage_uploader(config: Gitlab.config.terraform_state.object_store, uploader: uploader, - remote_directory: 'terraform_state', + remote_directory: 'terraform', **params) end @@ -89,8 +108,3 @@ module StubObjectStorage EOS end end - -require_relative '../../../ee/spec/support/helpers/ee/stub_object_storage' if - Dir.exist?("#{__dir__}/../../../ee") - -StubObjectStorage.prepend_if_ee('EE::StubObjectStorage') diff --git a/spec/support/helpers/test_env.rb b/spec/support/helpers/test_env.rb index 130650b7e2e..f787aedf7aa 100644 --- a/spec/support/helpers/test_env.rb +++ b/spec/support/helpers/test_env.rb @@ -6,6 +6,8 @@ module TestEnv ComponentFailedToInstallError = Class.new(StandardError) + SHA_REGEX = /\A[0-9a-f]{5,40}\z/i.freeze + # When developing the seed repository, comment out the branch you will modify. BRANCH_SHA = { 'signed-commits' => '6101e87', @@ -29,6 +31,10 @@ module TestEnv 'gitattributes' => '5a62481', 'expand-collapse-diffs' => '4842455', 'symlink-expand-diff' => '81e6355', + 'diff-files-symlink-to-image' => '8cfca84', + 'diff-files-image-to-symlink' => '3e94fda', + 'diff-files-symlink-to-text' => '689815e', + 'diff-files-text-to-symlink' => '5e2c270', 'expand-collapse-files' => '025db92', 'expand-collapse-lines' => '238e82d', 'pages-deploy' => '7897d5b', @@ -165,8 +171,9 @@ module TestEnv task: "gitlab:gitaly:install[#{install_gitaly_args}]") do Gitlab::SetupHelper::Gitaly.create_configuration(gitaly_dir, { 'default' => repos_path }, force: true) Gitlab::SetupHelper::Praefect.create_configuration(gitaly_dir, { 'praefect' => repos_path }, force: true) - start_gitaly(gitaly_dir) end + + start_gitaly(gitaly_dir) end def gitaly_socket_path @@ -459,7 +466,6 @@ module TestEnv end def component_timed_setup(component, install_dir:, version:, task:) - puts "\n==> Setting up #{component}..." start = Time.now ensure_component_dir_name_is_correct!(component, install_dir) @@ -468,22 +474,22 @@ module TestEnv return if File.exist?(install_dir) && ci? if component_needs_update?(install_dir, version) + puts "\n==> Setting up #{component}..." # Cleanup the component entirely to ensure we start fresh FileUtils.rm_rf(install_dir) unless system('rake', task) raise ComponentFailedToInstallError end - end - yield if block_given? + yield if block_given? + puts " #{component} set up in #{Time.now - start} seconds...\n" + end rescue ComponentFailedToInstallError puts "\n#{component} failed to install, cleaning up #{install_dir}!\n" FileUtils.rm_rf(install_dir) exit 1 - ensure - puts " #{component} set up in #{Time.now - start} seconds...\n" end def ci? @@ -504,6 +510,8 @@ module TestEnv # Allow local overrides of the component for tests during development return false if Rails.env.test? && File.symlink?(component_folder) + return false if component_matches_git_sha?(component_folder, expected_version) + version = File.read(File.join(component_folder, 'VERSION')).strip # Notice that this will always yield true when using branch versions @@ -513,6 +521,16 @@ module TestEnv rescue Errno::ENOENT true end + + def component_matches_git_sha?(component_folder, expected_version) + # Not a git SHA, so return early + return false unless expected_version =~ SHA_REGEX + + sha, exit_status = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} rev-parse HEAD), component_folder) + return false if exit_status != 0 + + expected_version == sha.chomp + end end require_relative('../../../ee/spec/support/helpers/ee/test_env') if Gitlab.ee? diff --git a/spec/support/helpers/trigger_helpers.rb b/spec/support/helpers/trigger_helpers.rb index fa4f499b900..67c62cf4869 100644 --- a/spec/support/helpers/trigger_helpers.rb +++ b/spec/support/helpers/trigger_helpers.rb @@ -27,7 +27,10 @@ module TriggerHelpers expected_timing, expected_events = fires_on.first expect(timing).to eq(expected_timing.to_s) expect(events).to match_array(Array.wrap(expected_events)) - expect(definition).to eq("execute procedure #{fn_name}()") + + # TODO: Update CREATE TRIGGER syntax to use EXECUTE FUNCTION + # https://gitlab.com/gitlab-org/gitlab/-/issues/227089 + expect(definition).to match(%r{execute (?:procedure|function) #{fn_name}()}) end private diff --git a/spec/support/helpers/usage_data_helpers.rb b/spec/support/helpers/usage_data_helpers.rb index f6c415a75bc..a4f40a4af0a 100644 --- a/spec/support/helpers/usage_data_helpers.rb +++ b/spec/support/helpers/usage_data_helpers.rb @@ -78,7 +78,6 @@ module UsageDataHelpers labels lfs_objects merge_requests - merge_requests_users milestone_lists milestones notes @@ -89,8 +88,6 @@ module UsageDataHelpers projects_jira_active projects_jira_server_active projects_jira_cloud_active - projects_slack_notifications_active - projects_slack_slash_active projects_slack_active projects_slack_slash_commands_active projects_custom_issue_tracker_active @@ -231,6 +228,15 @@ module UsageDataHelpers def allow_prometheus_queries allow_next_instance_of(Gitlab::PrometheusClient) do |client| allow(client).to receive(:aggregate).and_return({}) + allow(client).to receive(:query).and_return({}) + end + end + + def for_defined_days_back(days: [29, 2]) + days.each do |n| + Timecop.travel(n.days.ago) do + yield + end end end end |