From 5c6ae5e374d7ab51de8030d129159efce4557a05 Mon Sep 17 00:00:00 2001
From: Alex Kalderimis <akalderimis@gitlab.com>
Date: Thu, 25 Jun 2020 16:48:26 +0000
Subject: [PATCH] Allow tests to be quiet

Our tests print a lot of repetitive guff that is helpful in debugging
circumstances, but otherwise gets in the way. This change allows
(opt-in) for a developer to turn off these messages by setting
`GITLAB_TESTING_QUIET` to anything other than `"false"`.
---
 scripts/gitaly_test.rb           | 23 ++++++++++++++++-------
 spec/support/helpers/test_env.rb | 12 ++++++------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/scripts/gitaly_test.rb b/scripts/gitaly_test.rb
index c69c4ea747ba..54bf07b37733 100644
--- a/scripts/gitaly_test.rb
+++ b/scripts/gitaly_test.rb
@@ -6,8 +6,16 @@
 
 require 'securerandom'
 require 'socket'
+require 'logger'
 
 module GitalyTest
+  LOGGER = begin
+             default_name = ENV['CI'] ? 'DEBUG' : 'WARN'
+             level_name = ENV['GITLAB_TESTING_LOG_LEVEL']&.upcase
+             level = Logger.const_get(level_name || default_name, true) # rubocop: disable Gitlab/ConstGetInheritFalse
+             Logger.new(STDOUT, level: level, formatter: ->(_, _, _, msg) { msg })
+           end
+
   def tmp_tests_gitaly_dir
     File.expand_path('../tmp/tests/gitaly', __dir__)
   end
@@ -98,7 +106,7 @@ def ensure_gitlab_shell_secret!
   end
 
   def check_gitaly_config!
-    puts "Checking gitaly-ruby Gemfile..."
+    LOGGER.debug "Checking gitaly-ruby Gemfile...\n"
 
     unless File.exist?(gemfile)
       message = "#{gemfile} does not exist."
@@ -106,8 +114,9 @@ def check_gitaly_config!
       abort message
     end
 
-    puts 'Checking gitaly-ruby bundle...'
-    abort 'bundle check failed' unless system(env, 'bundle', 'check', chdir: File.dirname(gemfile))
+    LOGGER.debug "Checking gitaly-ruby bundle...\n"
+    out = ENV['CI'] ? STDOUT : '/dev/null'
+    abort 'bundle check failed' unless system(env, 'bundle', 'check', out: out, chdir: File.dirname(gemfile))
   end
 
   def read_socket_path(service)
@@ -126,22 +135,22 @@ def read_socket_path(service)
   end
 
   def try_connect!(service)
-    print "Trying to connect to #{service}: "
+    LOGGER.debug "Trying to connect to #{service}: "
     timeout = 20
     delay = 0.1
     socket = read_socket_path(service)
 
     Integer(timeout / delay).times do
       UNIXSocket.new(socket)
-      puts ' OK'
+      LOGGER.debug " OK\n"
 
       return
     rescue Errno::ENOENT, Errno::ECONNREFUSED
-      print '.'
+      LOGGER.debug '.'
       sleep delay
     end
 
-    puts ' FAILED'
+    LOGGER.warn " FAILED to connect to #{service}\n"
 
     raise "could not connect to #{socket}"
   end
diff --git a/spec/support/helpers/test_env.rb b/spec/support/helpers/test_env.rb
index db70c3e82481..09d16f306fdc 100644
--- a/spec/support/helpers/test_env.rb
+++ b/spec/support/helpers/test_env.rb
@@ -169,8 +169,9 @@ def setup_gitaly
       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
@@ -463,7 +464,6 @@ def set_repo_refs(repo_path, branch_sha)
   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)
@@ -472,22 +472,22 @@ def component_timed_setup(component, install_dir:, version:, task:)
     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?
-- 
GitLab