From f67778c0624521150cb10dbfc5687f90fb008757 Mon Sep 17 00:00:00 2001
From: Toon Claes <toon@gitlab.com>
Date: Tue, 14 Dec 2021 19:41:52 +0100
Subject: [PATCH] test: Move spawning of Gitaly to GitalySetup

GitalySetup was shelling out to scripts/gitaly-test-spawn to spawn
Gitaly processes, which was quite complicated to understand.

This change simplifies the back-and-forth between the script and the
Ruby file. It avoids writing the PID of the Gitaly processes to file, to
pass them back to the test environment.

With the previous changes included, now scripts/gitaly-test-build &
scripts/gitaly-test-spawn are *only* used in CI. When running tests in a
local GDK, the Ruby code to build and spawn Gitaly in test is directly
called from TestEnv.init.
---
 scripts/gitaly-test-spawn            | 12 ++----------
 spec/support/helpers/gitaly_setup.rb | 24 +++++++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/scripts/gitaly-test-spawn b/scripts/gitaly-test-spawn
index 2e0e51124afe4..3bf2de7676085 100755
--- a/scripts/gitaly-test-spawn
+++ b/scripts/gitaly-test-spawn
@@ -9,16 +9,8 @@ class GitalyTestSpawn
   include GitalySetup
 
   def run
-    install_gitaly_gems if ENV['CI']
-    check_gitaly_config!
-
-    # # Uncomment line below to see all gitaly logs merged into CI trace
-    # spawn('sleep 1; tail -f log/gitaly-test.log')
-
-    # In local development this pid file is used by rspec.
-    IO.write(File.expand_path('../tmp/tests/gitaly.pid', __dir__), start_gitaly)
-    IO.write(File.expand_path('../tmp/tests/gitaly2.pid', __dir__), start_gitaly2)
-    IO.write(File.expand_path('../tmp/tests/praefect.pid', __dir__), start_praefect)
+    install_gitaly_gems
+    spawn_gitaly
   end
 end
 
diff --git a/spec/support/helpers/gitaly_setup.rb b/spec/support/helpers/gitaly_setup.rb
index 3902d2b18472b..6284a1bcef886 100644
--- a/spec/support/helpers/gitaly_setup.rb
+++ b/spec/support/helpers/gitaly_setup.rb
@@ -285,25 +285,27 @@ def stop(pid)
   end
 
   def spawn_gitaly
-    spawn_script = Rails.root.join('scripts/gitaly-test-spawn').to_s
-    Bundler.with_original_env do
-      unless system(spawn_script)
-        message = 'gitaly spawn failed'
-        message += " (try `rm -rf #{gitaly_dir}` ?)" unless ENV['CI']
-        raise message
-      end
-    end
+    check_gitaly_config!
 
-    gitaly_pid = Integer(File.read(TMP_TEST_PATH.join('gitaly.pid')))
-    gitaly2_pid = Integer(File.read(TMP_TEST_PATH.join('gitaly2.pid')))
-    praefect_pid = Integer(File.read(TMP_TEST_PATH.join('praefect.pid')))
+    gitaly_pid = start_gitaly
+    gitaly2_pid = start_gitaly2
+    praefect_pid = start_praefect
 
     Kernel.at_exit do
+      # In CI this function is called by scripts/gitaly-test-spawn, triggered a
+      # before_script. Gitaly needs to remain running until the container is
+      # stopped.
+      next if ENV['CI']
+
       pids = [gitaly_pid, gitaly2_pid, praefect_pid]
       pids.each { |pid| stop(pid) }
     end
 
     wait('gitaly')
     wait('praefect')
+  rescue StandardError
+    message = 'gitaly spawn failed'
+    message += " (try `rm -rf #{gitaly_dir}` ?)" unless ENV['CI']
+    raise message
   end
 end
-- 
GitLab