Skip to content

Commit f863558

Browse files
author
Jeff McCune
committed
Merge branch 'add/2.x/13974-has_interface_with' into 2.x
* add/2.x/13974-has_interface_with: (#13974) Add predicate functions for interface facts
2 parents bf66ded + f819417 commit f863558

File tree

9 files changed

+258
-0
lines changed

9 files changed

+258
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# has_interface_with
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:has_interface_with, :type => :rvalue, :doc => <<-EOS
7+
Returns boolean based on kind and value:
8+
* macaddress
9+
* netmask
10+
* ipaddress
11+
* network
12+
13+
has_interface_with("macaddress", "x:x:x:x:x:x")
14+
has_interface_with("ipaddress", "127.0.0.1") => true
15+
etc.
16+
17+
If no "kind" is given, then the presence of the interface is checked:
18+
has_interface_with("lo") => true
19+
EOS
20+
) do |args|
21+
22+
raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments " +
23+
"given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
24+
25+
interfaces = lookupvar('interfaces')
26+
27+
# If we do not have any interfaces, then there are no requested attributes
28+
return false if (interfaces == :undefined)
29+
30+
interfaces = interfaces.split(',')
31+
32+
if args.size == 1
33+
return interfaces.member?(args[0])
34+
end
35+
36+
kind, value = args
37+
38+
if lookupvar(kind) == value
39+
return true
40+
end
41+
42+
result = false
43+
interfaces.each do |iface|
44+
if value == lookupvar("#{kind}_#{iface}")
45+
result = true
46+
break
47+
end
48+
end
49+
50+
result
51+
end
52+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# has_ip_address
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:has_ip_address, :type => :rvalue, :doc => <<-EOS
7+
Returns true if the client has the requested IP address on some interface.
8+
9+
This function iterates through the 'interfaces' fact and checks the
10+
'ipaddress_IFACE' facts, performing a simple string comparison.
11+
EOS
12+
) do |args|
13+
14+
raise(Puppet::ParseError, "has_ip_address(): Wrong number of arguments " +
15+
"given (#{args.size} for 1)") if args.size != 1
16+
17+
Puppet::Parser::Functions.autoloader.load(:has_interface_with) \
18+
unless Puppet::Parser::Functions.autoloader.loaded?(:has_interface_with)
19+
20+
function_has_interface_with(['ipaddress', args[0]])
21+
22+
end
23+
end
24+
25+
# vim:sts=2 sw=2
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# has_ip_network
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:has_ip_network, :type => :rvalue, :doc => <<-EOS
7+
Returns true if the client has an IP address within the requested network.
8+
9+
This function iterates through the 'interfaces' fact and checks the
10+
'network_IFACE' facts, performing a simple string comparision.
11+
EOS
12+
) do |args|
13+
14+
raise(Puppet::ParseError, "has_ip_network(): Wrong number of arguments " +
15+
"given (#{args.size} for 1)") if args.size != 1
16+
17+
Puppet::Parser::Functions.autoloader.load(:has_interface_with) \
18+
unless Puppet::Parser::Functions.autoloader.loaded?(:has_interface_with)
19+
20+
function_has_interface_with(['network', args[0]])
21+
22+
end
23+
end
24+
25+
# vim:sts=2 sw=2
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe Puppet::Parser::Functions.function(:has_interface_with) do
5+
6+
let(:scope) do
7+
PuppetlabsSpec::PuppetInternals.scope
8+
end
9+
10+
# The subject of these examples is the method itself.
11+
subject do
12+
function_name = Puppet::Parser::Functions.function(:has_interface_with)
13+
scope.method(function_name)
14+
end
15+
16+
# We need to mock out the Facts so we can specify how we expect this function
17+
# to behave on different platforms.
18+
context "On Mac OS X Systems" do
19+
before :each do
20+
scope.stubs(:lookupvar).with("interfaces").returns('lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0')
21+
end
22+
it 'should have loopback (lo0)' do
23+
subject.call(['lo0']).should be_true
24+
end
25+
it 'should not have loopback (lo)' do
26+
subject.call(['lo']).should be_false
27+
end
28+
end
29+
context "On Linux Systems" do
30+
before :each do
31+
scope.stubs(:lookupvar).with("interfaces").returns('eth0,lo')
32+
scope.stubs(:lookupvar).with("ipaddress").returns('10.0.0.1')
33+
scope.stubs(:lookupvar).with("ipaddress_lo").returns('127.0.0.1')
34+
scope.stubs(:lookupvar).with("ipaddress_eth0").returns('10.0.0.1')
35+
scope.stubs(:lookupvar).with('muppet').returns('kermit')
36+
scope.stubs(:lookupvar).with('muppet_lo').returns('mspiggy')
37+
scope.stubs(:lookupvar).with('muppet_eth0').returns('kermit')
38+
end
39+
it 'should have loopback (lo)' do
40+
subject.call(['lo']).should be_true
41+
end
42+
it 'should not have loopback (lo0)' do
43+
subject.call(['lo0']).should be_false
44+
end
45+
it 'should have ipaddress with 127.0.0.1' do
46+
subject.call(['ipaddress', '127.0.0.1']).should be_true
47+
end
48+
it 'should have ipaddress with 10.0.0.1' do
49+
subject.call(['ipaddress', '10.0.0.1']).should be_true
50+
end
51+
it 'should not have ipaddress with 10.0.0.2' do
52+
subject.call(['ipaddress', '10.0.0.2']).should be_false
53+
end
54+
it 'should have muppet named kermit' do
55+
subject.call(['muppet', 'kermit']).should be_true
56+
end
57+
it 'should have muppet named mspiggy' do
58+
subject.call(['muppet', 'mspiggy']).should be_true
59+
end
60+
it 'should not have muppet named bigbird' do
61+
subject.call(['muppet', 'bigbird']).should be_false
62+
end
63+
end
64+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe Puppet::Parser::Functions.function(:has_ip_address) do
5+
6+
let(:scope) do
7+
PuppetlabsSpec::PuppetInternals.scope
8+
end
9+
10+
subject do
11+
function_name = Puppet::Parser::Functions.function(:has_ip_address)
12+
scope.method(function_name)
13+
end
14+
15+
context "On Linux Systems" do
16+
before :each do
17+
scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo')
18+
scope.stubs(:lookupvar).with('ipaddress').returns('10.0.2.15')
19+
scope.stubs(:lookupvar).with('ipaddress_eth0').returns('10.0.2.15')
20+
scope.stubs(:lookupvar).with('ipaddress_lo').returns('127.0.0.1')
21+
end
22+
23+
it 'should have primary address (10.0.2.15)' do
24+
subject.call(['10.0.2.15']).should be_true
25+
end
26+
27+
it 'should have lookupback address (127.0.0.1)' do
28+
subject.call(['127.0.0.1']).should be_true
29+
end
30+
31+
it 'should not have other address' do
32+
subject.call(['192.1681.1.1']).should be_false
33+
end
34+
35+
it 'should not have "mspiggy" on an interface' do
36+
subject.call(['mspiggy']).should be_false
37+
end
38+
end
39+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe Puppet::Parser::Functions.function(:has_ip_network) do
5+
6+
let(:scope) do
7+
PuppetlabsSpec::PuppetInternals.scope
8+
end
9+
10+
subject do
11+
function_name = Puppet::Parser::Functions.function(:has_ip_network)
12+
scope.method(function_name)
13+
end
14+
15+
context "On Linux Systems" do
16+
before :each do
17+
scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo')
18+
scope.stubs(:lookupvar).with('network').returns(:undefined)
19+
scope.stubs(:lookupvar).with('network_eth0').returns('10.0.2.0')
20+
scope.stubs(:lookupvar).with('network_lo').returns('127.0.0.1')
21+
end
22+
23+
it 'should have primary network (10.0.2.0)' do
24+
subject.call(['10.0.2.0']).should be_true
25+
end
26+
27+
it 'should have loopback network (127.0.0.0)' do
28+
subject.call(['127.0.0.1']).should be_true
29+
end
30+
31+
it 'should not have other network' do
32+
subject.call(['192.168.1.0']).should be_false
33+
end
34+
end
35+
end
36+

