Skip to content

Commit 421e9ae

Browse files
author
Ashley Penney
committed
Merge pull request puppetlabs#212 from hunner/handle_refreshonly
Handle refreshonly correctly
2 parents b2b9b13 + b85a118 commit 421e9ae

File tree

5 files changed

+211
-3
lines changed

5 files changed

+211
-3
lines changed

lib/puppet/provider/postgresql_psql/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
def command()
44
if ((! resource[:unless]) or (resource[:unless].empty?))
5-
if (resource[:refreshonly])
5+
if (resource.refreshonly?)
66
# So, if there's no 'unless', and we're in "refreshonly" mode,
77
# we need to return the target command here. If we don't,
88
# then Puppet will generate an event indicating that this

lib/puppet/type/postgresql_psql.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def sync(refreshing = false)
2424
# method, and then inside of the body of 'sync' we can tell
2525
# whether or not we're refreshing.
2626

27-
if ((@resource[:refreshonly] == :false) || refreshing)
27+
if (!@resource.refreshonly? || refreshing)
2828
# If we're not in 'refreshonly' mode, or we're not currently
2929
# refreshing, then we just call the parent method.
3030
super()
@@ -69,10 +69,11 @@ def sync(refreshing = false)
6969
defaultto("/tmp")
7070
end
7171

72-
newparam(:refreshonly) do
72+
newparam(:refreshonly, :boolean => true) do
7373
desc "If 'true', then the SQL will only be executed via a notify/subscribe event."
7474

7575
defaultto(:false)
76+
newvalues(:true, :false)
7677
end
7778

7879
def refresh()

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require 'puppetlabs_spec_helper/module_spec_helper'
22

33
RSpec.configure do |c|
4+
c.mock_with :rspec do |mock|
5+
mock.syntax = [:expect, :should]
6+
end
47
c.include PuppetlabsSpec::Files
58

