-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpanos_provider_spec.rb
253 lines (227 loc) · 7.71 KB
/
panos_provider_spec.rb
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
require 'spec_helper'
require 'support/matchers/have_xml'
require 'puppet/provider/panos_provider'
RSpec.describe Puppet::Provider::PanosProvider do
subject(:provider) { described_class.new }
let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') }
let(:transport) { instance_double('Puppet::ResourceApi::Transport::Panos', 'transport') }
let(:typedef) { instance_double('Puppet::ResourceApi::TypeDefinition', 'typedef') }
let(:attrs) do
{
name: {
type: 'String',
xpath: 'string(@name)',
},
description: {
type: 'Optional[String]',
xpath: 'description/text()',
},
is_enabled: {
type: 'Boolean',
xpath: 'isenabled/text()',
},
maybe_enabled: {
type: 'Optional[Boolean]',
xpath: 'enabled/text()',
},
tags: {
type: 'Array[String]',
xpath_array: 'tag/member/text()',
},
}
end
let(:example_data) do
REXML::Document.new <<EOF
<response>
<result>
#{test_entry_1}
#{test_entry_2}
</result>
</response>
EOF
end
let(:test_entry_1) do
String.new <<EOF
<entry name="value1">
<isenabled>Yes</isenabled>
<enabled>No</enabled>
<description><eas&lt;yxss/></description>
<tag>
<member>one</member>
<member>two</member>
<member>three</member>
</tag>
</entry>
EOF
end
let(:test_entry_2) do
String.new <<EOF
<entry name="value2">
<isenabled>No</isenabled>
<enabled>Yes</enabled>
<description>desc test 2</description>
<tag></tag>
</entry>
EOF
end
let(:resource_data) do
[
{
name: 'value1',
description: '<eas<yxss/>',
is_enabled: 'Yes',
maybe_enabled: 'No',
tags: ['one', 'two', 'three'],
},
{
name: 'value2',
description: 'desc test 2',
is_enabled: 'No',
maybe_enabled: 'Yes',
tags: [],
},
]
end
before(:each) do
allow(context).to receive(:transport).with(no_args).and_return(transport)
allow(context).to receive(:type).with(no_args).and_return(typedef)
allow(context).to receive(:notice)
allow(typedef).to receive(:definition).with(no_args).and_return(base_xpath: 'some xpath')
allow(provider).to receive(:validate_should) # rubocop:disable RSpec/SubjectStub
allow(provider).to receive(:xml_from_should).and_return(test_entry_1) # rubocop:disable RSpec/SubjectStub
end
describe '#get' do
it 'processes resources' do
allow(typedef).to receive(:attributes).with(no_args).and_return(attrs)
allow(transport).to receive(:get_config).with('some xpath/entry').and_return(example_data)
expect(provider.get(context)).to eq resource_data
end
it 'allows transport api error to bubble up' do
allow(typedef).to receive(:attributes).with(no_args).and_return(attrs)
allow(transport).to receive(:get_config).with('some xpath/entry').and_raise(Puppet::ResourceError, 'Some Error Message')
expect { provider.get(context) }.to raise_error Puppet::ResourceError
end
end
describe 'match(entry, attr)' do
let(:attr) { { type: 'String' } }
let(:attr_name) { :name }
context 'when attr_name is :ensure' do
let(:attr_name) { :ensure }
it { expect(provider.match(test_entry_1, attr, attr_name)).to eq 'present' }
end
context 'when attr_name is not :ensure' do
context 'when attr contains :xpath' do
let(:attr) { { type: 'String', xpath: 'isenabled/text()' } }
it do
expect(provider).to receive(:text_match) # rubocop:disable RSpec/SubjectStub
provider.match(test_entry_1, attr, attr_name)
end
end
context 'when attr contains :xpath_array' do
let(:attr) { { type: 'Array[String]]', xpath_array: 'tag/member/text()' } }
let(:attr_name) { :tags }
it do
expect(provider).to receive(:array_match) # rubocop:disable RSpec/SubjectStub
provider.match(test_entry_1, attr, attr_name)
end
end
end
end
describe 'string_to_bool(value)' do
context 'when the value is `yes`' do
it { expect(provider.string_to_bool('yes')).to be_truthy }
end
context 'when the value is `no`' do
it { expect(provider.string_to_bool('no')).to be_falsey }
end
context 'when the value is nil' do
it { expect(provider.string_to_bool(nil)).to be_falsey }
end
context 'when the value is anything else' do
it 'returns the value passed in' do
expect(provider.string_to_bool('foo')).to eq('foo')
end
end
end
describe 'bool_to_string(value)' do
context 'when the value is `true`' do
it { expect(provider.bool_to_string(true)).to eq('yes') }
end
context 'when the value is `false`' do
it { expect(provider.bool_to_string(false)).to eq('no') }
end
context 'when the value is nil' do
it { expect(provider.bool_to_string(nil)).to eq(nil) }
end
context 'when the value is anything else' do
it 'returns the value passed in' do
expect(provider.bool_to_string('foo')).to eq('foo')
end
end
end
describe 'build_tags(builder, should)' do
let(:builder) { Builder::XmlMarkup.new }
context 'if should contains :tags' do
let(:attrs) do
{
name: 'build_tags',
description: 'desc test',
is_enabled: 'Yes',
maybe_enabled: 'No',
tags: ['one', 'two'],
}
end
let(:xml) { '<tag><member>one</member><member>two</member></tag>' }
it 'will return builder content with correct XML' do
expect(provider.build_tags(builder, attrs)).to eq(xml)
end
end
context 'if :tags is empty' do
let(:attrs) do
{
name: 'build_tags',
description: 'desc test',
is_enabled: 'Yes',
maybe_enabled: 'No',
tags: [],
}
end
let(:xml) { '<tag></tag>' }
it 'will return builder content with correct XML' do
expect(provider.build_tags(builder, attrs)).to eq(xml)
end
end
end
describe 'create(context, name, should)' do
it 'calls provider functions' do
expect(transport).to receive(:set_config).with('some xpath', instance_of(String)) do |_xpath, doc|
expect(doc).to have_xml('entry/description', '<eas&lt;yxss/>')
expect(doc).to have_xml('entry/isenabled', 'Yes')
expect(doc).to have_xml('entry/enabled', 'No')
expect(doc).to have_xml('entry/tag/member', 'one')
expect(doc).to have_xml('entry/tag/member', 'two')
expect(doc).to have_xml('entry/tag/member', 'three')
end
provider.create(context, resource_data[0][:name], resource_data[0])
end
end
describe 'update(context, name, should)' do
it 'calls provider functions' do
expect(transport).to receive(:edit_config).with('some xpath/entry[@name=\'value1\']', instance_of(String)) do |_xpath, doc|
expect(doc).to have_xml('entry/description', '<eas&lt;yxss/>')
expect(doc).to have_xml('entry/isenabled', 'Yes')
expect(doc).to have_xml('entry/enabled', 'No')
expect(doc).to have_xml('entry/tag/member', 'one')
expect(doc).to have_xml('entry/tag/member', 'two')
expect(doc).to have_xml('entry/tag/member', 'three')
end
provider.update(context, resource_data[0][:name], resource_data[0])
end
end
describe 'delete(context, name)' do
it 'calls provider functions' do
expect(transport).to receive(:delete_config).with('some xpath/entry[@name=\'value1\']')
provider.delete(context, resource_data[0][:name])
end
end
end