summaryrefslogtreecommitdiffstats
path: root/spec/unit/type/file.rb
blob: 663c5dc155db30dd115600c60266bb683167ed6c (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

describe Puppet::Type.type(:file) do
    before do
        @path = Tempfile.new("puppetspec")
        @path.close!()
        @path = @path.path
        @file = Puppet::Type::File.create(:name => @path)
    end

    describe "when used with content and replace=>false" do
        before do
            @file[:content] = "foo"
            @file[:replace] = false
        end

        it "should be insync if the file exists and the content is different" do
            File.open(@path, "w") do |f| f.puts "bar" end
            @file.property(:content).insync?("bar").should be_true
        end

        it "should be insync if the file exists and the content is right" do
            File.open(@path, "w") do |f| f.puts "foo" end
            @file.property(:content).insync?("foo").should be_true
        end

        it "should not be insync if the file does not exist" do
            @file.property(:content).insync?(:nil).should be_false
        end
    end

    describe "when specifying a source" do
        before do
            @file[:source] = "/bar"
        end

        it "should raise if source doesn't exist" do
            @file.property(:source).expects(:found?).returns(false)
            lambda { @file.retrieve }.should raise_error(Puppet::Error)
        end

    end

    describe "when retrieving remote files" do
        before do
            @filesource = Puppet::Type::File::FileSource.new
            @filesource.server = mock 'fileserver'

            @file.stubs(:uri2obj).returns(@filesource)

            @file[:source] = "puppet:///test"
        end

        it "should fail without writing if it cannot retrieve remote contents" do
            # create the file, because we only get the problem when it starts
            # out absent.
            File.open(@file[:path], "w") { |f| f.puts "a" }
            @file.expects(:write).never

            @filesource.server.stubs(:describe).returns("493\tfile\t100\t0\t{md5}3f5fef3bddbc4398c46a7bd7ba7b3af7")
            @filesource.server.stubs(:retrieve).raises(RuntimeError)
            @file.property(:source).retrieve
            lambda { @file.property(:source).sync }.should raise_error(Puppet::Error)
        end

        it "should fail if it cannot describe remote contents" do
            @filesource.server.stubs(:describe).raises(Puppet::Network::XMLRPCClientError.new("Testing"))
            lambda { @file.retrieve }.should raise_error(Puppet::Error)
        end

        it "should fail during eval_generate if no remote sources exist" do
            file = Puppet::Type.type(:file).create :path => "/foobar", :source => "/this/file/does/not/exist", :recurse => true

            lambda { file.eval_generate }.should raise_error(Puppet::Error)
        end
    end

    describe "when managing links" do
        require 'puppettest/support/assertions'
        include PuppetTest
        require 'tempfile'

        before do
            @basedir = tempfile
            Dir.mkdir(@basedir)
            @file = File.join(@basedir, "file")
            @link = File.join(@basedir, "link")

            File.open(@file, "w", 0644) { |f| f.puts "yayness"; f.flush }
            File.symlink(@file, @link)

            @resource = Puppet.type(:file).create(
                :path => @link,
                :mode => "755"
            )
            @catalog = Puppet::Node::Catalog.new
            @catalog.add_resource @resource
        end

        after do
            remove_tmp_files
        end

        it "should default to managing the link" do
            @catalog.apply
            # I convert them to strings so they display correctly if there's an error.
            ("%o" % (File.stat(@file).mode & 007777)).should == "%o" % 0644
        end

        it "should be able to follow links" do
            @resource[:links] = :follow
            @catalog.apply

            ("%o" % (File.stat(@file).mode & 007777)).should == "%o" % 0755
        end
    end
end