summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-04-10 17:18:21 +0200
committerGuido Günther <agx@sigxcpu.org>2014-04-11 09:08:57 +0200
commitc0eab58bc6bcfb26275349d817bf8746902a221a (patch)
treef40261bdb699c3b734babbc3ff2476c4b0c93e10
Initial commit
-rw-r--r--.gitignore6
-rw-r--r--.rspec4
-rw-r--r--.travis.yml22
-rw-r--r--Gemfile11
-rw-r--r--LICENSE17
-rw-r--r--README.markdown24
-rw-r--r--Rakefile18
-rw-r--r--lib/puppet/parser/functions/validate_nonemptystring.rb35
-rw-r--r--spec/spec.opts6
-rw-r--r--spec/spec_helper.rb31
-rw-r--r--spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb71
11 files changed, 245 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5e8e622
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+pkg/
+coverage/
+spec/fixtures/
+Gemfile.lock
+.bundle/
+vendor/
diff --git a/.rspec b/.rspec
new file mode 100644
index 0000000..7ab5f55
--- /dev/null
+++ b/.rspec
@@ -0,0 +1,4 @@
+--color
+--format
+progress
+--backtrace
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..e0240d7
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,22 @@
+rvm:
+ - 1.8.7
+ - 1.9.3
+ - 2.0.0
+notifications:
+ email:
+ - agx@sigxcpu.org
+env:
+ - PUPPET_VERSION=2.7.23
+ - PUPPET_VERSION=3.0.2
+ - PUPPET_VERSION=3.1.1
+ - PUPPET_VERSION=3.2.4
+ - PUPPET_VERSION=3.3.2
+ - PUPPET_VERSION=3.4.1
+matrix:
+ exclude:
+ - rvm: 2.0.0
+ env: PUPPET_VERSION=2.7.23
+ - rvm: 2.0.0
+ env: PUPPET_VERSION=3.0.2
+ - rvm: 2.0.0
+ env: PUPPET_VERSION=3.1.1
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..3171355
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,11 @@
+source ENV['GEM_SOURCE'] || 'https://rubygems.org'
+puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 3.4']
+
+group :development, :test do
+ gem 'puppetlabs_spec_helper', :require => false
+ gem 'puppet-lint', :require => false
+ gem 'puppet', puppetversion, :require => false
+ gem 'rake', '~> 10.1.0', :require => false
+ gem 'rspec-puppet', :require => false
+ gem 'simplecov', :require => false
+end
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..8366f51
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,17 @@
+Copyright (C) 2014 Guido Günther <agx@sigxcpu.org>
+
+based in parts on puppetlabs stdlib which is
+
+Copyright (C) 2011 Puppet Labs Inc
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/README.markdown b/README.markdown
new file mode 100644
index 0000000..447ef7f
--- /dev/null
+++ b/README.markdown
@@ -0,0 +1,24 @@
+# Standard Library-- #
+
+This puppet module provides some (in fact currently only one) function on top
+of the standard library. It's called "stdlib minus minus".
+
+# Functions #
+
+validate_nonemptystring
+-----------------------
+Validate that all passed values are strings with a length greater 0 and not undef. Abort catalog
+compilation if any value fails this check.
+
+The following values will pass:
+
+ $my_string = "one two"
+ validate_nonemptystring($my_string, 'three')
+
+The following values will fail, causing compilation to abort:
+
+ validate_nonemptystring(true)
+ validate_nonemptystring([ 'some', 'array' ])
+ $undefined = undef
+ validate_nonemptystring($undefined)
+
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..4ed1327
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,18 @@
+require 'rubygems'
+require 'puppetlabs_spec_helper/rake_tasks'
+require 'puppet-lint/tasks/puppet-lint'
+PuppetLint.configuration.send('disable_80chars')
+PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
+
+desc "Validate manifests, templates, and ruby files in lib."
+task :validate do
+ Dir['manifests/**/*.pp'].each do |manifest|
+ sh "puppet parser validate --noop #{manifest}"
+ end
+ Dir['lib/**/*.rb'].each do |lib_file|
+ sh "ruby -c #{lib_file}"
+ end
+ Dir['templates/**/*.erb'].each do |template|
+ sh "erb -P -x -T '-' #{template} | ruby -c"
+ end
+end
diff --git a/lib/puppet/parser/functions/validate_nonemptystring.rb b/lib/puppet/parser/functions/validate_nonemptystring.rb
new file mode 100644
index 0000000..d45fae5
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_nonemptystring.rb
@@ -0,0 +1,35 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_nonemptystring, :doc => <<-'ENDHEREDOC') do |args|
+ Validate that all passed values are string data structures and not empty
+ i.e. not undef or ''. Abort catalog compilation if any value fails this
+ check.
+
+ The following values will pass:
+
+ $my_string = "one two"
+ validate_string($my_string, 'three')
+
+ The following values will fail, causing compilation to abort:
+
+ validate_string(true)
+ validate_string([ 'some', 'array' ])
+ $undefined = undef
+ validate_string($undefined)
+
+ ENDHEREDOC
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_string(): 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. It looks to be a #{arg.class}")
+ end
+ if arg == nil || arg == '' || arg == :undef
+ raise Puppet::ParseError, ("#{arg.inspect} is empty or undef")
+ end
+ end
+ end
+end
diff --git a/spec/spec.opts b/spec/spec.opts
new file mode 100644
index 0000000..91cd642
--- /dev/null
+++ b/spec/spec.opts
@@ -0,0 +1,6 @@
+--format
+s
+--colour
+--loadby
+mtime
+--backtrace
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..a63422d
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,31 @@
+dir = File.expand_path(File.dirname(__FILE__))
+$LOAD_PATH.unshift File.join(dir, 'lib')
+
+# So everyone else doesn't have to include this base constant.
+module PuppetSpec
+ FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR)
+end
+
+require 'puppet'
+require 'rspec-puppet'
+require 'simplecov'
+require 'puppetlabs_spec_helper/module_spec_helper'
+require 'mocha/setup'
+
+
+SimpleCov.start do
+ add_filter "/spec/"
+end
+
+
+RSpec.configure do |config|
+ config.before :each do
+ # Ensure that we don't accidentally cache facts and environment between
+ # test cases. This requires each example group to explicitly load the
+ # facts being exercised with something like
+ # Facter.collection.loader.load(:ipaddress)
+ Facter::Util::Loader.any_instance.stubs(:load_all)
+ Facter.clear
+ Facter.clear_messages
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb b/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb
new file mode 100644
index 0000000..41b9dcd
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb
@@ -0,0 +1,71 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe Puppet::Parser::Functions.function(:validate_nonemptystring) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe 'when calling validate_nonemptystring from puppet' do
+
+ %w{ foo bar baz }.each do |the_string|
+
+ it "should compile when #{the_string} is a string" do
+ Puppet[:code] = "validate_nonemptystring('#{the_string}')"
+ scope.compiler.compile
+ end
+
+ it "should compile when #{the_string} is a bare word" do
+ Puppet[:code] = "validate_nonemptystring(#{the_string})"
+ scope.compiler.compile
+ end
+
+ end
+
+ %w{ true false }.each do |the_string|
+ it "should compile when #{the_string} is a string" do
+ Puppet[:code] = "validate_nonemptystring('#{the_string}')"
+ scope.compiler.compile
+ end
+
+ it "should not compile when #{the_string} is a bare word" do
+ Puppet[:code] = "validate_nonemptystring(#{the_string})"
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
+ end
+ end
+
+ it "should compile when multiple string arguments are passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = 'one'
+ $bar = 'two'
+ validate_nonemptystring($foo, $bar)
+ ENDofPUPPETcode
+ scope.compiler.compile
+ end
+
+ it "should not compile when an explicitly undef variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = undef
+ validate_nonemptystring($foo)
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
+ end
+
+ it "should not compile when an undefined variable is passed" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ validate_nonemptystring($foobarbazishouldnotexist)
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
+ end
+
+ it "should not compile when one string is empty" do
+ Puppet[:code] = <<-'ENDofPUPPETcode'
+ $foo = 'one'
+ $bar = ''
+ $baz = 'three'
+ validate_nonemptystring($foo, $bar)
+ ENDofPUPPETcode
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
+ end
+
+ end
+end