summaryrefslogtreecommitdiffstats
path: root/spec/unit/application/filebucket.rb
blob: e87bab4027bd39aac9a93c9a0bc74dde16548aed (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env ruby

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

require 'puppet/application/filebucket'

describe "Filebucket" do
    before :each do
        @filebucket = Puppet::Application[:filebucket]
    end

    it "should ask Puppet::Application to not parse Puppet configuration file" do
        @filebucket.should_parse_config?.should be_false
    end

    it "should declare a get command" do
        @filebucket.should respond_to(:get)
    end

    it "should declare a backup command" do
        @filebucket.should respond_to(:backup)
    end

    it "should declare a restore command" do
        @filebucket.should respond_to(:restore)
    end

    [:bucket, :debug, :local, :remote, :verbose].each do |option|
        it "should declare handle_#{option} method" do
            @filebucket.should respond_to("handle_#{option}".to_sym)
        end

        it "should store argument value when calling handle_#{option}" do
            @filebucket.options.expects(:[]=).with("#{option}".to_sym, 'arg')
            @filebucket.send("handle_#{option}".to_sym, 'arg')
        end
    end

    describe "during setup" do

        before :each do
            Puppet::Log.stubs(:newdestination)
            Puppet.stubs(:settraps)
            Puppet::Log.stubs(:level=)
            Puppet.stubs(:parse_config)
            Puppet::Network::Client.dipper.stubs(:new)
            @filebucket.options.stubs(:[]).with(any_parameters)
        end


        it "should set console as the log destination" do
            Puppet::Log.expects(:newdestination).with(:console)

            @filebucket.run_setup
        end

        it "should trap INT" do
            @filebucket.expects(:trap).with(:INT)

            @filebucket.run_setup
        end

        it "should set log level to debug if --debug was passed" do
            @filebucket.options.stubs(:[]).with(:debug).returns(true)

            Puppet::Log.expects(:level=).with(:debug)

            @filebucket.run_setup
        end

        it "should set log level to info if --verbose was passed" do
            @filebucket.options.stubs(:[]).with(:verbose).returns(true)

            Puppet::Log.expects(:level=).with(:info)

            @filebucket.run_setup
        end

        it "should Parse puppet config" do
            Puppet.expects(:parse_config)

            @filebucket.run_setup
        end

        it "should print puppet config if asked to in Puppet config" do
            @filebucket.stubs(:exit)
            Puppet.settings.stubs(:print_configs?).returns(true)

            Puppet.settings.expects(:print_configs)

            @filebucket.run_setup
        end

        it "should exit after printing puppet config if asked to in Puppet config" do
            Puppet.settings.stubs(:print_configs?).returns(true)

            lambda { @filebucket.run_setup }.should raise_error(SystemExit)
        end

        describe "with local bucket" do

            before :each do
                @filebucket.options.stubs(:[]).with(:local).returns(true)
            end

            it "should create a client with the default bucket if none passed" do
                Puppet.stubs(:[]).with(:bucketdir).returns("path")

                Puppet::Network::Client::Dipper.expects(:new).with { |h| h[:Path] == "path" }

                @filebucket.run_setup
            end

            it "should create a local Client dipper with the given bucket" do
                @filebucket.options.stubs(:[]).with(:bucket).returns("path")

                Puppet::Network::Client::Dipper.expects(:new).with { |h| h[:Path] == "path" }

                @filebucket.run_setup
            end

        end

        describe "with remote bucket" do

            it "should create a remote Client to the configured server" do
                Puppet.stubs(:[]).with(:server).returns("puppet.reductivelabs.com")

                Puppet::Network::Client::Dipper.expects(:new).with { |h| h[:Server] == "puppet.reductivelabs.com" }

                @filebucket.run_setup
            end

        end

    end

    describe "when running" do

        before :each do
            Puppet::Log.stubs(:newdestination)
            Puppet.stubs(:settraps)
            Puppet::Log.stubs(:level=)
            Puppet.stubs(:parse_config)
            Puppet::Network::Client.dipper.stubs(:new)
            @filebucket.options.stubs(:[]).with(any_parameters)

            @client = stub 'client'
            Puppet::Network::Client::Dipper.stubs(:new).returns(@client)

            @filebucket.run_setup
        end

        it "should use the first non-option parameter as the dispatch" do
            ARGV.stubs(:shift).returns(:get)

            @filebucket.get_command.should == :get
        end

        describe "the command get" do

            before :each do
                @filebucket.stubs(:print)
            end

            it "should call the client getfile method" do
                @client.expects(:getfile)

                @filebucket.get
            end

            it "should call the client getfile method with the given md5" do
                md5="DEADBEEF"
                ARGV.stubs(:shift).returns(md5)

                @client.expects(:getfile).with(md5)

                @filebucket.get
            end

            it "should print the file content" do
                @client.stubs(:getfile).returns("content")

                @filebucket.expects(:print).returns("content")

                @filebucket.get
            end

        end

        describe "the command backup" do
            it "should call the client backup method for each given parameter" do
                @filebucket.stubs(:puts)
                FileTest.stubs(:exists?).returns(true)
                FileTest.stubs(:readable?).returns(true)
                ARGV.stubs(:each).multiple_yields("file1","file2")

                @client.expects(:backup).with("file1")
                @client.expects(:backup).with("file2")

                @filebucket.backup
            end
        end

        describe "the command restore" do
            it "should call the client getfile method with the given md5" do
                md5="DEADBEEF"
                file="testfile"
                ARGV.stubs(:shift).returns(file,md5)

                @client.expects(:restore).with(file,md5)

                @filebucket.restore
            end
        end

    end


end