Skip to content

Commit 1e69539

Browse files
authored
Merge pull request #229 from lukebigum/master
Allow Type network_config to take a Numeric value for the MTU parameter
2 parents ebb38dc + 620003e commit 1e69539

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/puppet/type/network_config.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@
8787
validate do |value|
8888
# reject floating point and negative integers
8989
# XXX this lets 1500.0 pass
90-
unless value =~ %r{^\d+$}
91-
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
90+
if value.is_a? Numeric
91+
unless value.integer?
92+
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
93+
end
94+
else
95+
unless value =~ %r{^\d+$}
96+
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
97+
end
9298
end
9399

94100
# Intel 82598 & 82599 chips support MTUs up to 16110; is there any

spec/unit/type/network_config_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,26 @@
146146
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '42')
147147
end
148148

149+
it 'validates a tiny mtu size as a number' do
150+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 42)
151+
end
152+
149153
it 'validates a normal mtu size' do
150154
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500')
151155
end
152156

157+
it 'validates a normal mtu size as a number' do
158+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500)
159+
end
160+
153161
it 'validates a large mtu size' do
154162
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '16384')
155163
end
156164

165+
it 'validates a large mtu size as a number' do
166+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 16_384)
167+
end
168+
157169
it 'fails if an random string is passed' do
158170
expect do
159171
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 'This is clearly not a mtu')
@@ -166,29 +178,59 @@
166178
end.to raise_error
167179
end
168180

181+
it 'fails on numeric values < 42' do
182+
expect do
183+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 41)
184+
end.to raise_error
185+
end
186+
169187
it 'fails on zero' do
170188
expect do
171189
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '0')
172190
end.to raise_error
173191
end
174192

193+
it 'fails on numeric zero' do
194+
expect do
195+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 0)
196+
end.to raise_error
197+
end
198+
175199
it 'fails on values > 65536' do
176200
expect do
177201
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '65537')
178202
end.to raise_error
179203
end
180204

205+
it 'fails on numeric values > 65536' do
206+
expect do
207+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 65_537)
208+
end.to raise_error
209+
end
210+
181211
it 'fails on negative values' do
182212
expect do
183213
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '-1500')
184214
end.to raise_error
185215
end
186216

217+
it 'fails on negative numbers' do
218+
expect do
219+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: -1500)
220+
end.to raise_error
221+
end
222+
187223
it 'fails on non-integer values' do
188224
expect do
189225
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1')
190226
end.to raise_error
191227
end
228+
229+
it 'fails on numeric non-integer values' do
230+
expect do
231+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1)
232+
end.to raise_error
233+
end
192234
end
193235

194236
describe 'mode' do

0 commit comments

Comments
 (0)