From 3e5477b3ab1838a75dc5461e4176b5631bf08412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 14 Sep 2017 17:22:49 +0200 Subject: Force `RAILS_ENV` to `test` in `spec/spec_helper.rb` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ff1754fbe7e..92735336572 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ require './spec/simplecov_env' SimpleCovEnv.start! -ENV["RAILS_ENV"] ||= 'test' +ENV["RAILS_ENV"] = 'test' ENV["IN_MEMORY_APPLICATION_SETTINGS"] = 'true' require File.expand_path("../../config/environment", __FILE__) -- cgit v1.2.1 From 827783054fc0281351f37bbea102703675b7048c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 14 Sep 2017 17:23:10 +0200 Subject: Abort when `TestEnv.init` is not called in the `test` environment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/test_env.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index 71b9deeabc3..44540e39437 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -63,6 +63,11 @@ module TestEnv # See gitlab.yml.example test section for paths # def init(opts = {}) + unless Rails.env.test? + puts "\nTestEnv.init can only be run if `RAILS_ENV` is set to 'test' not '#{Rails.env}'!\n" + exit 1 + end + # Disable mailer for spinach tests disable_mailer if opts[:mailer] == false -- cgit v1.2.1 From 895ec1a15e5f014d1a7b4641c822c5b65dd4bbb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 14 Sep 2017 17:40:41 +0200 Subject: Make TestInit.setup_gitlab_shell and TestInit.setup_gitaly more robust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/test_env.rb | 81 +++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index 44540e39437..6e5b9700b54 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -3,6 +3,8 @@ require 'rspec/mocks' module TestEnv extend self + ComponentFailedToInstallError = Class.new(StandardError) + # When developing the seed repository, comment out the branch you will modify. BRANCH_SHA = { 'signed-commits' => '2d1096e', @@ -127,50 +129,23 @@ module TestEnv end def setup_gitlab_shell - puts "\n==> Setting up Gitlab Shell..." - start = Time.now - gitlab_shell_dir = Gitlab.config.gitlab_shell.path - shell_needs_update = component_needs_update?(gitlab_shell_dir, - Gitlab::Shell.version_required) - - unless !shell_needs_update || system('rake', 'gitlab:shell:install') - puts "\nGitLab Shell failed to install, cleaning up #{gitlab_shell_dir}!\n" - FileUtils.rm_rf(gitlab_shell_dir) - exit 1 - end - - puts " GitLab Shell setup in #{Time.now - start} seconds...\n" + component_timed_setup('GitLab Shell', + install_dir: Gitlab.config.gitlab_shell.path, + version: Gitlab::Shell.version_required, + task: 'gitlab:shell:install') end def setup_gitaly - puts "\n==> Setting up Gitaly..." - start = Time.now socket_path = Gitlab::GitalyClient.address('default').sub(/\Aunix:/, '') gitaly_dir = File.dirname(socket_path) - if gitaly_dir_stale?(gitaly_dir) - puts " Gitaly is outdated, cleaning up #{gitaly_dir}!" - FileUtils.rm_rf(gitaly_dir) - end + component_timed_setup('Gitaly', + install_dir: gitaly_dir, + version: Gitlab::GitalyClient.expected_server_version, + task: "gitlab:gitaly:install[#{gitaly_dir}]") do - gitaly_needs_update = component_needs_update?(gitaly_dir, - Gitlab::GitalyClient.expected_server_version) - - unless !gitaly_needs_update || system('rake', "gitlab:gitaly:install[#{gitaly_dir}]") - puts "\nGitaly failed to install, cleaning up #{gitaly_dir}!\n" - FileUtils.rm_rf(gitaly_dir) - exit 1 + start_gitaly(gitaly_dir) end - - start_gitaly(gitaly_dir) - puts " Gitaly setup in #{Time.now - start} seconds...\n" - end - - def gitaly_dir_stale?(dir) - gitaly_executable = File.join(dir, 'gitaly') - return false unless File.exist?(gitaly_executable) - - File.mtime(gitaly_executable) < File.mtime(Rails.root.join('GITALY_SERVER_VERSION')) end def start_gitaly(gitaly_dir) @@ -325,6 +300,40 @@ module TestEnv end 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) + + if component_needs_update?(install_dir, version) + # 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? + + rescue ComponentFailedToInstallError + puts "\n#{component} failed to install, cleaning up #{install_dir}!\n" + FileUtils.rm_rf(install_dir) + exit 1 + ensure + puts " #{component} setup in #{Time.now - start} seconds...\n" + end + + def ensure_component_dir_name_is_correct!(component, path) + actual_component_dir_name = File.basename(path) + expected_component_dir_name = component.parameterize + + unless actual_component_dir_name == expected_component_dir_name + puts " #{component} install dir should be named '#{expected_component_dir_name}', not '#{actual_component_dir_name}' (full install path given was '#{path}')!\n" + exit 1 + end + end + def component_needs_update?(component_folder, expected_version) version = File.read(File.join(component_folder, 'VERSION')).strip -- cgit v1.2.1