|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'spec_helper' |
| 4 | +require 'open-uri' |
| 5 | +require 'stringio' |
4 | 6 |
|
5 | | -describe 'loadjson' do |
| 7 | +describe 'stdlib::loadjson' do |
6 | 8 | it { is_expected.not_to be_nil } |
7 | | - it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } |
| 9 | + it { is_expected.to run.with_params.and_raise_error(ArgumentError, "'stdlib::loadjson' expects between 1 and 2 arguments, got none") } |
8 | 10 |
|
9 | 11 | describe 'when calling with valid arguments' do |
10 | | - before :each do |
11 | | - # In Puppet 7, there are two prior calls to File.read prior to the responses we want to mock |
12 | | - allow(File).to receive(:read).with(anything, anything).and_call_original |
13 | | - allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}, encoding: 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') |
14 | | - allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') |
15 | | - # Additional modules used by litmus which are identified while running these dues to being in fixtures |
16 | | - allow(File).to receive(:read).with(%r{/(provision|puppet_agent|facts)/metadata.json}, encoding: 'utf-8') |
17 | | - end |
18 | | - |
19 | 12 | context 'when a non-existing file is specified' do |
20 | 13 | let(:filename) do |
21 | | - if Puppet::Util::Platform.windows? |
22 | | - 'C:/tmp/doesnotexist' |
23 | | - else |
24 | | - '/tmp/doesnotexist' |
25 | | - end |
| 14 | + file = Tempfile.create |
| 15 | + file.close |
| 16 | + File.unlink(file.path) |
| 17 | + file.path |
26 | 18 | end |
27 | 19 |
|
28 | 20 | before(:each) do |
29 | | - allow(File).to receive(:exist?).and_call_original |
30 | | - allow(File).to receive(:exist?).with(filename).and_return(false).once |
31 | 21 | if Puppet::PUPPETVERSION[0].to_i < 8 |
32 | 22 | allow(PSON).to receive(:load).never # rubocop:disable RSpec/ReceiveNever Switching to not_to receive breaks testing in this case |
33 | 23 | else |
34 | 24 | allow(JSON).to receive(:parse).never # rubocop:disable RSpec/ReceiveNever |
35 | 25 | end |
36 | 26 | end |
37 | 27 |
|
38 | | - it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } |
39 | | - it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } |
40 | | - it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') } |
| 28 | + it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) } |
| 29 | + it { is_expected.to run.with_params(filename, {'đẽƒằưļŧ' => '٧ẵłựέ'}).and_return({'đẽƒằưļŧ' => '٧ẵłựέ'}) } |
| 30 | + it { is_expected.to run.with_params(filename, {'デフォルト' => '値'}).and_return({'デフォルト' => '値'}) } |
41 | 31 | end |
42 | 32 |
|
43 | 33 | context 'when an existing file is specified' do |
44 | | - let(:filename) do |
45 | | - if Puppet::Util::Platform.windows? |
46 | | - 'C:/tmp/doesexist' |
47 | | - else |
48 | | - '/tmp/doesexist' |
49 | | - end |
50 | | - end |
51 | 34 | let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
52 | 35 | let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
53 | 36 |
|
54 | | - before(:each) do |
55 | | - allow(File).to receive(:exist?).and_call_original |
56 | | - allow(File).to receive(:exist?).with(filename).and_return(true).once |
57 | | - allow(File).to receive(:read).with(filename).and_return(json).once |
58 | | - allow(File).to receive(:read).with(filename).and_return(json).once |
59 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
60 | | - allow(PSON).to receive(:load).with(json).and_return(data).once |
61 | | - else |
62 | | - allow(JSON).to receive(:parse).with(json).and_return(data).once |
| 37 | + it do |
| 38 | + Tempfile.new do |file| |
| 39 | + file.write(json) |
| 40 | + file.flush |
| 41 | + |
| 42 | + is_expected.to run.with_params(file.path).and_return(data) |
63 | 43 | end |
64 | 44 | end |
65 | | - |
66 | | - it { is_expected.to run.with_params(filename).and_return(data) } |
67 | 45 | end |
68 | 46 |
|
69 | 47 | context 'when the file could not be parsed' do |
70 | | - let(:filename) do |
71 | | - if Puppet::Util::Platform.windows? |
72 | | - 'C:/tmp/doesexist' |
73 | | - else |
74 | | - '/tmp/doesexist' |
75 | | - end |
76 | | - end |
77 | 48 | let(:json) { '{"key":"value"}' } |
78 | 49 |
|
79 | | - before(:each) do |
80 | | - allow(File).to receive(:exist?).and_call_original |
81 | | - allow(File).to receive(:exist?).with(filename).and_return(true).once |
82 | | - allow(File).to receive(:read).with(filename).and_return(json).once |
83 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
84 | | - allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
85 | | - else |
86 | | - allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
| 50 | + it do |
| 51 | + Tempfile.new do |file| |
| 52 | + file.write(json) |
| 53 | + file.flush |
| 54 | + |
| 55 | + is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) |
87 | 56 | end |
88 | 57 | end |
89 | | - |
90 | | - it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } |
91 | 58 | end |
92 | 59 |
|
93 | | - context 'when an existing URL is specified' do |
| 60 | + context 'when an existing URL with username and password is specified' do |
94 | 61 | let(:filename) do |
95 | | - 'https://example.local/myhash.json' |
| 62 | + 'https://user1:pass1@example.local/myhash.json' |
96 | 63 | end |
97 | | - let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
98 | | - let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
99 | 64 |
|
100 | | - it { |
101 | | - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) |
102 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
103 | | - expect(PSON).to receive(:load).with(json).and_return(data).once |
104 | | - else |
105 | | - expect(JSON).to receive(:parse).with(json).and_return(data).once |
106 | | - end |
107 | | - expect(subject).to run.with_params(filename).and_return(data) |
108 | | - } |
109 | | - end |
| 65 | + it do |
| 66 | + uri = URI.parse(filename) |
| 67 | + allow(URI).to receive(:parse).and_call_original |
| 68 | + expect(URI).to receive(:parse).with(filename).and_return(uri) |
| 69 | + expect(uri).to receive(:open).with(http_basic_authentication: ['user1', 'pass1']).and_yield(StringIO.new('{"key":"value"}')) |
110 | 70 |
|
111 | | - context 'when an existing URL (with username and password) is specified' do |
112 | | - let(:filename) do |
113 | | - 'https://user1:[email protected]/myhash.json' |
| 71 | + is_expected.to run.with_params(filename).and_return({'key' => 'value'}) |
| 72 | + expect(uri.user).to be_nil |
114 | 73 | end |
115 | | - let(:url_no_auth) { 'https://example.local/myhash.json' } |
116 | | - let(:basic_auth) { { http_basic_authentication: ['user1', 'pass1'] } } |
117 | | - let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
118 | | - let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
119 | | - |
120 | | - it { |
121 | | - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) |
122 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
123 | | - expect(PSON).to receive(:load).with(json).and_return(data).once |
124 | | - else |
125 | | - expect(JSON).to receive(:parse).with(json).and_return(data).once |
126 | | - end |
127 | | - expect(subject).to run.with_params(filename).and_return(data) |
128 | | - } |
129 | 74 | end |
130 | 75 |
|
131 | | - context 'when an existing URL (with username) is specified' do |
| 76 | + context 'when an existing URL is specified' do |
132 | 77 | let(:filename) do |
133 | | - 'https://user1@example.local/myhash.json' |
| 78 | + 'https://example.com/myhash.json' |
134 | 79 | end |
135 | | - let(:url_no_auth) { 'https://example.local/myhash.json' } |
136 | | - let(:basic_auth) { { http_basic_authentication: ['user1', ''] } } |
137 | 80 | let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
138 | | - let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
| 81 | + let(:json) { '{"key":"value", "ķęŷ":"νậŀųề", "キー":"値" }' } |
139 | 82 |
|
140 | 83 | it { |
141 | | - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) |
142 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
143 | | - expect(PSON).to receive(:load).with(json).and_return(data).once |
144 | | - else |
145 | | - expect(JSON).to receive(:parse).with(json).and_return(data).once |
146 | | - end |
| 84 | + uri = URI.parse(filename) |
| 85 | + allow(URI).to receive(:parse).and_call_original |
| 86 | + expect(URI).to receive(:parse).with(filename).and_return(uri) |
| 87 | + expect(uri).to receive(:open).with(no_args).and_yield(StringIO.new(json)) |
147 | 88 | expect(subject).to run.with_params(filename).and_return(data) |
148 | 89 | } |
149 | 90 | end |
150 | 91 |
|
151 | 92 | context 'when the URL output could not be parsed, with default specified' do |
152 | 93 | let(:filename) do |
153 | | - 'https://example.local/myhash.json' |
| 94 | + 'https://example.com/myhash.json' |
154 | 95 | end |
155 | 96 | let(:json) { ',;{"key":"value"}' } |
156 | 97 |
|
157 | 98 | it { |
158 | | - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) |
159 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
160 | | - expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
161 | | - else |
162 | | - expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
163 | | - end |
164 | | - expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') |
| 99 | + uri = URI.parse(filename) |
| 100 | + allow(URI).to receive(:parse).and_call_original |
| 101 | + expect(URI).to receive(:parse).with(filename).and_return(uri) |
| 102 | + expect(uri).to receive(:open).with(no_args).and_yield(StringIO.new(json)) |
| 103 | + expect(subject).to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) |
165 | 104 | } |
166 | 105 | end |
167 | 106 |
|
168 | 107 | context 'when the URL does not exist, with default specified' do |
169 | 108 | let(:filename) do |
170 | | - 'https://example.local/myhash.json' |
| 109 | + 'https://example.com/myhash.json' |
171 | 110 | end |
172 | 111 |
|
173 | 112 | it { |
174 | | - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found' |
175 | | - expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') |
| 113 | + uri = URI.parse(filename) |
| 114 | + allow(URI).to receive(:parse).and_call_original |
| 115 | + expect(URI).to receive(:parse).with(filename).and_return(uri) |
| 116 | + expect(uri).to receive(:open).with(no_args).and_raise(OpenURI::HTTPError, '404 File not Found') |
| 117 | + expect(subject).to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) |
176 | 118 | } |
177 | 119 | end |
178 | 120 | end |
|
0 commit comments