summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/functions/validate_ipv4_address.rb48
-rw-r--r--lib/puppet/parser/functions/validate_ipv6_address.rb49
-rw-r--r--spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb64
-rw-r--r--spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb67
4 files changed, 228 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb
new file mode 100644
index 0000000..fc02748
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ipv4_address.rb
@@ -0,0 +1,48 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IPv4 addresses.
+ Fail compilation if any value fails this check.
+
+ The following values will pass:
+
+ $my_ip = "1.2.3.4"
+ validate_ipv4_address($my_ip)
+ validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+
+ $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
+ validate_ipv4_address($some_array)
+
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv4?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_ipv6_address.rb b/lib/puppet/parser/functions/validate_ipv6_address.rb
new file mode 100644
index 0000000..b0f2558
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ipv6_address.rb
@@ -0,0 +1,49 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IPv6 addresses.
+ Fail compilation if any value fails this check.
+
+ The following values will pass:
+
+ $my_ip = "3ffe:505:2"
+ validate_ipv6_address(1)
+ validate_ipv6_address($my_ip)
+ validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+
+ $some_array = [ true, false, "garbage string", "1.2.3.4" ]
+ validate_ipv6_address($some_array)
+
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv6?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
+ end
+ end
+
+ end
+
+end
diff --git a/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb
new file mode 100644
index 0000000..85536d3
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb
@@ -0,0 +1,64 @@
+#! /usr/bin/env/ruby -S rspec
+
+require "spec_helper"
+
+describe Puppet::Parser::Functions.function(:validate_ipv4_address) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe "when calling validate_ipv4_address from puppet" do
+ describe "when given IPv4 address strings" do
+ it "should compile with one argument" do
+ Puppet[:code] = "validate_ipv4_address('1.2.3.4')"
+ scope.compiler.compile
+ end
+
+ it "should compile with multiple arguments" do
+ Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')"
+ scope.compiler.compile
+ end
+ end
+
+ describe "when given an IPv6 address" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address('3ffe:505')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
+ end
+ end
+
+ describe "when given other strings" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address('hello', 'world')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
+ end
+ end
+
+ describe "when given numbers" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address(1, 2)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/)
+ end
+ end
+
+ describe "when given booleans" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address(true, false)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /is not a string/)
+ end
+ end
+
+ it "should not compile when no arguments are passed" do
+ Puppet[:code] = "validate_ipv4_address()"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb
new file mode 100644
index 0000000..1fe5304
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb
@@ -0,0 +1,67 @@
+#! /usr/bin/env/ruby -S rspec
+
+require "spec_helper"
+
+describe Puppet::Parser::Functions.function(:validate_ipv6_address) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe "when calling validate_ipv6_address from puppet" do
+ describe "when given IPv6 address strings" do
+ it "should compile with one argument" do
+ Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')"
+ scope.compiler.compile
+ end
+
+ it "should compile with multiple arguments" do
+ Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')"
+ scope.compiler.compile
+ end
+ end
+
+ describe "when given an ipv4 address" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address('1.2.3.4')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+ end
+ end
+
+ describe "when given other strings" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address('hello', 'world')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+ end
+ end
+
+ # 1.8.7 is EOL'd and also absolutely insane about ipv6
+ unless RUBY_VERSION == '1.8.7'
+ describe "when given numbers" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address(1, 2)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+ end
+ end
+ end
+
+ describe "when given booleans" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address(true, false)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /is not a string/)
+ end
+ end
+
+ it "should not compile when no arguments are passed" do
+ Puppet[:code] = "validate_ipv6_address()"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ end
+ end
+end