69
c.before :each do
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
require 'spec_helper'
2+
3+
describe Puppet::Type.type(:postgresql_psql).provider(:ruby) do
4+
let(:name) { 'rspec psql test' }
5+
let(:resource) do
6+
Puppet::Type.type(:postgresql_psql).new({ :name => name, :provider => :ruby }.merge attributes)
7+
end
8+
9+
let(:provider) { resource.provider }
10+
11+
context("#run_sql_command") do
12+
describe "with default attributes" do
13+
let(:attributes) do { :db => 'spec_db' } end
14+
15+
it "executes with the given psql_path on the given DB" do
16+
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture).with(
17+
['psql', '-d', attributes[:db], '-t', '-c', 'SELECT something'],
18+
'postgres',
19+
'postgres'
20+
)
21+
provider.run_sql_command("SELECT something")
22+
end
23+
end
24+
describe "with psql_path and db" do
25+
let(:attributes) do {
26+
:psql_path => '/opt/postgres/psql',
27+
:psql_user => 'spec_user',
28+
:psql_group => 'spec_group',
29+
:cwd => '/spec',
30+
:db => 'spec_db'
31+
} end
32+
33+
it "executes with the given psql_path on the given DB" do
34+
expect(Dir).to receive(:chdir).with(attributes[:cwd]).and_yield
35+
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture).with(
36+
[attributes[:psql_path], '-d', attributes[:db], '-t', '-c', 'SELECT something'],
37+
attributes[:psql_user],
38+
attributes[:psql_group]
39+
)
40+
provider.run_sql_command("SELECT something")
41+
end
42+
end
43+
end
44+
45+
context("#command") do
46+
context "when unless is specified" do
47+
[:true, :false, true, false].each do |refresh|
48+
context "and refreshonly is #{refresh}" do
49+
let(:attributes) { {
50+
:command => 'SELECT something',
51+
:db => 'spec_db',
52+
:unless => 'SELECT something',
53+
:refreshonly => refresh
54+
} }
55+
56+
it "does not fail when the status is successful" do
57+
expect(provider).to receive(:run_unless_sql_command).and_return ["1 row returned", 0]
58+
provider.command
59+
end
60+
61+
it "returns the given command when rows are returned" do
62+
expect(provider).to receive(:run_unless_sql_command).and_return ["1 row returned", 0]
63+
expect(provider.command).to eq("SELECT something")
64+
end
65+
66+
it "does not return the given command when no rows are returned" do
67+
expect(provider).to receive(:run_unless_sql_command).and_return ["0 rows returned", 0]
68+
expect(provider.command).to_not eq("SELECT something")
69+
end
70+
71+
it "raises an error when the sql command fails" do
72+
allow(provider).to receive(:run_unless_sql_command).and_return ["Something went wrong", 1]
73+
expect { provider.command }.to raise_error(Puppet::Error, /Something went wrong/)
74+
end
75+
end
76+
end
77+
end
78+
79+
context "when unless is not specified" do
80+
context "and refreshonly is true" do
81+
let(:attributes) do {
82+
:command => 'SELECT something',
83+
:db => 'spec_db',
84+
:refreshonly => :true
85+
} end
86+
it "does not run unless sql command" do
87+
expect(provider).to_not receive(:run_unless_sql_command)
88+
provider.command
89+
end
90+
91+
it "returns the given command do disable sync" do
92+
expect(provider.command).to eq("SELECT something")
93+
end
94+
end
95+
96+
context "and refreshonly is false" do
97+
let(:attributes) do {
98+
:command => 'SELECT something',
99+
:db => 'spec_db',
100+
:refreshonly => :false
101+
} end
102+
it "does not run unless sql command" do
103+
expect(provider).to_not receive(:run_unless_sql_command)
104+
provider.command
105+
end
106+
107+
it "does not return the command so as to enable sync" do
108+
expect(provider.command).to_not eq("SELECT something")
109+
end
110+
end
111+
end
112+
end
113+
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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].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+
}.each do |attr, value|
34+
context attr do
35+
let(:attributes) do { attr => value } end
36+
its([attr]) { should == value }
37+
end
38+
end
39+
40+
context "default values" do
41+
let(:attributes) do {} end
42+
its([:psql_path]) { should eq("psql") }
43+
its([:psql_user]) { should eq("postgres") }
44+
its([:psql_group]) { should eq("postgres") }
45+
its([:cwd]) { should eq("/tmp") }
46+
its(:refreshonly?) { should be_false }
47+
end
48+
end
49+
50+
describe "#refreshonly" do
51+
[true, :true].each do |refreshonly|
52+
context "=> #{refreshonly.inspect}" do
53+
let(:attributes) do { :refreshonly => refreshonly } end
54+
it "has a value of true" do
55+
expect(subject.refreshonly?).to be_true
56+
end
57+
it "will not enforce command on sync because refresh() will be called" do
58+
expect(subject.provider).to_not receive(:command=)
59+
subject.property(:command).sync
60+
end
61+
end
62+
end
63+
64+
[false, :false].each do |refreshonly|
65+
context "=> #{refreshonly.inspect}" do
66+
let(:attributes) do { :refreshonly => refreshonly } end
67+
it "has a value of false" do
68+
expect(subject.refreshonly?).to be_false
69+
end
70+
it "will enforce command on sync because refresh() will not be called" do
71+
expect(subject.provider).to receive(:command=)
72+
subject.property(:command).sync
73+
end
74+
end
75+
end
76+
end
77+
78+
## If we refresh the resource, the command should always be run regardless of
79+
## refreshonly
80+
describe "when responding to refresh" do
81+
[true, :true, false, :false].each do |refreshonly|
82+
context "with refreshonly => #{refreshonly.inspect}" do
83+
let(:attributes) do { :refreshonly => refreshonly } end
84+
it "will enforce command on sync" do
85+
expect(subject.provider).to receive(:command=)
86+
subject.refresh
87+
end
88+
end
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)