Skip to content

Commit 8d6c5ba

Browse files
committed
(maint) Fix required features validation for service enable property
Before, validation on values from the `enable` property was overwritten with a custom one which ommited checking required features for values such as :mask. This resulted in a not very useful error message instead of the intended `Provider x must have features y to set z`. This fix re-enables check for required features before the custom validation.
1 parent 08206ae commit 8d6c5ba

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

lib/puppet/type/service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def insync?(current)
9292
end
9393

9494
validate do |value|
95+
super(value)
9596
if (value == :manual || value == :delayed) && !Puppet::Util::Platform.windows?
9697
raise Puppet::Error.new(_("Setting enable to %{value} is only supported on Microsoft Windows.") % { value: value.to_s} )
9798
end

spec/unit/type/service_spec.rb

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,50 +72,69 @@ def safely_load_service_type
7272
allow(@provider.class).to receive(:supports_parameter?).and_return(true)
7373
end
7474

75-
it "should support :true as a value" do
76-
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :true)
77-
expect(srv.should(:enable)).to eq(:true)
78-
end
75+
describe "for value without required features" do
76+
before :each do
77+
allow(@provider).to receive(:satisfies?)
78+
end
7979

80-
it "should support :false as a value" do
81-
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :false)
82-
expect(srv.should(:enable)).to eq(:false)
83-
end
80+
it "should not support :mask as a value" do
81+
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :mask) }.to raise_error(
82+
Puppet::ResourceError,
83+
/Provider .+ must have features 'maskable' to set 'enable' to 'mask'/
84+
)
85+
end
8486

85-
it "should support :mask as a value" do
86-
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :mask)
87-
expect(srv.should(:enable)).to eq(:mask)
88-
end
87+
it "should not support :manual as a value when not on Windows" do
88+
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
8989

90-
it "should support :manual as a value on Windows" do
91-
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
92-
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :manual)
93-
expect(srv.should(:enable)).to eq(:manual)
94-
end
90+
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :manual) }.to raise_error(
91+
Puppet::Error,
92+
/Setting enable to manual is only supported on Microsoft Windows\./
93+
)
94+
end
9595

96-
it "should support :delayed as a value on Windows" do
97-
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
96+
it "should not support :delayed as a value when not on Windows" do
97+
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
9898

99-
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :delayed)
100-
expect(srv.should(:enable)).to eq(:delayed)
99+
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :delayed) }.to raise_error(
100+
Puppet::Error,
101+
/Setting enable to delayed is only supported on Microsoft Windows\./
102+
)
103+
end
101104
end
102105

103-
it "should not support :manual as a value when not on Windows" do
104-
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
106+
describe "for value with required features" do
107+
before :each do
108+
allow(@provider).to receive(:satisfies?).and_return(:true)
109+
end
110+
111+
it "should support :true as a value" do
112+
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :true)
113+
expect(srv.should(:enable)).to eq(:true)
114+
end
105115

106-
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :manual) }.to raise_error(
107-
Puppet::Error,
108-
/Setting enable to manual is only supported on Microsoft Windows\./
109-
)
110-
end
116+
it "should support :false as a value" do
117+
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :false)
118+
expect(srv.should(:enable)).to eq(:false)
119+
end
111120

112-
it "should not support :delayed as a value when not on Windows" do
113-
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
121+
it "should support :mask as a value" do
122+
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :mask)
123+
expect(srv.should(:enable)).to eq(:mask)
124+
end
114125

115-
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :delayed) }.to raise_error(
116-
Puppet::Error,
117-
/Setting enable to delayed is only supported on Microsoft Windows\./
118-
)
126+
it "should support :manual as a value on Windows" do
127+
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
128+
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :manual)
129+
expect(srv.should(:enable)).to eq(:manual)
130+
end
131+
132+
it "should support :delayed as a value on Windows" do
133+
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
134+
135+
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :delayed)
136+
expect(srv.should(:enable)).to eq(:delayed)
137+
end
119138
end
120139
end
121140

0 commit comments

Comments
 (0)