summaryrefslogtreecommitdiff
path: root/spec/unit/configurer/downloader.rb
blob: efd280d93602195a05782aeec8659c14cc6a5932 (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
#!/usr/bin/env ruby

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

require 'puppet/configurer/downloader'

describe Puppet::Configurer::Downloader do
    it "should require a name" do
        lambda { Puppet::Configurer::Downloader.new }.should raise_error(ArgumentError)
    end

    it "should require a path and a source at initialization" do
        lambda { Puppet::Configurer::Downloader.new("name") }.should raise_error(ArgumentError)
    end

    it "should set the name, path and source appropriately" do
        dler = Puppet::Configurer::Downloader.new("facts", "path", "source")
        dler.name.should == "facts"
        dler.path.should == "path"
        dler.source.should == "source"
    end

    it "should be able to provide a timeout value" do
        Puppet::Configurer::Downloader.should respond_to(:timeout)
    end

    it "should use the configtimeout, converted to an integer, as its timeout" do
        Puppet.settings.expects(:value).with(:configtimeout).returns "50"
        Puppet::Configurer::Downloader.timeout.should == 50
    end

    describe "when creating the file that does the downloading" do
        before do
            @dler = Puppet::Configurer::Downloader.new("foo", "path", "source")
        end

        it "should create a file instance with the right path and source" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:path] == "path" and opts[:source] == "source" }
            @dler.file
        end

        it "should tag the file with the downloader name" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:tag] == "foo" }
            @dler.file
        end

        it "should always recurse" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:recurse] == true }
            @dler.file
        end

        it "should always purge" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:purge] == true }
            @dler.file
        end

        it "should never be in noop" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:noop] == false }
            @dler.file
        end

        it "should always set the owner to the current UID" do
            Process.expects(:uid).returns 51
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:owner] == 51 }
            @dler.file
        end

        it "should always set the group to the current GID" do
            Process.expects(:gid).returns 61
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:group] == 61 }
            @dler.file
        end

        it "should always force the download" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:force] == true }
            @dler.file
        end

        it "should never back up when downloading" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:backup] == false }
            @dler.file
        end

        it "should support providing an 'ignore' parameter" do
            Puppet::Type.type(:file).expects(:create).with { |opts| opts[:ignore] == ".svn" }
            @dler = Puppet::Configurer::Downloader.new("foo", "path", "source", ".svn")
            @dler.file
        end
    end

    describe "when creating the catalog to do the downloading" do
        before do
            @dler = Puppet::Configurer::Downloader.new("foo", "path", "source")
        end

        it "should create a catalog and add the file to it" do
            file = mock 'file'
            catalog = mock 'catalog'

            @dler.expects(:file).returns file

            Puppet::Resource::Catalog.expects(:new).returns catalog
            catalog.expects(:add_resource).with(file)

            @dler.catalog.should equal(catalog)
        end
    end

    describe "when downloading" do
        before do
            @dler = Puppet::Configurer::Downloader.new("foo", "path", "source")
        end

        it "should log that it is downloading" do
            Puppet.expects(:info)
            Timeout.stubs(:timeout)

            @dler.evaluate
        end

        it "should set a timeout for the download" do
            Puppet::Configurer::Downloader.expects(:timeout).returns 50
            Timeout.expects(:timeout).with(50)

            @dler.evaluate
        end

        it "should apply the catalog within the timeout block" do
            catalog = mock 'catalog'
            @dler.expects(:catalog).returns(catalog)

            Timeout.expects(:timeout).yields

            catalog.expects(:apply)

            @dler.evaluate
        end

        it "should return all changed file paths" do
            trans = mock 'transaction'

            catalog = mock 'catalog'
            @dler.expects(:catalog).returns(catalog)
            catalog.expects(:apply).yields(trans)

            Timeout.expects(:timeout).yields

            resource = mock 'resource'
            resource.expects(:[]).with(:path).returns "/changed/file"

            trans.expects(:changed?).returns([resource])

            @dler.evaluate.should == %w{/changed/file}
        end

        it "should yield the resources if a block is given" do
            trans = mock 'transaction'

            catalog = mock 'catalog'
            @dler.expects(:catalog).returns(catalog)
            catalog.expects(:apply).yields(trans)

            Timeout.expects(:timeout).yields

            resource = mock 'resource'
            resource.expects(:[]).with(:path).returns "/changed/file"

            trans.expects(:changed?).returns([resource])

            yielded = nil
            @dler.evaluate { |r| yielded = r }
            yielded.should == resource
        end

        it "should catch and log exceptions" do
            Puppet.expects(:err)
            Timeout.stubs(:timeout).raises(Puppet::Error, "testing")

            lambda { @dler.evaluate }.should_not raise_error
        end
    end
end