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)
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)
17 describe Puppet::Type.type(:postgresql_psql), :unless => Puppet.features.microsoft_windows? do
19 Puppet::Type.type(:postgresql_psql).new({:name => 'rspec'}.merge attributes)
22 describe "available attributes" do
25 :command => "SELECT stuff",
26 :unless => "SELECT other,stuff",
28 :psql_path => "/bin/false",
29 :psql_user => "postgres",
30 :psql_group => "postgres",
32 :refreshonly => :true,
33 :search_path => [ "schema1", "schema2"],
34 :connect_settings => { 'PGHOST' => 'postgres-db-server',
35 'DBVERSION' => '9.1', },
36 }.each do |attr, value|
38 let(:attributes) do { attr => value } end
41 subject { super()[attr] }
42 it { is_expected.to eq(value) }
47 context "default values" do
48 let(:attributes) do {} end
50 describe '[:psql_path]' do
51 subject { super()[:psql_path] }
52 it { is_expected.to eq("psql") }
55 describe '[:psql_user]' do
56 subject { super()[:psql_user] }
57 it { is_expected.to eq("postgres") }
60 describe '[:psql_group]' do
61 subject { super()[:psql_group] }
62 it { is_expected.to eq("postgres") }
66 subject { super()[:cwd] }
67 it { is_expected.to eq("/tmp") }
70 describe '#refreshonly?' do
71 subject { super().refreshonly? }
72 it { is_expected.to be_falsey }
77 describe "#command" do
78 let(:attributes) do {:command => 'SELECT stuff'} end
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)
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')
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
97 let(:attributes) do {:unless => 'SELECT something'} end
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')
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
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
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]
118 subject.parameter(:unless).matches('SELECT something')
119 }.to raise_error(Puppet::Error, /Something went wrong/)
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,
132 context "not refreshing" do
133 it { expect(subject.should_run_sql).to be_falsey }
136 context "refreshing" do
137 it { expect(subject.should_run_sql(true)).to be_truthy }
142 [false, :false].each do |refreshonly|
143 context "refreshonly => #{refreshonly.inspect}" do
144 let(:attributes) do {
145 :refreshonly => refreshonly,
148 context "not refreshing" do
149 it { expect(subject.should_run_sql).to be_truthy }
152 context "refreshing" do
153 it { expect(subject.should_run_sql(true)).to be_truthy }
159 context "with matching 'unless'" do
160 before { expect(subject.parameter(:unless)).to receive(:matches).with('SELECT something').and_return(true) }
162 [true, :true].each do |refreshonly|
163 context "refreshonly => #{refreshonly.inspect}" do
164 let(:attributes) do {
165 :refreshonly => refreshonly,
166 :unless => 'SELECT something',
169 context "not refreshing" do
170 it { expect(subject.should_run_sql).to be_falsey }
173 context "refreshing" do
174 it { expect(subject.should_run_sql(true)).to be_falsey }
179 [false, :false].each do |refreshonly|
180 context "refreshonly => #{refreshonly.inspect}" do
181 let(:attributes) do {
182 :refreshonly => refreshonly,
183 :unless => 'SELECT something',
186 context "not refreshing" do
187 it { expect(subject.should_run_sql).to be_falsey }
190 context "refreshing" do
191 it { expect(subject.should_run_sql(true)).to be_falsey }
197 context "when not matching 'unless'" do
198 before { expect(subject.parameter(:unless)).to receive(:matches).with('SELECT something').and_return(false) }
200 [true, :true].each do |refreshonly|
201 context "refreshonly => #{refreshonly.inspect}" do
202 let(:attributes) do {
203 :refreshonly => refreshonly,
204 :unless => 'SELECT something',
207 context "not refreshing" do
208 it { expect(subject.should_run_sql).to be_falsey }
211 context "refreshing" do
212 it { expect(subject.should_run_sql(true)).to be_truthy }
217 [false, :false].each do |refreshonly|
218 context "refreshonly => #{refreshonly.inspect}" do
219 let(:attributes) do {
220 :refreshonly => refreshonly,
221 :unless => 'SELECT something',
224 context "not refreshing" do
225 it { expect(subject.should_run_sql).to be_truthy }
228 context "refreshing" do
229 it { expect(subject.should_run_sql(true)).to be_truthy }
236 describe "#refresh" do
237 let(:attributes) do {} end
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)
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)