diff options
author | Ken Barber <ken@bob.sh> | 2011-07-28 18:10:16 +0100 |
---|---|---|
committer | Ken Barber <ken@bob.sh> | 2011-07-28 18:10:16 +0100 |
commit | 635ed82e5cae38b0ba82098c340cbeed70d483c3 (patch) | |
tree | 6da1b936dd267452c8f89711a518a3b5d6500f50 | |
parent | 7efd6ec5819775226d669c15b58649d375f72959 (diff) |
(#2) - unstubbed is_valid_ip_address
-rw-r--r-- | lib/puppet/parser/functions/is_valid_ip_address.rb | 13 | ||||
-rw-r--r-- | spec/unit/parser/functions/is_valid_ip_address_spec.rb | 20 |
2 files changed, 30 insertions, 3 deletions
diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index e91dda8..4f45890 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -7,11 +7,24 @@ module Puppet::Parser::Functions EOS ) do |arguments| + require 'ipaddr' + if (arguments.size != 1) then raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ "given #{arguments.size} for 1") 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 diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb index ee53ee1..2883aaa 100644 --- a/spec/unit/parser/functions/is_valid_ip_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_ip_address_spec.rb @@ -18,14 +18,28 @@ describe "the is_valid_ip_address function" do lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) end - it "should return true if an IP address" do - result = @scope.function_is_valid_ip_address("1.2.3.4") + it "should return true if an IPv4 address" do + 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)) end 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)) 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 |