Skip to content

Commit

Permalink
Merge pull request #47 from anynines/ds_561_a9s-pg_prepare_rspec
Browse files Browse the repository at this point in the history
[DS-561] Extend PGWebServiceClient features
  • Loading branch information
a9shdemelo authored Feb 10, 2021
2 parents 3b5965e + 934eb31 commit 40e5215
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 41 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ source 'https://rubygems.org'
gem 'rspec'
gem 'rubocop'
gem 'destructor'
gem 'httparty', '0.15.6'
gem 'pg', '0.19'
59 changes: 34 additions & 25 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.0)
ast (2.4.2)
destructor (0.1.0)
diff-lcs (1.3)
jaro_winkler (1.5.2)
parallel (1.17.0)
parser (2.6.2.1)
ast (~> 2.4.0)
psych (3.1.0)
diff-lcs (1.4.4)
httparty (0.15.6)
multi_xml (>= 0.5.2)
multi_xml (0.6.0)
parallel (1.20.1)
parser (3.0.0.0)
ast (~> 2.4.1)
pg (0.19.0)
rainbow (3.0.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
regexp_parser (2.0.3)
rexml (3.2.4)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.67.2)
jaro_winkler (~> 1.5.1)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.9.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
psych (>= 3.1.0)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
ruby-progressbar (1.10.0)
unicode-display_width (1.5.0)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.4.1)
parser (>= 2.7.1.5)
ruby-progressbar (1.11.0)
unicode-display_width (2.0.0)

PLATFORMS
ruby

DEPENDENCIES
destructor
httparty (= 0.15.6)
pg (= 0.19)
rspec
rubocop

Expand Down
42 changes: 42 additions & 0 deletions lib/unit_tests_utils/postgresql_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'pg'

class UnitTestsUtils::PostgreSQLClient
attr_reader :args

POSTGRESQL_PROPERTIES_PATH = '/instance_groups/name=pg/jobs/name=postgresql-ha/properties'

def initialize(args)
@args = args
@args[:dbname] ||= 'area51'
@args[:sslmode] ||= 'disable'
end

def self.create_from_manifest(manifest, host = nil)
host = manifest.hostname unless host
user = manifest.properties['postgresql-ha']['admin_credentials']['username']
password = manifest.properties['postgresql-ha']['admin_credentials']['password']
if manifest.properties("#{POSTGRESQL_PROPERTIES_PATH}/postgresql-ha/ssl?/enable")
sslmode = "require"
end

self.new(host: host, user: user, password: password, sslmode: sslmode)
end

def ping
logger.debug("* Creating ping with args: *#{args}*")
return PG::Connection.ping(args)
end

def connect(extra_args = {})
args.merge!(extra_args)
logger.debug("* Creating connection with args: *#{args}*")
return PG::Connection.new(args)
end

def execute(sql, args = {})
conn = connect(args)
return conn.exec(sql)
ensure
conn.close if !conn.nil?
end
end
39 changes: 23 additions & 16 deletions lib/unit_tests_utils/postgresql_web_service_client.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require 'httparty'
require_relative 'postgresql_client'

class UnitTestsUtils::PGWebServiceClient
attr_reader :manifest
attr_reader :manifest, :manifest_name, :password

DEFAULT_WEB_SERVICE_PASSWORD = 'password'

def initialize(manifest, options = {})
@manifest_name = options[:new_manifest_name] || manifest.name
@password = options[:pg_web_service_password] || DEFAULT_WEB_SERVICE_PASSWORD

def initialize(manifest)
@manifest = manifest
end

Expand All @@ -12,7 +18,7 @@ def self.master_hostname(manifest)
end

def node_ips
instances = UnitTestsUtils::Bosh.instance_status(manifest.name, manifest.instance_names.first)
instances = UnitTestsUtils::Bosh.instance_status(manifest_name, manifest.instance_names.first)
instances.map { |instance| instance["ips"] }
end

Expand Down Expand Up @@ -147,7 +153,7 @@ def all_standby_nodes_unblocked(tolerance = 120)
standby_nodes = cluster_nodes.select { |node| node["data"]["pg_mode"] == "standby" }

