Skip to content

Commit

Permalink
Tests for prometheus::daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
sathieu committed Oct 24, 2017
1 parent a7bc980 commit ab6a559
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 0 deletions.
157 changes: 157 additions & 0 deletions spec/defines/daemon_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
require 'spec_helper'

describe 'prometheus::daemon' do
let :title do
'smurf_exporter'
end

on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts
end

let :pre_condition do
'include ::prometheus::params'
end

[
{
version: '1.2.3',
real_download_url: 'https://github.com/prometheus/smurf_exporter/releases/v1.2.3/smurf_exporter-1.2.3.any.tar.gz',
notify_service: 'Service[smurf_exporter]',
user: 'smurf_user',
group: 'smurf_group'
}
].each do |parameters|
context "with parameters #{parameters}" do
let(:params) do
parameters
end

prom_os = facts[:kernel].downcase
prom_arch = facts[:architecture] == 'i386' ? '386' : 'amd64'

it {
is_expected.to contain_archive("/tmp/smurf_exporter-#{parameters[:version]}.tar.gz").with(
'ensure' => 'present',
'extract' => true,
'extract_path' => '/opt',
'source' => params[:real_download_url],
'checksum_verify' => false,
'creates' => "/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter",
'cleanup' => true
).that_comes_before("File[/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter]")
}

it {
is_expected.to contain_file("/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter").with(
'owner' => 'root',
'group' => 0,
'mode' => '0555'
)
}

it {
is_expected.to contain_file('/usr/local/bin/smurf_exporter').with(
'ensure' => 'link',
'target' => "/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter"
).that_notifies('Service[smurf_exporter]')
}

it {
is_expected.to contain_user('smurf_user').with(
'ensure' => 'present',
'system' => true,
'groups' => []
)
}

it {
is_expected.to contain_group('smurf_group').with(
'ensure' => 'present',
'system' => true
)
}

# prometheus::config
if ['debian-7-x86_64'].include?(os)
# init_style = 'debian'

it {
is_expected.to contain_file('/etc/init.d/smurf_exporter').with(
'mode' => '0555',
'owner' => 'root',
'group' => 'root',
'content' => File.read(fixtures('files', 'daemon.debian'))
)
}
elsif ['centos-6-x86_64', 'redhat-6-x86_64'].include?(os)
# init_style = 'sysv'

it {
is_expected.to contain_file('/etc/init.d/smurf_exporter').with(
'mode' => '0555',
'owner' => 'root',
'group' => 'root',
'content' => File.read(fixtures('files', 'daemon.sysv'))
)
}
elsif ['centos-7-x86_64', 'debian-8-x86_64', 'redhat-7-x86_64', 'ubuntu-16.04-x86_64'].include?(os)
# init_style = 'systemd'

it {
is_expected.to contain_file('/etc/systemd/system/smurf_exporter.service').with(
'mode' => '0644',
'owner' => 'root',
'group' => 'root',
'content' => File.read(fixtures('files', 'daemon.systemd'))
).that_notifies('Exec[smurf_exporter-systemd-reload]')
}

it {
is_expected.to contain_exec('smurf_exporter-systemd-reload').with(
'command' => 'systemctl daemon-reload',
'path' => ['/usr/bin', '/bin', '/usr/sbin'],
'refreshonly' => true
).that_notifies('Service[smurf_exporter]')
}
elsif ['ubuntu-14.04-x86_64'].include?(os)
# init_style = 'upstart'

it {
is_expected.to contain_file('/etc/init/smurf_exporter.conf').with(
'mode' => '0444',
'owner' => 'root',
'group' => 'root',
'content' => File.read(fixtures('files', 'daemon.upstart'))
)
}

it {
is_expected.to contain_file('/etc/init.d/smurf_exporter').with(
'ensure' => 'link',
'target' => '/lib/init/upstart-job',
'owner' => 'root',
'group' => 'root',
'mode' => '0755'
)
}
else
it {
is_expected.to raise_error(Puppet::Error, %r{I don.t know how to create an init script for style})
}
end

it {
is_expected.to contain_service('smurf_exporter').with(
'ensure' => 'running',
'name' => 'smurf_exporter',
'enable' => true
)
}
end
end
end
end
end
161 changes: 161 additions & 0 deletions spec/fixtures/files/daemon.debian
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: smurf_exporter
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Prometheus smurf_exporter
# Description: Prometheus smurf_exporter
#
### END INIT INFO

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin
DESC="Prometheus smurf_exporter"
NAME=smurf_exporter
DAEMON=/usr/local/bin/$NAME
PIDFILE=/var/run/$NAME/$NAME.pid
DAEMON_ARGS=''
USER=smurf_user
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function to create run directory
#
mkrundir() {
[ ! -d /var/run/smurf_exporter ] && mkdir -p /var/run/smurf_exporter
chown $USER /var/run/smurf_exporter
}

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
mkrundir
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER --background --make-pidfile --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER --background --make-pidfile -- \
$DAEMON_ARGS \
|| return 2
}


#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}

case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
status)
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac

:
17 changes: 17 additions & 0 deletions spec/fixtures/files/daemon.systemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Prometheus smurf_exporter
Wants=basic.target
After=basic.target network.target

[Service]
User=smurf_user
Group=smurf_group
ExecStart=/usr/local/bin/smurf_exporter

ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
RestartSec=42s

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit ab6a559

Please sign in to comment.