summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/parsedfile_spec.rb
blob: f20b6b235f1d84d102bb286194a29dea432e6cd1 (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
#!/usr/bin/env ruby

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

require 'puppet/provider/parsedfile'

# Most of the tests for this are still in test/ral/provider/parsedfile.rb.
describe Puppet::Provider::ParsedFile do
    before do
        @class = Class.new(Puppet::Provider::ParsedFile)
    end

    describe "when looking up records loaded from disk" do
        it "should return nil if no records have been loaded" do
            @class.record?("foo").should be_nil
        end
    end

    describe "when generating a list of instances" do
        it "should return an instance for each record parsed from all of the registered targets" do
            @class.expects(:targets).returns %w{/one /two}
            @class.stubs(:skip_record?).returns false
            one = [:uno1, :uno2]
            two = [:dos1, :dos2]
            @class.expects(:prefetch_target).with("/one").returns one
            @class.expects(:prefetch_target).with("/two").returns two

            results = []
            (one + two).each do |inst|
                results << inst.to_s + "_instance"
                @class.expects(:new).with(inst).returns(results[-1])
            end

            @class.instances.should == results
        end

        it "should skip specified records" do
            @class.expects(:targets).returns %w{/one}
            @class.expects(:skip_record?).with(:uno).returns false
            @class.expects(:skip_record?).with(:dos).returns true
            one = [:uno, :dos]
            @class.expects(:prefetch_target).returns one

            @class.expects(:new).with(:uno).returns "eh"
            @class.expects(:new).with(:dos).never

            @class.instances
        end
    end

    describe "when flushing a file's records to disk" do
        before do
            # This way we start with some @records, like we would in real life.
            @class.stubs(:retrieve).returns []
            @class.default_target = "/foo/bar"
            @class.initvars
            @class.prefetch

            @filetype = Puppet::Util::FileType.filetype(:flat).new("/my/file")
            Puppet::Util::FileType.filetype(:flat).stubs(:new).with("/my/file").returns @filetype

            @filetype.stubs(:write)
        end

        it "should back up the file being written if the filetype can be backed up" do
            @filetype.expects(:backup)

            @class.flush_target("/my/file")
        end

        it "should not try to back up the file if the filetype cannot be backed up" do
            @filetype = Puppet::Util::FileType.filetype(:ram).new("/my/file")
            Puppet::Util::FileType.filetype(:flat).expects(:new).returns @filetype

            @filetype.stubs(:write)

            @class.flush_target("/my/file")
        end

        it "should not back up the file more than once between calls to 'prefetch'" do
            @filetype.expects(:backup).once

            @class.flush_target("/my/file")
            @class.flush_target("/my/file")
        end

        it "should back the file up again once the file has been reread" do
            @filetype.expects(:backup).times(2)

            @class.flush_target("/my/file")
            @class.prefetch
            @class.flush_target("/my/file")
        end
    end
end