newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / unit / classes / server_spec.rb
1 require 'spec_helper'
2
3 describe 'postgresql::server', :type => :class do
4   let :facts do
5     {
6       :os => {
7         :family  => 'Debian',
8         :name => 'Debian',
9         :release => {
10           :full => '6.0'
11         }
12       },
13       :osfamily => 'Debian',
14       :operatingsystem => 'Debian',
15       :lsbdistid => 'Debian',
16       :lsbdistcodename => 'jessie',
17       :operatingsystemrelease => '8.0',
18       :concat_basedir => tmpfilename('server'),
19       :kernel => 'Linux',
20       :id => 'root',
21       :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
22     }
23   end
24
25   describe 'with no parameters' do
26     it { is_expected.to contain_class("postgresql::params") }
27     it { is_expected.to contain_class("postgresql::server") }
28     it { is_expected.to contain_exec('postgresql_reload').with({
29       'command' => 'service postgresql reload',
30     })
31     }
32     it 'should validate connection' do
33       is_expected.to contain_postgresql_conn_validator('validate_service_is_running')
34     end
35   end
36
37   describe 'service_ensure => running' do
38     let(:params) do
39       {
40         :service_ensure    => 'running',
41         :postgres_password => 'new-p@s$word-to-set'
42       }
43     end
44     it { is_expected.to contain_class("postgresql::params") }
45     it { is_expected.to contain_class("postgresql::server") }
46     it { is_expected.to contain_class("postgresql::server::passwd") }
47     it 'should validate connection' do
48       is_expected.to contain_postgresql_conn_validator('validate_service_is_running')
49     end
50     it 'should set postgres password' do
51       is_expected.to contain_exec('set_postgres_postgrespw').with({
52         'command'     => '/usr/bin/psql -c "ALTER ROLE \"postgres\" PASSWORD ${NEWPASSWD_ESCAPED}"',
53         'user'        => 'postgres',
54         'environment' => [
55           "PGPASSWORD=new-p@s$word-to-set",
56           "PGPORT=5432",
57           "NEWPASSWD_ESCAPED=$$new-p@s$word-to-set$$"
58         ],
59         'unless'      => "/usr/bin/psql -h localhost -p 5432 -c 'select 1' > /dev/null",
60       })
61     end
62   end
63
64   describe 'service_ensure => stopped' do
65     let(:params) {{ :service_ensure => 'stopped' }}
66     it { is_expected.to contain_class("postgresql::params") }
67     it { is_expected.to contain_class("postgresql::server") }
68     it 'shouldnt validate connection' do
69       is_expected.not_to contain_postgresql_conn_validator('validate_service_is_running')
70     end
71   end
72
73   describe 'service_restart_on_change => false' do
74     let(:params) {{ :service_restart_on_change => false }}
75     it { is_expected.to contain_class("postgresql::params") }
76     it { is_expected.to contain_class("postgresql::server") }
77     it { is_expected.to_not contain_Postgresql_conf('data_directory').that_notifies('Class[postgresql::server::service]')
78     }
79     it 'should validate connection' do
80       is_expected.to contain_postgresql_conn_validator('validate_service_is_running')
81     end
82   end
83
84   describe 'service_restart_on_change => true' do
85     let(:params) {{ :service_restart_on_change => true }}
86     it { is_expected.to contain_class("postgresql::params") }
87     it { is_expected.to contain_class("postgresql::server") }
88     it { is_expected.to contain_Postgresql_conf('data_directory').that_notifies('Class[postgresql::server::service]')
89     }
90     it 'should validate connection' do
91       is_expected.to contain_postgresql_conn_validator('validate_service_is_running')
92     end
93   end
94
95   describe 'service_reload => /bin/true' do
96     let(:params) {{ :service_reload => '/bin/true' }}
97     it { is_expected.to contain_class("postgresql::params") }
98     it { is_expected.to contain_class("postgresql::server") }
99     it { is_expected.to contain_exec('postgresql_reload').with({
100       'command' => '/bin/true',
101     })
102     }
103     it 'should validate connection' do
104       is_expected.to contain_postgresql_conn_validator('validate_service_is_running')
105     end
106   end
107
108   describe 'service_manage => true' do
109     let(:params) {{ :service_manage => true }}
110     it { is_expected.to contain_service('postgresqld') }
111   end
112
113   describe 'service_manage => false' do
114     let(:params) {{ :service_manage => false }}
115     it { is_expected.not_to contain_service('postgresqld') }
116     it 'shouldnt validate connection' do
117       is_expected.not_to contain_postgresql_conn_validator('validate_service_is_running')
118     end
119   end
120
121   describe 'package_ensure => absent' do
122     let(:params) do
123       {
124         :package_ensure => 'absent',
125       }
126     end
127
128     it 'should remove the package' do
129       is_expected.to contain_package('postgresql-server').with({
130         :ensure => 'purged',
131       })
132     end
133
134     it 'should still enable the service' do
135       is_expected.to contain_service('postgresqld').with({
136         :ensure => 'running',
137       })
138     end
139   end
140
141   describe 'needs_initdb => true' do
142     let(:params) do
143       {
144         :needs_initdb => true,
145       }
146     end
147
148     it 'should contain proper initdb exec' do
149       is_expected.to contain_exec('postgresql_initdb')
150     end
151   end
152
153   describe 'postgresql_version' do
154     let(:pre_condition) do
155       <<-EOS
156       class { 'postgresql::globals':
157         manage_package_repo => true,
158         version             => '99.5',
159         before              => Class['postgresql::server'],
160       }
161       EOS
162     end
163     it 'contains the correct package version' do
164       is_expected.to contain_class('postgresql::repo').with_version('99.5')
165     end
166   end
167 end