Skip to content
Snippets Groups Projects
Commit ad724066 authored by Robert Marshall's avatar Robert Marshall
Browse files

Refactor service_state and add queries


- Refactor service_state to be a read_only attribute
- Rename old service_state method to get_service_state
- Refactor rspecs associated to old service_state method
- Add method queries for service state via metaprogramming with an array
  whose position indicates the return code
- Add rspec tests for the service state method queries

Signed-off-by: default avatarRobert Marshall <rmarshall@gitlab.com>
parent 8e75cbff
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -5,6 +5,7 @@ require_relative '../pg_version'
class BasePgHelper < BaseHelper
include ShellOutHelper
attr_reader :node
attr_reader :service_state
 
PG_HASH_PATTERN ||= /\{(.*)\}/.freeze
PG_HASH_PAIR_SEPARATOR ||= ','.freeze
Loading
Loading
@@ -18,7 +19,18 @@ class BasePgHelper < BaseHelper
omnibus_helper.service_up?(service_name) || (delegated? && omnibus_helper.service_up?(delegate_service_name) && ready?)
end
 
def service_state
[
'accepting_connections?',
'rejecting_connections?',
'not_responding?',
'invalid_connection_parameters?'
].each.with_index do |method, index|
define_method method do
get_service_state == index
end
end
def get_service_state
bin = "#{node['package']['install-dir']}/embedded/bin/pg_isready"
dbname = node['gitlab']['gitlab-rails']['db_database']
dbhost = node['postgresql']['unix_socket_directory']
Loading
Loading
@@ -27,7 +39,7 @@ class BasePgHelper < BaseHelper
 
cmd = %W(#{bin} -d #{dbname} -h #{dbhost} -p #{port} -U #{pguser})
 
do_shell_out(cmd).exitstatus
@service_state = do_shell_out(cmd).exitstatus
end
 
def is_managed_and_offline?
Loading
Loading
Loading
Loading
@@ -135,18 +135,6 @@ RSpec.describe BasePgHelper do
end
end
 
describe '#service_state' do
before do
result = spy('shellout')
allow(result).to receive(:exitstatus).and_return(42)
allow(subject).to receive(:do_shell_out).and_return(result)
end
it 'returns an exit status from pg_isready' do
expect(subject.service_state).to eq(42)
end
end
describe '#user_options' do
before do
result = spy('shellout')
Loading
Loading
@@ -326,4 +314,62 @@ RSpec.describe BasePgHelper do
expect(subject.is_standby?).to be false
end
end
context 'when checking the connection to PostgreSQL' do
describe '#service_state' do
it 'returns an exit status from pg_isready' do
result = spy('shellout')
allow(subject).to receive(:do_shell_out).and_return(result)
allow(result).to receive(:exitstatus).and_return(42)
expect(subject.get_service_state).to eq(42)
end
end
context 'when invoking pg_isready' do
using RSpec::Parameterized::TableSyntax
where(:exit_code, :accepting, :rejecting, :not_responding, :invalid_parameters) do
0 | true | false | false | false
1 | false | true | false | false
2 | false | false | true | false
3 | false | false | false | true
end
with_them do
before do
result = spy('shellout')
allow(subject).to receive(:do_shell_out).and_return(result)
allow(result).to receive(:exitstatus).and_return(exit_code)
end
it 'returns the last service state checked' do
expect { subject.get_service_state }.to change { subject.service_state }.from(nil).to(exit_code)
end
describe '#accepting_connections?' do
it 'returns the expected boolean' do
expect(subject.accepting_connections?).to be accepting
end
end
describe '#rejecting_connections?' do
it 'returns the expected boolean' do
expect(subject.rejecting_connections?).to be rejecting
end
end
describe '#not_responding?' do
it 'returns the expected boolean' do
expect(subject.not_responding?).to be not_responding
end
end
describe '#invalid_connection_parameters?' do
it 'returns the expected boolean' do
expect(subject.invalid_connection_parameters?).to be invalid_parameters
end
end
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment