Commit a0497a7b authored by Pawel Chojnacki's avatar Pawel Chojnacki

Move helpers to spec file, use const's instead of helper methods

parent 8067efdf
...@@ -2,6 +2,9 @@ module Gitlab ...@@ -2,6 +2,9 @@ module Gitlab
module HealthChecks module HealthChecks
class FsShardsCheck class FsShardsCheck
extend BaseAbstractCheck extend BaseAbstractCheck
RANDOM_STRING = SecureRandom.hex(1000).freeze
COMMAND_TIMEOUT = '1'.freeze
TIMEOUT_EXECUTABLE = 'timeout'.freeze
class << self class << self
def readiness def readiness
...@@ -41,9 +44,6 @@ module Gitlab ...@@ -41,9 +44,6 @@ module Gitlab
private private
RANDOM_STRING = SecureRandom.hex(1000).freeze
COMMAND_TIMEOUT = 1.second
def operation_metrics(ok_metric, latency_metric, operation, **labels) def operation_metrics(ok_metric, latency_metric, operation, **labels)
with_timing operation do |result, elapsed| with_timing operation do |result, elapsed|
[ [
...@@ -64,12 +64,8 @@ module Gitlab ...@@ -64,12 +64,8 @@ module Gitlab
@storage_paths ||= Gitlab.config.repositories.storages @storage_paths ||= Gitlab.config.repositories.storages
end end
def with_timeout(args)
%W{timeout #{COMMAND_TIMEOUT.to_i}}.concat(args)
end
def exec_with_timeout(cmd_args, *args, &block) 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 end
def tmp_file_path(storage_name) def tmp_file_path(storage_name)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::HealthChecks::FsShardsCheck do 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(:metric_class) { Gitlab::HealthChecks::Metric }
let(:result_class) { Gitlab::HealthChecks::Result } let(:result_class) { Gitlab::HealthChecks::Result }
...@@ -17,6 +33,7 @@ describe Gitlab::HealthChecks::FsShardsCheck do ...@@ -17,6 +33,7 @@ describe Gitlab::HealthChecks::FsShardsCheck do
before do before do
allow(described_class).to receive(:repository_storages) { repository_storages } allow(described_class).to receive(:repository_storages) { repository_storages }
allow(described_class).to receive(:storages_paths) { storages_paths } allow(described_class).to receive(:storages_paths) { storages_paths }
stub_const('Gitlab::HealthChecks::FsShardsCheck::TIMEOUT_EXECUTABLE', timeout_command)
end end
after do after do
...@@ -109,10 +126,10 @@ describe Gitlab::HealthChecks::FsShardsCheck do ...@@ -109,10 +126,10 @@ describe Gitlab::HealthChecks::FsShardsCheck do
end end
context 'when timeout kills fs checks' do context 'when timeout kills fs checks' do
let(:timeout_seconds) { 1.to_s }
before do 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) FileUtils.chmod_R(0755, tmp_dir)
end end
...@@ -140,7 +157,6 @@ describe Gitlab::HealthChecks::FsShardsCheck do ...@@ -140,7 +157,6 @@ describe Gitlab::HealthChecks::FsShardsCheck do
end end
context 'when popen always finds required binaries' do context 'when popen always finds required binaries' do
let(:timeout_seconds) { 30.to_s }
before do before do
allow(described_class).to receive(:exec_with_timeout).and_wrap_original do |method, *args, &block| allow(described_class).to receive(:exec_with_timeout).and_wrap_original do |method, *args, &block|
begin begin
...@@ -150,9 +166,7 @@ describe Gitlab::HealthChecks::FsShardsCheck do ...@@ -150,9 +166,7 @@ describe Gitlab::HealthChecks::FsShardsCheck do
end end
end end
allow(described_class).to receive(:with_timeout) do |args, &block| stub_const('Gitlab::HealthChecks::FsShardsCheck::COMMAND_TIMEOUT', '10')
[timeout_command, timeout_seconds].concat(args)
end
end end
it_behaves_like 'filesystem checks' it_behaves_like 'filesystem checks'
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment