Added modules

This commit is contained in:
Ciaby 2014-07-11 13:30:23 -05:00
parent c53c931217
commit 59ec520742
646 changed files with 35182 additions and 0 deletions

View file

@ -0,0 +1,36 @@
require 'spec_helper'
describe 'postgresql::client', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
describe 'with parameters' do
let :params do
{
:package_ensure => 'absent',
:package_name => 'mypackage',
}
end
it 'should modify package' do
should contain_package("postgresql-client").with({
:ensure => 'absent',
:name => 'mypackage',
:tag => 'postgresql',
})
end
end
describe 'with no parameters' do
it 'should create package with postgresql tag' do
should contain_package('postgresql-client').with({
:tag => 'postgresql',
})
end
end
end

View file

@ -0,0 +1,29 @@
require 'spec_helper'
describe 'postgresql::globals', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
:lsbdistid => 'Debian',
}
end
describe 'with no parameters' do
it 'should work' do
should contain_class("postgresql::globals")
end
end
describe 'manage_package_repo => true' do
let(:params) do
{
:manage_package_repo => true,
}
end
it 'should pull in class postgresql::repo' do
should contain_class("postgresql::repo")
end
end
end

View file

@ -0,0 +1,12 @@
require 'spec_helper'
describe 'postgresql::lib::devel', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
it { should contain_class("postgresql::lib::devel") }
end

View file

@ -0,0 +1,40 @@
require 'spec_helper'
describe 'postgresql::lib::java', :type => :class do
describe 'on a debian based os' do
let :facts do {
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
it { should contain_package('postgresql-jdbc').with(
:name => 'libpostgresql-jdbc-java',
:ensure => 'present'
)}
end
describe 'on a redhat based os' do
let :facts do {
:osfamily => 'RedHat',
:operatingsystem => 'RedHat',
:operatingsystemrelease => '6.4',
}
end
it { should contain_package('postgresql-jdbc').with(
:name => 'postgresql-jdbc',
:ensure => 'present'
)}
describe 'when parameters are supplied' do
let :params do
{:package_ensure => 'latest', :package_name => 'somepackage'}
end
it { should contain_package('postgresql-jdbc').with(
:name => 'somepackage',
:ensure => 'latest'
)}
end
end
end

View file

@ -0,0 +1,31 @@
require 'spec_helper'
describe 'postgresql::lib::python', :type => :class do
describe 'on a redhat based os' do
let :facts do {
:osfamily => 'RedHat',
:operatingsystem => 'RedHat',
:operatingsystemrelease => '6.4',
}
end
it { should contain_package('python-psycopg2').with(
:name => 'python-psycopg2',
:ensure => 'present'
)}
end
describe 'on a debian based os' do
let :facts do {
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
it { should contain_package('python-psycopg2').with(
:name => 'python-psycopg2',
:ensure => 'present'
)}
end
end

View file

@ -0,0 +1,12 @@
require 'spec_helper'
describe 'postgresql::params', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
it { should contain_class("postgresql::params") }
end

View file

@ -0,0 +1,18 @@
require 'spec_helper'
describe 'postgresql::repo', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
:lsbdistid => 'Debian',
}
end
describe 'with no parameters' do
it 'should instantiate apt_postgresql_org class' do
should contain_class('postgresql::repo::apt_postgresql_org')
end
end
end

View file

@ -0,0 +1,42 @@
require 'spec_helper'
describe 'postgresql::server::contrib', :type => :class do
let :pre_condition do
"class { 'postgresql::server': }"
end
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
:kernel => 'Linux',
:concat_basedir => tmpfilename('contrib'),
}
end
describe 'with parameters' do
let(:params) do
{
:package_name => 'mypackage',
:package_ensure => 'absent',
}
end
it 'should create package with correct params' do
should contain_package('postgresql-contrib').with({
:ensure => 'absent',
:name => 'mypackage',
:tag => 'postgresql',
})
end
end
describe 'with no parameters' do
it 'should create package with postgresql tag' do
should contain_package('postgresql-contrib').with({
:tag => 'postgresql',
})
end
end
end

View file

@ -0,0 +1,29 @@
require 'spec_helper'
describe 'postgresql::server::initdb', :type => :class do
let (:pre_condition) do
"include postgresql::server"
end
describe 'on RedHat' do
let :facts do
{
:osfamily => 'RedHat',
:operatingsystem => 'CentOS',
:operatingsystemrelease => '6.0',
:concat_basedir => tmpfilename('server'),
}
end
it { should contain_file('/var/lib/pgsql/data').with_ensure('directory') }
end
describe 'on Amazon' do
let :facts do
{
:osfamily => 'RedHat',
:operatingsystem => 'Amazon',
:concat_basedir => tmpfilename('server'),
}
end
it { should contain_file('/var/lib/pgsql9/data').with_ensure('directory') }
end
end

View file

@ -0,0 +1,45 @@
require 'spec_helper'
describe 'postgresql::server::plperl', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
:kernel => 'Linux',
:concat_basedir => tmpfilename('plperl'),
}
end
let :pre_condition do
"class { 'postgresql::server': }"
end
describe 'with no parameters' do
it { should contain_class("postgresql::server::plperl") }
it 'should create package' do
should contain_package('postgresql-plperl').with({
:ensure => 'present',
:tag => 'postgresql',
})
end
end
describe 'with parameters' do
let :params do
{
:package_ensure => 'absent',
:package_name => 'mypackage',
}
end
it { should contain_class("postgresql::server::plperl") }
it 'should create package with correct params' do
should contain_package('postgresql-plperl').with({
:ensure => 'absent',
:name => 'mypackage',
:tag => 'postgresql',
})
end
end
end

View file

@ -0,0 +1,100 @@
require 'spec_helper'
describe 'postgresql::server', :type => :class do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
:concat_basedir => tmpfilename('server'),
:kernel => 'Linux',
}
end
describe 'with no parameters' do
it { should contain_class("postgresql::params") }
it { should contain_class("postgresql::server") }
it 'should validate connection' do
should contain_postgresql__validate_db_connection('validate_service_is_running')
end
end
describe 'manage_firewall => true' do
let(:params) do
{
:manage_firewall => true,
:ensure => true,
}
end
it 'should create firewall rule' do
should contain_firewall("5432 accept - postgres")
end
end
describe 'ensure => absent' do
let(:params) do
{
:ensure => 'absent',
:datadir => '/my/path',
:xlogdir => '/xlog/path',
}
end
it 'should make package purged' do
should contain_package('postgresql-server').with({
:ensure => 'purged',
})
end
it 'stop the service' do
should contain_service('postgresqld').with({
:ensure => false,
})
end
it 'should remove datadir' do
should contain_file('/my/path').with({
:ensure => 'absent',
})
end
it 'should remove xlogdir' do
should contain_file('/xlog/path').with({
:ensure => 'absent',
})
end
end
describe 'package_ensure => absent' do
let(:params) do
{
:package_ensure => 'absent',
}
end
it 'should remove the package' do
should contain_package('postgresql-server').with({
:ensure => 'purged',
})
end
it 'should still enable the service' do
should contain_service('postgresqld').with({
:ensure => true,
})
end
end
describe 'needs_initdb => true' do
let(:params) do
{
:needs_initdb => true,
}
end
it 'should contain proper initdb exec' do
should contain_exec('postgresql_initdb')
end
end
end

View file

@ -0,0 +1,23 @@
require 'spec_helper'
describe 'postgresql::server::config_entry', :type => :define do
let :facts do
{
:osfamily => 'RedHat',
:operatingsystem => 'RedHat',
:operatingsystemrelease => '6.4',
}
end
let(:title) { 'config_entry'}
let :target do
tmpfilename('postgresql_conf')
end
context "syntax check" do
let(:params) { { :ensure => 'present'} }
it { should contain_postgresql__server__config_entry('config_entry') }
end
end

View file

@ -0,0 +1,26 @@
require 'spec_helper'
describe 'postgresql::server::database_grant', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
let :params do
{
:privilege => 'ALL',
:db => 'test',
:role => 'test',
}
end
it { should contain_postgresql__server__database_grant('test') }
it { should contain_postgresql__server__grant('database:test') }
end

View file

@ -0,0 +1,16 @@
require 'spec_helper'
describe 'postgresql::server::database', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
it { should contain_postgresql__server__database('test') }
it { should contain_postgresql_psql("Check for existence of db 'test'") }
end

View file

@ -0,0 +1,28 @@
require 'spec_helper'
describe 'postgresql::server::db', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
let :params do
{
:user => 'test',
:password => 'test',
:owner => 'tester',
}
end
it { should contain_postgresql__server__db('test') }
it { should contain_postgresql__server__database('test').with_owner('tester') }
it { should contain_postgresql__server__role('test') }
it { should contain_postgresql__server__database_grant('GRANT test - ALL - test') }
end

View file

@ -0,0 +1,24 @@
require 'spec_helper'
describe 'postgresql::server::grant', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
let :params do
{
:db => 'test',
:role => 'test',
}
end
it { should contain_postgresql__server__grant('test') }
end

View file

@ -0,0 +1,161 @@
require 'spec_helper'
describe 'postgresql::server::pg_hba_rule', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
:concat_basedir => tmpfilename('pg_hba'),
}
end
let :title do
'test'
end
let :target do
tmpfilename('pg_hba_rule')
end
context 'test template 1' do
let :params do
{
:type => 'host',
:database => 'all',
:user => 'all',
:address => '1.1.1.1/24',
:auth_method => 'md5',
:target => target,
}
end
it do
content = param('concat::fragment', 'pg_hba_rule_test', 'content')
content.should =~ /host\s+all\s+all\s+1\.1\.1\.1\/24\s+md5/
end
end
context 'test template 2' do
let :params do
{
:type => 'local',
:database => 'all',
:user => 'all',
:auth_method => 'ident',
:target => target,
}
end
it do
content = param('concat::fragment', 'pg_hba_rule_test', 'content')
content.should =~ /local\s+all\s+all\s+ident/
end
end
context 'test template 3' do
let :params do
{
:type => 'host',
:database => 'all',
:user => 'all',
:address => '0.0.0.0/0',
:auth_method => 'ldap',
:auth_option => 'foo=bar',
:target => target,
}
end
it do
content = param('concat::fragment', 'pg_hba_rule_test', 'content')
content.should =~ /host\s+all\s+all\s+0\.0\.0\.0\/0\s+ldap\s+foo=bar/
end
end
context 'validation' do
context 'validate type test 1' do
let :params do
{
:type => 'invalid',
:database => 'all',
:user => 'all',
:address => '0.0.0.0/0',
:auth_method => 'ldap',
:target => target,
}
end
it 'should fail parsing when type is not valid' do
expect {subject}.to raise_error(Puppet::Error,
/The type you specified \[invalid\] must be one of/)
end
end
context 'validate auth_method' do
let :params do
{
:type => 'local',
:database => 'all',
:user => 'all',
:address => '0.0.0.0/0',
:auth_method => 'invalid',
:target => target,
}
end
it 'should fail parsing when auth_method is not valid' do
expect {subject}.to raise_error(Puppet::Error,
/The auth_method you specified \[invalid\] must be one of/)
end
end
context 'validate unsupported auth_method' do
let :pre_condition do
<<-EOS
class { 'postgresql::globals':
version => '9.0',
}
class { 'postgresql::server': }
EOS
end
let :params do
{
:type => 'local',
:database => 'all',
:user => 'all',
:address => '0.0.0.0/0',
:auth_method => 'peer',
:target => target,
}
end
it 'should fail parsing when auth_method is not valid' do
expect {subject}.to raise_error(Puppet::Error,
/The auth_method you specified \[peer\] must be one of: trust, reject, md5, sha1, password, gss, sspi, krb5, ident, ldap, radius, cert, pam/)
end
end
context 'validate supported auth_method' do
let :pre_condition do
<<-EOS
class { 'postgresql::globals':
version => '9.2',
}
class { 'postgresql::server': }
EOS
end
let :params do
{
:type => 'local',
:database => 'all',
:user => 'all',
:address => '0.0.0.0/0',
:auth_method => 'peer',
:target => target,
}
end
it do
content = param('concat::fragment', 'pg_hba_rule_test', 'content')
content.should =~ /local\s+all\s+all\s+0\.0\.0\.0\/0\s+peer/
end
end
end
end

View file

@ -0,0 +1,23 @@
require 'spec_helper'
describe 'postgresql::server::role', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
let :params do
{
:password_hash => 'test',
}
end
it { should contain_postgresql__server__role('test') }
end

View file

@ -0,0 +1,27 @@
require 'spec_helper'
describe 'postgresql::server::table_grant', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
let :params do
{
:privilege => 'ALL',
:db => 'test',
:role => 'test',
:table => 'foo',
}
end
it { should contain_postgresql__server__table_grant('test') }
it { should contain_postgresql__server__grant('table:test') }
end

View file

@ -0,0 +1,23 @@
require 'spec_helper'
describe 'postgresql::server::tablespace', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
let :params do
{
:location => '/srv/data/foo',
}
end
it { should contain_postgresql__server__tablespace('test') }
end

View file

@ -0,0 +1,35 @@
require 'spec_helper'
describe 'postgresql::validate_db_connection', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'Debian',
:operatingsystemrelease => '6.0',
}
end
let :title do
'test'
end
describe 'should work with only default parameters' do
it { should contain_postgresql__validate_db_connection('test') }
end
describe 'should work with all parameters' do
let :params do
{
:database_host => 'test',
:database_name => 'test',
:database_password => 'test',
:database_username => 'test',
:database_port => 5432,
:run_as => 'postgresq',
:sleep => 4,
:tries => 30,
}
end
it { should contain_postgresql__validate_db_connection('test') }
end
end

View file

@ -0,0 +1,137 @@
require 'spec_helper'
describe 'postgresql_acls_to_resources_hash', :type => :puppet_function do
context 'individual transform tests' do
it do
input = 'local all postgres ident'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"local",
"database"=>"all",
"user"=>"postgres",
"auth_method"=>"ident",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
it do
input = 'local all root ident'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"local",
"database"=>"all",
"user"=>"root",
"auth_method"=>"ident",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
it do
input_array = [
'local all all ident',
]
result = {
"postgresql class generated rule test 0"=>{
"type"=>"local",
"database"=>"all",
"user"=>"all",
"auth_method"=>"ident",
"order"=>"100",
},
}
should run.with_params(input_array, 'test', 100).and_return(result)
end
it do
input = 'host all all 127.0.0.1/32 md5'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"host",
"database"=>"all",
"user"=>"all",
"address"=>"127.0.0.1/32",
"auth_method"=>"md5",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
it do
input = 'host all all 0.0.0.0/0 md5'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"host",
"database"=>"all",
"user"=>"all",
"address"=>"0.0.0.0/0",
"auth_method"=>"md5",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
it do
input = 'host all all ::1/128 md5'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"host",
"database"=>"all",
"user"=>"all",
"address"=>"::1/128",
"auth_method"=>"md5",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
it do
input = 'host all all 1.1.1.1 255.255.255.0 md5'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"host",
"database"=>"all",
"user"=>"all",
"address"=>"1.1.1.1 255.255.255.0",
"auth_method"=>"md5",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
it do
input = 'host all all 1.1.1.1 255.255.255.0 ldap ldapserver=ldap.example.net ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"'
result = {
"postgresql class generated rule test 0"=>{
"type"=>"host",
"database"=>"all",
"user"=>"all",
"address"=>"1.1.1.1 255.255.255.0",
"auth_method"=>"ldap",
"auth_option"=>"ldapserver=ldap.example.net ldapprefix=\"cn=\" ldapsuffix=\", dc=example, dc=net\"",
"order"=>"100",
},
}
should run.with_params([input], 'test', 100).and_return(result)
end
end
it 'should return an empty hash when input is empty array' do
should run.with_params([], 'test', 100).and_return({})
end
end

View file

@ -0,0 +1,10 @@
require 'spec_helper'
describe 'postgresql_escape', :type => :puppet_function do
it { should run.with_params('foo').
and_return('$$foo$$') }
end
describe 'postgresql_escape', :type => :puppet_function do
it { should run.with_params('fo$$o').
and_return('$ed$fo$$o$ed$') }
end

View file

@ -0,0 +1,6 @@
require 'spec_helper'
describe 'postgresql_password', :type => :puppet_function do
it { should run.with_params('foo', 'bar').
and_return('md596948aad3fcae80c08a35c9b5958cd89') }
end

View file

@ -0,0 +1,112 @@
require 'spec_helper'
require "tempfile"
provider_class = Puppet::Type.type(:postgresql_conf).provider(:parsed)
describe provider_class do
let(:title) { 'postgresql_conf' }
let(:provider) {
conf_class = Puppet::Type.type(:postgresql_conf)
provider = conf_class.provider(:parsed)
conffile = tmpfilename('postgresql.conf')
provider.any_instance.stubs(:target).returns conffile
provider
}
before do
end
after :each do
provider.initvars
end
describe "simple configuration that should be allowed" do
it "should parse a simple ini line" do
provider.parse_line("listen_addreses = '*'").should ==
{ :name=>"listen_addreses", :value=>"*", :comment=>nil, :record_type=>:parsed }
end
it "should parse a simple ini line (2)" do
provider.parse_line(" listen_addreses = '*'").should ==
{ :name=>"listen_addreses", :value=>"*", :comment=>nil, :record_type=>:parsed }
end
it "should parse a simple ini line (3)" do
provider.parse_line("listen_addreses = '*' # dont mind me").should ==
{ :name=>"listen_addreses", :value=>"*", :comment=>"dont mind me", :record_type=>:parsed }
end
it "should parse a comment" do
provider.parse_line("# dont mind me").should ==
{ :line=>"# dont mind me", :record_type=>:comment }
end
it "should parse a comment (2)" do
provider.parse_line(" \t# dont mind me").should ==
{ :line=>" \t# dont mind me", :record_type=>:comment }
end
it "should allow includes" do
provider.parse_line("include puppetextra").should ==
{ :name=>"include", :value=>"puppetextra", :comment=>nil, :record_type=>:parsed }
end
it "should allow numbers thorugh without quotes" do
provider.parse_line("wal_keep_segments = 32").should ==
{ :name=>"wal_keep_segments", :value=>"32", :comment=>nil, :record_type=>:parsed }
end
it "should allow blanks thorugh " do
provider.parse_line("").should ==
{ :line=>"", :record_type=>:blank }
end
it "should parse keys with dots " do
provider.parse_line("auto_explain.log_min_duration = 1ms").should ==
{ :name => "auto_explain.log_min_duration", :value => "1ms", :comment => nil, :record_type => :parsed }
end
end
describe "configuration that should be set" do
it "should set comment lines" do
provider.to_line({ :line=>"# dont mind me", :record_type=>:comment }).should ==
'# dont mind me'
end
it "should set blank lines" do
provider.to_line({ :line=>"", :record_type=>:blank }).should ==
''
end
it "should set simple configuration" do
provider.to_line({:name=>"listen_addresses", :value=>"*", :comment=>nil, :record_type=>:parsed }).should ==
"listen_addresses = '*'"
end
it "should set simple configuration with period in name" do
provider.to_line({:name => "auto_explain.log_min_duration", :value => '100ms', :comment => nil, :record_type => :parsed }).should ==
"auto_explain.log_min_duration = 100ms"
end
it "should set simple configuration even with comments" do
provider.to_line({:name=>"listen_addresses", :value=>"*", :comment=>'dont mind me', :record_type=>:parsed }).should ==
"listen_addresses = '*' # dont mind me"
end
it 'should quote includes' do
provider.to_line( {:name=>"include", :value=>"puppetextra", :comment=>nil, :record_type=>:parsed }).should ==
"include 'puppetextra'"
end
it 'should quote multiple words' do
provider.to_line( {:name=>"archive_command", :value=>"rsync up", :comment=>nil, :record_type=>:parsed }).should ==
"archive_command = 'rsync up'"
end
it 'shouldn\'t quote numbers' do
provider.to_line( {:name=>"wal_segments", :value=>"32", :comment=>nil, :record_type=>:parsed }).should ==
"wal_segments = 32"
end
end
end

