5d50d6e8d61e3022f3e90603158b262d8eaf883c
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / spec / unit / puppet / provider / archive / wget_spec.rb
1 require 'spec_helper'
2
3 wget_provider = Puppet::Type.type(:archive).provider(:wget)
4
5 RSpec.describe wget_provider do
6   it_behaves_like 'an archive provider', wget_provider
7
8   describe '#download' do
9     let(:name)      { '/tmp/example.zip' }
10     let(:resource)  { Puppet::Type::Archive.new(resource_properties) }
11     let(:provider)  { wget_provider.new(resource) }
12     let(:execution) { Puppet::Util::Execution }
13
14     let(:default_options) do
15       [
16         'wget',
17         'http://home.lan/example.zip',
18         '-O',
19         '/tmp/example.zip',
20         '--max-redirect=5'
21       ]
22     end
23
24     before do
25       allow(FileUtils).to receive(:mv)
26       allow(execution).to receive(:execute)
27     end
28
29     context 'no extra properties specified' do
30       let(:resource_properties) do
31         {
32           name: name,
33           source: 'http://home.lan/example.zip'
34         }
35       end
36
37       it 'calls wget with input, output and --max-redirects=5' do
38         provider.download(name)
39         expect(execution).to have_received(:execute).with(default_options.join(' '))
40       end
41     end
42
43     context 'username specified' do
44       let(:resource_properties) do
45         {
46           name: name,
47           source: 'http://home.lan/example.zip',
48           username: 'foo'
49         }
50       end
51
52       it 'calls wget with default options and username' do
53         provider.download(name)
54         expect(execution).to have_received(:execute).with([default_options, '--user=foo'].join(' '))
55       end
56     end
57
58     context 'password specified' do
59       let(:resource_properties) do
60         {
61           name: name,
62           source: 'http://home.lan/example.zip',
63           password: 'foo'
64         }
65       end
66
67       it 'calls wget with default options and password' do
68         provider.download(name)
69         expect(execution).to have_received(:execute).with([default_options, '--password=foo'].join(' '))
70       end
71     end
72
73     context 'cookie specified' do
74       let(:resource_properties) do
75         {
76           name: name,
77           source: 'http://home.lan/example.zip',
78           cookie: 'foo'
79         }
80       end
81
82       it 'calls wget with default options and header containing cookie' do
83         provider.download(name)
84         expect(execution).to have_received(:execute).with([default_options, '--header="Cookie: foo"'].join(' '))
85       end
86     end
87
88     context 'proxy specified' do
89       let(:resource_properties) do
90         {
91           name: name,
92           source: 'http://home.lan/example.zip',
93           proxy_server: 'https://home.lan:8080'
94         }
95       end
96
97       it 'calls wget with default options and header containing cookie' do
98         provider.download(name)
99         expect(execution).to have_received(:execute).with([default_options, '-e use_proxy=yes', '-e https_proxy=https://home.lan:8080'].join(' '))
100       end
101     end
102
103     context 'allow_insecure true' do
104       let(:resource_properties) do
105         {
106           name: name,
107           source: 'http://home.lan/example.zip',
108           allow_insecure: true
109         }
110       end
111
112       it 'calls wget with default options and --no-check-certificate' do
113         provider.download(name)
114         expect(execution).to have_received(:execute).with([default_options, '--no-check-certificate'].join(' '))
115       end
116     end
117
118     describe '#checksum' do
119       subject { provider.checksum }
120
121       let(:url) { nil }
122       let(:resource_properties) do
123         {
124           name: name,
125           source: 'http://home.lan/example.zip'
126         }
127       end
128
129       before do
130         resource[:checksum_url] = url if url
131       end
132
133       context 'with a url' do
134         let(:wget_params) do
135           [
136             'wget',
137             '-qO-',
138             'http://example.com/checksum',
139             '--max-redirect=5'
140           ]
141         end
142
143         let(:url) { 'http://example.com/checksum' }
144
145         context 'responds with hash' do
146           let(:remote_hash) { 'a0c38e1aeb175201b0dacd65e2f37e187657050a' }
147
148           it 'parses checksum value' do
149             allow(Puppet::Util::Execution).to receive(:execute).with(wget_params.join(' ')).and_return("a0c38e1aeb175201b0dacd65e2f37e187657050a README.md\n")
150             expect(provider.checksum).to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a')
151           end
152         end
153       end
154     end
155   end
156 end