summaryrefslogtreecommitdiffstats
path: root/spec/integration/type/file_spec.rb
blob: 4bed8c6c145110880b0ef4984d10a67c43dd04d9 (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
507
508
509
510
511
#!/usr/bin/env rspec
require 'spec_helper'

require 'puppet_spec/files'

describe Puppet::Type.type(:file) do
  include PuppetSpec::Files

  before do
    # stub this to not try to create state.yaml
    Puppet::Util::Storage.stubs(:store)
  end

  it "should not attempt to manage files that do not exist if no means of creating the file is specified" do
    file = Puppet::Type.type(:file).new :path => "/my/file", :mode => "755"
    catalog = Puppet::Resource::Catalog.new
    catalog.add_resource file

    file.parameter(:mode).expects(:retrieve).never

    transaction = Puppet::Transaction.new(catalog)
    transaction.resource_harness.evaluate(file).should_not be_failed
  end

  describe "when writing files" do
    it "should backup files to a filebucket when one is configured" do
      bucket = Puppet::Type.type(:filebucket).new :path => tmpfile("filebucket"), :name => "mybucket"
      file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => "mybucket", :content => "foo"
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file
      catalog.add_resource bucket

      File.open(file[:path], "w") { |f| f.puts "bar" }

      md5 = Digest::MD5.hexdigest(File.read(file[:path]))

      catalog.apply

      bucket.bucket.getfile(md5).should == "bar\n"
    end

    it "should backup files in the local directory when a backup string is provided" do
      file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => ".bak", :content => "foo"
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file

      File.open(file[:path], "w") { |f| f.puts "bar" }

      catalog.apply

      backup = file[:path] + ".bak"
      FileTest.should be_exist(backup)
      File.read(backup).should == "bar\n"
    end

    it "should fail if no backup can be performed" do
      dir = tmpfile("backups")
      Dir.mkdir(dir)
      path = File.join(dir, "testfile")
      file = Puppet::Type.type(:file).new :path => path, :backup => ".bak", :content => "foo"
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file

      File.open(file[:path], "w") { |f| f.puts "bar" }

      # Create a directory where the backup should be so that writing to it fails
      Dir.mkdir(File.join(dir, "testfile.bak"))

      Puppet::Util::Log.stubs(:newmessage)

      catalog.apply

      File.read(file[:path]).should == "bar\n"
    end

    it "should not backup symlinks" do
      link = tmpfile("link")
      dest1 = tmpfile("dest1")
      dest2 = tmpfile("dest2")
      bucket = Puppet::Type.type(:filebucket).new :path => tmpfile("filebucket"), :name => "mybucket"
      file = Puppet::Type.type(:file).new :path => link, :target => dest2, :ensure => :link, :backup => "mybucket"
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file
      catalog.add_resource bucket

      File.open(dest1, "w") { |f| f.puts "whatever" }
      File.symlink(dest1, link)

      md5 = Digest::MD5.hexdigest(File.read(file[:path]))

      catalog.apply

      File.readlink(link).should == dest2
      Find.find(bucket[:path]) { |f| File.file?(f) }.should be_nil
    end

    it "should backup directories to the local filesystem by copying the whole directory" do
      file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => ".bak", :content => "foo", :force => true
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file

      Dir.mkdir(file[:path])
      otherfile = File.join(file[:path], "foo")
      File.open(otherfile, "w") { |f| f.print "yay" }

      catalog.apply

      backup = file[:path] + ".bak"
      FileTest.should be_directory(backup)
      File.read(File.join(backup, "foo")).should == "yay"
    end

    it "should backup directories to filebuckets by backing up each file separately" do
      bucket = Puppet::Type.type(:filebucket).new :path => tmpfile("filebucket"), :name => "mybucket"
      file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => "mybucket", :content => "foo", :force => true
      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file
      catalog.add_resource bucket

      Dir.mkdir(file[:path])
      foofile = File.join(file[:path], "foo")
      barfile = File.join(file[:path], "bar")
      File.open(foofile, "w") { |f| f.print "fooyay" }
      File.open(barfile, "w") { |f| f.print "baryay" }


      foomd5 = Digest::MD5.hexdigest(File.read(foofile))
      barmd5 = Digest::MD5.hexdigest(File.read(barfile))

      catalog.apply

      bucket.bucket.getfile(foomd5).should == "fooyay"
      bucket.bucket.getfile(barmd5).should == "baryay"
    end

    it "should propagate failures encountered when renaming the temporary file" do
      file = Puppet::Type.type(:file).new :path => tmpfile("fail_rename"), :content => "foo"
      file.stubs(:remove_existing) # because it tries to make a backup

      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file

      File.open(file[:path], "w") { |f| f.print "bar" }

      File.expects(:rename).raises ArgumentError

      lambda { file.write(:content) }.should raise_error(Puppet::Error)
      File.read(file[:path]).should == "bar"
    end
  end

  describe "when recursing" do
    def build_path(dir)
      Dir.mkdir(dir)
      File.chmod(0750, dir)

      @dirs = [dir]
      @files = []

      %w{one two}.each do |subdir|
        fdir = File.join(dir, subdir)
        Dir.mkdir(fdir)
        File.chmod(0750, fdir)
        @dirs << fdir

        %w{three}.each do |file|
          ffile = File.join(fdir, file)
          @files << ffile
          File.open(ffile, "w") { |f| f.puts "test #{file}" }
          File.chmod(0640, ffile)
        end
      end
    end

    it "should be able to recurse over a nonexistent file" do
      @path = tmpfile("file_integration_tests")

      @file = Puppet::Type::File.new(
        :name    => @path,
        :mode    => 0644,
        :recurse => true,
        :backup  => false
      )

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource @file

      lambda { @file.eval_generate }.should_not raise_error
    end

    it "should be able to recursively set properties on existing files" do
      @path = tmpfile("file_integration_tests")

      build_path(@path)

      @file = Puppet::Type::File.new(
        :name    => @path,
        :mode    => 0644,
        :recurse => true,
        :backup  => false
      )

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource @file

      @catalog.apply

      @dirs.each do |path|
        (File.stat(path).mode & 007777).should == 0755
      end

      @files.each do |path|
        (File.stat(path).mode & 007777).should == 0644
      end
    end

    it "should be able to recursively make links to other files" do
      source = tmpfile("file_link_integration_source")

      build_path(source)

      dest = tmpfile("file_link_integration_dest")

      @file = Puppet::Type::File.new(:name => dest, :target => source, :recurse => true, :ensure => :link, :backup => false)

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource @file

      @catalog.apply

      @dirs.each do |path|
        link_path = path.sub(source, dest)

        File.lstat(link_path).should be_directory
      end

      @files.each do |path|
        link_path = path.sub(source, dest)

        File.lstat(link_path).ftype.should == "link"
      end
    end

    it "should be able to recursively copy files" do
      source = tmpfile("file_source_integration_source")

      build_path(source)

      dest = tmpfile("file_source_integration_dest")

      @file = Puppet::Type::File.new(:name => dest, :source => source, :recurse => true, :backup => false)

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource @file

      @catalog.apply

      @dirs.each do |path|
        newpath = path.sub(source, dest)

        File.lstat(newpath).should be_directory
      end

      @files.each do |path|
        newpath = path.sub(source, dest)

        File.lstat(newpath).ftype.should == "file"
      end
    end

    it "should not recursively manage files managed by a more specific explicit file" do
      dir = tmpfile("recursion_vs_explicit_1")

      subdir = File.join(dir, "subdir")
      file = File.join(subdir, "file")

      FileUtils.mkdir_p(subdir)
      File.open(file, "w") { |f| f.puts "" }

      base = Puppet::Type::File.new(:name => dir, :recurse => true, :backup => false, :mode => "755")
      sub = Puppet::Type::File.new(:name => subdir, :recurse => true, :backup => false, :mode => "644")

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource base
      @catalog.add_resource sub

      @catalog.apply

      (File.stat(file).mode & 007777).should == 0644
    end

    it "should recursively manage files even if there is an explicit file whose name is a prefix of the managed file" do
      dir = tmpfile("recursion_vs_explicit_2")

      managed   = File.join(dir, "file")
      generated = File.join(dir, "file_with_a_name_starting_with_the_word_file")

      FileUtils.mkdir_p(dir)
      File.open(managed,   "w") { |f| f.puts "" }
      File.open(generated, "w") { |f| f.puts "" }

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource Puppet::Type::File.new(:name => dir,     :recurse => true, :backup => false, :mode => "755")
      @catalog.add_resource Puppet::Type::File.new(:name => managed, :recurse => true, :backup => false, :mode => "644")

      @catalog.apply

      (File.stat(generated).mode & 007777).should == 0755
    end
  end

  describe "when generating resources" do
    before do
      @source = tmpfile("generating_in_catalog_source")

      @dest = tmpfile("generating_in_catalog_dest")

      Dir.mkdir(@source)

      s1 = File.join(@source, "one")
      s2 = File.join(@source, "two")

      File.open(s1, "w") { |f| f.puts "uno" }
      File.open(s2, "w") { |f| f.puts "dos" }

      @file = Puppet::Type::File.new(:name => @dest, :source => @source, :recurse => true, :backup => false)

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource @file
    end

    it "should add each generated resource to the catalog" do
      @catalog.apply do |trans|
        @catalog.resource(:file, File.join(@dest, "one")).should be_instance_of(@file.class)
        @catalog.resource(:file, File.join(@dest, "two")).should be_instance_of(@file.class)
      end
    end

    it "should have an edge to each resource in the relationship graph" do
      @catalog.apply do |trans|
        one = @catalog.resource(:file, File.join(@dest, "one"))
        @catalog.relationship_graph.edge?(@file, one).should be

        two = @catalog.resource(:file, File.join(@dest, "two"))
        @catalog.relationship_graph.edge?(@file, two).should be
      end
    end
  end

  describe "when copying files" do
    # Ticket #285.
    it "should be able to copy files with pound signs in their names" do
      source = tmpfile("filewith#signs")

      dest = tmpfile("destwith#signs")

      File.open(source, "w") { |f| f.print "foo" }

      file = Puppet::Type::File.new(:name => dest, :source => source)

      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file

      catalog.apply

      File.read(dest).should == "foo"
    end

    it "should be able to copy files with spaces in their names" do
      source = tmpfile("filewith spaces")

      dest = tmpfile("destwith spaces")

      File.open(source, "w") { |f| f.print "foo" }
      File.chmod(0755, source)

      file = Puppet::Type::File.new(:path => dest, :source => source)

      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file

      catalog.apply

      File.read(dest).should == "foo"
      (File.stat(dest).mode & 007777).should == 0755
    end

    it "should be able to copy individual files even if recurse has been specified" do
      source = tmpfile("source")
      dest = tmpfile("dest")

      File.open(source, "w") { |f| f.print "foo" }

      file = Puppet::Type::File.new(:name => dest, :source => source, :recurse => true)

      catalog = Puppet::Resource::Catalog.new
      catalog.add_resource file
      catalog.apply

      File.read(dest).should == "foo"
    end
  end

  it "should be able to create files when 'content' is specified but 'ensure' is not" do
    dest = tmpfile("files_with_content")


    file = Puppet::Type.type(:file).new(
      :name    => dest,
      :content => "this is some content, yo"
    )

    catalog = Puppet::Resource::Catalog.new
    catalog.add_resource file
    catalog.apply

    File.read(dest).should == "this is some content, yo"
  end

  it "should create files with content if both 'content' and 'ensure' are set" do
    dest = tmpfile("files_with_content")


    file = Puppet::Type.type(:file).new(
      :name    => dest,
      :ensure  => "file",
      :content => "this is some content, yo"
    )

    catalog = Puppet::Resource::Catalog.new
    catalog.add_resource file
    catalog.apply

    File.read(dest).should == "this is some content, yo"
  end

  it "should delete files with sources but that are set for deletion" do
    dest = tmpfile("dest_source_with_ensure")
    source = tmpfile("source_source_with_ensure")
    File.open(source, "w") { |f| f.puts "yay" }
    File.open(dest, "w") { |f| f.puts "boo" }


    file = Puppet::Type.type(:file).new(
      :name   => dest,
      :ensure => :absent,
      :source => source,
      :backup => false
    )

    catalog = Puppet::Resource::Catalog.new
    catalog.add_resource file
    catalog.apply

    File.should_not be_exist(dest)
  end

  describe "when purging files" do
    before do
      @sourcedir = tmpfile("purge_source")
      @destdir = tmpfile("purge_dest")
      Dir.mkdir(@sourcedir)
      Dir.mkdir(@destdir)
      @sourcefile = File.join(@sourcedir, "sourcefile")
      @copiedfile = File.join(@destdir, "sourcefile")
      @localfile = File.join(@destdir, "localfile")
      @purgee = File.join(@destdir, "to_be_purged")
      File.open(@localfile, "w") { |f| f.puts "rahtest" }
      File.open(@sourcefile, "w") { |f| f.puts "funtest" }
      # this file should get removed
      File.open(@purgee, "w") { |f| f.puts "footest" }


      @lfobj = Puppet::Type.newfile(
        :title   => "localfile",
        :path    => @localfile,
        :content => "rahtest\n",
        :ensure  => :file,
        :backup  => false
      )


      @destobj = Puppet::Type.newfile(
        :title   => "destdir",
        :path    => @destdir,
        :source  => @sourcedir,
        :backup  => false,
        :purge   => true,
        :recurse => true
      )

      @catalog = Puppet::Resource::Catalog.new
      @catalog.add_resource @lfobj, @destobj
    end

    it "should still copy remote files" do
      @catalog.apply
      FileTest.should be_exist(@copiedfile)
    end

    it "should not purge managed, local files" do
      @catalog.apply
      FileTest.should be_exist(@localfile)
    end

    it "should purge files that are neither remote nor otherwise managed" do
      @catalog.apply
      FileTest.should_not be_exist(@purgee)
    end
  end
end