From a0497a7bc0726dfc7bc1b0f5568f0353436e128b Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Thu, 25 May 2017 20:53:42 +0200 Subject: [PATCH] Move helpers to spec file, use const's instead of helper methods --- lib/gitlab/health_checks/fs_shards_check.rb | 12 +++----- .../health_checks/fs_shards_check_spec.rb | 30 ++++++++++++++----- spec/support/timeout_helper.rb | 19 ------------ 3 files changed, 26 insertions(+), 35 deletions(-) delete mode 100644 spec/support/timeout_helper.rb diff --git a/lib/gitlab/health_checks/fs_shards_check.rb b/lib/gitlab/health_checks/fs_shards_check.rb index 90612af3d63..e78b7f22e03 100644 --- a/lib/gitlab/health_checks/fs_shards_check.rb +++ b/lib/gitlab/health_checks/fs_shards_check.rb @@ -2,6 +2,9 @@ module Gitlab module HealthChecks class FsShardsCheck extend BaseAbstractCheck + RANDOM_STRING = SecureRandom.hex(1000).freeze + COMMAND_TIMEOUT = '1'.freeze + TIMEOUT_EXECUTABLE = 'timeout'.freeze class << self def readiness @@ -41,9 +44,6 @@ module Gitlab private - RANDOM_STRING = SecureRandom.hex(1000).freeze - COMMAND_TIMEOUT = 1.second - def operation_metrics(ok_metric, latency_metric, operation, **labels) with_timing operation do |result, elapsed| [ @@ -64,12 +64,8 @@ module Gitlab @storage_paths ||= Gitlab.config.repositories.storages end - def with_timeout(args) - %W{timeout #{COMMAND_TIMEOUT.to_i}}.concat(args) - end - def exec_with_timeout(cmd_args, *args, &block) - Gitlab::Popen.popen(with_timeout(cmd_args), *args, &block) + Gitlab::Popen.popen([TIMEOUT_EXECUTABLE, COMMAND_TIMEOUT].concat(cmd_args), *args, &block) end def tmp_file_path(storage_name) diff --git a/spec/lib/gitlab/health_checks/fs_shards_check_spec.rb b/spec/lib/gitlab/health_checks/fs_shards_check_spec.rb index 1f0cfc1dcf6..61c10d47434 100644 --- a/spec/lib/gitlab/health_checks/fs_shards_check_spec.rb +++ b/spec/lib/gitlab/health_checks/fs_shards_check_spec.rb @@ -1,7 +1,23 @@ require 'spec_helper' describe Gitlab::HealthChecks::FsShardsCheck do - include TimeoutHelper + def command_exists?(command) + _, status = Gitlab::Popen.popen(%W{ #{command} 1 echo }) + status == 0 + rescue Errno::ENOENT + false + end + + def timeout_command + @timeout_command ||= + if command_exists?('timeout') + 'timeout' + elsif command_exists?('gtimeout') + 'gtimeout' + else + '' + end + end let(:metric_class) { Gitlab::HealthChecks::Metric } let(:result_class) { Gitlab::HealthChecks::Result } @@ -17,6 +33,7 @@ describe Gitlab::HealthChecks::FsShardsCheck do before do allow(described_class).to receive(:repository_storages) { repository_storages } allow(described_class).to receive(:storages_paths) { storages_paths } + stub_const('Gitlab::HealthChecks::FsShardsCheck::TIMEOUT_EXECUTABLE', timeout_command) end after do @@ -109,10 +126,10 @@ describe Gitlab::HealthChecks::FsShardsCheck do end context 'when timeout kills fs checks' do - let(:timeout_seconds) { 1.to_s } - before do - allow(described_class).to receive(:with_timeout) { [timeout_command, timeout_seconds, 'sleep', '20'] } + stub_const('Gitlab::HealthChecks::FsShardsCheck::COMMAND_TIMEOUT', '1') + + allow(described_class).to receive(:exec_with_timeout).and_wrap_original { |m| m.call(%w(sleep 60)) } FileUtils.chmod_R(0755, tmp_dir) end @@ -140,7 +157,6 @@ describe Gitlab::HealthChecks::FsShardsCheck do end context 'when popen always finds required binaries' do - let(:timeout_seconds) { 30.to_s } before do allow(described_class).to receive(:exec_with_timeout).and_wrap_original do |method, *args, &block| begin @@ -150,9 +166,7 @@ describe Gitlab::HealthChecks::FsShardsCheck do end end - allow(described_class).to receive(:with_timeout) do |args, &block| - [timeout_command, timeout_seconds].concat(args) - end + stub_const('Gitlab::HealthChecks::FsShardsCheck::COMMAND_TIMEOUT', '10') end it_behaves_like 'filesystem checks' diff --git a/spec/support/timeout_helper.rb b/spec/support/timeout_helper.rb deleted file mode 100644 index 94d50049569..00000000000 --- a/spec/support/timeout_helper.rb +++ /dev/null @@ -1,19 +0,0 @@ -module TimeoutHelper - def command_exists?(command) - _, status = Gitlab::Popen.popen(%W{ #{command} 1 echo }) - status == 0 - rescue Errno::ENOENT - false - end - - def timeout_command - @timeout_command ||= - if command_exists?('timeout') - 'timeout' - elsif command_exists?('gtimeout') - 'gtimeout' - else - '' - end - end -end