diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 4c66d64..bce0da9 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -1,101 +1,101 @@ require 'puppet' require 'beaker-rspec/spec_helper' require 'beaker-rspec/helpers/serverspec' require 'beaker/puppet_install_helper' require 'beaker/module_install_helper' require 'beaker/task_helper' run_puppet_install_helper install_ca_certs unless pe_install? UNSUPPORTED_PLATFORMS = ['AIX', 'windows', 'Solaris', 'Suse'].freeze install_bolt_on(hosts) unless pe_install? install_module_on(hosts) install_module_dependencies_on(hosts) DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant' 'vagrant' elsif default[:hypervisor] == 'vcloud' 'Qu@lity!' end +# Class String - unindent - Provide ability to remove indentation from strings, for the purpose of +# left justifying heredoc blocks. class String - # Provide ability to remove indentation from strings, for the purpose of - # left justifying heredoc blocks. def unindent gsub(%r{^#{scan(%r{^\s*}).min_by { |l| l.length }}}, '') end end def shellescape(str) str = str.to_s # An empty argument will be skipped, so return empty quotes. return "''" if str.empty? str = str.dup # Treat multibyte characters as is. It is caller's responsibility # to encode the string in the right encoding for the shell # environment. str.gsub!(%r{([^A-Za-z0-9_\-.,:\/@\n])}, '\\\\\\1') # A LF cannot be escaped with a backslash because a backslash + LF # combo is regarded as line continuation and simply ignored. str.gsub!(%r{\n}, "'\n'") str end def psql(psql_cmd, user = 'postgres', exit_codes = [0, 1], &block) psql = "psql #{psql_cmd}" shell("su #{shellescape(user)} -c #{shellescape(psql)}", acceptable_exit_codes: exit_codes, &block) end RSpec.configure do |c| # Readable test descriptions c.formatter = :documentation # Configure all nodes in nodeset c.before :suite do run_puppet_access_login(user: 'admin') if pe_install? && puppet_version =~ %r{(5\.\d\.\d)} # Set up selinux if appropriate. if fact('osfamily') == 'RedHat' && fact('selinux') == 'true' pp = <<-EOS if $::osfamily == 'RedHat' and $::selinux == 'true' { $semanage_package = $::operatingsystemmajrelease ? { '5' => 'policycoreutils', default => 'policycoreutils-python', } package { $semanage_package: ensure => installed } exec { 'set_postgres': command => 'semanage port -a -t postgresql_port_t -p tcp 5433', path => '/bin:/usr/bin/:/sbin:/usr/sbin', subscribe => Package[$semanage_package], } } EOS apply_manifest_on(agents, pp, catch_failures: false) end # net-tools required for netstat utility being used by be_listening if fact('osfamily') == 'RedHat' && fact('operatingsystemmajrelease') == '7' pp = <<-EOS package { 'net-tools': ensure => installed } EOS apply_manifest_on(agents, pp, catch_failures: false) end hosts.each do |host| on host, 'chmod 755 /root' next unless fact_on(host, 'osfamily') == 'Debian' on host, "echo \"en_US ISO-8859-1\nen_NG.UTF-8 UTF-8\nen_US.UTF-8 UTF-8\n\" > /etc/locale.gen" on host, '/usr/sbin/locale-gen' on host, '/usr/sbin/update-locale' end end end diff --git a/spec/unit/puppet/provider/postgresql_replication_slot/ruby_spec.rb b/spec/unit/puppet/provider/postgresql_replication_slot/ruby_spec.rb index 40298b0..9b33b64 100644 --- a/spec/unit/puppet/provider/postgresql_replication_slot/ruby_spec.rb +++ b/spec/unit/puppet/provider/postgresql_replication_slot/ruby_spec.rb @@ -1,97 +1,99 @@ require 'spec_helper' type = Puppet::Type.type(:postgresql_replication_slot) describe type.provider(:ruby) do + # class SuccessStatus class SuccessStatus def success? true end end + # class FailStatus class FailStatus def success? false end end let(:name) { 'standby' } let(:resource) do type.new({ name: name, provider: :ruby }.merge(attributes)) end let(:sql_instances) do "abc | | physical | | | t | | | 0/3000420 def | | physical | | | t | | | 0/3000420\n" end let(:success_status) { SuccessStatus.new } let(:fail_status) { FailStatus.new } let(:provider) { resource.provider } context 'when listing instances' do before(:each) do provider.class.expects(:run_command).with(['psql', '-t', '-c', 'SELECT * FROM pg_replication_slots;'], 'postgres', 'postgres').returns([sql_instances, nil]) end let(:attributes) { {} } let(:instances) { provider.class.instances } let(:expected) { ['abc', 'def'] } it 'lists instances #size' do expect(instances.size).to eq 2 end it 'lists instances #content' do expected.each_with_index do |expect, index| expect(instances[index].name).to eq expect end end end context 'when creating slot' do let(:attributes) { { ensure: 'present' } } context 'when creation works' do it 'calls psql and succeed' do provider.class.expects(:run_command).with( ['psql', '-t', '-c', "SELECT * FROM pg_create_physical_replication_slot('standby');"], 'postgres', 'postgres' ).returns([nil, success_status]) expect { provider.create }.not_to raise_error end end context 'when creation fails' do it 'calls psql and fail' do provider.class.expects(:run_command).with( ['psql', '-t', '-c', "SELECT * FROM pg_create_physical_replication_slot('standby');"], 'postgres', 'postgres' ).returns([nil, fail_status]) expect { provider.create }.to raise_error(Puppet::Error, %r{Failed to create replication slot standby:}) end end end context 'when destroying slot' do let(:attributes) { { ensure: 'absent' } } context 'when destruction works' do it 'calls psql and succeed' do provider.class.expects(:run_command).with( ['psql', '-t', '-c', "SELECT pg_drop_replication_slot('standby');"], 'postgres', 'postgres' ).returns([nil, success_status]) expect { provider.destroy }.not_to raise_error end end context 'when destruction fails' do it 'calls psql and fail' do provider.class.expects(:run_command).with( ['psql', '-t', '-c', "SELECT pg_drop_replication_slot('standby');"], 'postgres', 'postgres' ).returns([nil, fail_status]) expect { provider.destroy }.to raise_error(Puppet::Error, %r{Failed to destroy replication slot standby:}) end end end end