summaryrefslogtreecommitdiff
path: root/spec/unit/provider/remote_file/http_spec.rb
blob: 9a5a25253d8bcd5fa3a1fd843993d95da27e4a7c (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#
# Author:: Lamont Granquist (<lamont@chef.io>)
# Copyright:: Copyright 2013-2016, Lamont Granquist
# 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 "spec_helper"

describe Chef::Provider::RemoteFile::HTTP do

  let(:uri) { URI.parse("http://opscode.com/seattle.txt") }

  let(:existing_file_source) { nil }

  let(:current_resource_checksum) { "41e78735319af11327e9d2ca8535ea1c191e5ac1f76bb08d88fe6c3f93a8c8e5" }

  let(:current_resource) do
    current_resource = Chef::Resource::RemoteFile.new("/tmp/foo.txt")
    current_resource.source(existing_file_source) if existing_file_source
    current_resource.checksum(current_resource_checksum)
    current_resource
  end

  let(:new_resource) do
    Chef::Resource::RemoteFile.new("/tmp/foo.txt")
  end

  subject(:fetcher) do
    Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
  end

  let(:cache_control_data) { Chef::Provider::RemoteFile::CacheControlData.new(uri) }

  describe "generating cache control headers" do

    context "and there is no valid cache control data for this URI on disk" do

      before do
        expect(Chef::Provider::RemoteFile::CacheControlData).to receive(:load_and_validate).with(uri, current_resource_checksum).and_return(cache_control_data)
      end

      it "does not add conditional GET headers" do
        expect(fetcher.conditional_get_headers).to eq({})
      end

      context "and the resource specifies custom headers" do
        before do
          new_resource.headers("x-myapp-header" => "custom-header-value")
        end

        it "has the user-specified custom headers" do
          expect(fetcher.headers).to eq({ "x-myapp-header" => "custom-header-value" })
        end
      end

    end

    context "and the cache control data matches the existing file" do

      # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
      let(:etag) { "\"a-strong-unique-identifier\"" }

      # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
      let(:mtime) { "Tue, 21 May 2013 19:19:23 GMT" }

      before do
        cache_control_data.etag = etag
        cache_control_data.mtime = mtime

        expect(Chef::Provider::RemoteFile::CacheControlData).to receive(:load_and_validate).with(uri, current_resource_checksum).and_return(cache_control_data)
      end

      context "and no conditional get features are enabled" do
        before do
          new_resource.use_conditional_get(false)
        end

        it "does not add headers to the request" do
          expect(fetcher.headers).to eq({})
        end
      end

      context "and conditional get is enabled" do
        before do
          new_resource.use_conditional_get(true)
        end

        it "adds If-None-Match and If-Modified-Since headers to the request" do
          headers = fetcher.headers
          expect(headers["if-none-match"]).to eq(etag)
          expect(headers["if-modified-since"]).to eq(mtime)
        end

        context "and custom headers are provided" do
          before do
            new_resource.headers("x-myapp-header" => "app-specific-header",
                                 "if-none-match" => "custom-etag",
                                 "if-modified-since" => "custom-last-modified")
          end

          it "preserves non-conflicting headers" do
            expect(fetcher.headers["x-myapp-header"]).to eq("app-specific-header")
          end

          it "prefers user-supplied cache control headers" do
            headers = fetcher.headers
            expect(headers["if-none-match"]).to eq("custom-etag")
            expect(headers["if-modified-since"]).to eq("custom-last-modified")
          end
        end

      end

      context "and etag support is enabled" do
        before do
          new_resource.use_conditional_get(false)
          new_resource.use_etags(true)
        end

        it "only adds If-None-Match headers to the request" do
          headers = fetcher.headers
          expect(headers["if-none-match"]).to eq(etag)
          expect(headers).not_to have_key("if-modified-since")
        end
      end

      context "and mtime support is enabled" do
        before do
          new_resource.use_conditional_get(false)
          new_resource.use_last_modified(true)
        end

        it "only adds If-Modified-Since headers to the request" do
          headers = fetcher.headers
          expect(headers["if-modified-since"]).to eq(mtime)
          expect(headers).not_to have_key("if-none-match")
        end
      end
    end

  end

  describe "when fetching the uri" do

    let(:expected_http_opts) { {} }
    let(:expected_http_args) { [uri, expected_http_opts] }

    let(:tempfile_path) { tempfile.path }

    let(:tempfile) { Tempfile.open("muhtempfile") }

    let(:last_response) { {} }

    let(:event_dispatcher) do
      event_dispatcher = double(Chef::EventDispatch::Dispatcher)
      allow(event_dispatcher).to receive(:formatter?).and_return(false)
      event_dispatcher
    end

    let(:rest) do
      rest = double(Chef::HTTP::Simple)
      allow_any_instance_of(Chef::FileContentManagement::Tempfile).to receive(:tempfile).and_return(tempfile)
      allow(rest).to receive(:last_response).and_return(last_response)
      rest
    end

    before do
      new_resource.headers({})
      new_resource.use_last_modified(false)
      allow(new_resource).to receive(:events).and_return(event_dispatcher)
      expect(Chef::Provider::RemoteFile::CacheControlData).to receive(:load_and_validate).with(uri, current_resource_checksum).and_return(cache_control_data)

      expect(Chef::HTTP::Simple).to receive(:new).with(*expected_http_args).and_return(rest)
    end

    it "should clean up the tempfile, and return a nil when streaming_request returns nil" do
      # Streaming request returns nil for a 304 not modified (etags / last-modified)
      expect(rest).to receive(:streaming_request).with(uri, {}, tempfile).and_return(nil)
      expect(tempfile).to receive(:close)
      expect(tempfile).to receive(:unlink)
      expect(fetcher.fetch).to be_nil
    end

    context "with progress reports" do
      let(:fetched_content_checksum) { "e2a8938cc31754f6c067b35aab1d0d4864272e9bf8504536ef3e79ebf8432305" }

      before do
        expect(cache_control_data).to receive(:save)
        expect(Chef::Digester).to receive(:checksum_for_file).with(tempfile_path).and_return(fetched_content_checksum)
        Chef::Config[:show_download_progress] = true
      end

      it "should yield its progress" do
        expect(rest).to receive(:streaming_request_with_progress).with(uri, {}, tempfile).and_yield(50, 100).and_yield(70, 100).and_return(tempfile)
        expect(event_dispatcher).to receive(:formatter?).and_return(true)
        expect(event_dispatcher).to receive(:resource_update_progress).with(new_resource, 50, 100, 10).ordered
        expect(event_dispatcher).to receive(:resource_update_progress).with(new_resource, 70, 100, 10).ordered
        fetcher.fetch
      end
    end

    describe "and the request returns new content" do
      let(:fetched_content_checksum) { "e2a8938cc31754f6c067b35aab1d0d4864272e9bf8504536ef3e79ebf8432305" }

      before do
        expect(rest).to receive(:streaming_request).with(uri, {}, tempfile).and_return(tempfile)
        expect(cache_control_data).to receive(:save)
        expect(Chef::Digester).to receive(:checksum_for_file).with(tempfile_path).and_return(fetched_content_checksum)
      end

      it "should return a tempfile" do
        result = fetcher.fetch
        expect(result).to eq(tempfile)
        expect(cache_control_data.etag).to be_nil
        expect(cache_control_data.mtime).to be_nil
        expect(cache_control_data.checksum).to eq(fetched_content_checksum)
      end

      context "and the response does not contain an etag" do
        let(:last_response) { { "etag" => nil } }
        it "does not include an etag in the result" do
          fetcher.fetch
          expect(cache_control_data.etag).to be_nil
          expect(cache_control_data.mtime).to be_nil
          expect(cache_control_data.checksum).to eq(fetched_content_checksum)
        end
      end

      context "and the response has an etag header" do
        let(:last_response) { { "etag" => "abc123" } }

        it "includes the etag value in the response" do
          fetcher.fetch
          expect(cache_control_data.etag).to eq("abc123")
          expect(cache_control_data.mtime).to be_nil
          expect(cache_control_data.checksum).to eq(fetched_content_checksum)
        end

      end

      context "and the response has no Date or Last-Modified header" do
        let(:last_response) { { "date" => nil, "last-modified" => nil } }
        it "does not set an mtime in the result" do
          # RFC 2616 suggests that servers that do not set a Date header do not
          # have a reliable clock, so no use in making them deal with dates.
          fetcher.fetch
          expect(cache_control_data.etag).to be_nil
          expect(cache_control_data.mtime).to be_nil
          expect(cache_control_data.checksum).to eq(fetched_content_checksum)
        end
      end

      context "and the response has a Last-Modified header" do
        let(:last_response) do
          # Last-Modified should be preferred to Date if both are set
          { "date" => "Fri, 17 May 2013 23:23:23 GMT", "last-modified" => "Fri, 17 May 2013 11:11:11 GMT" }
        end

        it "sets the mtime to the Last-Modified time in the response" do
          fetcher.fetch
          expect(cache_control_data.etag).to be_nil
          expect(cache_control_data.mtime).to eq(last_response["last-modified"])
        end
      end

      context "and the response has a Date header but no Last-Modified header" do
        let(:last_response) do
          { "date" => "Fri, 17 May 2013 23:23:23 GMT", "last-modified" => nil }
        end

        it "sets the mtime to the Date in the response" do
          fetcher.fetch
          expect(cache_control_data.etag).to be_nil
          expect(cache_control_data.mtime).to eq(last_response["date"])
          expect(cache_control_data.checksum).to eq(fetched_content_checksum)
        end

      end

      context "and the target file is a tarball [CHEF-3140]" do

        let(:uri) { URI.parse("http://opscode.com/tarball.tgz") }
        let(:expected_http_opts) { { disable_gzip: true } }

        # CHEF-3140
        # Some servers return tarballs as content type tar and encoding gzip, which
        # is totally wrong. When this happens and gzip isn't disabled, Chef::HTTP::Simple
        # will decompress the file for you, which is not at all what you expected
        # to happen (you end up with an uncomressed tar archive instead of the
        # gzipped tar archive you expected). To work around this behavior, we
        # detect when users are fetching gzipped files and turn off gzip in
        # Chef::HTTP::Simple.

        it "should disable gzip compression in the client" do
          # Before block in the parent context has set an expectation on
          # Chef::HTTP::Simple.new() being called with expected arguments. Here we fulfill
          # that expectation, so that we can explicitly set it for this test.
          # This is intended to provide insurance that refactoring of the parent
          # context does not negate the value of this particular example.
          Chef::HTTP::Simple.new(*expected_http_args)
          expect(Chef::HTTP::Simple).to receive(:new).once.with(*expected_http_args).and_return(rest)
          fetcher.fetch
          expect(cache_control_data.etag).to be_nil
          expect(cache_control_data.mtime).to be_nil
          expect(cache_control_data.checksum).to eq(fetched_content_checksum)
        end
      end
    end

  end

  describe "#http_client_opts" do
    before do
      new_resource.http_options({ retries: 2, retry_delay: 3 })
    end

    it "should set http client options" do
      expect(fetcher.send(:http_client_opts)).to eq({ retries: 2, retry_delay: 3 })
    end
  end

end