4
require 'puppet/settings'
5
require 'puppet/settings/file_setting'
7
describe Puppet::Settings::FileSetting do
8
FileSetting = Puppet::Settings::FileSetting
10
include PuppetSpec::Files
13
@basepath = make_absolute("/somepath")
16
describe "when determining whether the service user should be used" do
18
@settings = mock 'settings'
19
@settings.stubs(:[]).with(:mkusers).returns false
20
@settings.stubs(:service_user_available?).returns true
23
it "should be true if the service user is available" do
24
@settings.expects(:service_user_available?).returns true
25
setting = FileSetting.new(:settings => @settings, :owner => "root", :desc => "a setting")
26
setting.should be_use_service_user
29
it "should be true if 'mkusers' is set" do
30
@settings.expects(:[]).with(:mkusers).returns true
31
setting = FileSetting.new(:settings => @settings, :owner => "root", :desc => "a setting")
32
setting.should be_use_service_user
35
it "should be false if the service user is not available and 'mkusers' is unset" do
36
setting = FileSetting.new(:settings => @settings, :owner => "root", :desc => "a setting")
37
setting.should be_use_service_user
41
describe "when setting the owner" do
42
it "should allow the file to be owned by root" do
43
root_owner = lambda { FileSetting.new(:settings => mock("settings"), :owner => "root", :desc => "a setting") }
44
root_owner.should_not raise_error
47
it "should allow the file to be owned by the service user" do
48
service_owner = lambda { FileSetting.new(:settings => mock("settings"), :owner => "service", :desc => "a setting") }
49
service_owner.should_not raise_error
52
it "should allow the ownership of the file to be unspecified" do
53
no_owner = lambda { FileSetting.new(:settings => mock("settings"), :desc => "a setting") }
54
no_owner.should_not raise_error
57
it "should not allow other owners" do
58
invalid_owner = lambda { FileSetting.new(:settings => mock("settings"), :owner => "invalid", :desc => "a setting") }
59
invalid_owner.should raise_error(FileSetting::SettingError)
63
describe "when reading the owner" do
64
it "should be root when the setting specifies root" do
65
setting = FileSetting.new(:settings => mock("settings"), :owner => "root", :desc => "a setting")
66
setting.owner.should == "root"
69
it "should be the owner of the service when the setting specifies service and the service user should be used" do
70
settings = mock("settings")
71
settings.stubs(:[]).returns "the_service"
73
setting = FileSetting.new(:settings => settings, :owner => "service", :desc => "a setting")
74
setting.expects(:use_service_user?).returns true
75
setting.owner.should == "the_service"
78
it "should be the root when the setting specifies service and the service user should not be used" do
79
settings = mock("settings")
80
settings.stubs(:[]).returns "the_service"
82
setting = FileSetting.new(:settings => settings, :owner => "service", :desc => "a setting")
83
setting.expects(:use_service_user?).returns false
84
setting.owner.should == "root"
87
it "should be nil when the owner is unspecified" do
88
FileSetting.new(:settings => mock("settings"), :desc => "a setting").owner.should be_nil
92
describe "when setting the group" do
93
it "should allow the group to be service" do
94
service_group = lambda { FileSetting.new(:settings => mock("settings"), :group => "service", :desc => "a setting") }
95
service_group.should_not raise_error
98
it "should allow the group to be unspecified" do
99
no_group = lambda { FileSetting.new(:settings => mock("settings"), :desc => "a setting") }
100
no_group.should_not raise_error
103
it "should not allow invalid groups" do
104
invalid_group = lambda { FileSetting.new(:settings => mock("settings"), :group => "invalid", :desc => "a setting") }
105
invalid_group.should raise_error(FileSetting::SettingError)
109
describe "when reading the group" do
110
it "should be service when the setting specifies service" do
111
setting = FileSetting.new(:settings => mock("settings", :[] => "the_service"), :group => "service", :desc => "a setting")
112
setting.group.should == "the_service"
115
it "should be nil when the group is unspecified" do
116
FileSetting.new(:settings => mock("settings"), :desc => "a setting").group.should be_nil
120
it "should be able to be converted into a resource" do
121
FileSetting.new(:settings => mock("settings"), :desc => "eh").should respond_to(:to_resource)
124
describe "when being converted to a resource" do
126
@settings = mock 'settings'
127
@file = Puppet::Settings::FileSetting.new(:settings => @settings, :desc => "eh", :name => :myfile, :section => "mysect")
128
@file.stubs(:create_files?).returns true
129
@settings.stubs(:value).with(:myfile).returns @basepath
132
it "should return :file as its type" do
133
@file.type.should == :file
136
it "should skip non-existent files if 'create_files' is not enabled" do
137
@file.expects(:create_files?).returns false
138
@file.expects(:type).returns :file
139
File.expects(:exist?).with(@basepath).returns false
140
@file.to_resource.should be_nil
143
it "should manage existent files even if 'create_files' is not enabled" do
144
@file.expects(:create_files?).returns false
145
@file.expects(:type).returns :file
146
File.expects(:exist?).with(@basepath).returns true
147
@file.to_resource.should be_instance_of(Puppet::Resource)
150
describe "on POSIX systems", :if => Puppet.features.posix? do
151
it "should skip files in /dev" do
152
@settings.stubs(:value).with(:myfile).returns "/dev/file"
153
@file.to_resource.should be_nil
157
it "should skip files whose paths are not strings" do
158
@settings.stubs(:value).with(:myfile).returns :foo
159
@file.to_resource.should be_nil
162
it "should return a file resource with the path set appropriately" do
163
resource = @file.to_resource
164
resource.type.should == "File"
165
resource.title.should == @basepath
168
it "should fully qualified returned files if necessary (#795)" do
169
@settings.stubs(:value).with(:myfile).returns "myfile"
170
path = File.join(Dir.getwd, "myfile")
171
# Dir.getwd can return windows paths with backslashes, so we normalize them using expand_path
172
path = File.expand_path(path) if Puppet.features.microsoft_windows?
173
@file.to_resource.title.should == path
176
it "should set the mode on the file if a mode is provided as an octal number" do
179
@file.to_resource[:mode].should == '755'
182
it "should set the mode on the file if a mode is provided as a string" do
185
@file.to_resource[:mode].should == '755'
188
it "should not set the mode on a the file if manage_internal_file_permissions is disabled" do
189
Puppet[:manage_internal_file_permissions] = false
191
@file.stubs(:mode).returns(0755)
193
@file.to_resource[:mode].should == nil
196
it "should set the owner if running as root and the owner is provided" do
197
Puppet.features.expects(:root?).returns true
198
Puppet.features.stubs(:microsoft_windows?).returns false
200
@file.stubs(:owner).returns "foo"
201
@file.to_resource[:owner].should == "foo"
204
it "should not set the owner if manage_internal_file_permissions is disabled" do
205
Puppet[:manage_internal_file_permissions] = false
206
Puppet.features.stubs(:root?).returns true
207
@file.stubs(:owner).returns "foo"
209
@file.to_resource[:owner].should == nil
212
it "should set the group if running as root and the group is provided" do
213
Puppet.features.expects(:root?).returns true
214
Puppet.features.stubs(:microsoft_windows?).returns false
216
@file.stubs(:group).returns "foo"
217
@file.to_resource[:group].should == "foo"
220
it "should not set the group if manage_internal_file_permissions is disabled" do
221
Puppet[:manage_internal_file_permissions] = false
222
Puppet.features.stubs(:root?).returns true
223
@file.stubs(:group).returns "foo"
225
@file.to_resource[:group].should == nil
229
it "should not set owner if not running as root" do
230
Puppet.features.expects(:root?).returns false
231
Puppet.features.stubs(:microsoft_windows?).returns false
232
@file.stubs(:owner).returns "foo"
233
@file.to_resource[:owner].should be_nil
236
it "should not set group if not running as root" do
237
Puppet.features.expects(:root?).returns false
238
Puppet.features.stubs(:microsoft_windows?).returns false
239
@file.stubs(:group).returns "foo"
240
@file.to_resource[:group].should be_nil
243
describe "on Microsoft Windows systems" do
245
Puppet.features.stubs(:microsoft_windows?).returns true
248
it "should not set owner" do
249
@file.stubs(:owner).returns "foo"
250
@file.to_resource[:owner].should be_nil
253
it "should not set group" do
254
@file.stubs(:group).returns "foo"
255
@file.to_resource[:group].should be_nil
259
it "should set :ensure to the file type" do
260
@file.expects(:type).returns :directory
261
@file.to_resource[:ensure].should == :directory
264
it "should set the loglevel to :debug" do
265
@file.to_resource[:loglevel].should == :debug
268
it "should set the backup to false" do
269
@file.to_resource[:backup].should be_false
272
it "should tag the resource with the settings section" do
273
@file.expects(:section).returns "mysect"
274
@file.to_resource.should be_tagged("mysect")
277
it "should tag the resource with the setting name" do
278
@file.to_resource.should be_tagged("myfile")
281
it "should tag the resource with 'settings'" do
282
@file.to_resource.should be_tagged("settings")
285
it "should set links to 'follow'" do
286
@file.to_resource[:links].should == :follow