diff options
Diffstat (limited to 'spec/support/helpers')
-rw-r--r-- | spec/support/helpers/board_helpers.rb | 9 | ||||
-rw-r--r-- | spec/support/helpers/ci_artifact_metadata_generator.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/cycle_analytics_helpers.rb | 8 | ||||
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 16 | ||||
-rw-r--r-- | spec/support/helpers/javascript_fixtures_helpers.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/jira_service_helper.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/key_generator_helper.rb | 3 | ||||
-rw-r--r-- | spec/support/helpers/navbar_structure_helper.rb | 12 | ||||
-rw-r--r-- | spec/support/helpers/next_instance_of.rb | 15 | ||||
-rw-r--r-- | spec/support/helpers/query_recorder.rb | 40 | ||||
-rw-r--r-- | spec/support/helpers/reload_helpers.rb | 12 | ||||
-rw-r--r-- | spec/support/helpers/rubygems_helpers.rb | 11 | ||||
-rw-r--r-- | spec/support/helpers/seed_repo.rb | 60 | ||||
-rw-r--r-- | spec/support/helpers/stub_env.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/stub_requests.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/test_env.rb | 11 | ||||
-rw-r--r-- | spec/support/helpers/usage_data_helpers.rb | 24 |
17 files changed, 154 insertions, 77 deletions
diff --git a/spec/support/helpers/board_helpers.rb b/spec/support/helpers/board_helpers.rb index 683ee3e4bf2..6e145fed733 100644 --- a/spec/support/helpers/board_helpers.rb +++ b/spec/support/helpers/board_helpers.rb @@ -5,14 +5,5 @@ module BoardHelpers within card do first('.board-card-number').click end - - wait_for_sidebar - end - - def wait_for_sidebar - # loop until the CSS transition is complete - Timeout.timeout(0.5) do - loop until evaluate_script('$(".right-sidebar").outerWidth()') == 290 - end end end diff --git a/spec/support/helpers/ci_artifact_metadata_generator.rb b/spec/support/helpers/ci_artifact_metadata_generator.rb index e02501565a9..ae821d6582b 100644 --- a/spec/support/helpers/ci_artifact_metadata_generator.rb +++ b/spec/support/helpers/ci_artifact_metadata_generator.rb @@ -7,7 +7,7 @@ class CiArtifactMetadataGenerator attr_accessor :entries, :output - ARTIFACT_METADATA = "GitLab Build Artifacts Metadata 0.0.2\n".freeze + ARTIFACT_METADATA = "GitLab Build Artifacts Metadata 0.0.2\n" def initialize(stream) @entries = {} diff --git a/spec/support/helpers/cycle_analytics_helpers.rb b/spec/support/helpers/cycle_analytics_helpers.rb index 14041ad0ac6..9e62eef14de 100644 --- a/spec/support/helpers/cycle_analytics_helpers.rb +++ b/spec/support/helpers/cycle_analytics_helpers.rb @@ -3,15 +3,15 @@ module CycleAnalyticsHelpers include GitHelpers - def wait_for_stages_to_load - expect(page).to have_selector '.js-stage-table' + def wait_for_stages_to_load(selector = '.js-path-navigation') + expect(page).to have_selector selector wait_for_requests end - def select_group(target_group) + def select_group(target_group, ready_selector = '.js-path-navigation') visit group_analytics_cycle_analytics_path(target_group) - wait_for_stages_to_load + wait_for_stages_to_load(ready_selector) end def toggle_dropdown(field) diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index 75d9508f470..d714f04fbba 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -222,9 +222,12 @@ module GraphqlHelpers lazy_vals.is_a?(Array) ? lazy_vals.map { |val| sync(val) } : sync(lazy_vals) end - def graphql_query_for(name, args = {}, selection = nil) + def graphql_query_for(name, args = {}, selection = nil, operation_name = nil) type = GitlabSchema.types['Query'].fields[GraphqlHelpers.fieldnamerize(name)]&.type - wrap_query(query_graphql_field(name, args, selection, type)) + query = wrap_query(query_graphql_field(name, args, selection, type)) + query = "query #{operation_name}#{query}" if operation_name + + query end def wrap_query(query) @@ -274,11 +277,11 @@ module GraphqlHelpers # prepare_input_for_mutation({ 'my_key' => 1 }) # => { 'myKey' => 1} def prepare_input_for_mutation(input) - input.map do |name, value| + input.to_h do |name, value| value = prepare_input_for_mutation(value) if value.is_a?(Hash) [GraphqlHelpers.fieldnamerize(name), value] - end.to_h + end end def input_variable_name_for_mutation(mutation_name) @@ -304,7 +307,10 @@ module GraphqlHelpers def query_graphql_field(name, attributes = {}, fields = nil, type = nil) type ||= name.to_s.classify - attributes, fields = [nil, attributes] if fields.nil? && !attributes.is_a?(Hash) + if fields.nil? && !attributes.is_a?(Hash) + fields = attributes + attributes = nil + end field = field_with_params(name, attributes) diff --git a/spec/support/helpers/javascript_fixtures_helpers.rb b/spec/support/helpers/javascript_fixtures_helpers.rb index 09425c3742a..28375c1d51e 100644 --- a/spec/support/helpers/javascript_fixtures_helpers.rb +++ b/spec/support/helpers/javascript_fixtures_helpers.rb @@ -31,7 +31,7 @@ module JavaScriptFixturesHelpers # def clean_frontend_fixtures(directory_name) full_directory_name = File.expand_path(directory_name, fixture_root_path) - Dir[File.expand_path('*.html', full_directory_name)].each do |file_name| + Dir[File.expand_path('*.{html,json,md}', full_directory_name)].each do |file_name| FileUtils.rm(file_name) end end diff --git a/spec/support/helpers/jira_service_helper.rb b/spec/support/helpers/jira_service_helper.rb index 698490c8c92..ce908d53f88 100644 --- a/spec/support/helpers/jira_service_helper.rb +++ b/spec/support/helpers/jira_service_helper.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module JiraServiceHelper - JIRA_URL = "http://jira.example.net".freeze + JIRA_URL = "http://jira.example.net" JIRA_API = JIRA_URL + "/rest/api/2" def jira_service_settings diff --git a/spec/support/helpers/key_generator_helper.rb b/spec/support/helpers/key_generator_helper.rb index 59c8eeb3692..58bde80a31f 100644 --- a/spec/support/helpers/key_generator_helper.rb +++ b/spec/support/helpers/key_generator_helper.rb @@ -27,7 +27,8 @@ module Spec # Encodes an openssh-mpi-encoded integer. def encode_mpi(n) # rubocop:disable Naming/UncommunicativeMethodParamName - chars, n = [], n.to_i + chars = [] + n = n.to_i chars << (n & 0xff) && n >>= 8 while n != 0 chars << 0 if chars.empty? || chars.last >= 0x80 chars.reverse.pack('C*') diff --git a/spec/support/helpers/navbar_structure_helper.rb b/spec/support/helpers/navbar_structure_helper.rb index e18a708e41c..826108a63a5 100644 --- a/spec/support/helpers/navbar_structure_helper.rb +++ b/spec/support/helpers/navbar_structure_helper.rb @@ -29,7 +29,7 @@ module NavbarStructureHelper ) end - def insert_container_nav(within) + def insert_container_nav insert_after_sub_nav_item( _('Package Registry'), within: _('Packages & Registries'), @@ -37,11 +37,19 @@ module NavbarStructureHelper ) end - def insert_dependency_proxy_nav(within) + def insert_dependency_proxy_nav insert_after_sub_nav_item( _('Package Registry'), within: _('Packages & Registries'), new_sub_nav_item_name: _('Dependency Proxy') ) end + + def insert_infrastructure_registry_nav + insert_after_sub_nav_item( + _('Package Registry'), + within: _('Packages & Registries'), + new_sub_nav_item_name: _('Infrastructure Registry') + ) + end end diff --git a/spec/support/helpers/next_instance_of.rb b/spec/support/helpers/next_instance_of.rb index a8e9ab2bafe..95d8936588c 100644 --- a/spec/support/helpers/next_instance_of.rb +++ b/spec/support/helpers/next_instance_of.rb @@ -2,25 +2,26 @@ module NextInstanceOf def expect_next_instance_of(klass, *new_args, &blk) - stub_new(expect(klass), nil, *new_args, &blk) + stub_new(expect(klass), nil, false, *new_args, &blk) end - def expect_next_instances_of(klass, number, *new_args, &blk) - stub_new(expect(klass), number, *new_args, &blk) + def expect_next_instances_of(klass, number, ordered = false, *new_args, &blk) + stub_new(expect(klass), number, ordered, *new_args, &blk) end def allow_next_instance_of(klass, *new_args, &blk) - stub_new(allow(klass), nil, *new_args, &blk) + stub_new(allow(klass), nil, false, *new_args, &blk) end - def allow_next_instances_of(klass, number, *new_args, &blk) - stub_new(allow(klass), number, *new_args, &blk) + def allow_next_instances_of(klass, number, ordered = false, *new_args, &blk) + stub_new(allow(klass), number, ordered, *new_args, &blk) end private - def stub_new(target, number, *new_args, &blk) + def stub_new(target, number, ordered = false, *new_args, &blk) receive_new = receive(:new) + receive_new.ordered if ordered receive_new.exactly(number).times if number receive_new.with(*new_args) if new_args.any? diff --git a/spec/support/helpers/query_recorder.rb b/spec/support/helpers/query_recorder.rb index 61634813a1c..2d880c7a8fe 100644 --- a/spec/support/helpers/query_recorder.rb +++ b/spec/support/helpers/query_recorder.rb @@ -3,37 +3,53 @@ module ActiveRecord class QueryRecorder attr_reader :log, :skip_cached, :cached, :data - UNKNOWN = %w(unknown unknown).freeze - def initialize(skip_cached: true, query_recorder_debug: false, &block) - @data = Hash.new { |h, k| h[k] = { count: 0, occurrences: [], backtrace: [] } } + UNKNOWN = %w[unknown unknown].freeze + + def initialize(skip_cached: true, log_file: nil, query_recorder_debug: false, &block) + @data = Hash.new { |h, k| h[k] = { count: 0, occurrences: [], backtrace: [], durations: [] } } @log = [] @cached = [] @skip_cached = skip_cached - @query_recorder_debug = query_recorder_debug + @query_recorder_debug = ENV['QUERY_RECORDER_DEBUG'] || query_recorder_debug + @log_file = log_file # force replacement of bind parameters to give tests the ability to check for ids ActiveRecord::Base.connection.unprepared_statement do ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block) end end - def show_backtrace(values) - Rails.logger.debug("QueryRecorder SQL: #{values[:sql]}") + def show_backtrace(values, duration) + values[:sql].lines.each do |line| + print_to_log(:SQL, line) + end + print_to_log(:DURATION, duration) Gitlab::BacktraceCleaner.clean_backtrace(caller).each do |line| - Rails.logger.debug("QueryRecorder backtrace: --> #{line}") + print_to_log(:backtrace, line) + end + end + + def print_to_log(label, line) + msg = "QueryRecorder #{label}: --> #{line}" + + if @log_file + @log_file.puts(msg) + else + Rails.logger.debug(msg) end end def get_sql_source(sql) - matches = sql.match(/,line:(?<line>.*):in\s+`(?<method>.*)'\*\//) + matches = sql.match(%r{,line:(?<line>.*):in\s+`(?<method>.*)'\*/}) matches ? [matches[:line], matches[:method]] : UNKNOWN end - def store_sql_by_source(values: {}, backtrace: nil) + def store_sql_by_source(values: {}, duration: nil, backtrace: nil) full_name = get_sql_source(values[:sql]).join(':') @data[full_name][:count] += 1 @data[full_name][:occurrences] << values[:sql] @data[full_name][:backtrace] << backtrace + @data[full_name][:durations] << duration end def find_query(query_regexp, limit, first_only: false) @@ -55,14 +71,14 @@ module ActiveRecord end def callback(name, start, finish, message_id, values) - store_backtrace = ENV['QUERY_RECORDER_DEBUG'] || @query_recorder_debug - backtrace = store_backtrace ? show_backtrace(values) : nil + duration = finish - start if values[:cached] && skip_cached @cached << values[:sql] elsif !values[:name]&.include?("SCHEMA") + backtrace = @query_recorder_debug ? show_backtrace(values, duration) : nil @log << values[:sql] - store_sql_by_source(values: values, backtrace: backtrace) + store_sql_by_source(values: values, duration: duration, backtrace: backtrace) end end diff --git a/spec/support/helpers/reload_helpers.rb b/spec/support/helpers/reload_helpers.rb new file mode 100644 index 00000000000..60811e4604f --- /dev/null +++ b/spec/support/helpers/reload_helpers.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module ReloadHelpers + def reload_models(*models) + models.map(&:reload) + end + + def subject_and_reload(*models) + subject + reload_models(*models) + end +end diff --git a/spec/support/helpers/rubygems_helpers.rb b/spec/support/helpers/rubygems_helpers.rb new file mode 100644 index 00000000000..6a808f52e97 --- /dev/null +++ b/spec/support/helpers/rubygems_helpers.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module RubygemsHelpers + def gem_from_file(file) + full_path = File.expand_path( + Rails.root.join('spec', 'fixtures', 'packages', 'rubygems', file.filename) + ) + + Gem::Package.new(File.open(full_path)) + end +end diff --git a/spec/support/helpers/seed_repo.rb b/spec/support/helpers/seed_repo.rb index 20738b45129..74ac529a3de 100644 --- a/spec/support/helpers/seed_repo.rb +++ b/spec/support/helpers/seed_repo.rb @@ -31,64 +31,64 @@ module SeedRepo module BigCommit - ID = "913c66a37b4a45b9769037c55c2d238bd0942d2e".freeze - PARENT_ID = "cfe32cf61b73a0d5e9f13e774abde7ff789b1660".freeze - MESSAGE = "Files, encoding and much more".freeze - AUTHOR_FULL_NAME = "Dmitriy Zaporozhets".freeze + ID = "913c66a37b4a45b9769037c55c2d238bd0942d2e" + PARENT_ID = "cfe32cf61b73a0d5e9f13e774abde7ff789b1660" + MESSAGE = "Files, encoding and much more" + AUTHOR_FULL_NAME = "Dmitriy Zaporozhets" FILES_COUNT = 2 end module Commit - ID = "570e7b2abdd848b95f2f578043fc23bd6f6fd24d".freeze - PARENT_ID = "6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9".freeze - MESSAGE = "Change some files\n\nSigned-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>\n".freeze - AUTHOR_FULL_NAME = "Dmitriy Zaporozhets".freeze + ID = "570e7b2abdd848b95f2f578043fc23bd6f6fd24d" + PARENT_ID = "6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9" + MESSAGE = "Change some files\n\nSigned-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>\n" + AUTHOR_FULL_NAME = "Dmitriy Zaporozhets" FILES = ["files/ruby/popen.rb", "files/ruby/regex.rb"].freeze FILES_COUNT = 2 - C_FILE_PATH = "files/ruby".freeze + C_FILE_PATH = "files/ruby" C_FILES = ["popen.rb", "regex.rb", "version_info.rb"].freeze - BLOB_FILE = %{%h3= @key.title\n%hr\n%pre= @key.key\n.actions\n = link_to 'Remove', @key, :confirm => 'Are you sure?', :method => :delete, :class => \"btn danger delete-key\"\n\n\n}.freeze - BLOB_FILE_PATH = "app/views/keys/show.html.haml".freeze + BLOB_FILE = %{%h3= @key.title\n%hr\n%pre= @key.key\n.actions\n = link_to 'Remove', @key, :confirm => 'Are you sure?', :method => :delete, :class => \"btn danger delete-key\"\n\n\n} + BLOB_FILE_PATH = "app/views/keys/show.html.haml" end module EmptyCommit - ID = "b0e52af38d7ea43cf41d8a6f2471351ac036d6c9".freeze - PARENT_ID = "40f4a7a617393735a95a0bb67b08385bc1e7c66d".freeze - MESSAGE = "Empty commit".freeze - AUTHOR_FULL_NAME = "Rémy Coutable".freeze + ID = "b0e52af38d7ea43cf41d8a6f2471351ac036d6c9" + PARENT_ID = "40f4a7a617393735a95a0bb67b08385bc1e7c66d" + MESSAGE = "Empty commit" + AUTHOR_FULL_NAME = "Rémy Coutable" FILES = [].freeze FILES_COUNT = FILES.count end module EncodingCommit - ID = "40f4a7a617393735a95a0bb67b08385bc1e7c66d".freeze - PARENT_ID = "66028349a123e695b589e09a36634d976edcc5e8".freeze - MESSAGE = "Add ISO-8859-encoded file".freeze - AUTHOR_FULL_NAME = "Stan Hu".freeze + ID = "40f4a7a617393735a95a0bb67b08385bc1e7c66d" + PARENT_ID = "66028349a123e695b589e09a36634d976edcc5e8" + MESSAGE = "Add ISO-8859-encoded file" + AUTHOR_FULL_NAME = "Stan Hu" FILES = ["encoding/iso8859.txt"].freeze FILES_COUNT = FILES.count end module FirstCommit - ID = "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863".freeze + ID = "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863" PARENT_ID = nil - MESSAGE = "Initial commit".freeze - AUTHOR_FULL_NAME = "Dmitriy Zaporozhets".freeze + MESSAGE = "Initial commit" + AUTHOR_FULL_NAME = "Dmitriy Zaporozhets" FILES = ["LICENSE", ".gitignore", "README.md"].freeze FILES_COUNT = 3 end module LastCommit - ID = "4b4918a572fa86f9771e5ba40fbd48e1eb03e2c6".freeze - PARENT_ID = "0e1b353b348f8477bdbec1ef47087171c5032cd9".freeze - MESSAGE = "Merge branch 'master' into 'master'".freeze - AUTHOR_FULL_NAME = "Stan Hu".freeze + ID = "4b4918a572fa86f9771e5ba40fbd48e1eb03e2c6" + PARENT_ID = "0e1b353b348f8477bdbec1ef47087171c5032cd9" + MESSAGE = "Merge branch 'master' into 'master'" + AUTHOR_FULL_NAME = "Stan Hu" FILES = ["bin/executable"].freeze FILES_COUNT = FILES.count end module Repo - HEAD = "master".freeze + HEAD = "master" BRANCHES = %w[ feature fix @@ -111,9 +111,9 @@ module SeedRepo end module RubyBlob - ID = "7e3e39ebb9b2bf433b4ad17313770fbe4051649c".freeze - NAME = "popen.rb".freeze - CONTENT = <<-eos.freeze + ID = "7e3e39ebb9b2bf433b4ad17313770fbe4051649c" + NAME = "popen.rb" + CONTENT = <<-eos require 'fileutils' require 'open3' diff --git a/spec/support/helpers/stub_env.rb b/spec/support/helpers/stub_env.rb index 8107ffc939f..5f344f8fb52 100644 --- a/spec/support/helpers/stub_env.rb +++ b/spec/support/helpers/stub_env.rb @@ -14,7 +14,7 @@ module StubENV private - STUBBED_KEY = '__STUBBED__'.freeze + STUBBED_KEY = '__STUBBED__' def add_stubbed_value(key, value) allow(ENV).to receive(:[]).with(key).and_return(value) diff --git a/spec/support/helpers/stub_requests.rb b/spec/support/helpers/stub_requests.rb index 473f07dd413..a3810323fee 100644 --- a/spec/support/helpers/stub_requests.rb +++ b/spec/support/helpers/stub_requests.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module StubRequests - IP_ADDRESS_STUB = '8.8.8.9'.freeze + IP_ADDRESS_STUB = '8.8.8.9' # Fully stubs a request using WebMock class. This class also # stubs the IP address the URL is translated to (DNS lookup). diff --git a/spec/support/helpers/test_env.rb b/spec/support/helpers/test_env.rb index 266c0e18ccd..7ba15a9c00b 100644 --- a/spec/support/helpers/test_env.rb +++ b/spec/support/helpers/test_env.rb @@ -92,7 +92,7 @@ module TestEnv }.freeze TMP_TEST_PATH = Rails.root.join('tmp', 'tests').freeze - REPOS_STORAGE = 'default'.freeze + REPOS_STORAGE = 'default' SECOND_STORAGE_PATH = Rails.root.join('tmp', 'tests', 'second_storage') # Test environment @@ -170,7 +170,14 @@ module TestEnv install_dir: gitaly_dir, version: Gitlab::GitalyClient.expected_server_version, task: "gitlab:gitaly:install[#{install_gitaly_args}]") do - Gitlab::SetupHelper::Gitaly.create_configuration(gitaly_dir, { 'default' => repos_path }, force: true) + Gitlab::SetupHelper::Gitaly.create_configuration( + gitaly_dir, + { 'default' => repos_path }, + force: true, + options: { + prometheus_listen_addr: 'localhost:9236' + } + ) Gitlab::SetupHelper::Gitaly.create_configuration( gitaly_dir, { 'default' => repos_path }, diff --git a/spec/support/helpers/usage_data_helpers.rb b/spec/support/helpers/usage_data_helpers.rb index df79049123d..d05676a649e 100644 --- a/spec/support/helpers/usage_data_helpers.rb +++ b/spec/support/helpers/usage_data_helpers.rb @@ -174,6 +174,22 @@ module UsageDataHelpers allow(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false) end + def stub_prometheus_queries + stub_request(:get, %r{^https?://::1:9090/-/ready}) + .to_return( + status: 200, + body: [{}].to_json, + headers: { 'Content-Type' => 'application/json' } + ) + + stub_request(:get, %r{^https?://::1:9090/api/v1/query\?query=.*}) + .to_return( + status: 200, + body: [{}].to_json, + headers: { 'Content-Type' => 'application/json' } + ) + end + def clear_memoized_values(values) values.each { |v| described_class.clear_memoization(v) } end @@ -242,4 +258,12 @@ module UsageDataHelpers end end end + + def load_sample_metric_definition(filename: 'sample_metric.yml') + load_metric_yaml(fixture_file("lib/generators/gitlab/usage_metric_definition_generator/#{filename}")) + end + + def load_metric_yaml(data) + ::Gitlab::Config::Loader::Yaml.new(data).load_raw! + end end |