summaryrefslogtreecommitdiffstats
path: root/spec/unit/file_serving/content.rb
blob: 5441dff53ea02412f1c0707cd09c7301a1f40acc (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
#!/usr/bin/env ruby

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

require 'puppet/file_serving/content'

describe Puppet::FileServing::Content do
    it "should should be a subclass of Base" do
        Puppet::FileServing::Content.superclass.should equal(Puppet::FileServing::Base)
    end

    it "should indirect file_content" do
        Puppet::FileServing::Content.indirection.name.should == :file_content
    end

    it "should should include the IndirectionHooks module in its indirection" do
        Puppet::FileServing::Content.indirection.metaclass.included_modules.should include(Puppet::FileServing::IndirectionHooks)
    end

    it "should have a method for collecting its attributes" do
        Puppet::FileServing::Content.new("/path").should respond_to(:collect)
    end

    it "should retrieve and store its contents when its attributes are collected" do
        content = Puppet::FileServing::Content.new("/path")

        result = "foo"
        File.stubs(:lstat).returns(stub("stat", :ftype => "file"))
        File.expects(:read).with("/path").returns result
        content.collect

        content.instance_variable_get("@content").should_not be_nil
    end
end

describe Puppet::FileServing::Content, "when returning the contents" do
    before do
        @path = "/my/path"
        @content = Puppet::FileServing::Content.new(@path, :links => :follow)
    end

    it "should fail if the file is a symlink and links are set to :manage" do
        @content.links = :manage
        File.expects(:lstat).with(@path).returns stub("stat", :ftype => "symlink")
        proc { @content.content }.should raise_error(ArgumentError)
    end

    it "should fail if a path is not set" do
        proc { @content.content() }.should raise_error(Errno::ENOENT)
    end

    it "should raise Errno::ENOENT if the file is absent" do
        @content.path = "/there/is/absolutely/no/chance/that/this/path/exists"
        proc { @content.content() }.should raise_error(Errno::ENOENT)
    end

    it "should return the contents of the path if the file exists" do
        File.expects(:stat).with(@path).returns stub("stat", :ftype => "file")
        File.expects(:read).with(@path).returns(:mycontent)
        @content.content.should == :mycontent
    end

    it "should cache the returned contents" do
        File.expects(:stat).with(@path).returns stub("stat", :ftype => "file")
        File.expects(:read).with(@path).returns(:mycontent)
        @content.content

        # The second run would throw a failure if the content weren't being cached.
        @content.content
    end
end