diff --git a/manifests/client/service.pp b/manifests/client/service.pp index d87b1a0cf9..64b771f71c 100644 --- a/manifests/client/service.pp +++ b/manifests/client/service.pp @@ -8,8 +8,29 @@ # Bolean. Value of hasrestart attribute for this service. # Default: true # +# [*log_level*] +# String. Sensu log level to be used +# Default: $::sensu::log_level +# Valid values: debug, info, warn, error, fatal +# +# [*windows_logrotate*] +# Boolean. Whether or not to use logrotate on Windows OS family. +# Default: $::sensu::windows_logrotate +# +# [*windows_log_size*] +# Integer. The integer value for the size of log files on Windows OS family. sizeThreshold in sensu-client.xml. +# Default: $::sensu::windows_log_size +# +# [*windows_log_number*] +# Integer. The integer value for the number of log files to keep on Windows OS family. keepFiles in sensu-client.xml. +# Default: $::sensu::windows_log_number +# class sensu::client::service ( - $hasrestart = true, + $hasrestart = true, + $log_level = $::sensu::log_level, + $windows_logrotate = $::sensu::windows_logrotate, + $windows_log_size = $::sensu::windows_log_size, + $windows_log_number = $::sensu::windows_log_number, ) { validate_bool($hasrestart) diff --git a/manifests/extension.pp b/manifests/extension.pp index 1351f5a572..51a915358f 100644 --- a/manifests/extension.pp +++ b/manifests/extension.pp @@ -40,8 +40,7 @@ $notify_services = [] } - $filename = inline_template('<%= scope.lookupvar(\'source\').split(\'/\').last %>') - $handler = "${install_path}/${filename}" + $handler = "${install_path}/${basename($source)}" $file_ensure = $ensure ? { 'absent' => 'absent', diff --git a/manifests/handler.pp b/manifests/handler.pp index f22fae5751..4b1dbf4645 100644 --- a/manifests/handler.pp +++ b/manifests/handler.pp @@ -130,8 +130,7 @@ } if $source { - $filename = inline_template('<%= scope.lookupvar(\'source\').split(\'/\').last %>') - $handler = "${install_path}/${filename}" + $handler = "${install_path}/${basename($source)}" ensure_resource('file', $handler, { ensure => $file_ensure, diff --git a/manifests/init.pp b/manifests/init.pp index 463d1e35de..2639bb9097 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -342,6 +342,7 @@ # # [*path*] # String. Used to set PATH in /etc/default/sensu +# Default: '$PATH' # # [*redact*] # Array of strings. Use to redact passwords from checks on the client side @@ -389,6 +390,18 @@ # String. Used to set package_checksum for windows installs # Default: undef # +# [*windows_logrotate*] +# Boolean. Whether or not to use logrotate on Windows OS family. +# Default: false +# +# [*windows_log_size*] +# Integer. The integer value for the size of log files on Windows OS family. sizeThreshold in sensu-client.xml. +# Default: '10240' +# +# [*windows_log_number*] +# Integer. The integer value for the number of log files to keep on Windows OS family. keepFiles in sensu-client.xml. +# Default: '10' +# # [*windows_pkg_url*] # String. If specified, override the behavior of computing the package source # URL from windows_repo_prefix and os major release fact. This parameter is @@ -517,7 +530,7 @@ $enterprise_dashboard_github = undef, $enterprise_dashboard_gitlab = undef, $enterprise_dashboard_ldap = undef, - $path = undef, + $path = '$PATH', $redact = undef, $deregister_on_stop = false, $deregister_handler = undef, diff --git a/manifests/mutator.pp b/manifests/mutator.pp index 350fc26f05..db3d477709 100644 --- a/manifests/mutator.pp +++ b/manifests/mutator.pp @@ -45,9 +45,7 @@ } if $source { - - $filename = inline_template('<%= scope.lookupvar(\'source\').split(\'/\').last %>') - $mutator = "${install_path}/${filename}" + $mutator = "${install_path}/${basename($source)}" $file_ensure = $ensure ? { 'absent' => 'absent', diff --git a/manifests/package.pp b/manifests/package.pp index 6e6496111f..f43d1eb8ae 100644 --- a/manifests/package.pp +++ b/manifests/package.pp @@ -2,12 +2,71 @@ # # Installs the Sensu packages # -class sensu::package { +# == Parameters +# +# [*deregister_handler*] +# String. The handler to use when deregistering a client on stop. +# Default: $::sensu::deregister_handler +# +# [*deregister_on_stop*] +# Boolean. Whether the sensu client should deregister from the API on service stop +# Default: $::sensu::deregister_on_stop +# +# [*gem_path*] +# String. Paths to add to GEM_PATH if we need to look for different dirs. +# Default: $::sensu::gem_path +# +# [*init_stop_max_wait*] +# Integer. Number of seconds to wait for the init stop script to run +# Default: $::sensu::init_stop_max_wait +# +# [*log_dir*] +# String. Sensu log directory to be used +# Default: $::sensu::log_dir +# Valid values: Any valid log directory path, accessible by the sensu user +# +# [*log_level*] +# String. Sensu log level to be used +# Default: $::sensu::log_level +# Valid values: debug, info, warn, error, fatal +# +# [*path*] +# String. Used to set PATH in /etc/default/sensu +# Default: $::sensu::path +# +# [*rubyopt*] +# String. Ruby opts to be passed to the sensu services +# Default: $::sensu::rubyopt +# +# [*use_embedded_ruby*] +# Boolean. If the embedded ruby should be used, e.g. to install the +# sensu-plugin gem. This value is overridden by a defined +# sensu_plugin_provider. Note, the embedded ruby should always be used to +# provide full compatibility. Using other ruby runtimes, e.g. the system +# ruby, is not recommended. +# Default: $::sensu::use_embedded_ruby +# Valid values: true, false +# +class sensu::package ( + $deregister_handler = $::sensu::deregister_handler, + $deregister_on_stop = $::sensu::deregister_on_stop, + $gem_path = $::sensu::gem_path, + $init_stop_max_wait = $::sensu::init_stop_max_wait, + $log_dir = $::sensu::log_dir, + $log_level = $::sensu::log_level, + $path = $::sensu::path, + $rubyopt = $::sensu::rubyopt, + $use_embedded_ruby = $::sensu::use_embedded_ruby, +) { if $caller_module_name != $module_name { fail("Use of private class ${name} by ${caller_module_name}") } + if $path != '$PATH' { + validate_absolute_path($path) + } + case $::osfamily { 'Debian': { @@ -49,7 +108,7 @@ # $pkg_version is passed to Package[sensu] { ensure }. The Windows MSI # provider translates hyphens to dots, e.g. '0.29.0-11' maps to # '0.29.0.11' on the system. This mapping is necessary to converge. - $pkg_version = template('sensu/sensu-windows-package-version.erb') + $pkg_version = regsubst($::sensu::version, '-', '.') # The version used to construct the download URL. $pkg_url_version = $::sensu::version ? { 'installed' => 'latest', @@ -69,7 +128,7 @@ # The OS Release specific sub-folder $os_release = $facts['os']['release']['major'] # e.g. '2012 R2' => '2012r2' - $pkg_url_dir = template('sensu/sensu-version.erb') + $pkg_url_dir = regsubst($os_release, '^(\d+)\s*[rR](\d+)', '\\1r\\2') $pkg_arch = $facts['os']['architecture'] $pkg_url = "${sensu::windows_repo_prefix}/${pkg_url_dir}/sensu-${pkg_url_version}-${pkg_arch}.msi" } diff --git a/manifests/plugin.pp b/manifests/plugin.pp index 66cd626447..75676d502f 100644 --- a/manifests/plugin.pp +++ b/manifests/plugin.pp @@ -69,7 +69,7 @@ case $type { 'file': { - $filename = inline_template('<%= scope.lookupvar(\'name\').split(\'/\').last %>') + $filename = basename($name) sensu::plugins_dir { "${name}-${install_path}": path => $install_path, @@ -86,7 +86,7 @@ } } 'url': { - $filename = inline_template('<%= scope.lookupvar(\'name\').split(\'/\').last %>') + $filename = basename($name) sensu::plugins_dir { "${name}-${install_path}": path => $install_path, diff --git a/spec/classes/sensu_init_spec.rb b/spec/classes/sensu_init_spec.rb index bba6aeaec8..2ef883f6ba 100644 --- a/spec/classes/sensu_init_spec.rb +++ b/spec/classes/sensu_init_spec.rb @@ -5,10 +5,17 @@ it 'should compile' do should create_class('sensu') end it { should contain_user('sensu') } + it { should contain_file('/etc/default/sensu').with_content(%r{^EMBEDDED_RUBY=true$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_LEVEL=info$}) } it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_DIR=/var/log/sensu$}) } - - - context 'osfamily windows' do + it { should contain_file('/etc/default/sensu').without_content(%r{^RUBYOPT=.*$}) } + it { should contain_file('/etc/default/sensu').without_content(%r{^GEM_PATH=.*$}) } + it { should contain_file('/etc/default/sensu').without_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER=.*$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^SERVICE_MAX_WAIT="10"$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^PATH=\$PATH$}) } + it { should_not contain_file('C:/opt/sensu/bin/sensu-client.xml') } + + describe 'osfamily windows' do let(:facts) do { :osfamily => 'windows', @@ -21,16 +28,92 @@ }, }, } + end - describe 'with manage_user => true' do - it { should_not contain_user('sensu') } + context 'with default setting' do + content = <<-END.gsub(/^\s+\|/, '') + | + | + | sensu-client + | Sensu Client + | This service runs a Sensu Client + | C:\\opt\\sensu\\embedded\\bin\\ruby + | C:\\opt\\sensu\\embedded\\bin\\sensu-client -d C:\\opt\\sensu\\conf.d -L info + | C:\\opt\\sensu\\ + | + END + + it do + should contain_file('C:/opt/sensu/bin/sensu-client.xml').with({ + 'ensure' => 'file', + 'content' => content, + }) end - describe 'with manage_user => false' do - let(:params) { {:manage_user => false} } - it { should_not contain_user('sensu') } - end + it { should_not contain_user('sensu') } + end + + context 'with log_level => debug' do + let(:params) { {:log_level => 'debug' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(%r{^\s*C:\\opt\\sensu\\embedded\\bin\\sensu-client -d C:\\opt\\sensu\\conf.d -L debug$}) } + end + + context 'with windows_logrotate => true' do + let(:params) { {:windows_logrotate => true } } + content = <<-END.gsub(/^\s+\|/, '') + | + | + | sensu-client + | Sensu Client + | This service runs a Sensu Client + | C:\\opt\\sensu\\embedded\\bin\\ruby + | C:\\opt\\sensu\\embedded\\bin\\sensu-client -d C:\\opt\\sensu\\conf.d -L info + | C:\\opt\\sensu\\ + | + | 10240 + | 10 + | + | + END + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(content) } + end + + # without windows_logrotate => true windows_log_size will be ignored + context 'windows_log_size => 242' do + let(:params) { {:windows_log_size => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').without_content(%r{^\s*.*$}) } end + + context 'windows_logrotate => true & windows_log_size => 242' do + let(:params) { {:windows_logrotate => true, :windows_log_size => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(%r{^\s*242$}) } + end + + # without windows_logrotate => true windows_log_number will be ignored + context 'windows_log_number => 242' do + let(:params) { {:windows_log_number => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').without_content(%r{^\s*.*$}) } + end + + context 'windows_logrotate => true & windows_log_number => 242' do + let(:params) { {:windows_logrotate => true, :windows_log_number => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(%r{^\s*242$}) } + end + + context 'with manage_user => false' do + let(:params) { {:manage_user => false} } + it { should_not contain_user('sensu') } + end + end + + context 'with use_embedded_ruby => false' do + let(:params) { {:use_embedded_ruby => false } } + it { should contain_file('/etc/default/sensu').with_content(%r{^EMBEDDED_RUBY=false$}) } + end + + context 'with log_level => debug' do + let(:params) { {:log_level => 'debug' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_LEVEL=debug$}) } end context 'with log_dir => /var/log/tests' do @@ -38,6 +121,42 @@ it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_DIR=/var/log/tests$}) } end + context 'rubyopt => -rbundler/test' do + let(:params) { {:rubyopt => '-rbundler/test' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^RUBYOPT="-rbundler/test"$}) } + end + + context 'gem_path => /path/to/gems' do + let(:params) { {:gem_path => '/path/to/gems' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^GEM_PATH="/path/to/gems"$}) } + end + + context 'deregister_on_stop => true' do + let(:params) { {:deregister_on_stop => true } } + it { should contain_file('/etc/default/sensu').with_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER=""$}) } + end + + # without deregister_on_stop == true deregister_handler will be ignored + context 'deregister_handler => testing' do + let(:params) { {:deregister_handler => 'testing' } } + it { should contain_file('/etc/default/sensu').without_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER=.*$}) } + end + + context 'deregister_on_stop => true & deregister_handler => testing' do + let(:params) { {:deregister_on_stop => true, :deregister_handler => 'testing' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER="testing"$}) } + end + + context 'init_stop_max_wait => 242' do + let(:params) { {:init_stop_max_wait => 242 } } + it { should contain_file('/etc/default/sensu').with_content(%r{^SERVICE_MAX_WAIT="242"$}) } + end + + context 'path => /spec/tests' do + let(:params) { {:path => '/spec/tests' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^PATH=/spec/tests$}) } + end + context 'with plugins => puppet:///data/sensu/plugins/teststring.rb' do let(:params) { {:plugins => 'puppet:///data/sensu/plugins/teststring.rb' } } it { should contain_sensu__plugin('puppet:///data/sensu/plugins/teststring.rb').with_install_path('/etc/sensu/plugins') } @@ -243,12 +362,30 @@ :invalid => ['./relative/path', %w(array), { 'ha' => 'sh' }, 3, 2.42, true, false, nil], :message => 'is not an absolute path', }, + 'boolean' => { + :name => %w(deregister_on_stop), + :valid => [true, false], + :invalid => ['false', %w(array), { 'ha' => 'sh' }, 3, 2.42, nil], + :message => 'is not a boolean', + }, + 'integer' => { + :name => %w(init_stop_max_wait), + :valid => [3, '242'], + :invalid => ['string', %w(array), { 'ha' => 'sh' }, 2.42, true, nil], + :message => 'must be an integer', + }, 'plugins' => { :name => %w[plugins], :valid => ['/string', %w(/array), { '/hash' => {} }], :invalid => [3, 2.42, true], :message => 'Invalid data type', }, + 'validate_re log_level' => { + :name => %w[log_level], + :valid => %w[debug info warn error fatal], + :invalid => ['string', %w(array), { 'ha' => 'sh' }, 3, 2.42, true, nil], + :message => 'validate_re()', + }, } validations.sort.each do |type, var| diff --git a/spec/classes/sensu_package_spec.rb b/spec/classes/sensu_package_spec.rb index 4354fa542b..b751a171a0 100644 --- a/spec/classes/sensu_package_spec.rb +++ b/spec/classes/sensu_package_spec.rb @@ -11,8 +11,14 @@ it { should create_class('sensu::package') } it { should contain_package('sensu').with_ensure('installed') } it { should contain_file('/etc/default/sensu') } - it { should_not contain_file('/etc/default/sensu').with(:content => /RUBYOPT/) } - it { should_not contain_file('/etc/default/sensu').with(:content => /GEM_PATH/) } + it { should contain_file('/etc/default/sensu').with_content(%r{^EMBEDDED_RUBY=true$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_LEVEL=info$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_DIR=/var/log/sensu$}) } + it { should contain_file('/etc/default/sensu').without_content(%r{^RUBYOPT=.*$}) } + it { should contain_file('/etc/default/sensu').without_content(%r{^GEM_PATH=.*$}) } + it { should contain_file('/etc/default/sensu').without_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER=.*$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^SERVICE_MAX_WAIT="10"$}) } + it { should contain_file('/etc/default/sensu').with_content(%r{^PATH=\$PATH$}) } directories.each do |dir| it { should contain_file(dir).with( :ensure => 'directory', @@ -407,4 +413,89 @@ end end end + + context 'with use_embedded_ruby => false' do + let(:params) { {:use_embedded_ruby => false } } + it { should contain_file('/etc/default/sensu').with_content(%r{^EMBEDDED_RUBY=false$}) } + end + + context 'with log_level => debug' do + let(:params) { {:log_level => 'debug' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_LEVEL=debug$}) } + end + + context 'with log_dir => /var/log/tests' do + let(:params) { {:log_dir => '/var/log/tests' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^LOG_DIR=/var/log/tests$}) } + end + + context 'rubyopt => -rbundler/test' do + let(:params) { {:rubyopt => '-rbundler/test' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^RUBYOPT="-rbundler/test"$}) } + end + + context 'gem_path => /path/to/gems' do + let(:params) { {:gem_path => '/path/to/gems' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^GEM_PATH="/path/to/gems"$}) } + end + + context 'deregister_on_stop => true' do + let(:params) { {:deregister_on_stop => true } } + it { should contain_file('/etc/default/sensu').with_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER=""$}) } + end + + # without deregister_on_stop == true deregister_handler will be ignored + context 'deregister_handler => testing' do + let(:params) { {:deregister_handler => 'testing' } } + it { should contain_file('/etc/default/sensu').without_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER=.*$}) } + end + + context 'deregister_on_stop => true & deregister_handler => testing' do + let(:params) { {:deregister_on_stop => true, :deregister_handler => 'testing' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^CLIENT_DEREGISTER_ON_STOP=true\nCLIENT_DEREGISTER_HANDLER="testing"$}) } + end + + context 'init_stop_max_wait => 242' do + let(:params) { {:init_stop_max_wait => 242 } } + it { should contain_file('/etc/default/sensu').with_content(%r{^SERVICE_MAX_WAIT="242"$}) } + end + + context 'path => /spec/tests' do + let(:params) { {:path => '/spec/tests' } } + it { should contain_file('/etc/default/sensu').with_content(%r{^PATH=/spec/tests$}) } + end + + describe 'variable type and content validations' do + mandatory_params = {} + + validations = { + 'absolute_path' => { + :name => %w[path], + :valid => %w[/absolute/filepath /absolute/directory/], + :invalid => ['./relative/path', %w(array), { 'ha' => 'sh' }, 3, 2.42, true, nil], + :message => 'is not an absolute path', + }, + } + + validations.sort.each do |type, var| + var[:name].each do |var_name| + var[:params] = {} if var[:params].nil? + var[:valid].each do |valid| + context "when #{var_name} (#{type}) is set to valid #{valid} (as #{valid.class})" do + let(:params) { [mandatory_params, var[:params], { :"#{var_name}" => valid, }].reduce(:merge) } + it { should compile } + end + end + + var[:invalid].each do |invalid| + context "when #{var_name} (#{type}) is set to invalid #{invalid} (as #{invalid.class})" do + let(:params) { [mandatory_params, var[:params], { :"#{var_name}" => invalid, }].reduce(:merge) } + it 'should fail' do + expect { should contain_class(subject) }.to raise_error(Puppet::Error, /#{var[:message]}/) + end + end + end + end # var[:name].each + end # validations.sort.each + end # describe 'variable type and content validations' end diff --git a/spec/classes/sensu_service_spec.rb b/spec/classes/sensu_service_spec.rb new file mode 100644 index 0000000000..a0348e8031 --- /dev/null +++ b/spec/classes/sensu_service_spec.rb @@ -0,0 +1,180 @@ +require 'spec_helper' + +describe 'sensu', :type => :class do + context 'config' do + context 'service' do + context 'running on Linux' do + context 'with defaults for all parameters' do + it { should contain_class('sensu::client::service') } + it { should compile.with_all_deps } + + it do + should contain_service('sensu-client').with({ + 'ensure' => 'running', + 'enable' => 'true', + 'hasrestart' => 'true', + 'subscribe' => [ + 'Class[Sensu::Package]', + 'Class[Sensu::Client::Config]', + 'Class[Sensu::Rabbitmq::Config]', + ], + }) + end + + it { should_not contain_file('C:/opt/sensu/bin/sensu-client.xml') } + it { should_not contain_exec('install-sensu-client') } + end + end + + context 'running on Windows' do + let(:facts) do + { + :osfamily => 'windows', + # needed for sensu::package + :os => { + :release => { + :major => '2012 R2', + }, + }, + } + end + + context 'with defaults for all parameters' do + it { should contain_class('sensu::client::service') } + # FIXME: test causes issues in sensu::package + # it { should compile.with_all_deps } + + content = <<-END.gsub(/^\s+\|/, '') + | + | + | sensu-client + | Sensu Client + | This service runs a Sensu Client + | C:\\opt\\sensu\\embedded\\bin\\ruby + | C:\\opt\\sensu\\embedded\\bin\\sensu-client -d C:\\opt\\sensu\\conf.d -L info + | C:\\opt\\sensu\\ + | + END + + it do + should contain_file('C:/opt/sensu/bin/sensu-client.xml').with({ + 'ensure' => 'file', + 'content' => content, + }) + end + + it do + should contain_exec('install-sensu-client').with({ + 'provider' => 'powershell', + 'command' => "New-Service -Name sensu-client -BinaryPathName c:\\opt\\sensu\\bin\\sensu-client.exe -DisplayName 'Sensu Client' -StartupType Automatic", + 'unless' => 'if (Get-Service sensu-client -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }', + 'before' => 'Service[sensu-client]', + 'require' => 'File[C:/opt/sensu/bin/sensu-client.xml]', + }) + end + + it do + should contain_service('sensu-client').with({ + 'ensure' => 'running', + 'enable' => 'true', + 'hasrestart' => 'true', + 'subscribe' => [ + 'Class[Sensu::Package]', + 'Class[Sensu::Client::Config]', + 'Class[Sensu::Rabbitmq::Config]', + ], + }) + end + end + + context 'with log_level => debug' do + let(:params) { {:log_level => 'debug' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(%r{^\s*C:\\opt\\sensu\\embedded\\bin\\sensu-client -d C:\\opt\\sensu\\conf.d -L debug$}) } + end + + context 'with windows_logrotate => true' do + let(:params) { {:windows_logrotate => true } } + content = <<-END.gsub(/^\s+\|/, '') + | + | + | sensu-client + | Sensu Client + | This service runs a Sensu Client + | C:\\opt\\sensu\\embedded\\bin\\ruby + | C:\\opt\\sensu\\embedded\\bin\\sensu-client -d C:\\opt\\sensu\\conf.d -L info + | C:\\opt\\sensu\\ + | + | 10240 + | 10 + | + | + END + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(content) } + end + + # without windows_logrotate => true windows_log_size will be ignored + context 'windows_log_size => 242' do + let(:params) { {:windows_log_size => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').without_content(%r{^\s*.*$}) } + end + + context 'windows_logrotate => true & windows_log_size => 242' do + let(:params) { {:windows_logrotate => true, :windows_log_size => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(%r{^\s*242$}) } + end + + # without windows_logrotate => true windows_log_number will be ignored + context 'windows_log_number => 242' do + let(:params) { {:windows_log_number => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').without_content(%r{^\s*.*$}) } + end + + context 'windows_logrotate => true & windows_log_number => 242' do + let(:params) { {:windows_logrotate => true, :windows_log_number => '242' } } + it { should contain_file('C:/opt/sensu/bin/sensu-client.xml').with_content(%r{^\s*242$}) } + end + end + end + + context 'with hasrestart => false' do + let(:params) { { :hasrestart => false } } + it { should contain_service('sensu-client').with_hasrestart(false) } + end + + + + describe 'variable type and content validations' do + mandatory_params = {} + + validations = { + 'boolean' => { + :name => %w(hasrestart), + :valid => [true, false], + :invalid => ['false', %w(array), { 'ha' => 'sh' }, 3, 2.42, nil], + :message => 'is not a boolean', + }, + } + + validations.sort.each do |type, var| + var[:name].each do |var_name| + var[:params] = {} if var[:params].nil? + var[:valid].each do |valid| + context "when #{var_name} (#{type}) is set to valid #{valid} (as #{valid.class})" do + let(:params) { [mandatory_params, var[:params], { :"#{var_name}" => valid, }].reduce(:merge) } + it { should compile } + end + end + + var[:invalid].each do |invalid| + context "when #{var_name} (#{type}) is set to invalid #{invalid} (as #{invalid.class})" do + let(:params) { [mandatory_params, var[:params], { :"#{var_name}" => invalid, }].reduce(:merge) } + it 'should fail' do + expect { should contain_class(subject) }.to raise_error(Puppet::Error, /#{var[:message]}/) + end + end + end + end # var[:name].each + end # validations.sort.each + end # describe 'variable type and content validations' + end +end diff --git a/templates/sensu-client.erb b/templates/sensu-client.erb index cc80cb8ff7..2035390978 100644 --- a/templates/sensu-client.erb +++ b/templates/sensu-client.erb @@ -4,12 +4,12 @@ Sensu Client This service runs a Sensu Client C:\opt\sensu\embedded\bin\ruby - C:\opt\sensu\embedded\bin\sensu-client -d C:\opt\sensu\conf.d -L <%= scope.lookupvar("sensu::log_level") -%> + C:\opt\sensu\embedded\bin\sensu-client -d C:\opt\sensu\conf.d -L <%= @log_level -%> C:\opt\sensu\ -<% if scope.lookupvar("sensu::windows_logrotate") -%> +<% if @windows_logrotate -%> - <%= scope.lookupvar("sensu::windows_log_size") -%> - <%= scope.lookupvar("sensu::windows_log_number") -%> + <%= @windows_log_size -%> + <%= @windows_log_number -%> <% end -%> diff --git a/templates/sensu-version.erb b/templates/sensu-version.erb deleted file mode 100644 index fe5f3b3f64..0000000000 --- a/templates/sensu-version.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @os_release.sub(/^(\d+)\s*[rR](\d+)/, '\\1r\\2') -%> diff --git a/templates/sensu-windows-package-version.erb b/templates/sensu-windows-package-version.erb deleted file mode 100644 index bb33594b44..0000000000 --- a/templates/sensu-windows-package-version.erb +++ /dev/null @@ -1 +0,0 @@ -<%= scope.lookupvar('sensu::version').gsub('-', '.') -%> diff --git a/templates/sensu.erb b/templates/sensu.erb index 8661c554fc..7402678bc5 100644 --- a/templates/sensu.erb +++ b/templates/sensu.erb @@ -1,16 +1,15 @@ -EMBEDDED_RUBY=<%= scope.lookupvar("sensu::use_embedded_ruby") %> -LOG_LEVEL=<%= scope.lookupvar("sensu::log_level") %> -LOG_DIR=<%= scope.lookupvar("sensu::log_dir") %> -<% if ![:undef, nil].include?(scope.lookupvar("sensu::rubyopt")) -%> -RUBYOPT="<%= scope.lookupvar("sensu::rubyopt") %>" +EMBEDDED_RUBY=<%= @use_embedded_ruby %> +LOG_LEVEL=<%= @log_level %> +LOG_DIR=<%= @log_dir %> +<% if not @rubyopt.nil? -%> +RUBYOPT="<%= @rubyopt %>" <% end -%> -<% if ![:undef, nil].include?(scope.lookupvar("sensu::gem_path")) -%> -GEM_PATH="<%= scope.lookupvar("sensu::gem_path") %>" +<% if not @gem_path.nil? -%> +GEM_PATH="<%= @gem_path %>" <% end -%> -<% if scope.lookupvar("sensu::deregister_on_stop") -%> +<% if @deregister_on_stop -%> CLIENT_DEREGISTER_ON_STOP=true -CLIENT_DEREGISTER_HANDLER="<%= scope.lookupvar("sensu::deregister_handler") %>" +CLIENT_DEREGISTER_HANDLER="<%= @deregister_handler %>" <% end -%> -SERVICE_MAX_WAIT="<%= scope.lookupvar("sensu::init_stop_max_wait") %>" -<% path = scope.lookupvar("sensu::path") -%> -PATH=<%= [:undef, nil].include?(path) ? "$PATH" : path %> +SERVICE_MAX_WAIT="<%= @init_stop_max_wait %>" +PATH=<%= @path %>