summaryrefslogtreecommitdiff
path: root/spec/unit/provider/file_spec.rb
blob: d2208d0b1665d3f32266b1ae5d41c233c525e004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


require 'spec_helper'
require 'tmpdir'

describe Chef::Provider::File do
  before(:each) do
    @node = Chef::Node.new
    @node.name "latte"
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @resource = Chef::Resource::File.new("seattle")
    @resource.path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates", "seattle.txt")))

    @provider = Chef::Provider::File.new(@resource, @run_context)
  end

  it "should return a Chef::Provider::File" do
    @provider.should be_a_kind_of(Chef::Provider::File)
  end

  it "should store the resource passed to new as new_resource" do
    @provider.new_resource.should eql(@resource)
  end

  it "should store the node passed to new as node" do
    @provider.node.should eql(@node)
  end

  it "should load a current resource based on the one specified at construction" do
    @provider.load_current_resource
    @provider.current_resource.should be_a_kind_of(Chef::Resource::File)
    @provider.current_resource.name.should eql(@resource.name)
    @provider.current_resource.path.should eql(@resource.path)
    @provider.current_resource.content.should eql(nil)
  end

  describe "examining file security metadata on Unix" do
    it "should collect the current state of the file on the filesystem and populate current_resource" do
      # test setup
      stat_struct = mock("::File.stat", :mode => 0600, :uid => 0, :gid => 0, :mtime => 10000) 
      ::File.should_receive(:stat).exactly(2).with(@resource.path).and_return(stat_struct)

      Etc.should_receive(:getgrgid).with(0).and_return(mock("Group Ent", :name => "wheel"))
      Etc.should_receive(:getpwuid).with(0).and_return(mock("User Ent", :name => "root"))

      # test execution 
      @provider.load_current_resource

      # post-condition checks
      @provider.current_resource.mode.should == "600"
      @provider.current_resource.owner.should == "root"
      @provider.current_resource.group.should == "wheel"
    end

    it "should NOT update the new_resource state with the current_resourse state if new_resource state is already specified" do
      # test setup
      stat_struct = mock("::File.stat", :mode => 0600, :uid => 0, :gid => 0, :mtime => 10000) 
      ::File.should_receive(:stat).exactly(2).with(@resource.path).and_return(stat_struct)

      @provider.new_resource.group(1)
      @provider.new_resource.owner(1)
      @provider.new_resource.mode(0644)

      # test execution 
      @provider.load_current_resource

      # post-condition checks
      @provider.new_resource.group.should == 1
      @provider.new_resource.owner.should == 1
      @provider.new_resource.mode.should == 0644
    end

    context "when the new_resource does not specify the desired access control" do
      it "records access control information in the new resource after modifying the file" do
        # test setup
        stat_struct = mock("::File.stat", :mode => 0600, :uid => 0, :gid => 0, :mtime => 10000) 
        # called once in update_new_file_state and once in checksum
        ::File.should_receive(:stat).twice.with(@provider.new_resource.path).and_return(stat_struct)  
        ::File.should_receive(:directory?).once.with(@provider.new_resource.path).and_return(false)

        Etc.should_receive(:getpwuid).with(0).and_return(mock("User Ent", :name => "root"))
        Etc.should_receive(:getgrgid).with(0).and_return(mock("Group Ent", :name => "wheel"))

        @provider.new_resource.group(nil)
        @provider.new_resource.owner(nil)
        @provider.new_resource.mode(nil)

        # test exectution
        @provider.update_new_file_state

        # post-condition checks
        @provider.new_resource.group.should == "wheel"
        @provider.new_resource.owner.should == "root"
        @provider.new_resource.mode.should == "600"
      end
    end
  end

  describe "when reporting security metadata on windows" do

    it "records the file owner" do
      pending
    end

    it "records rights for each user in the ACL" do
      pending
    end

    it "records deny_rights for each user in the ACL" do
      pending
    end
  end

  it "should load a mostly blank current resource if the file specified in new_resource doesn't exist/isn't readable" do
    resource = Chef::Resource::File.new("seattle")
    resource.path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates", "woot.txt")))
    node = Chef::Node.new
    node.name "latte"
    provider = Chef::Provider::File.new(resource, @run_context)
    provider.load_current_resource
    provider.current_resource.should be_a_kind_of(Chef::Resource::File)
    provider.current_resource.name.should eql(resource.name)
    provider.current_resource.path.should eql(resource.path)
  end

  it "should not backup symbolic links on delete" do
    path = File.expand_path(File.join(CHEF_SPEC_DATA, "detroit.txt"))
    ::File.open(path, "w") do |file|
      file.write("Detroit's not so nice, so you should come to Seattle instead and buy me a beer instead.")
    end
    @resource = Chef::Resource::File.new("detroit")
    @resource.path(path)
    @node = Chef::Node.new
    @node.name "latte"
    @provider = Chef::Provider::File.new(@resource, @run_context)

    ::File.stub!(:symlink?).and_return(true)
    @provider.should_not_receive(:backup)
    @provider.run_action(:delete)
    @resource.should be_updated_by_last_action
  end

  it "should compare the current content with the requested content" do
    @provider.load_current_resource

    @provider.new_resource.content "foobar"
    @provider.compare_content.should eql(false)

    @provider.new_resource.content IO.read(@resource.path)
    @provider.compare_content.should eql(true)
  end

  it "should set the content of the file to the requested content" do
    io = StringIO.new
    @provider.load_current_resource
    @provider.new_resource.content "foobar"
    @provider.should_receive(:diff_current_from_content).and_return("")
    @provider.should_receive(:backup)
    File.should_receive(:open).with(@provider.new_resource.path, "w").and_yield(io)
    @provider.set_content
    io.string.should == "foobar"
  end

  it "should not set the content of the file if it already matches the requested content" do
    @provider.load_current_resource
    @provider.new_resource.content IO.read(@resource.path)
    File.stub!(:open).and_return(1)
    File.should_not_receive(:open).with(@provider.new_resource.path, "w")
    lambda { @provider.set_content }.should_not raise_error
    @resource.should_not be_updated_by_last_action
  end

  it "should create the file if it is missing, then set the attributes on action_create" do
    @provider.load_current_resource
    @provider.stub!(:update_new_file_state)
    @provider.new_resource.stub!(:path).and_return(File.join(Dir.tmpdir, "monkeyfoo"))
    @provider.access_controls.should_receive(:set_all)
    @provider.should_receive(:diff_current_from_content).and_return("")
    File.stub!(:open).and_return(1)
    #File.should_receive(:directory?).with("/tmp").and_return(true)
    File.should_receive(:open).with(@provider.new_resource.path, "w+")
    @provider.run_action(:create)
    @resource.should be_updated_by_last_action
  end

  it "should create the file with the proper content if it is missing, then set attributes on action_create" do
    io = StringIO.new
    @provider.load_current_resource
    @provider.new_resource.content "foobar"
    @provider.new_resource.stub!(:path).and_return(File.join(Dir.tmpdir, "monkeyfoo"))
    @provider.should_receive(:diff_current_from_content).and_return("")
    @provider.stub!(:update_new_file_state)
    File.should_receive(:open).with(@provider.new_resource.path, "w+").and_yield(io)
    @provider.access_controls.should_receive(:set_all)
    @provider.run_action(:create)
    io.string.should == "foobar"
    @resource.should be_updated_by_last_action
  end

  it "should delete the file if it exists and is writable on action_delete" do
    @provider.new_resource.stub!(:path).and_return(File.join(Dir.tmpdir, "monkeyfoo"))
    @provider.stub!(:backup).and_return(true)
    File.should_receive("exists?").exactly(2).times.with(@provider.new_resource.path).and_return(true)
    File.should_receive("writable?").with(@provider.new_resource.path).and_return(true)
    File.should_receive(:delete).with(@provider.new_resource.path).and_return(true)
    @provider.run_action(:delete)
    @resource.should be_updated_by_last_action
  end

  it "should not raise an error if it cannot delete the file because it does not exist" do
    @provider.new_resource.stub!(:path).and_return(File.join(Dir.tmpdir, "monkeyfoo"))
    @provider.stub!(:backup).and_return(true)
    File.should_receive("exists?").exactly(2).times.with(@provider.new_resource.path).and_return(false)
    lambda { @provider.run_action(:delete) }.should_not raise_error()
    @resource.should_not be_updated_by_last_action
  end

  it "should update the atime/mtime on action_touch" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return(File.join(Dir.tmpdir, "monkeyfoo"))
    @provider.should_receive(:diff_current_from_content).and_return("")
    @provider.stub!(:update_new_file_state)
    File.should_receive(:utime).once.and_return(1)
    File.stub!(:open).and_return(1)
    @provider.access_controls.should_receive(:set_all).once
    @provider.run_action(:touch)
    @resource.should be_updated_by_last_action
  end

  it "should keep 1 backup copy if specified" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return("/tmp/s-20080705111233")
    @provider.new_resource.stub!(:backup).and_return(1)
    Dir.stub!(:[]).and_return([ "/tmp/s-20080705111233", "/tmp/s-20080705111232", "/tmp/s-20080705111223"])
    FileUtils.should_receive(:rm).with("/tmp/s-20080705111223").once.and_return(true)
    FileUtils.should_receive(:rm).with("/tmp/s-20080705111232").once.and_return(true)
    FileUtils.stub!(:cp).and_return(true)
    FileUtils.stub!(:mkdir_p).and_return(true)
    File.stub!(:exist?).and_return(true)
    @provider.backup
  end

  it "should backup a file no more than :backup times" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return("/tmp/s-20080705111233")
    @provider.new_resource.stub!(:backup).and_return(2)
    Dir.stub!(:[]).and_return([ "/tmp/s-20080705111233", "/tmp/s-20080705111232", "/tmp/s-20080705111223"])
    FileUtils.should_receive(:rm).with("/tmp/s-20080705111223").once.and_return(true)
    FileUtils.stub!(:cp).and_return(true)
    FileUtils.stub!(:mkdir_p).and_return(true)
    File.stub!(:exist?).and_return(true)
    @provider.backup
  end

  it "should not attempt to backup a file if :backup == 0" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return("/tmp/s-20080705111233")
    @provider.new_resource.stub!(:backup).and_return(0)
    FileUtils.stub!(:cp).and_return(true)
    File.stub!(:exist?).and_return(true)
    FileUtils.should_not_receive(:cp)
    @provider.backup
  end

  it "should put the backup backup file in the directory specified by Chef::Config[:file_backup_path]" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return("/tmp/s-20080705111233")
    @provider.new_resource.stub!(:backup).and_return(1)
    Chef::Config.stub!(:[]).with(:file_backup_path).and_return("/some_prefix")
    Dir.stub!(:[]).and_return([ "/some_prefix/tmp/s-20080705111233", "/some_prefix/tmp/s-20080705111232", "/some_prefix/tmp/s-20080705111223"])
    FileUtils.should_receive(:mkdir_p).with("/some_prefix/tmp").once
    FileUtils.should_receive(:rm).with("/some_prefix/tmp/s-20080705111232").once.and_return(true)
    FileUtils.should_receive(:rm).with("/some_prefix/tmp/s-20080705111223").once.and_return(true)
    FileUtils.stub!(:cp).and_return(true)
    File.stub!(:exist?).and_return(true)
    @provider.backup
  end

  it "should strip the drive letter from the backup resource path (for Windows platforms)" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return("C:/tmp/s-20080705111233")
    @provider.new_resource.stub!(:backup).and_return(1)
    Chef::Config.stub!(:[]).with(:file_backup_path).and_return("C:/some_prefix")
    Dir.stub!(:[]).and_return([ "C:/some_prefix/tmp/s-20080705111233", "C:/some_prefix/tmp/s-20080705111232", "C:/some_prefix/tmp/s-20080705111223"])
    FileUtils.should_receive(:mkdir_p).with("C:/some_prefix/tmp").once
    FileUtils.should_receive(:rm).with("C:/some_prefix/tmp/s-20080705111232").once.and_return(true)
    FileUtils.should_receive(:rm).with("C:/some_prefix/tmp/s-20080705111223").once.and_return(true)
    FileUtils.stub!(:cp).and_return(true)
    File.stub!(:exist?).and_return(true)
    @provider.backup
  end

  it "should keep the same ownership on backed up files" do
    @provider.load_current_resource
    @provider.new_resource.stub!(:path).and_return("/tmp/s-20080705111233")
    @provider.new_resource.stub!(:backup).and_return(1)
    Chef::Config.stub!(:[]).with(:file_backup_path).and_return("/some_prefix")
    Dir.stub!(:[]).and_return([ "/some_prefix/tmp/s-20080705111233", "/some_prefix/tmp/s-20080705111232", "/some_prefix/tmp/s-20080705111223"])
    FileUtils.stub!(:mkdir_p).and_return(true)
    FileUtils.stub!(:rm).and_return(true)
    File.stub!(:exist?).and_return(true)
    Time.stub!(:now).and_return(Time.at(1272147455).getgm)
    FileUtils.should_receive(:cp).with("/tmp/s-20080705111233", "/some_prefix/tmp/s-20080705111233.chef-20100424221735", {:preserve => true}).and_return(true)
    @provider.backup
  end

  describe "when the enclosing directory does not exist" do
    before do
      @resource.path("/tmp/no-such-path/file.txt")
    end

    it "raises a specific error describing the problem" do
      lambda {@provider.run_action(:create)}.should raise_error(Chef::Exceptions::EnclosingDirectoryDoesNotExist)
    end
  end

  describe "when creating a file which may be missing" do
    it "should not call action create if the file exists" do
      @resource.path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates", "seattle.txt")))
      @provider = Chef::Provider::File.new(@resource, @run_context)
      File.should_not_receive(:open)
      @provider.run_action(:create_if_missing)
      @resource.should_not be_updated_by_last_action
    end

    it "should call action create if the does not file exist" do
      @resource.path("/tmp/example-dir/non_existant_file")
      @provider = Chef::Provider::File.new(@resource, @run_context)
      @provider.should_receive(:diff_current_from_content).and_return("")
      ::File.stub!(:exists?).with(@resource.path).and_return(false)
      ::File.stub!(:directory?).with("/tmp/example-dir/non_existant_file").and_return(false)
      ::File.stub!(:directory?).with("/tmp/example-dir").and_return(true)
      @provider.stub!(:update_new_file_state)
      io = StringIO.new
      File.should_receive(:open).with(@provider.new_resource.path, "w+").and_yield(io)
      #@provider.should_receive(:action_create).and_return(true)
      @provider.run_action(:create_if_missing)
      @resource.should be_updated_by_last_action
    end
  end

  describe "when a diff is requested", :uses_diff => true do

    before(:each) do
      @original_config = Chef::Config.hash_dup
    end

    after(:each) do
      Chef::Config.configuration = @original_config if @original_config
    end

    describe "when identifying files as binary or text" do

      it "should identify zero-length files as text" do
        Tempfile.open("some-temp") do |file|
          @resource.path(file.path)
          @provider = Chef::Provider::File.new(@resource, @run_context)
          @provider.is_binary?(file.path).should be_false
        end
      end

      it "should correctly identify text files as being text" do
        Tempfile.open("some-temp") do |file|
          @resource.path(file.path)
          file.puts("This is a text file.")
          file.puts("That has a couple of lines in it.")
          file.puts("And lets make sure that other printable chars work too: ~!@\#$%^&*()`:\"<>?{}|_+,./;'[]\\-=")
          file.close
          @provider = Chef::Provider::File.new(@resource, @run_context)
          @provider.is_binary?(file.path).should be_false
        end
      end

      it "should identify a null-terminated string as binary" do
        Tempfile.open("some-temp") do |file|
          @resource.path(file.path)
          file.write("This is a binary file.\0")
          file.close
          @provider = Chef::Provider::File.new(@resource, @run_context)
          @provider.is_binary?(file.path).should be_true
        end
      end

    end

    it "should not return diff output when chef config has disabled it" do
      Chef::Config[:diff_disabled] = true
      Tempfile.open("some-temp") do |file|
        @resource.path(file.path)
        @provider = Chef::Provider::File.new(@resource, @run_context)
        @provider.load_current_resource
        result = @provider.diff_current_from_content "foo baz"
        result.should == [ "(diff output suppressed by config)" ]
        @resource.diff.should be_nil
      end
    end

    it "should not return diff output when there is no new file to compare it to" do
      Tempfile.open("some-temp") do |file|
        Tempfile.open("other-temp") do |missing_file|
          missing_path = missing_file.path
          missing_file.close
          missing_file.unlink
          @resource.path(file.path)
          @provider = Chef::Provider::File.new(@resource, @run_context)
          @provider.load_current_resource
          result = @provider.diff_current missing_path
          result.should == [ "(no temp file with new content, diff output suppressed)" ]
          @resource.diff.should be_nil
        end
      end
    end

    it "should produce diff output when the file does not exist yet, but suppress reporting it" do
      Tempfile.open("some-temp") do |file|
        @resource.path(file.path)
        file.close
        file.unlink
        @provider = Chef::Provider::File.new(@resource, @run_context)
        @provider.load_current_resource
        result = @provider.diff_current_from_content "foo baz"
        result.length.should == 4
        @resource.diff.should be_nil
      end
    end

    it "should not produce a diff when the current resource file is above the filesize threshold" do
      Chef::Config[:diff_filesize_threshold] = 5
      Tempfile.open("some-temp") do |file|
        @resource.path(file.path)
        file.puts("this is a line which is longer than 5 characters")
        file.flush
        @provider = Chef::Provider::File.new(@resource, @run_context)
        @provider.load_current_resource
        result = @provider.diff_current_from_content "foo"  # not longer than 5
        result.should == [ "(file sizes exceed 5 bytes, diff output suppressed)" ]
        @resource.diff.should be_nil
      end
    end

    it "should not produce a diff when the new content is above the filesize threshold" do
      Chef::Config[:diff_filesize_threshold] = 5
      Tempfile.open("some-temp") do |file|
        @resource.path(file.path)
        file.puts("foo")
        file.flush
        @provider = Chef::Provider::File.new(@resource, @run_context)
        @provider.load_current_resource
        result = @provider.diff_current_from_content "this is a line that is longer than 5 characters"
        result.should == [ "(file sizes exceed 5 bytes, diff output suppressed)" ]
        @resource.diff.should be_nil
      end
    end

    it "should not produce a diff when the generated diff size is above the diff size threshold" do
      Chef::Config[:diff_output_threshold] = 5
      Tempfile.open("some-temp") do |file|
        @resource.path(file.path)
        file.puts("some text to increase the size of the diff")
        file.flush
        @provider = Chef::Provider::File.new(@resource, @run_context)
        @provider.load_current_resource
        result = @provider.diff_current_from_content "this is a line that is longer than 5 characters"
        result.should == [ "(long diff of over 5 characters, diff output suppressed)" ]
        @resource.diff.should be_nil
      end
    end

    it "should return valid diff output when content does not match the string content provided" do
       Tempfile.open("some-temp") do |file|
         @resource.path file.path
         @provider = Chef::Provider::File.new(@resource, @run_context) 
         @provider.load_current_resource
         result = @provider.diff_current_from_content "foo baz"
         # remove the file name info which varies.
         result.shift(2)
         # Result appearance seems to vary slightly under solaris diff
         # So we'll compare the second line which is common to both.
         # Solaris: -1,1 +1,0 @@, "+foo baz"
         # Linux/Mac: -1,0, +1 @@, "+foo baz"
         result.length.should == 2
         result[1].should == "+foo baz"
         @resource.diff.should_not be_nil
       end
    end
  end
end