~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to spec/unit/settings/file_setting_spec.rb

  • Committer: Benjamin Kerensa
  • Date: 2012-11-21 23:50:52 UTC
  • mfrom: (1.1.30)
  • Revision ID: bkerensa@ubuntu.com-20121121235052-ah7nzabp77sh69gb
New Upstream Release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env ruby
 
2
require 'spec_helper'
 
3
 
 
4
require 'puppet/settings'
 
5
require 'puppet/settings/file_setting'
 
6
 
 
7
describe Puppet::Settings::FileSetting do
 
8
  FileSetting = Puppet::Settings::FileSetting
 
9
 
 
10
  include PuppetSpec::Files
 
11
 
 
12
  before do
 
13
    @basepath = make_absolute("/somepath")
 
14
  end
 
15
 
 
16
  describe "when determining whether the service user should be used" do
 
17
    before do
 
18
      @settings = mock 'settings'
 
19
      @settings.stubs(:[]).with(:mkusers).returns false
 
20
      @settings.stubs(:service_user_available?).returns true
 
21
    end
 
22
 
 
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
 
27
    end
 
28
 
 
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
 
33
    end
 
34
 
 
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
 
38
    end
 
39
  end
 
40
 
 
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
 
45
    end
 
46
 
 
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
 
50
    end
 
51
 
 
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
 
55
    end
 
56
 
 
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)
 
60
    end
 
61
  end
 
62
 
 
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"
 
67
    end
 
68
 
 
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"
 
72
 
 
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"
 
76
    end
 
77
 
 
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"
 
81
 
 
82
      setting = FileSetting.new(:settings => settings, :owner => "service", :desc => "a setting")
 
83
      setting.expects(:use_service_user?).returns false
 
84
      setting.owner.should == "root"
 
85
    end
 
86
 
 
87
    it "should be nil when the owner is unspecified" do
 
88
      FileSetting.new(:settings => mock("settings"), :desc => "a setting").owner.should be_nil
 
89
    end
 
90
  end
 
91
 
 
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
 
96
    end
 
97
 
 
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
 
101
    end
 
102
 
 
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)
 
106
    end
 
107
  end
 
108
 
 
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"
 
113
    end
 
114
 
 
115
    it "should be nil when the group is unspecified" do
 
116
      FileSetting.new(:settings => mock("settings"), :desc => "a setting").group.should be_nil
 
117
    end
 
118
  end
 
119
 
 
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)
 
122
  end
 
123
 
 
124
  describe "when being converted to a resource" do
 
125
    before 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
 
130
    end
 
131
 
 
132
    it "should return :file as its type" do
 
133
      @file.type.should == :file
 
134
    end
 
135
 
 
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
 
141
    end
 
142
 
 
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)
 
148
    end
 
149
 
 
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
 
154
      end
 
155
    end
 
156
 
 
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
 
160
    end
 
161
 
 
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
 
166
    end
 
167
 
 
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
 
174
    end
 
175
 
 
176
    it "should set the mode on the file if a mode is provided as an octal number" do
 
177
      @file.mode = 0755
 
178
 
 
179
      @file.to_resource[:mode].should == '755'
 
180
    end
 
181
 
 
182
    it "should set the mode on the file if a mode is provided as a string" do
 
183
      @file.mode = '0755'
 
184
 
 
185
      @file.to_resource[:mode].should == '755'
 
186
    end
 
187
 
 
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
 
190
 
 
191
      @file.stubs(:mode).returns(0755)
 
192
 
 
193
      @file.to_resource[:mode].should == nil
 
194
    end
 
195
 
 
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
 
199
 
 
200
      @file.stubs(:owner).returns "foo"
 
201
      @file.to_resource[:owner].should == "foo"
 
202
    end
 
203
 
 
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"
 
208
 
 
209
      @file.to_resource[:owner].should == nil
 
210
    end
 
211
 
 
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
 
215
 
 
216
      @file.stubs(:group).returns "foo"
 
217
      @file.to_resource[:group].should == "foo"
 
218
    end
 
219
 
 
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"
 
224
 
 
225
      @file.to_resource[:group].should == nil
 
226
    end
 
227
 
 
228
 
 
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
 
234
    end
 
235
 
 
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
 
241
    end
 
242
 
 
243
    describe "on Microsoft Windows systems" do
 
244
      before :each do
 
245
        Puppet.features.stubs(:microsoft_windows?).returns true
 
246
      end
 
247
 
 
248
      it "should not set owner" do
 
249
        @file.stubs(:owner).returns "foo"
 
250
        @file.to_resource[:owner].should be_nil
 
251
      end
 
252
 
 
253
      it "should not set group" do
 
254
        @file.stubs(:group).returns "foo"
 
255
        @file.to_resource[:group].should be_nil
 
256
      end
 
257
    end
 
258
 
 
259
    it "should set :ensure to the file type" do
 
260
      @file.expects(:type).returns :directory
 
261
      @file.to_resource[:ensure].should == :directory
 
262
    end
 
263
 
 
264
    it "should set the loglevel to :debug" do
 
265
      @file.to_resource[:loglevel].should == :debug
 
266
    end
 
267
 
 
268
    it "should set the backup to false" do
 
269
      @file.to_resource[:backup].should be_false
 
270
    end
 
271
 
 
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")
 
275
    end
 
276
 
 
277
    it "should tag the resource with the setting name" do
 
278
      @file.to_resource.should be_tagged("myfile")
 
279
    end
 
280
 
 
281
    it "should tag the resource with 'settings'" do
 
282
      @file.to_resource.should be_tagged("settings")
 
283
    end
 
284
 
 
285
    it "should set links to 'follow'" do
 
286
      @file.to_resource[:links].should == :follow
 
287
    end
 
288
  end
 
289
end
 
290