standby_nodes.each do |node|
pg_client = PostgreSQLClient.create_from_manifest(manifest, node["data"]["node"]["ip"])
pg_client = UnitTestsUtils::PostgreSQLClient.create_from_manifest(manifest, node["data"]["node"]["ip"])
begin
if pg_client.ping > 0

Expand Down Expand Up @@ -178,13 +184,13 @@ def cluster_is_sync

host = master_ip
logger.debug("Checking if cluster has replicated the data - host: #{host}")
pg_client = PostgreSQLClient.create_from_manifest(manifest, host)
pg_client = UnitTestsUtils::PostgreSQLClient.create_from_manifest(manifest, host)
res = pg_client.execute("SELECT pg_current_wal_lsn();", { dbname: "postgres" })

master_lsn = lsn_to_i(res.values.flatten.first)

standby_ips.each do |node|
pg_client = PostgreSQLClient.create_from_manifest(manifest, node)
pg_client = UnitTestsUtils::PostgreSQLClient.create_from_manifest(manifest, node)
res = pg_client.execute("SELECT pg_last_wal_receive_lsn();", { dbname: "postgres" })

standby_lsn = lsn_to_i(res.values.flatten.first)
Expand Down Expand Up @@ -278,11 +284,21 @@ def cluster_status
nodes.select { |node| node != nil }
end

def is_cluster_ready
logger.debug("Getting cluster status...")
nodes_status = cluster_status
logger.debug("Cluster status: #{nodes_status}")
nodes = nodes_status.select { |node_status| node_status["status"] == "succeeded" && node_status["data"]["status"] == "running" }

return nodes.length == nodes_status.length && has_a_master(nodes_status)
end

private

def webservice_credentials
{
:username => "admin",
:password => "password"
:password => password
}
end

Expand All @@ -297,15 +313,6 @@ def has_a_master(nodes)
return masters.length > 0
end

def is_cluster_ready
logger.debug("Getting cluster status...")
nodes_status = cluster_status
logger.debug("Cluster status: #{nodes_status}")
nodes = nodes_status.select { |node_status| node_status["status"] == "succeeded" && node_status["data"]["status"] == "running" }

return nodes.length == nodes_status.length && has_a_master(nodes_status)
end

def lsn_to_i(lsn)
split_lsn = lsn.split("/")
split_lsn[0].hex << 32 | split_lsn[1].hex
Expand Down
38 changes: 38 additions & 0 deletions spec/lib/unit_tests_utils/postgresql_web_service_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

describe UnitTestsUtils::PGWebServiceClient do
describe "#initialize" do
let(:manifest_id) { 'manifest_id' }
let(:manifest) do
manifest_mocked = double('')
allow(manifest_mocked).to receive(:name).and_return(manifest_id)
manifest_mocked
end
subject(:pg_web_service_client) { UnitTestsUtils::PGWebServiceClient.new(manifest, pg_web_service_options) }

context 'when there are not options' do
let(:pg_web_service_options) { {} }

specify do
expect(pg_web_service_client.password).to eq UnitTestsUtils::PGWebServiceClient::DEFAULT_WEB_SERVICE_PASSWORD
expect(pg_web_service_client.manifest_name).to eq manifest_id
end
end

context 'when there are options' do
let(:manifest_name) { 'manifest_name' }
let(:password) { UnitTestsUtils::PGWebServiceClient::DEFAULT_WEB_SERVICE_PASSWORD }
let(:pg_web_service_options) do
{
new_manifest_name: manifest_name,
pg_web_service_password: password
}
end

specify do
expect(pg_web_service_client.password).to eq password
expect(pg_web_service_client.manifest_name).to eq manifest_name
end
end
end
end
1 change: 1 addition & 0 deletions unit_tests_utils.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.license = 'Nonstandard'

s.add_runtime_dependency 'httparty', '0.15.6'
s.add_runtime_dependency 'pg', '0.19'
end

0 comments on commit 40e5215

Please sign in to comment.