Commit 0aed640e authored by Nathan Ward's avatar Nathan Ward
Browse files

Rework rspec tests quite a lot for the classes and facts.

There is still a lot more to do here to get full coverage, and a lot of work to figure out testing across multiple OSes etc. however this is a solid start.
parent 72386004
fixtures:
symlinks:
freeradius: "#{source_dir}"
repositories:
stdlib:
repo: https://github.com/puppetlabs/puppetlabs-stdlib.git
ref: 4.3.2
forge_modules:
forge_modules:
concat: "puppetlabs/concat"
firewall: "puppetlabs/firewall"
logrotate: "puppet/logrotate"
rsyslog: "saz/rsyslog"
stdlib: "puppetlabs/stdlib"
# Grab the FreeRADIUS version from the output of radiusd -v
# Set path to binary for our platform
dist = Facter.value(:osfamily)
case dist
when /RedHat/
binary = 'radiusd'
when /Debian/
binary = 'freeradius'
else
binary = 'radiusd'
end
module Facter::Util::FreeradiusVersion
class << self
def version_string
# Set path to binary for our platform
dist = Facter.value(:osfamily)
case dist
when /RedHat/
binary = 'radiusd'
when /Debian/
binary = 'freeradius'
else
binary = 'radiusd'
end
# Execute call to fetch version info
version = Facter::Core::Execution.exec("#{binary} -v")
Facter::Core::Execution.exec("#{binary} -v")
end
end
end
# Extract full version number
Facter.add(:freeradius_version) do
setcode do
if !version.nil?
minver = version.split(/\n/)[0].match(/FreeRADIUS Version (\d+\.\d+\.\d+)/)[1].to_s
version_string = Facter::Util::FreeradiusVersion.version_string
if !version_string.nil?
version = version_string.split(/\n/)[0].match(/FreeRADIUS Version (\d+\.\d+\.\d+)/)[1].to_s
end
minver
version
end
end
# Extract major version number
Facter.add(:freeradius_maj_version) do
setcode do
if !version.nil?
majver = version.split(/\n/)[0].match(/FreeRADIUS Version (\d+)\.\d+\.\d+/)[1].to_s
version_string = Facter::Util::FreeradiusVersion.version_string
if !version_string.nil?
majver = version_string.split(/\n/)[0].match(/FreeRADIUS Version (\d+)\.\d+\.\d+/)[1].to_s
end
majver
end
......
......@@ -63,6 +63,8 @@ class freeradius (
"${freeradius::fr_basepath}/mods-config/attr_filter",
"${freeradius::fr_basepath}/mods-config/preprocess",
"${freeradius::fr_basepath}/mods-config/sql",
"${freeradius::fr_basepath}/sites-available",
"${freeradius::fr_basepath}/mods-available",
]:
ensure => directory,
mode => '0755',
......@@ -138,12 +140,12 @@ class freeradius (
}
concat::fragment { 'policy_header':
target => "${freeradius::fr_basepath}/policy.conf",
content => "policy {\n",
content => 'policy {',
order => 10,
}
concat::fragment { 'policy_footer':
target => "${freeradius::fr_basepath}/policy.conf",
content => "}\n",
content => '}',
order => '99',
}
......@@ -162,7 +164,7 @@ class freeradius (
}
concat::fragment { 'template_footer':
target => "${freeradius::fr_basepath}/templates.conf",
content => "}\n",
content => '}',
order => '95',
}
......@@ -177,7 +179,7 @@ class freeradius (
}
concat::fragment { 'proxy_header':
target => "${freeradius::fr_basepath}/proxy.conf",
content => "# Proxy config\n\n",
content => '# Proxy config\n',
order => '05',
}
......@@ -341,7 +343,7 @@ class freeradius (
# Syslog rules
if $syslog == true {
rsyslog::snippet { '12-radiusd-log':
content => "if \$programname == \'radiusd\' then ${freeradius::fr_logpath}/radius.log\n&~",
content => "if \$programname == \'radiusd\' then ${freeradius::fr_logpath}/radius.log\n\&\~",
}
}
......@@ -434,7 +436,7 @@ class freeradius (
"${freeradius::fr_basepath}/clients.conf",
"${freeradius::fr_basepath}/sql.conf",
]:
content => "# FILE INTENTIONALLY BLANK\n",
content => '# FILE INTENTIONALLY BLANK\n',
mode => '0644',
owner => 'root',
group => $freeradius::fr_group,
......
require 'spec_helper'
require 'shared_contexts'
describe 'freeradius::control_socket' do
# by default the hiera integration uses hiera data from the shared_contexts.rb file
# but basically to mock hiera you first need to add a key/value pair
# to the specific context in the spec/shared_contexts.rb file
# Note: you can only use a single hiera context per describe/context block
# rspec-puppet does not allow you to swap out hiera data on a per test block
#include_context :hiera
on_supported_os.each do |os, os_facts|
context "on #{os}" do
include_context 'redhat_params'
include_context 'freeradius_default'
# below is the facts hash that gives you the ability to mock
# facts on a per describe/context block. If you use a fact in your
# manifest you should mock the facts below.
let(:facts) do
{}
end
# below is a list of the resource parameters that you can override.
# By default all non-required parameters are commented out,
# while all required parameters will require you to add a value
let(:params) do
{
#:mode => "ro",
}
end
# add these two lines in a single test block to enable puppet and hiera debug mode
# Puppet::Util::Log.level = :debug
# Puppet::Util::Log.newdestination(:console)
it do
is_expected.to contain_freeradius__site('control-socket')
.with(
'content' => 'template(freeradius/sites-enabled/control-socket.erb)'
)
let(:facts) { os_facts }
# Empty params hash by default so we can super().merge
let(:params) { {} }
describe 'freeradius::control_socket' do
it do
is_expected.to contain_freeradius__site('control-socket')
end
end
end
end
end
This diff is collapsed.
require 'spec_helper'
describe 'freeradius' do
context 'with defaults for all parameters' do
it { should contain_class('freeradius') }
end
end
require 'spec_helper'
require 'shared_contexts'
describe 'freeradius::params' do
# by default the hiera integration uses hiera data from the shared_contexts.rb file
# but basically to mock hiera you first need to add a key/value pair
# to the specific context in the spec/shared_contexts.rb file
# Note: you can only use a single hiera context per describe/context block
# rspec-puppet does not allow you to swap out hiera data on a per test block
#include_context :hiera
# below is the facts hash that gives you the ability to mock
# facts on a per describe/context block. If you use a fact in your
# manifest you should mock the facts below.
let(:facts) do
{}
end
# below is a list of the resource parameters that you can override.
# By default all non-required parameters are commented out,
# while all required parameters will require you to add a value
let(:params) do
{
}
end
# add these two lines in a single test block to enable puppet and hiera debug mode
# Puppet::Util::Log.level = :debug
# Puppet::Util::Log.newdestination(:console)
end
require 'spec_helper'
require 'shared_contexts'
describe 'freeradius::status_server' do
# by default the hiera integration uses hiera data from the shared_contexts.rb file
# but basically to mock hiera you first need to add a key/value pair
# to the specific context in the spec/shared_contexts.rb file
# Note: you can only use a single hiera context per describe/context block
# rspec-puppet does not allow you to swap out hiera data on a per test block
#include_context :hiera
on_supported_os.each do |os, os_facts|
context "on #{os}" do
include_context 'redhat_params'
include_context 'freeradius_default'
# below is the facts hash that gives you the ability to mock
# facts on a per describe/context block. If you use a fact in your
# manifest you should mock the facts below.
let(:facts) do
{}
end
# below is a list of the resource parameters that you can override.
# By default all non-required parameters are commented out,
# while all required parameters will require you to add a value
let(:params) do
{
:secret => 'place_value_here',
#:port => "18121",
#:listen => "*",
}
end
# add these two lines in a single test block to enable puppet and hiera debug mode
# Puppet::Util::Log.level = :debug
# Puppet::Util::Log.newdestination(:console)
it do
is_expected.to contain_freeradius__site('status')
.with(
'content' => 'template(freeradius/sites-enabled/status.erb)'
)
let(:facts) { os_facts }
# Empty params hash by default so we can super().merge
let(:params) { {} }
describe 'freeradius::status_server' do
it do
is_expected.to contain_freeradius__site('status')
end
end
end
end
end
# optional, this should be the path to where the hiera data config file is in this repo
# You must update this if your actual hiera data lives inside your module.
# I only assume you have a separate repository for hieradata and its include in your .fixtures
hiera_config_file = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures','modules','hieradata', 'hiera.yaml'))
# hiera_config and hiera_data are mutually exclusive contexts.
shared_context :global_hiera_data do
let(:hiera_data) do
{
#"freeradius::control_socket" => '',
#"freeradius::control_socket::mode" => '',
#"freeradius::ldap_support" => '',
#"freeradius::krb5_support" => '',
#"freeradius::max_requests" => '',
#"freeradius::max_request_time" => '',
#"freeradius::max_servers" => '',
#"freeradius::mysql_support" => '',
#"freeradius::perl_support" => '',
#"freeradius::status_server::listen" => '',
#"freeradius::status_server::port" => '',
#"freeradius::status_server::secret" => '',
#"freeradius::syslog" => '',
#"freeradius::utils_support" => '',
#"freeradius::winbind_support" => '',
#"freeradius::wpa_supplicant" => '',
}
end
end
shared_context :hiera do
# example only,
let(:hiera_data) do
{:some_key => "some_value" }
end
end
shared_context :linux_hiera do
# example only,
let(:hiera_data) do
{:some_key => "some_value" }
end
end
# In case you want a more specific set of mocked hiera data for windows
shared_context :windows_hiera do
# example only,
let(:hiera_data) do
{:some_key => "some_value" }
end
end
# you cannot use this in addition to any of the hiera_data contexts above
shared_context :real_hiera_data do
let(:hiera_config) do
hiera_config_file
end
end
RSpec.configure do |c|
c.after(:suite) do
RSpec::Puppet::Coverage.report!
end
end
# Set up freeradius::params with the redhat values, so we have something
# to test for in the freeradius spec without defining separate tests for
# every OS
shared_context 'redhat_params' do
let(:pre_condition) do
[
'class freeradius::params {
$fr_basepath = "/etc/raddb"
$fr_configdir = "mods-config"
$fr_db_dir = "\${localstatedir}/lib/radiusd"
$fr_group = "radiusd"
$fr_libdir = "/usr/lib64/freeradius"
$fr_logpath = "/var/log/radius"
$fr_moduleconfigpath = "/etc/raddb/mods-config"
$fr_moduledir = "mods-enabled"
$fr_modulepath = "/etc/raddb/mods-enabled"
$fr_package = "freeradius"
$fr_pidfile = "/var/run/radiusd/radiusd.pid"
$fr_raddbdir = "\${sysconfdir}/raddb"
$fr_service = "radiusd"
$fr_service_has_status = true
$fr_user = "radiusd"
$fr_version = "3"
$fr_wbpriv_user = "wbpriv"
$fr_wpa_supplicant = "wpa_supplicant"
$radacctdir = "\${logdir}/radacct"
}'
]
end
end
# Set up a default freeradius instance, so we can test other classes which
# require freeradius to exist first
shared_context 'freeradius_default' do
let(:pre_condition) do
[
'class { freeradius: }'
]
end
end
require 'spec_helper'
require 'facter'
describe :freeradius_maj_version, :type => :fact do
before :all do
# perform any action that should be run for the entire test suite
end
before :each do
# perform any action that should be run before every test
Facter.clear
# This will mock the facts that confine uses to limit facts running under certain conditions
# below is how you mock responses from the command line
# you will need to built tests that plugin different mocked values in order to fully test your facter code
end
it 'should return a value' do
expect(Facter.fact(:freeradius_maj_version).value).to eq('value123') #<-- change the value to match your expectation
end
end
require 'spec_helper'
require 'facter'
require 'facter/freeradius_version'
describe :freeradius_version, :type => :fact do
before :all do
# perform any action that should be run for the entire test suite
end
before :each do
# perform any action that should be run before every test
Facter.clear
# This will mock the facts that confine uses to limit facts running under certain conditions
# below is how you mock responses from the command line
# you will need to built tests that plugin different mocked values in order to fully test your facter code
expect(Facter::Core::Execution).to receive(:exec).with('radiusd -v').and_return('FreeRADIUS Version 3.0.21')
end
it 'should set freeradius_version' do
expect(Facter.fact(:freeradius_version).value).to eq('3.0.21')
end
it 'should return a value' do
expect(Facter.fact(:freeradius_version).value).to eq('value123') #<-- change the value to match your expectation
it 'should set freeradius_maj_version' do
expect(Facter.fact(:freeradius_maj_version).value).to eq('3')
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment