summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/file_spec.rb
blob: 2505a0cd5305e231bae48532f85664090c84e7fa (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector/file'


describe Puppet::Indirector::File do
    before :each do
        Puppet::Indirector::Terminus.stubs(:register_terminus_class)
        @model = mock 'model'
        @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
        Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)

        @file_class = Class.new(Puppet::Indirector::File) do
            def self.to_s
                "Testing::Mytype"
            end
        end

        @searcher = @file_class.new

        @path = "/my/file"
        @dir = "/my"

        @request = stub 'request', :key => @path
    end

    describe "when finding files" do
        it "should provide a method to return file contents at a specified path" do
            @searcher.should respond_to(:find)
        end

        it "should use the server data directory plus the indirection name if the run_mode is master" do
            Puppet.run_mode.expects(:master?).returns true
            Puppet.settings.expects(:value).with(:server_datadir).returns "/my/dir"

            @searcher.data_directory.should == File.join("/my/dir", "mystuff")
        end

        it "should use the client data directory plus the indirection name if the run_mode is not master" do
            Puppet.run_mode.expects(:master?).returns false
            Puppet.settings.expects(:value).with(:client_datadir).returns "/my/dir"

            @searcher.data_directory.should == File.join("/my/dir", "mystuff")
        end

        it "should use the newest file in the data directory matching the indirection key without extension" do
            @searcher.expects(:data_directory).returns "/data/dir"
            @request.stubs(:key).returns "foo"
            Dir.expects(:glob).with("/data/dir/foo.*").returns %w{/data1.stuff /data2.stuff}

            stat1 = stub 'data1', :mtime => (Time.now - 5)
            stat2 = stub 'data2', :mtime => Time.now
            File.expects(:stat).with("/data1.stuff").returns stat1
            File.expects(:stat).with("/data2.stuff").returns stat2

            @searcher.latest_path(@request).should == "/data2.stuff"
        end

        it "should return nil when no files are found" do
            @searcher.stubs(:latest_path).returns nil

            @searcher.find(@request).should be_nil
        end

        it "should determine the file format from the file extension" do
            @searcher.file_format("/data2.pson").should == "pson"
        end

        it "should fail if the model does not support the file format" do
            @searcher.stubs(:latest_path).returns "/my/file.pson"

            @model.expects(:support_format?).with("pson").returns false

            lambda { @searcher.find(@request) }.should raise_error(ArgumentError)
        end
    end

    describe "when saving files" do
        before do
            @content = "my content"
            @file = stub 'file', :content => @content, :path => @path, :name => @path, :render => "mydata"
            @request.stubs(:instance).returns @file
        end

        it "should provide a method to save file contents at a specified path" do
            @searcher.should respond_to(:save)
        end

        it "should choose the file extension based on the default format of the model" do
            @model.expects(:default_format).returns "pson"

            @searcher.serialization_format.should == "pson"
        end

        it "should place the file in the data directory, named after the indirection, key, and format" do
            @searcher.stubs(:data_directory).returns "/my/dir"
            @searcher.stubs(:serialization_format).returns "pson"

            @request.stubs(:key).returns "foo"
            @searcher.file_path(@request).should == File.join("/my/dir", "foo.pson")
        end

        it "should fail intelligently if the file's parent directory does not exist" do
            @searcher.stubs(:file_path).returns "/my/dir/file.pson"
            @searcher.stubs(:serialization_format).returns "pson"

            @request.stubs(:key).returns "foo"
            File.expects(:directory?).with(File.join("/my/dir")).returns(false)

            proc { @searcher.save(@request) }.should raise_error(Puppet::Error)
        end

        it "should render the instance using the file format and print it to the file path" do
            @searcher.stubs(:file_path).returns "/my/file.pson"
            @searcher.stubs(:serialization_format).returns "pson"

            File.stubs(:directory?).returns true

            @request.instance.expects(:render).with("pson").returns "data"

            fh = mock 'filehandle'
            File.expects(:open).with("/my/file.pson", "w").yields fh
            fh.expects(:print).with("data")

            @searcher.save(@request)
        end

        it "should fail intelligently if a file cannot be written" do
            filehandle = mock 'file'
            File.stubs(:directory?).returns(true)
            File.stubs(:open).yields(filehandle)
            filehandle.expects(:print).raises(ArgumentError)

            @searcher.stubs(:file_path).returns "/my/file.pson"
            @model.stubs(:default_format).returns "pson"

            @instance.stubs(:render).returns "stuff"

            proc { @searcher.save(@request) }.should raise_error(Puppet::Error)
        end
    end

    describe "when removing files" do
        it "should provide a method to remove files" do
            @searcher.should respond_to(:destroy)
        end

        it "should remove files in all formats found in the data directory that match the request key" do
            @searcher.stubs(:data_directory).returns "/my/dir"
            @request.stubs(:key).returns "me"

            Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one /two}

            File.expects(:unlink).with("/one")
            File.expects(:unlink).with("/two")

            @searcher.destroy(@request)
        end

        it "should throw an exception if no file is found" do
            @searcher.stubs(:data_directory).returns "/my/dir"
            @request.stubs(:key).returns "me"

            Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns []

            proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error)
        end

        it "should fail intelligently if a file cannot be removed" do
            @searcher.stubs(:data_directory).returns "/my/dir"
            @request.stubs(:key).returns "me"

            Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one}

            File.expects(:unlink).with("/one").raises ArgumentError

            proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error)
        end
    end
end