View file

@ -0,0 +1,140 @@
require 'spec_helper'
describe Puppet::Type.type(:postgresql_psql).provider(:ruby) do
let(:name) { 'rspec psql test' }
let(:resource) do
Puppet::Type.type(:postgresql_psql).new({ :name => name, :provider => :ruby }.merge attributes)
end
let(:provider) { resource.provider }
context("#run_sql_command") do
describe "with default attributes" do
let(:attributes) do { :db => 'spec_db' } end
it "executes with the given psql_path on the given DB" do
expect(provider).to receive(:run_command).with(['psql', '-d',
attributes[:db], '-t', '-c', 'SELECT something'], 'postgres',
'postgres')
provider.run_sql_command("SELECT something")
end
end
describe "with psql_path and db" do
let(:attributes) do {
:psql_path => '/opt/postgres/psql',
:psql_user => 'spec_user',
:psql_group => 'spec_group',
:cwd => '/spec',
:db => 'spec_db'
} end
it "executes with the given psql_path on the given DB" do
expect(Dir).to receive(:chdir).with(attributes[:cwd]).and_yield
expect(provider).to receive(:run_command).with([attributes[:psql_path],
'-d', attributes[:db], '-t', '-c', 'SELECT something'],
attributes[:psql_user], attributes[:psql_group])
provider.run_sql_command("SELECT something")
end
end
describe "with search_path string" do
let(:attributes) do {
:search_path => "schema1"
} end
it "executes with the given search_path" do
expect(provider).to receive(:run_command).with(['psql', '-t', '-c',
'set search_path to schema1; SELECT something'],
'postgres', 'postgres')
provider.run_sql_command("SELECT something")
end
end
describe "with search_path array" do
let(:attributes) do {
:search_path => ['schema1','schema2'],
} end
it "executes with the given search_path" do
expect(provider).to receive(:run_command).with(['psql', '-t', '-c',
'set search_path to schema1,schema2; SELECT something'],
'postgres',
'postgres'
)
provider.run_sql_command("SELECT something")
end
end
end
context("#command") do
context "when unless is specified" do
[:true, :false, true, false].each do |refresh|
context "and refreshonly is #{refresh}" do
let(:attributes) { {
:command => 'SELECT something',
:db => 'spec_db',
:unless => 'SELECT something',
:refreshonly => refresh
} }
it "does not fail when the status is successful" do
expect(provider).to receive(:run_unless_sql_command).and_return ["1 row returned", 0]
provider.command
end
it "returns the given command when rows are returned" do
expect(provider).to receive(:run_unless_sql_command).and_return ["1 row returned", 0]
expect(provider.command).to eq("SELECT something")
end
it "does not return the given command when no rows are returned" do
expect(provider).to receive(:run_unless_sql_command).and_return ["0 rows returned", 0]
expect(provider.command).to_not eq("SELECT something")
end
it "raises an error when the sql command fails" do
allow(provider).to receive(:run_unless_sql_command).and_return ["Something went wrong", 1]
expect { provider.command }.to raise_error(Puppet::Error, /Something went wrong/)
end
end
end
end
context "when unless is not specified" do
context "and refreshonly is true" do
let(:attributes) do {
:command => 'SELECT something',
:db => 'spec_db',
:refreshonly => :true
} end
it "does not run unless sql command" do
expect(provider).to_not receive(:run_unless_sql_command)
provider.command
end
it "returns the given command do disable sync" do
expect(provider.command).to eq("SELECT something")
end
end
context "and refreshonly is false" do
let(:attributes) do {
:command => 'SELECT something',
:db => 'spec_db',
:refreshonly => :false
} end
it "does not run unless sql command" do
expect(provider).to_not receive(:run_unless_sql_command)
provider.command
end
it "does not return the command so as to enable sync" do
expect(provider.command).to_not eq("SELECT something")
end
end
end
end
end

View file

