Skip to content

Commit

Permalink
Tests and fixes for tp::stdmod, tp::test and tp:uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
alvagante committed Aug 28, 2017
1 parent 9fccc0f commit dad9b04
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 20 deletions.
25 changes: 13 additions & 12 deletions manifests/stdmod.pp
Expand Up @@ -80,19 +80,19 @@
}

if $package_ensure == 'absent' {
$manage_service_enable = undef
$manage_service_enable = false
$manage_service_ensure = stopped
$config_dir_ensure = absent
$config_file_ensure = absent
} else {
$manage_service_enable = $service_enable ? {
'' => undef,
'undef' => undef,
undef => true,
default => $service_enable,
}
$manage_service_ensure = $service_ensure ? {
'' => undef,
'undef' => undef,
undef => 'running',
default => $service_ensure,
}
$config_dir_ensure = directory
Expand All @@ -107,22 +107,23 @@


# Resources
if $settings[package_name] {
package { $settings[package_name]:
ensure => $settings[package_ensure],
if $settings['package_name'] {
package { $settings['package_name']:
ensure => $settings['package_ensure'],
}
}

if $settings[service_name] {
service { $settings[service_name]:
ensure => $settings[service_ensure],
enable => $settings[service_enable],
if $settings['service_name'] {
service { $settings['service_name']:
ensure => $manage_service_ensure,
enable => $manage_service_enable,
}
}

if $config_file_source
if $settings['config_file_path'] and
( $config_file_source
or $manage_config_file_content
or $config_file_ensure == 'absent' {
or $config_file_ensure == 'absent' ) {
file { $settings[config_file_path]:
ensure => $config_file_ensure,
path => $settings[config_file_path],
Expand Down
4 changes: 2 additions & 2 deletions manifests/test.pp
Expand Up @@ -53,8 +53,8 @@
file { "${base_dir}/${title}":
ensure => $ensure,
mode => '0755',
owner => root,
group => root,
owner => 'root',
group => 'root',
content => template($template),
tag => 'tp_test',
}
Expand Down
2 changes: 1 addition & 1 deletion manifests/uninstall.pp
Expand Up @@ -45,7 +45,7 @@

# Automatic repo management
if $auto_repo == true
and $settings[repo_url] {
and ( $settings['repo_url'] or $settings['yum_mirrorlist']) {
tp::repo { $title:
enabled => false,
before => Package[$settings[package_name]],
Expand Down
10 changes: 5 additions & 5 deletions spec/defines/tp_install_spec.rb
Expand Up @@ -21,11 +21,11 @@
}

# Resource counts with normal tp::install + package + service
total_count = '3'
package_count = '1'
service_count = '1'
exec_count = '0'
file_count = '0'
total_count = 3
package_count = 1
service_count = 1
exec_count = 0
file_count = 0
has_repo = false

# Define if there's a service to check
Expand Down
57 changes: 57 additions & 0 deletions spec/defines/tp_puppi_spec.no
@@ -0,0 +1,57 @@
require 'spec_helper'

# Apps to test against. Data is in spec/tpdata/
apps = ['rsyslog','openssh','elasticsearch','sysdig','puppet-agent']

describe 'tp::puppi', :type => :define do
on_supported_os(facterversion: '2.4').select { |k, _v| k == 'centos-7-x86_64' || k == 'ubuntu-16.04-x86_64' }.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
apps.each do | app |
appdata=YAML.safe_load(File.read(File.join(File.dirname(__FILE__), "../tpdata/#{os}/#{app}")))

# Default params
default_file_params = {
'ensure' => 'present',
'mode' => '0750',
'owner' => 'root',
'group ' => 'root',
'tag' => 'tp_test',
}

# Resource counts with normal tp::puppi
total_count = 3 # tp::puppi
package_count = 0
service_count = 0
exec_count = 0
file_count = 2

# Define if there's a service to check
if appdata['service_name']
has_service = true
else
has_service = false
end

# Interate contexts over os and over app
context "with app #{app}" do
let(:title) { app }

context 'without any param' do
it { is_expected.to compile }
# it { should have_tp__install_resource_count(1) }
it { should have_file_resource_count(file_count) }
it { should have_resource_count(total_count) }
it { is_expected.to contain_file("/etc/puppi/checks/#{app}").with(default_file_params) }
it { is_expected.to contain_file("/etc/puppi/info/#{app}").with(default_file_params) }
end
context 'with ensure => absent' do
let(:params) { { 'ensure' => 'absent' } }
it { is_expected.to contain_file("/etc/puppi/checks/#{app}").with(default_file_params.merge('ensure' => 'absent')) }
it { is_expected.to contain_file("/etc/puppi/info/#{app}").with(default_file_params.merge('ensure' => 'absent')) }
end
end
end
end
end
end
78 changes: 78 additions & 0 deletions spec/defines/tp_stdmod_spec.rb
@@ -0,0 +1,78 @@
require 'spec_helper'

# Apps to test against. Data is in spec/tpdata/
apps = ['rsyslog','openssh','elasticsearch','sysdig','puppet-agent']

describe 'tp::stdmod', :type => :define do
on_supported_os(facterversion: '2.4').select { |k, _v| k == 'centos-7-x86_64' || k == 'ubuntu-16.04-x86_64' }.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
apps.each do | app |
appdata=YAML.safe_load(File.read(File.join(File.dirname(__FILE__), "../tpdata/#{os}/#{app}")))

# Default params
default_package_params = {
'ensure' => 'present',
}
default_service_params = {
'ensure' => 'running',
'enable' => true,
}
default_file_params = {
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0644',
}

# Resource counts with normal tp::install + package + service
total_count = 1
package_count = 0
service_count = 0
exec_count = 0
file_count = 0
has_repo = false

# Define if there's a service to check
if appdata['service_name']
has_service = true
total_count = total_count.to_i + 1
service_count = service_count.to_i + 1
else
has_service = false
end
# Define if a package is installed
if appdata['package_name']
total_count = total_count.to_i + 1
package_count = package_count.to_i + 1
end

# Interate contexts over os and over app
context "with app #{app}" do
let(:title) { app }

context 'without any param' do
it { is_expected.to compile }
it { should have_tp__stdmod_resource_count(1) }
it { should have_package_resource_count(package_count) }
it { is_expected.to contain_package(appdata['package_name']).only_with(default_package_params) }
if has_service
it { should have_service_resource_count(service_count) }
it { is_expected.to contain_service(appdata['service_name']).only_with(default_service_params) }
end
it { should have_exec_resource_count(exec_count) }
it { should have_file_resource_count(file_count) }
it { should have_resource_count(total_count) }
end
context 'with package_ensure => absent' do
let(:params) { { 'package_ensure' => 'absent' } }
it { is_expected.to contain_package(appdata['package_name']).only_with(default_package_params.merge('ensure' => 'absent')) }
if has_service
it { is_expected.to contain_service(appdata['service_name']).only_with(default_service_params.merge('ensure' => 'stopped', 'enable' => false)) }
end
end
end
end
end
end
end
54 changes: 54 additions & 0 deletions spec/defines/tp_test_spec.rb
@@ -0,0 +1,54 @@
require 'spec_helper'

# Apps to test against. Data is in spec/tpdata/
apps = ['rsyslog','openssh','elasticsearch','sysdig','puppet-agent']

describe 'tp::test', :type => :define do
on_supported_os(facterversion: '2.4').select { |k, _v| k == 'centos-7-x86_64' || k == 'ubuntu-16.04-x86_64' }.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
apps.each do | app |
appdata=YAML.safe_load(File.read(File.join(File.dirname(__FILE__), "../tpdata/#{os}/#{app}")))

# Default params
default_file_params = {
'ensure' => 'present',
'mode' => '0755',
'owner' => 'root',
'group ' => 'root',
# 'content' => template($template),
'tag' => 'tp_test',
}

# Resource counts with normal tp::test
total_count = 2 # tp::test + file
package_count = 0
service_count = 0
exec_count = 0
file_count = 1

# Define if there's a service to check
if appdata['service_name']
has_service = true
else
has_service = false
end

# Interate contexts over os and over app
context "with app #{app}" do
let(:title) { app }

context 'without any param' do
it { is_expected.to compile }
it { should have_file_resource_count(file_count) }
it { should have_resource_count(total_count) }
end
context 'with ensure => absent' do
let(:params) { { 'ensure' => 'absent' } }
it { is_expected.to contain_file("/etc/tp/test/#{app}").with('ensure' => 'absent') }
end
end
end
end
end
end
110 changes: 110 additions & 0 deletions spec/defines/tp_uninstall_spec.rb
@@ -0,0 +1,110 @@
require 'spec_helper'

# Apps to test against. Data is in spec/tpdata/
apps = ['rsyslog','openssh','elasticsearch','sysdig','puppet-agent']

describe 'tp::uninstall', :type => :define do
on_supported_os(facterversion: '2.4').select { |k, _v| k == 'centos-7-x86_64' || k == 'ubuntu-16.04-x86_64' }.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
apps.each do | app |
appdata=YAML.safe_load(File.read(File.join(File.dirname(__FILE__), "../tpdata/#{os}/#{app}")))

# Default params
default_package_params = {
'ensure' => 'absent',
}
default_service_params = {
'ensure' => 'stopped',
'enable' => false,
}

# Resource counts with normal tp::uninstall
total_count = '1' # tp::uninstall
package_count = '0'
service_count = '0'
exec_count = '0'
file_count = '0'
has_repo = false

# Define if there's a service to check
if appdata['service_name']
has_service = true
total_count = total_count.to_i + 1
service_count = service_count.to_i + 1
else
has_service = false
end
# Evaluate packages presence
if appdata['package_name']
total_count = total_count.to_i + 1
package_count = package_count.to_i + 1
end
# Added resources when repos are managed
if appdata['repo_url'] or appdata['yum_mirrorlist']
total_count = total_count.to_i + 1 # tp::repo
end

# Increment counters for resources in tp::repo
if ( appdata['repo_url'] or appdata['yum_mirrorlist'] ) and os == 'centos-7-x86_64'
total_count = total_count.to_i + 1 # yumrepo
end
if appdata['key'] and appdata['key_url'] and appdata['repo_url'] and os == 'ubuntu-16.04-x86_64'
total_count = total_count.to_i + 1 # file $app.list
file_count = file_count.to_i + 1 # file $app.list
end
if appdata['key'] and ( appdata['package_name'] and appdata['package_name'] !=0 ) and os == 'ubuntu-16.04-x86_64'
exec_count = exec_count.to_i + 1 # exec apt-get update
total_count = total_count.to_i + 1 # exec apt-get update
end
if appdata['key'] and appdata['key_url'] and os == 'ubuntu-16.04-x86_64'
exec_count = exec_count.to_i + 1 # exec apt-key add
total_count = total_count.to_i + 1 # exec apt-key add
end
if appdata['key'] and appdata['apt_key_server'] and os == 'ubuntu-16.04-x86_64'
exec_count = exec_count.to_i + 1 # exec apt-key adv --keyserver
total_count = total_count.to_i + 1 # exec apt-key adv --keyserver
end

# Interate contexts over os and over app
context "with app #{app}" do
let(:title) { app }

context 'without any param' do
it { is_expected.to compile }
# it { should have_tp__install_resource_count(1) }
it { should have_package_resource_count(package_count) }
it { is_expected.to contain_package(appdata['package_name']).only_with(default_package_params) }
if has_service
it { should have_service_resource_count(service_count) }
it { is_expected.to contain_service(appdata['service_name']).only_with(default_service_params) }
end
it { should have_exec_resource_count(exec_count) }
it { should have_file_resource_count(file_count) }
it { should have_resource_count(total_count) }
end
context 'with settings_hash => { package_name => custom , service_name => custom }' do
custom_settings = {
'package_name' => 'custom',
'service_name' => 'custom',
}
let(:params) { { 'settings_hash' => custom_settings } }
it { is_expected.to contain_package('custom').only_with(default_package_params) }
if has_service
it { is_expected.to contain_service('custom').only_with(default_service_params) }
end
end
context 'with default settings should auto_repo if repo data is present' do
if has_repo
it { is_expected.to contain_tp__repo(app).with(repo_params) }
end
end
context 'with auto_repo => false' do
let(:params) { { 'auto_repo' => false } }
it { should have_tp__repo_resource_count(0) }
end
end
end
end
end
end

0 comments on commit dad9b04

Please sign in to comment.