tests/has_interface_with.pp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include stdlib
2+
info("has_interface_with('lo'):", has_interface_with('lo'))
3+
info("has_interface_with('loX'):", has_interface_with('loX'))
4+
info("has_interface_with('ipaddress', '127.0.0.1'):", has_interface_with('ipaddress', '127.0.0.1'))
5+
info("has_interface_with('ipaddress', '127.0.0.100'):", has_interface_with('ipaddress', '127.0.0.100'))
6+
info("has_interface_with('network', '127.0.0.0'):", has_interface_with('network', '127.0.0.0'))
7+
info("has_interface_with('network', '128.0.0.0'):", has_interface_with('network', '128.0.0.0'))
8+
info("has_interface_with('netmask', '255.0.0.0'):", has_interface_with('netmask', '255.0.0.0'))
9+
info("has_interface_with('netmask', '256.0.0.0'):", has_interface_with('netmask', '256.0.0.0'))
10+

tests/has_ip_address.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include stdlib
2+
info("has_ip_address('192.168.1.256'):", has_ip_address('192.168.1.256'))
3+
info("has_ip_address('127.0.0.1'):", has_ip_address('127.0.0.1'))

tests/has_ip_network.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include stdlib
2+
info("has_ip_network('127.0.0.0'):", has_ip_network('127.0.0.0'))
3+
info("has_ip_network('128.0.0.0'):", has_ip_network('128.0.0.0'))
4+

0 commit comments

Comments
 (0)