Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / loadyaml_spec.rb
1 require 'spec_helper'
2
3 describe 'loadyaml' do
4   it { is_expected.not_to eq(nil) }
5   it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
6
7   context 'when a non-existing file is specified' do
8     let(:filename) { '/tmp/doesnotexist' }
9
10     it "'default' => 'value'" do
11       expect(File).to receive(:exists?).with(filename).and_return(false).once
12       expect(YAML).to receive(:load_file).never
13       is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
14     end
15   end
16
17   context 'when an existing file is specified' do
18     let(:filename) { '/tmp/doesexist' }
19     let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
20
21     it "returns 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'" do
22       expect(File).to receive(:exists?).with(filename).and_return(true).once
23       expect(YAML).to receive(:load_file).with(filename).and_return(data).once
24       is_expected.to run.with_params(filename).and_return(data)
25     end
26   end
27
28   context 'when the file could not be parsed' do
29     let(:filename) { '/tmp/doesexist' }
30
31     it 'filename /tmp/doesexist' do
32       expect(File).to receive(:exists?).with(filename).and_return(true).once
33       allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!')
34       is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
35     end
36   end
37
38   context 'when an existing URL is specified' do
39     let(:filename) { 'https://example.local/myhash.yaml' }
40     let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
41     let(:yaml) { 'Dummy YAML' }
42     let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
43
44     it {
45       expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml)
46       expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once
47       is_expected.to run.with_params(filename).and_return(data)
48     }
49   end
50
51   context 'when an existing URL (with username and password) is specified' do
52     let(:filename) { 'https://user1:pass1@example.local/myhash.yaml' }
53     let(:url_no_auth) { 'https://example.local/myhash.yaml' }
54     let(:basic_auth) { { :http_basic_authentication => ['user1', 'pass1'] } }
55     let(:yaml) { 'Dummy YAML' }
56     let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
57
58     it {
59       expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml)
60       expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once
61       is_expected.to run.with_params(filename).and_return(data)
62     }
63   end
64
65   context 'when an existing URL (with username) is specified' do
66     let(:filename) { 'https://user1@example.local/myhash.yaml' }
67     let(:url_no_auth) { 'https://example.local/myhash.yaml' }
68     let(:basic_auth) { { :http_basic_authentication => ['user1', ''] } }
69     let(:yaml) { 'Dummy YAML' }
70     let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
71
72     it {
73       expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml)
74       expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once
75       is_expected.to run.with_params(filename).and_return(data)
76     }
77   end
78
79   context 'when an existing URL could not be parsed, with default specified' do
80     let(:filename) { 'https://example.local/myhash.yaml' }
81     let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
82     let(:yaml) { 'Dummy YAML' }
83
84     it {
85       expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml)
86       expect(YAML).to receive(:safe_load).with(yaml).and_raise StandardError, 'Cannot parse data'
87       is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
88     }
89   end
90
91   context 'when a URL does not exist, with default specified' do
92     let(:filename) { 'https://example.local/myhash.yaml' }
93     let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
94     let(:yaml) { 'Dummy YAML' }
95
96     it {
97       expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
98       is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
99     }
100   end
101 end