fa4cd9e817712cb3503e90fef059000455507adb
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / spec / unit / puppet / provider / archive / ruby_spec.rb
1 require 'spec_helper'
2
3 ruby_provider = Puppet::Type.type(:archive).provider(:ruby)
4
5 RSpec.describe ruby_provider do
6   it_behaves_like 'an archive provider', ruby_provider
7
8   describe 'ruby provider' do
9     let(:name) { '/tmp/example.zip' }
10     let(:resource_properties) do
11       {
12         name: name,
13         source: 's3://home.lan/example.zip'
14       }
15     end
16     let(:resource) { Puppet::Type::Archive.new(resource_properties) }
17     let(:provider) { ruby_provider.new(resource) }
18
19     let(:s3_download_options) do
20       ['s3', 'cp', 's3://home.lan/example.zip', String]
21     end
22
23     before do
24       allow(provider).to receive(:aws)
25     end
26
27     context 'default resource property' do
28       it '#s3_download' do
29         provider.s3_download(name)
30         expect(provider).to have_received(:aws).with(s3_download_options)
31       end
32
33       it '#extract nothing' do
34         expect(provider.extract).to be_nil
35       end
36     end
37
38     describe '#checksum' do
39       subject { provider.checksum }
40
41       let(:url) { nil }
42       let(:remote_hash) { nil }
43
44       before do
45         resource[:checksum_url] = url if url
46         allow(PuppetX::Bodeco::Util).to receive(:content) .\
47           with(url, any_args).and_return(remote_hash)
48       end
49
50       context 'unset' do
51         it { is_expected.to be_nil }
52       end
53
54       context 'with a url' do
55         let(:url) { 'http://example.com/checksum' }
56
57         context 'responds with hash' do
58           let(:remote_hash) { 'a0c38e1aeb175201b0dacd65e2f37e187657050a' }
59
60           it { is_expected.to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a') }
61         end
62         context 'responds with hash and newline' do
63           let(:remote_hash) { "a0c38e1aeb175201b0dacd65e2f37e187657050a\n" }
64
65           it { is_expected.to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a') }
66         end
67         context 'responds with `sha1sum README.md` output' do
68           let(:remote_hash) { "a0c38e1aeb175201b0dacd65e2f37e187657050a  README.md\n" }
69
70           it { is_expected.to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a') }
71         end
72         context 'responds with `openssl dgst -hex -sha256 README.md` output' do
73           let(:remote_hash) { "SHA256(README.md)= 8fa3f0ff1f2557657e460f0f78232679380a9bcdb8670e3dcb33472123b22428\n" }
74
75           it { is_expected.to eq('8fa3f0ff1f2557657e460f0f78232679380a9bcdb8670e3dcb33472123b22428') }
76         end
77       end
78     end
79
80     describe 'download options' do
81       let(:resource_properties) do
82         {
83           name: name,
84           source: 's3://home.lan/example.zip',
85           download_options: ['--region', 'eu-central-1']
86         }
87       end
88
89       context 'default resource property' do
90         it '#s3_download' do
91           provider.s3_download(name)
92           expect(provider).to have_received(:aws).with(s3_download_options << '--region' << 'eu-central-1')
93         end
94       end
95     end
96   end
97 end