@ -0,0 +1,92 @@
require 'spec_helper'
describe Puppet::Type.type(:postgresql_psql), "when validating attributes" do
[:name, :unless, :db, :psql_path, :psql_user, :psql_group].each do |attr|
it "should have a #{attr} parameter" do
expect(Puppet::Type.type(:postgresql_psql).attrtype(attr)).to eq(:param)
end
end
[:command].each do |attr|
it "should have a #{attr} property" do
expect(Puppet::Type.type(:postgresql_psql).attrtype(attr)).to eq(:property)
end
end
end
describe Puppet::Type.type(:postgresql_psql), :unless => Puppet.features.microsoft_windows? do
subject do
Puppet::Type.type(:postgresql_psql).new({:name => 'rspec'}.merge attributes)
end
describe "available attributes" do
{
:name => "rspec",
:command => "SELECT stuff",
:unless => "SELECT other,stuff",
:db => "postgres",
:psql_path => "/bin/false",
:psql_user => "postgres",
:psql_group => "postgres",
:cwd => "/var/lib",
:refreshonly => :true,
:search_path => [ "schema1", "schema2"]
}.each do |attr, value|
context attr do
let(:attributes) do { attr => value } end
its([attr]) { should == value }
end
end
context "default values" do
let(:attributes) do {} end
its([:psql_path]) { should eq("psql") }
its([:psql_user]) { should eq("postgres") }
its([:psql_group]) { should eq("postgres") }
its([:cwd]) { should eq("/tmp") }
its(:refreshonly?) { should be_false }
end
end
describe "#refreshonly" do
[true, :true].each do |refreshonly|
context "=> #{refreshonly.inspect}" do
let(:attributes) do { :refreshonly => refreshonly } end
it "has a value of true" do
expect(subject.refreshonly?).to be_true
end
it "will not enforce command on sync because refresh() will be called" do
expect(subject.provider).to_not receive(:command=)
subject.property(:command).sync
end
end
end
[false, :false].each do |refreshonly|
context "=> #{refreshonly.inspect}" do
let(:attributes) do { :refreshonly => refreshonly } end
it "has a value of false" do
expect(subject.refreshonly?).to be_false
end
it "will enforce command on sync because refresh() will not be called" do
expect(subject.provider).to receive(:command=)
subject.property(:command).sync
end
end
end
end
## If we refresh the resource, the command should always be run regardless of
## refreshonly
describe "when responding to refresh" do
[true, :true, false, :false].each do |refreshonly|
context "with refreshonly => #{refreshonly.inspect}" do
let(:attributes) do { :refreshonly => refreshonly } end
it "will enforce command on sync" do
expect(subject.provider).to receive(:command=)
subject.refresh
end
end
end
end
end

View file

@ -0,0 +1,50 @@
#! /usr/bin/env ruby
require 'spec_helper'
describe Puppet::Type.type(:postgresql_conf) do
before do
@provider_class = described_class.provide(:simple) { mk_resource_methods }
@provider_class.stubs(:suitable?).returns true
described_class.stubs(:defaultprovider).returns @provider_class
end
describe "namevar validation" do
it "should have :name as its namevar" do
described_class.key_attributes.should == [:name]
end
it "should not invalid names" do
expect { described_class.new(:name => 'foo bar') }.to raise_error(Puppet::Error, /Invalid value/)
end
it "should allow dots in names" do
expect { described_class.new(:name => 'foo.bar') }.to_not raise_error
end
end
describe "when validating attributes" do
[:name, :provider].each do |param|
it "should have a #{param} parameter" do
described_class.attrtype(param).should == :param
end
end
[:value, :target].each do |property|
it "should have a #{property} property" do
described_class.attrtype(property).should == :property
end
end
end
describe "when validating values" do
describe "ensure" do
it "should support present as a value for ensure" do
expect { described_class.new(:name => 'foo', :ensure => :present) }.to_not raise_error
end
it "should support absent as a value for ensure" do
expect { described_class.new(:name => 'foo', :ensure => :absent) }.to_not raise_error
end
it "should not support other values" do
expect { described_class.new(:name => 'foo', :ensure => :foo) }.to raise_error(Puppet::Error, /Invalid value/)
end
end
end
end