Skip to content

Commit

Permalink
Merge pull request #1097 from mrgum/master
Browse files Browse the repository at this point in the history
unify log_level checking and allow trace1-8 levels
  • Loading branch information
igalic committed Apr 10, 2015
2 parents 09da0d1 + c901662 commit 9202d40
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
27 changes: 27 additions & 0 deletions lib/puppet/parser/functions/validate_apache_log_level.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Puppet::Parser::Functions
newfunction(:validate_apache_log_level, :doc => <<-'ENDHEREDOC') do |args|
Perform simple validation of a string against the list of known log
levels as per http://httpd.apache.org/docs/current/mod/core.html#loglevel
validate_apache_loglevel('info')
Modules maybe specified with their own levels like these:
validate_apache_loglevel('warn ssl:info')
validate_apache_loglevel('warn mod_ssl.c:info')
validate_apache_loglevel('warn ssl_module:info')
Expected to be used from the main or vhost.
Might be used from directory too later as apaceh supports that
ENDHEREDOC
if (args.size != 1) then
raise Puppet::ParseError, ("validate_apache_loglevel(): wrong number of arguments (#{args.length}; must be 1)")
end

log_level = args[0]
msg = "Log level '${log_level}' is not one of the supported Apache HTTP Server log levels."

raise Puppet::ParseError, (msg) unless log_level =~ Regexp.compile('(emerg|alert|crit|error|warn|notice|info|debug|trace[1-8])')

end
end
5 changes: 1 addition & 4 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@
}
}

$valid_log_level_re = '(emerg|alert|crit|error|warn|notice|info|debug)'

validate_re($log_level, $valid_log_level_re,
"Log level '${log_level}' is not one of the supported Apache HTTP Server log levels.")
validate_apache_log_level($log_level)

class { '::apache::service':
service_name => $service_name,
Expand Down
3 changes: 1 addition & 2 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@
Allowed values are 'directory' and 'absent'.")

if $log_level {
validate_re($log_level, '^(emerg|alert|crit|error|warn|notice|info|debug)$',
"Log level '${log_level}' is not one of the supported Apache HTTP Server log levels.")
validate_apache_log_level($log_level)
}

if $access_log_file and $access_log_pipe {
Expand Down
39 changes: 39 additions & 0 deletions spec/unit/puppet/parser/functions/validate_apache_log_level.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the validate_apache_log_level function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
expect(Puppet::Parser::Functions.function("validate_apache_log_level")).to eq("function_validate_apache_log_level")
end

it "should raise a ParseError if there is less than 1 arguments" do
expect { scope.function_validate_apache_log_level([]) }.to( raise_error(Puppet::ParseError) )
end

it "should raise a ParseError when given garbage" do
expect { scope.function_validate_apache_log_level(['garbage']) }.to( raise_error(Puppet::ParseError) )
end

it "should not raise a ParseError when given a plain log level" do
expect { scope.function_validate_apache_log_level(['info']) }.to_not raise_error
end

it "should not raise a ParseError when given a log level and module log level" do
expect { scope.function_validate_apache_log_level(['warn ssl:info']) }.to_not raise_error
end

it "should not raise a ParseError when given a log level and module log level" do
expect { scope.function_validate_apache_log_level(['warn mod_ssl.c:info']) }.to_not raise_error
end

it "should not raise a ParseError when given a log level and module log level" do
expect { scope.function_validate_apache_log_level(['warn ssl_module:info']) }.to_not raise_error
end

it "should not raise a ParseError when given a trace level" do
expect { scope.function_validate_apache_log_level(['trace4']) }.to_not raise_error
end

end

0 comments on commit 9202d40

Please sign in to comment.