newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / unit / puppet / type / postgresql_psql_spec.rb
1 require 'spec_helper'
2
3 describe Puppet::Type.type(:postgresql_psql), "when validating attributes" do
4   [:name, :unless, :db, :psql_path, :psql_user, :psql_group, :connect_settings].each do |attr|
5     it "should have a #{attr} parameter" do
6       expect(Puppet::Type.type(:postgresql_psql).attrtype(attr)).to eq(:param)
7     end
8   end
9
10   [:command].each do |attr|
11     it "should have a #{attr} property" do
12       expect(Puppet::Type.type(:postgresql_psql).attrtype(attr)).to eq(:property)
13     end
14   end
15 end
16
17 describe Puppet::Type.type(:postgresql_psql), :unless => Puppet.features.microsoft_windows? do
18   subject do
19     Puppet::Type.type(:postgresql_psql).new({:name => 'rspec'}.merge attributes)
20   end
21
22   describe "available attributes" do
23     {
24       :name             => "rspec",
25       :command          => "SELECT stuff",
26       :unless           => "SELECT other,stuff",
27       :db               => "postgres",
28       :psql_path        => "/bin/false",
29       :psql_user        => "postgres",
30       :psql_group       => "postgres",
31       :cwd              => "/var/lib",
32       :refreshonly      => :true,
33       :search_path      => [ "schema1", "schema2"],
34       :connect_settings => { 'PGHOST' => 'postgres-db-server',
35                              'DBVERSION' => '9.1', },
36     }.each do |attr, value|
37       context attr do
38         let(:attributes) do { attr => value } end
39
40         describe [attr] do
41           subject { super()[attr] }
42           it { is_expected.to eq(value) }
43         end
44       end
45     end
46
47     context "default values" do
48       let(:attributes) do {} end
49
50       describe '[:psql_path]' do
51         subject { super()[:psql_path] }
52         it { is_expected.to eq("psql") }
53       end
54
55       describe '[:psql_user]' do
56         subject { super()[:psql_user] }
57         it { is_expected.to eq("postgres") }
58       end
59
60       describe '[:psql_group]' do
61         subject { super()[:psql_group] }
62         it { is_expected.to eq("postgres") }
63       end
64
65       describe '[:cwd]' do
66         subject { super()[:cwd] }
67         it { is_expected.to eq("/tmp") }
68       end
69
70       describe '#refreshonly?' do
71         subject { super().refreshonly? }
72         it { is_expected.to be_falsey }
73       end
74     end
75   end
76
77   describe "#command" do
78     let(:attributes) do {:command => 'SELECT stuff'} end
79
80     it "will have the value :notrun if the command should execute" do
81       expect(subject).to receive(:should_run_sql).and_return(true)
82       expect(subject.property(:command).retrieve).to eq(:notrun)
83     end
84
85     it "will be the 'should' value if the command should not execute" do
86       expect(subject).to receive(:should_run_sql).and_return(false)
87       expect(subject.property(:command).retrieve).to eq('SELECT stuff')
88     end
89
90     it "will call provider#run_sql_command on sync" do
91       expect(subject.provider).to receive(:run_sql_command).with('SELECT stuff').and_return(["done", 0])
92       subject.property(:command).sync
93     end
94   end
95
96   describe "#unless" do
97     let(:attributes) do {:unless => 'SELECT something'} end
98
99     describe "#matches" do
100       it "does not fail when the status is successful" do
101         expect(subject.provider).to receive(:run_unless_sql_command).and_return ["1 row returned", 0]
102         subject.parameter(:unless).matches('SELECT something')
103       end
104
105       it "returns true when rows are returned" do
106         expect(subject.provider).to receive(:run_unless_sql_command).and_return ["1 row returned", 0]
107         expect(subject.parameter(:unless).matches('SELECT something')).to be_truthy
108       end
109
110       it "returns false when no rows are returned" do
111         expect(subject.provider).to receive(:run_unless_sql_command).and_return ["0 rows returned", 0]
112         expect(subject.parameter(:unless).matches('SELECT something')).to be_falsey
113       end
114
115       it "raises an error when the sql command fails" do
116         allow(subject.provider).to receive(:run_unless_sql_command).and_return ["Something went wrong", 1]
117         expect {
118           subject.parameter(:unless).matches('SELECT something')
119         }.to raise_error(Puppet::Error, /Something went wrong/)
120       end
121     end
122   end
123
124   describe "#should_run_sql" do
125     context "without 'unless'" do
126       [true, :true].each do |refreshonly|
127         context "refreshonly => #{refreshonly.inspect}" do
128           let(:attributes) do {
129             :refreshonly => refreshonly,
130           } end
131
132           context "not refreshing" do
133             it { expect(subject.should_run_sql).to be_falsey }
134           end
135
136           context "refreshing" do
137             it { expect(subject.should_run_sql(true)).to be_truthy }
138           end
139         end
140       end
141
142       [false, :false].each do |refreshonly|
143         context "refreshonly => #{refreshonly.inspect}" do
144           let(:attributes) do {
145             :refreshonly => refreshonly,
146           } end
147
148           context "not refreshing" do
149             it { expect(subject.should_run_sql).to be_truthy }
150           end
151
152           context "refreshing" do
153             it { expect(subject.should_run_sql(true)).to be_truthy }
154           end
155         end
156       end
157     end
158
159     context "with matching 'unless'" do
160       before { expect(subject.parameter(:unless)).to receive(:matches).with('SELECT something').and_return(true) }
161
162       [true, :true].each do |refreshonly|
163         context "refreshonly => #{refreshonly.inspect}" do
164           let(:attributes) do {
165             :refreshonly => refreshonly,
166             :unless => 'SELECT something',
167           } end
168
169           context "not refreshing" do
170             it { expect(subject.should_run_sql).to be_falsey }
171           end
172
173           context "refreshing" do
174             it { expect(subject.should_run_sql(true)).to be_falsey }
175           end
176         end
177       end
178
179       [false, :false].each do |refreshonly|
180         context "refreshonly => #{refreshonly.inspect}" do
181           let(:attributes) do {
182             :refreshonly => refreshonly,
183             :unless => 'SELECT something',
184           } end
185
186           context "not refreshing" do
187             it { expect(subject.should_run_sql).to be_falsey }
188           end
189
190           context "refreshing" do
191             it { expect(subject.should_run_sql(true)).to be_falsey }
192           end
193         end
194       end
195     end
196
197     context "when not matching 'unless'" do
198       before { expect(subject.parameter(:unless)).to receive(:matches).with('SELECT something').and_return(false) }
199
200       [true, :true].each do |refreshonly|
201         context "refreshonly => #{refreshonly.inspect}" do
202           let(:attributes) do {
203             :refreshonly => refreshonly,
204             :unless => 'SELECT something',
205           } end
206
207           context "not refreshing" do
208             it { expect(subject.should_run_sql).to be_falsey }
209           end
210
211           context "refreshing" do
212             it { expect(subject.should_run_sql(true)).to be_truthy }
213           end
214         end
215       end
216
217       [false, :false].each do |refreshonly|
218         context "refreshonly => #{refreshonly.inspect}" do
219           let(:attributes) do {
220             :refreshonly => refreshonly,
221             :unless => 'SELECT something',
222           } end
223
224           context "not refreshing" do
225             it { expect(subject.should_run_sql).to be_truthy }
226           end
227
228           context "refreshing" do
229             it { expect(subject.should_run_sql(true)).to be_truthy }
230           end
231         end
232       end
233     end
234   end
235
236   describe "#refresh" do
237     let(:attributes) do {} end
238
239     it "syncs command property when command should run" do
240       expect(subject).to receive(:should_run_sql).with(true).and_return(true)
241       expect(subject.property(:command)).to receive(:sync)
242       subject.refresh
243     end
244
245     it "does not sync command property when command should not run" do
246       expect(subject).to receive(:should_run_sql).with(true).and_return(false)
247       expect(subject.property(:command)).not_to receive(:sync)
248       subject.refresh
249     end
250   end
251 end