(#2) - unstubbed is_valid_ip_address

This commit is contained in:
Ken Barber 2011-07-28 18:10:16 +01:00
parent 7efd6ec581
commit 635ed82e5c
2 changed files with 30 additions and 3 deletions

View file

@ -7,11 +7,24 @@ module Puppet::Parser::Functions
EOS EOS
) do |arguments| ) do |arguments|
require 'ipaddr'
if (arguments.size != 1) then if (arguments.size != 1) then
raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+
"given #{arguments.size} for 1") "given #{arguments.size} for 1")
end end
begin
ip = IPAddr.new(arguments[0])
rescue ArgumentError
return false
end
if ip.ipv4? or ip.ipv6? then
return true
else
return false
end
end end
end end

View file

@ -18,14 +18,28 @@ describe "the is_valid_ip_address function" do
lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError))
end end
it "should return true if an IP address" do it "should return true if an IPv4 address" do
result = @scope.function_is_valid_ip_address("1.2.3.4") result = @scope.function_is_valid_ip_address(["1.2.3.4"])
result.should(eq(true))
end
it "should return true if a full IPv6 address" do
result = @scope.function_is_valid_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"])
result.should(eq(true))
end
it "should return true if a compressed IPv6 address" do
result = @scope.function_is_valid_ip_address(["fe00::1"])
result.should(eq(true)) result.should(eq(true))
end end
it "should return false if not valid" do it "should return false if not valid" do
result = @scope.function_is_valid_ip_address("asdf") result = @scope.function_is_valid_ip_address(["asdf"])
result.should(eq(false)) result.should(eq(false))
end end
it "should return false if IP octets out of range" do
result = @scope.function_is_valid_ip_address(["1.1.1.300"])
result.should(eq(false))
end
end end