File: connection_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (636 lines) | stat: -rw-r--r-- 20,672 bytes parent folder | download | duplicates (3)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
require 'spec_helper'
require 'puppet/network/http/connection'
require 'puppet/test_ca'

describe Puppet::Network::HTTP::Connection do
  let(:host) { "me.example.com" }
  let(:port) { 8140 }
  let(:path) { '/foo' }
  let(:url) { "https://#{host}:#{port}#{path}" }
  let(:params) { { 'key' => 'a value' } }
  let(:encoded_url_with_params) { "#{url}?%7B%22key%22:%22a%20value%22%7D" }
  let(:ssl_context) { Puppet::SSL::SSLProvider.new.create_system_context(cacerts: []) }
  let(:verifier) { Puppet::SSL::Verifier.new(host, ssl_context) }

  shared_examples_for "an HTTP connection" do |klass|
    subject { klass.new(host, port, :verifier => verifier) }

    context "when providing HTTP connections" do
      context "when initializing http instances" do
        it "should return an http instance created with the passed host and port" do
          conn = klass.new(host, port, :verifier => verifier)

          expect(conn.address).to eq(host)
          expect(conn.port).to eq(port)
        end

        it "should enable ssl on the http instance by default" do
          conn = klass.new(host, port, :verifier => verifier)

          expect(conn).to be_use_ssl
        end

        it "can disable ssl using an option and ignore the verify" do
          conn = klass.new(host, port, :use_ssl => false)

          expect(conn).to_not be_use_ssl
        end

        it "can enable ssl using an option" do
          conn = klass.new(host, port, :use_ssl => true, :verifier => verifier)

          expect(conn).to be_use_ssl
        end

        it "ignores the ':verify' option when ssl is disabled" do
          conn = klass.new(host, port, :use_ssl => false, :verifier => verifier)

          expect(conn.verifier).to be_nil
        end

        it "wraps the validator in an adapter" do
          conn = klass.new(host, port, :verifier => verifier)

          expect(conn.verifier).to be_a(Puppet::SSL::Verifier)
        end

        it "should raise Puppet::Error when invalid options are specified" do
          expect { klass.new(host, port, :invalid_option => nil) }.to raise_error(Puppet::Error, 'Unrecognized option(s): :invalid_option')
        end

        it "accepts a verifier" do
          verifier = Puppet::SSL::Verifier.new(host, double('ssl_context'))
          conn = klass.new(host, port, :use_ssl => true, :verifier => verifier)

          expect(conn.verifier).to eq(verifier)
        end

        it "raises if the wrong verifier class is specified" do
          expect {
            klass.new(host, port, :verifier => Object.new)
          }.to raise_error(ArgumentError,
                           "Expected an instance of Puppet::SSL::Verifier but was passed a Object")
        end
      end
    end

    context "for streaming GET requests" do
      it 'yields the response' do
        stub_request(:get, url)

        expect { |b|
          subject.request_get('/foo', {}, &b)
        }.to yield_with_args(Net::HTTPResponse)
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:get, encoded_url_with_params)

        subject.request_get("#{path}?#{params.to_json}") { |_| }
      end

      it "merges custom headers with default ones" do
        stub_request(:get, url).with(headers: { 'X-Foo' => 'Bar', 'User-Agent' => /./ })

        subject.request_get(path, {'X-Foo' => 'Bar'}) { |_| }
      end

      it "returns the response" do
        stub_request(:get, url)

        response = subject.request_get(path) { |_| }
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:get, url_with_query)

        response = subject.request_get(url_with_query) { |_| }
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for streaming head requests" do
      it 'yields the response when request_head is called' do
        stub_request(:head, url)

        expect { |b|
          subject.request_head('/foo', {}, &b)
        }.to yield_with_args(Net::HTTPResponse)
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:head, encoded_url_with_params)

        subject.request_head("#{path}?#{params.to_json}") { |_| }
      end

      it "merges custom headers with default ones" do
        stub_request(:head, url).with(headers: { 'X-Foo' => 'Bar', 'User-Agent' => /./ })

        subject.request_head(path, {'X-Foo' => 'Bar'}) { |_| }
      end

      it "returns the response" do
        stub_request(:head, url)

        response = subject.request_head(path) { |_| }
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:head, url_with_query)

        response = subject.request_head(url_with_query) { |_| }
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for streaming post requests" do
      it 'yields the response when request_post is called' do
        stub_request(:post, url)

        expect { |b|
          subject.request_post('/foo', "param: value", &b)
        }.to yield_with_args(Net::HTTPResponse)
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:post, encoded_url_with_params)

        subject.request_post("#{path}?#{params.to_json}", "") { |_| }
      end

      it "merges custom headers with default ones" do
        stub_request(:post, url).with(headers: { 'X-Foo' => 'Bar', 'User-Agent' => /./ })

        subject.request_post(path, "", {'X-Foo' => 'Bar'}) { |_| }
      end

      it "returns the response" do
        stub_request(:post, url)

        response = subject.request_post(path, "") { |_| }
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:post, url_with_query)

        response = subject.request_post(url_with_query, "") { |_| }
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for GET requests" do
      it "includes default HTTP headers" do
        stub_request(:get, url).with(headers: {'User-Agent' => /./})

        subject.get(path)
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:get, encoded_url_with_params)

        subject.get("#{path}?#{params.to_json}")
      end

      it "merges custom headers with default ones" do
        stub_request(:get, url).with(headers: { 'X-Foo' => 'Bar', 'User-Agent' => /./ })

        subject.get(path, {'X-Foo' => 'Bar'})
      end

      it "returns the response" do
        stub_request(:get, url)

        response = subject.get(path)
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "returns the entire response body" do
        stub_request(:get, url).to_return(body: "abc")

        response = subject.get(path)
        expect(response.body).to eq("abc")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:get, url_with_query)

        response = subject.get(url_with_query)
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for HEAD requests" do
      it "includes default HTTP headers" do
        stub_request(:head, url).with(headers: {'User-Agent' => /./})

        subject.head(path)
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:head, encoded_url_with_params)

        subject.head("#{path}?#{params.to_json}")
      end

      it "merges custom headers with default ones" do
        stub_request(:head, url).with(headers: { 'X-Foo' => 'Bar', 'User-Agent' => /./ })

        subject.head(path, {'X-Foo' => 'Bar'})
      end

      it "returns the response" do
        stub_request(:head, url)

        response = subject.head(path)
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:head, url_with_query)

        response = subject.head(url_with_query)
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for PUT requests" do
      it "includes default HTTP headers" do
        stub_request(:put, url).with(headers: {'User-Agent' => /./})

        subject.put(path, "", {'Content-Type' => 'text/plain'})
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:put, encoded_url_with_params)

        subject.put("#{path}?#{params.to_json}", "")
      end

      it "includes custom headers" do
        stub_request(:put, url).with(headers: { 'X-Foo' => 'Bar' })

        subject.put(path, "", {'X-Foo' => 'Bar', 'Content-Type' => 'text/plain'})
      end

      it "returns the response" do
        stub_request(:put, url)

        response = subject.put(path, "", {'Content-Type' => 'text/plain'})
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "sets content-type for the body" do
        stub_request(:put, url).with(headers: {"Content-Type" => "text/plain"})

        subject.put(path, "hello", {'Content-Type' => 'text/plain'})
      end

      it 'sends an empty body' do
        stub_request(:put, url).with(body: '')

        subject.put(path, nil)
      end

      it 'defaults content-type to application/x-www-form-urlencoded' do
        stub_request(:put, url).with(headers: {'Content-Type' => 'application/x-www-form-urlencoded'})

        subject.put(path, '')
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:put, url_with_query)

        response = subject.put(url_with_query, '')
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for POST requests" do
      it "includes default HTTP headers" do
        stub_request(:post, url).with(headers: {'User-Agent' => /./})

        subject.post(path, "", {'Content-Type' => 'text/plain'})
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:post, encoded_url_with_params)

        subject.post("#{path}?#{params.to_json}", "", {'Content-Type' => 'text/plain'})
      end

      it "includes custom headers" do
        stub_request(:post, url).with(headers: { 'X-Foo' => 'Bar' })

        subject.post(path, "", {'X-Foo' => 'Bar', 'Content-Type' => 'text/plain'})
      end

      it "returns the response" do
        stub_request(:post, url)

        response = subject.post(path, "", {'Content-Type' => 'text/plain'})
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "sets content-type for the body" do
        stub_request(:post, url).with(headers: {"Content-Type" => "text/plain"})

        subject.post(path, "hello", {'Content-Type' => 'text/plain'})
      end

      it 'sends an empty body' do
        stub_request(:post, url).with(body: '')

        subject.post(path, nil)
      end

      it 'defaults content-type to application/x-www-form-urlencoded' do
        stub_request(:post, url).with(headers: {'Content-Type' => 'application/x-www-form-urlencoded'})

        subject.post(path, "")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:post, url_with_query)

        response = subject.post(url_with_query, '')
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "for DELETE requests" do
      it "includes default HTTP headers" do
        stub_request(:delete, url).with(headers: {'User-Agent' => /./})

        subject.delete(path)
      end

      it "merges custom headers with default ones" do
        stub_request(:delete, url).with(headers: { 'X-Foo' => 'Bar', 'User-Agent' => /./ })

        subject.delete(path, {'X-Foo' => 'Bar'})
      end

      it "stringifies keys and encodes values in the query" do
        stub_request(:delete, encoded_url_with_params)

        subject.delete("#{path}?#{params.to_json}")
      end

      it "returns the response" do
        stub_request(:delete, url)

        response = subject.delete(path)
        expect(response).to be_an_instance_of(Net::HTTPOK)
        expect(response.code).to eq("200")
      end

      it "returns the entire response body" do
        stub_request(:delete, url).to_return(body: "abc")

        expect(subject.delete(path).body).to eq("abc")
      end

      it "accepts a URL string as the path" do
        url_with_query = "#{url}?foo=bar"
        stub_request(:delete, url_with_query)

        response = subject.delete(url_with_query)
        expect(response).to be_an_instance_of(Net::HTTPOK)
      end
    end

    context "when response is a redirect" do
      subject { klass }

      def create_connection(options = {})
        options[:use_ssl] = false
        options[:verifier] = verifier
        subject.new(host, port, options)
      end

      def redirect_to(url)
        { status: 302, headers: { 'Location' => url } }
      end

      it "should follow the redirect to the final resource location" do
        stub_request(:get, "http://me.example.com:8140/foo").to_return(redirect_to("http://me.example.com:8140/bar"))
        stub_request(:get, "http://me.example.com:8140/bar").to_return(status: 200)

        create_connection.get('/foo')
      end

      def expects_limit_exceeded(conn)
        expect {
          conn.get('/')
        }.to raise_error(Puppet::Network::HTTP::RedirectionLimitExceededException)
      end

      it "should not follow any redirects when the limit is 0" do
        stub_request(:get, "http://me.example.com:8140/").to_return(redirect_to("http://me.example.com:8140/foo"))

        conn = create_connection(:redirect_limit => 0)
        expects_limit_exceeded(conn)
      end

      it "should follow the redirect once" do
        stub_request(:get, "http://me.example.com:8140/").to_return(redirect_to("http://me.example.com:8140/foo"))
        stub_request(:get, "http://me.example.com:8140/foo").to_return(redirect_to("http://me.example.com:8140/bar"))

        conn = create_connection(:redirect_limit => 1)
        expects_limit_exceeded(conn)
      end

      it "should raise an exception when the redirect limit is exceeded" do
        stub_request(:get, "http://me.example.com:8140/").to_return(redirect_to("http://me.example.com:8140/foo"))
        stub_request(:get, "http://me.example.com:8140/foo").to_return(redirect_to("http://me.example.com:8140/bar"))
        stub_request(:get, "http://me.example.com:8140/bar").to_return(redirect_to("http://me.example.com:8140/baz"))
        stub_request(:get, "http://me.example.com:8140/baz").to_return(redirect_to("http://me.example.com:8140/qux"))

        conn = create_connection(:redirect_limit => 3)
        expects_limit_exceeded(conn)
      end

      it 'raises an exception when the location header is missing' do
        stub_request(:get, "http://me.example.com:8140/").to_return(status: 302)

        expect {
            create_connection.get('/')
          }.to raise_error(Puppet::HTTP::ProtocolError, /Location response header is missing/)
      end
    end

    context "when response indicates an overloaded server" do
      def retry_after(datetime)
        stub_request(:get, url)
          .to_return(status: [503, 'Service Unavailable'], headers: {'Retry-After' => datetime}).then
          .to_return(status: 200)
      end

      it "should return a 503 response if Retry-After is not set" do
        stub_request(:get, url).to_return(status: [503, 'Service Unavailable'])

        result = subject.get('/foo')
        expect(result.code).to eq("503")
      end

      it "should return a 503 response if Retry-After is not convertible to an Integer or RFC 2822 Date" do
        retry_after('foo')

        expect {
          subject.get('/foo')
        }.to raise_error(Puppet::HTTP::ProtocolError, /Failed to parse Retry-After header 'foo'/)
      end

      it "should close the connection before sleeping" do
        retry_after('42')

        http1 = Net::HTTP.new(host, port)
        http1.use_ssl = true
        allow(http1).to receive(:started?).and_return(true)

        http2 = Net::HTTP.new(host, port)
        http2.use_ssl = true
        allow(http1).to receive(:started?).and_return(true)

        # The "with_connection" method is required to yield started connections
        pool = Puppet.runtime[:http].pool

        allow(pool).to receive(:with_connection).and_yield(http1).and_yield(http2)

        expect(http1).to receive(:finish).ordered
        expect(::Kernel).to receive(:sleep).with(42).ordered

        subject.get('/foo')
      end

      it "should sleep and retry if Retry-After is an Integer" do
        retry_after('42')

        expect(::Kernel).to receive(:sleep).with(42)

        result = subject.get('/foo')
        expect(result.code).to eq("200")
      end

      it "should sleep and retry if Retry-After is an RFC 2822 Date" do
        retry_after('Wed, 13 Apr 2005 15:18:05 GMT')

        now = DateTime.new(2005, 4, 13, 8, 17, 5, '-07:00')
        allow(DateTime).to receive(:now).and_return(now)

        expect(::Kernel).to receive(:sleep).with(60)

        result = subject.get('/foo')
        expect(result.code).to eq("200")
      end

      it "should sleep for no more than the Puppet runinterval" do
        retry_after('60')

        Puppet[:runinterval] = 30

        expect(::Kernel).to receive(:sleep).with(30)

        subject.get('/foo')
      end

      it "should sleep for 0 seconds if the RFC 2822 date has past" do
        retry_after('Wed, 13 Apr 2005 15:18:05 GMT')

        expect(::Kernel).to receive(:sleep).with(0)

        subject.get('/foo')
      end
    end

    context "basic auth" do
      let(:auth) { { :user => 'user', :password => 'password' } }
      let(:creds) { [ 'user', 'password'] }

      it "is allowed in get requests" do
        stub_request(:get, url).with(basic_auth: creds)

        subject.get('/foo', nil, :basic_auth => auth)
      end

      it "is allowed in post requests" do
        stub_request(:post, url).with(basic_auth: creds)

        subject.post('/foo', 'data', nil, :basic_auth => auth)
      end

      it "is allowed in head requests" do
        stub_request(:head, url).with(basic_auth: creds)

        subject.head('/foo', nil, :basic_auth => auth)
      end

      it "is allowed in delete requests" do
        stub_request(:delete, url).with(basic_auth: creds)

        subject.delete('/foo', nil, :basic_auth => auth)
      end

      it "is allowed in put requests" do
        stub_request(:put, url).with(basic_auth: creds)

        subject.put('/foo', 'data', nil, :basic_auth => auth)
      end
    end

    it "sets HTTP User-Agent header" do
      puppet_ua = "Puppet/#{Puppet.version} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_PLATFORM})"
      stub_request(:get, url).with(headers: { 'User-Agent' => puppet_ua })

      subject.get('/foo')
    end

    describe 'connection request errors' do
      it "logs and raises generic http errors" do
        generic_error = Net::HTTPError.new('generic error', double("response"))
        stub_request(:get, url).to_raise(generic_error)

        expect(Puppet).to receive(:log_exception).with(anything, /^.*failed.*: generic error$/)
        expect { subject.get('/foo') }.to raise_error(generic_error)
      end

      it "logs and raises timeout errors" do
        timeout_error = Net::OpenTimeout.new
        stub_request(:get, url).to_raise(timeout_error)

        expect(Puppet).to receive(:log_exception).with(anything, /^.*timed out .*after .* seconds/)
        expect { subject.get('/foo') }.to raise_error(timeout_error)
      end

      it "logs and raises eof errors" do
        eof_error = EOFError
        stub_request(:get, url).to_raise(eof_error)

        expect(Puppet).to receive(:log_exception).with(anything, /^.*interrupted after .* seconds$/)
        expect { subject.get('/foo') }.to raise_error(eof_error)
      end
    end
  end

  describe Puppet::Network::HTTP::Connection do
    it_behaves_like "an HTTP connection", described_class
  end
end