From 9dcbe55004ce8ef7b538755b50b8832e1a071fbc Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Wed, 9 May 2018 18:46:44 -0500 Subject: [PATCH] Add back pg_dump support to PostgresAdmin Removed as part of: https://github.com/ManageIQ/manageiq-gems-pending/pull/302 In commit 271febd3 https://github.com/ManageIQ/manageiq-gems-pending/commit/271febd3 Also adds some tests for future confirmation that other additions and refactoring will not brake what is already here. --- lib/gems/pending/util/postgres_admin.rb | 7 +++ spec/util/postgres_admin_spec.rb | 78 +++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/lib/gems/pending/util/postgres_admin.rb b/lib/gems/pending/util/postgres_admin.rb index 6db591700..cdb9aa767 100644 --- a/lib/gems/pending/util/postgres_admin.rb +++ b/lib/gems/pending/util/postgres_admin.rb @@ -118,6 +118,13 @@ def self.restore_pg_basebackup(file) file end + def self.backup_pg_dump(opts) + opts = opts.dup + dbname = opts.delete(:dbname) + runcmd("pg_dump", opts, :format => "c", :file => opts[:local_file], nil => dbname) + opts[:local_file] + end + def self.backup_pg_compress(opts) opts = opts.dup diff --git a/spec/util/postgres_admin_spec.rb b/spec/util/postgres_admin_spec.rb index 85236951d..73a0b4df6 100644 --- a/spec/util/postgres_admin_spec.rb +++ b/spec/util/postgres_admin_spec.rb @@ -1,6 +1,84 @@ require "util/postgres_admin" describe PostgresAdmin do + describe ".backup_pg_dump" do + subject { described_class } + + let(:local_file) { nil } + let(:expected_opts) { {} } + let(:expected_args) { default_args } + let(:default_args) do + { + :no_password => nil, + :format => "c", + :file => local_file, + nil => nil + } + end + + before do + expect(subject).to receive(:runcmd_with_logging) + .with("pg_dump", expected_opts, expected_args) + end + + context "with empty args" do + it "runs the command and returns the :local_file opt" do + expect(subject.backup_pg_dump({})).to eq(local_file) + end + end + + context "with :local_file in opts" do + let(:local_file) { "/some/path/to/pg.dump" } + let(:expected_opts) { { :local_file => local_file } } + + it "runs the command and returns the :local_file opt" do + opts = expected_opts + expect(subject.backup_pg_dump(opts)).to eq(local_file) + end + end + + context "with :local_file and :dbname in opts" do + let(:local_file) { "/some/path/to/pg.dump" } + let(:expected_opts) { { :local_file => local_file } } + let(:expected_args) { default_args.merge(nil => "mydb") } + + it "runs the command and returns the :local_file opt" do + opts = expected_opts.merge(:dbname => "mydb") + expect(subject.backup_pg_dump(opts)).to eq(local_file) + end + end + + context "with :local_file, :dbname, :username, and :password in opts" do + let(:local_file) { "/some/path/to/pg.dump" } + let(:expected_opts) do + { + :local_file => local_file, + :username => "admin", + :password => "smartvm" + } + end + let(:expected_args) do + default_args.merge(nil => "mydb", :username => "admin") + end + + it "runs the command and returns the :local_file opt" do + opts = expected_opts.merge(:dbname => "mydb") + expect(subject.backup_pg_dump(opts)).to eq(local_file) + end + end + + context "with :local_file, :dbname and :hostname in opts" do + let(:local_file) { "/some/path/to/pg.dump" } + let(:expected_opts) { { :local_file => local_file, :hostname => 'foo' } } + let(:expected_args) { default_args.merge(nil => "mydb", :host => 'foo') } + + it "runs the command and returns the :local_file opt" do + opts = expected_opts.merge(:dbname => "mydb") + expect(subject.backup_pg_dump(opts)).to eq(local_file) + end + end + end + context "ENV dependent" do after do ENV.delete_if { |k, _| k.start_with?("APPLIANCE") }