summaryrefslogtreecommitdiff
path: root/chef/spec/unit/provider/remote_file_spec.rb
blob: cce78a5e2a2e0fad8383d3e126741958208ea5ab (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))

describe Chef::Provider::RemoteFile, "action_create" do
  before(:each) do
    @resource = Chef::Resource::RemoteFile.new("seattle")
    @resource.path(File.join(File.dirname(__FILE__), "..", "..", "data", "seattle.txt"))
    @resource.source("http://foo")
    @node = Chef::Node.new
    @node.name "latte"
    @provider = Chef::Provider::RemoteFile.new(@node, @resource)
    @provider.current_resource = @resource.clone
  end
  
  it "should call do_remote_file" do
    @provider.should_receive(:do_remote_file).with(@resource.source, @resource.path)
    @provider.action_create
  end
  
end

describe Chef::Provider::RemoteFile, "do_remote_file" do
  before(:each) do
    @rest = mock(Chef::REST, { })
    @tempfile = mock(Tempfile, { :path => "/tmp/foo", })
    @tempfile.stub!(:open).and_return(@tempfile)
    @tempfile.stub!(:closed?).and_return(false)
    @tempfile.stub!(:close)
    @rest.stub!(:get_rest).and_return(@tempfile)
    @resource = Chef::Resource::RemoteFile.new("seattle")
    @resource.path(File.join(File.dirname(__FILE__), "..", "..", "data", "seattle.txt"))
    @resource.source("foo")
    @resource.cookbook_name = "monkey"
    @node = Chef::Node.new
    @node.name "latte"
    @node.fqdn "latte.local"
    @provider = Chef::Provider::RemoteFile.new(@node, @resource)
    @provider.stub!(:checksum).and_return("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
    @provider.current_resource = @resource.clone
    @provider.current_resource.checksum("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
    File.stub!(:exists?).and_return(true)
    FileUtils.stub!(:cp).and_return(true)
    Chef::Platform.stub!(:find_platform_and_version).and_return([ :mac_os_x, "10.5.1" ])
  end
  
  def do_remote_file
    Chef::REST.stub!(:new).and_return(@rest)    
    @provider.do_remote_file(@resource.source, @resource.path)
  end

  describe "when given a URI source" do
    describe "and given a checksum" do
      it "should not download the file if the checksum matches" do
        @resource.checksum("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
        @resource.source("http://opscode.com/seattle.txt")
        @rest.should_not_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
        do_remote_file
      end

      it "should not download the file if the checksum is a partial match from the beginning" do
        @resource.checksum("0fd012fd")
        @resource.source("http://opscode.com/seattle.txt")
        @rest.should_not_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
        do_remote_file
      end


      it "should download the file if the checksum does not match" do
        @resource.checksum("this hash doesn't match")
        @resource.source("http://opscode.com/seattle.txt")
        @rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
        do_remote_file
      end

      it "should download the file if the checksum matches, but not from the beginning" do
        @resource.checksum("fd012fd")
        @resource.source("http://opscode.com/seattle.txt")
        @rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
        do_remote_file
      end


    end

    describe "and not given a checksum" do
      it "should download the file from the remote URL" do
        @resource.checksum(nil)
        @resource.source("http://opscode.com/seattle.txt")
        @rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
        do_remote_file
      end
    end
  end
  
  describe "when given a non-URI source" do
    describe "and using chef-solo" do
      it "should load the file from the local cookbook" do
        Chef::Config[:solo] = true
        File.stub!(:open).and_return(@tempfile)
        @provider.should_receive(:find_preferred_file).with("monkey", :remote_file, @resource.source, "latte.local", nil, nil).and_return(@tempfile.path)
        do_remote_file
      end
      
      after(:each) do
        Chef::Config[:solo] = false
      end
    end
    
    it "should call generate_url with the current checksum as an extra attribute" do
      @provider.should_receive(:generate_url).with(@resource.source, "files", { :checksum => "0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa"})
      do_remote_file
    end

    it "should call get_rest with a correctly composed url" do
      url = "cookbooks/#{@resource.cookbook_name}/files?id=#{@resource.source}"
      url += "&platform=mac_os_x"
      url += "&version=10.5.1"
      url += "&fqdn=latte.local"
      url += "&node_name=latte"
      url += "&checksum=0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa"
      @rest.should_receive(:get_rest).with(url, true).and_return(@tempfile)
      do_remote_file
    end
  end

  it "should not transfer the file if it has not been changed" do
    r = Net::HTTPNotModified.new("one", "two", "three")
    e = Net::HTTPRetriableError.new("304", r)
    @rest.stub!(:get_rest).and_raise(e)
    do_remote_file.should eql(false)
  end
  
  it "should raise an exception if it's any other kind of retriable response than 304" do
    r = Net::HTTPMovedPermanently.new("one", "two", "three")
    e = Net::HTTPRetriableError.new("301", r)
    @rest.stub!(:get_rest).and_raise(e)
    lambda { do_remote_file }.should raise_error(Net::HTTPRetriableError)
  end
  
  it "should raise an exception if anything else happens" do
    r = Net::HTTPBadRequest.new("one", "two", "three")
    e = Net::HTTPServerException.new("fake exception", r)
    @rest.stub!(:get_rest).and_raise(e)
    lambda { do_remote_file }.should raise_error(Net::HTTPServerException)    
  end
  
  it "should checksum the raw file" do
    @provider.should_receive(:checksum).with(@tempfile.path).and_return("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
    do_remote_file
  end

  describe "when the target file does not exist" do
    before do
      ::File.stub!(:exists?).with(@resource.path).and_return(false)
      @provider.stub!(:get_from_server).and_return(@tempfile)
    end

    it "should copy the raw file to the new resource" do
      FileUtils.should_receive(:cp).with(@tempfile.path, @resource.path).and_return(true)    
      do_remote_file
    end

    it "should set the new resource to updated" do
      @resource.should_receive(:updated=).with(true)    
      do_remote_file
    end
  end
  
  describe "when the target file already exists" do
    before do
      ::File.stub!(:exists?).with(@resource.path).and_return(true)
      @provider.stub!(:get_from_server).and_return(@tempfile)
    end

    describe "and the checksum doesn't match" do
      before do
        @provider.
          current_resource.
          checksum("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924ab")
      end

      it "should backup the original file" do
        @provider.should_receive(:backup).with(@resource.path).and_return(true)
        do_remote_file
      end

      it "should copy the raw file to the new resource" do
        FileUtils.should_receive(:cp).with(@tempfile.path, @resource.path).and_return(true)    
        do_remote_file
      end

      it "should set the new resource to updated" do
        @resource.should_receive(:updated=).with(true)    
        do_remote_file
      end
    end

    it "shouldn't backup the original file when it's the same" do
      @provider.should_not_receive(:backup).with(@resource.path).and_return(true)
      do_remote_file
    end
  end
  
  it "should set the owner if provided" do
    @resource.owner("adam")
    @provider.should_receive(:set_owner).and_return(true)
    do_remote_file
  end
  
  it "should set the group if provided" do
    @resource.group("adam")
    @provider.should_receive(:set_group).and_return(true)
    do_remote_file
  end
  
  it "should set the mode if provided" do
    @resource.mode(0676)
    @provider.should_receive(:set_mode).and_return(true)
    do_remote_file
  end
  
  it "should close the file when done" do
    @tempfile.should_receive(:close)
    do_remote_file
  end
# TODO: Finish these tests

end