Skip to content

Commit 0a9f5de

Browse files
committed
Add puppet acceptance testing
1 parent fe07539 commit 0a9f5de

28 files changed

+419
-2
lines changed

.bundle/config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
BUNDLE_JOBS: "4"
2+
BUNDLE_WITH: "development"

.fixtures.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ fixtures:
77
puppet_agent: 'git://github.com/puppetlabs/puppetlabs-puppet_agent.git'
88
symlinks:
99
"randrust": "#{source_dir}"
10-
"my_randrust": "#{source_dir}/spec/fixtures/my_randrust"
10+
"my_randrust": "#{source_dir}/spec/fixtures/my_randrust"

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ Gemfile.lock
22
/target
33
**/*.rs.bk
44
helm/*.tgz
5+
.venv
6+
.vagrant
7+
.vscode
8+
pkg
9+
bin
10+
spec/fixtures

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ group :development do
3333
gem "puppet-module-posix-dev-r#{minor_version}", '~> 0.3', require: false, platforms: [:ruby]
3434
gem "puppet-module-win-default-r#{minor_version}", '~> 0.3', require: false, platforms: [:mswin, :mingw, :x64_mingw]
3535
gem "puppet-module-win-dev-r#{minor_version}", '~> 0.3', require: false, platforms: [:mswin, :mingw, :x64_mingw]
36+
gem 'puppet_litmus', git: 'https://github.com/puppetlabs/puppet_litmus.git'
37+
gem 'serverspec'
3638
end
3739

3840
puppet_version = ENV['PUPPET_GEM_VERSION']

Puppetfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod 'computology-packagecloud', '0.3.2'

check_randrust.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
2+
"""Hello world Nagios check."""
3+
4+
import nagiosplugin
5+
import base64
6+
import argparse
7+
import subprocess
8+
import logging
9+
import urllib.request
10+
import urllib.parse
11+
import time
12+
13+
_log = logging.getLogger()
14+
15+
class Randrust(nagiosplugin.Resource):
16+
"""Domain model: response body.
17+
18+
Determines the round trip time for the and the length of the decoded
19+
base64 string in the body for `url`.
20+
The `url` is a url string of the endpoint to check.
21+
"""
22+
23+
def __init__(self, url):
24+
self.url = url
25+
26+
def request(self, url):
27+
_log.info('making request to %s', self.url)
28+
start = time.time()
29+
req = urllib.request.urlopen(self.url)
30+
body = req.read()
31+
end = time.time()
32+
req.close()
33+
rtime = end - start
34+
_log.info('request took %s', rtime)
35+
return body, rtime
36+
37+
def decoded_length(self, body):
38+
return len(base64.b64decode(body))
39+
40+
def probe(self):
41+
body, rtime = self.request(self.url)
42+
yield nagiosplugin.Metric('rtime', rtime, min=0, context='rtime')
43+
length = self.decoded_length(body)
44+
yield nagiosplugin.Metric('length', length, min=0, context='length')
45+
46+
class ResponseSummary(nagiosplugin.Summary):
47+
"""Status line conveying response time information."""
48+
def ok(self, results):
49+
return 'response time is {}'.format(str(results.metric))
50+
51+
class LengthSummary(nagiosplugin.Summary):
52+
"""Status line conveying decoded base64 length.
53+
54+
The response body is expected to only contain a base64 string which
55+
is checked if the wlength of clength CLI arguments are set.
56+
"""
57+
def ok(self, results):
58+
return 'Decoded returned string {}'.format(str(results.by_name['length']))
59+
60+
61+
@nagiosplugin.guarded
62+
def main():
63+
argp = argparse.ArgumentParser(description=__doc__)
64+
argp.add_argument('-wt', '--twarning', metavar='RANGE', default='0.5',
65+
help='return warning if response time is greater out of RANGE in seconds')
66+
argp.add_argument('-ct', '--tcritical', metavar='RANGE', default='1',
67+
help='return critical if response time is longer out of RANGE in seconds)')
68+
argp.add_argument('-u', '--url', metavar='URL', required=True,
69+
help="URL to check")
70+
argp.add_argument('-wl', '--wlength', metavar='RANGE', default='1:',
71+
help="return warning if decoded string length is outside of RANGE")
72+
argp.add_argument('-cl', '--clength', metavar='RANGE', default='1:',
73+
help="return critical if decoded string length is outside of RANGE")
74+
argp.add_argument('-v', '--verbose', action='count', default=0,
75+
help='increase output verbosity (use up to 3 times)')
76+
args = argp.parse_args()
77+
check = nagiosplugin.Check(
78+
Randrust(args.url),
79+
nagiosplugin.ScalarContext('rtime', args.twarning, args.tcritical),
80+
nagiosplugin.ScalarContext('length', args.wlength, args.clength),
81+
ResponseSummary(),
82+
LengthSummary(),
83+
)
84+
85+
check.main(verbose=args.verbose)
86+
87+
if __name__ == '__main__':
88+
main()

check_randrust.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python
2+

check_user.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!python
2+
"""Nagios plugin to check number of logged in users."""
3+
4+
import argparse
5+
import logging
6+
import nagiosplugin
7+
import subprocess
8+
9+
_log = logging.getLogger('nagiosplugin')
10+
11+
12+
class Users(nagiosplugin.Resource):
13+
"""Domain model: system logins.
14+
15+
The `Users` class is a model of system aspects relevant for this
16+
check. It determines the logged in users and counts them.
17+
"""
18+
19+
who_cmd = 'who'
20+
21+
def __init__(self):
22+
self.users = []
23+
self.unique_users = set()
24+
25+
def list_users(self):
26+
"""Return list of logged in users.
27+
28+
The user list is determined by invoking an external command
29+
defined in `who_cmd` (default: who) and parsing its output. The
30+
command is expected to produce one line per user with the user
31+
name at the beginning.
32+
"""
33+
_log.info('querying users with "%s" command', self.who_cmd)
34+
users = []
35+
try:
36+
p = subprocess.Popen([self.who_cmd], stdout=subprocess.PIPE,
37+
stdin=subprocess.PIPE)
38+
for line in p.communicate()[0].splitlines():
39+
_log.debug('who output: %s', line.strip())
40+
users.append(line.split()[0].decode())
41+
except OSError:
42+
raise nagiosplugin.CheckError(
43+
'cannot determine number of users ({0} failed)'.format(
44+
self.who_cmd))
45+
return users
46+
47+
def probe(self):
48+
"""Create check metric for user counts.
49+
50+
This method returns two metrics: `total` is total number of user
51+
logins including users with multiple logins. `unique` counts
52+
only unique user id. This means that users with multiple logins
53+
are only counted once.
54+
"""
55+
self.users = self.list_users()
56+
self.unique_users = set(self.users)
57+
return [nagiosplugin.Metric('total', len(self.users), min=0),
58+
nagiosplugin.Metric('unique', len(self.unique_users), min=0)]
59+
60+
61+
class UsersSummary(nagiosplugin.Summary):
62+
"""Create status line and long output.
63+
64+
For the status line, the text snippets created by the contexts work
65+
quite well, so leave `ok` and `problem` with their default
66+
implementations. For the long output (-v) we wish to display *which*
67+
users are actually logged in. Note how we use the `resource`
68+
attribute in the resuls object to grab this piece of information
69+
from the domain model object.
70+
"""
71+
72+
def verbose(self, results):
73+
super(UsersSummary, self).verbose(results)
74+
if 'total' in results:
75+
return 'users: ' + ', '.join(results['total'].resource.users)
76+
77+
78+
@nagiosplugin.guarded
79+
def main():
80+
argp = argparse.ArgumentParser()
81+
argp.add_argument('-w', '--warning', metavar='RANGE',
82+
help='warning if total user count is outside RANGE'),
83+
argp.add_argument('-c', '--critical', metavar='RANGE',
84+
help='critical is total user count is outside RANGE')
85+
argp.add_argument('-W', '--warning-unique', metavar='RANGE',
86+
help='warning if unique user count is outside RANGE')
87+
argp.add_argument('-C', '--critical-unique', metavar='RANGE',
88+
help='critical if unique user count is outside RANGE')
89+
argp.add_argument('-v', '--verbose', action='count', default=0,
90+
help='increase output verbosity (use up to 3 times)')
91+
argp.add_argument('-t', '--timeout', default=10,
92+
help='abort execution after TIMEOUT seconds')
93+
args = argp.parse_args()
94+
check = nagiosplugin.Check(
95+
Users(),
96+
nagiosplugin.ScalarContext('total', args.warning, args.critical,
97+
fmt_metric='{value} users logged in'),
98+
nagiosplugin.ScalarContext(
99+
'unique', args.warning_unique, args.critical_unique,
100+
fmt_metric='{value} unique users logged in'),
101+
UsersSummary())
102+
check.main(args.verbose, args.timeout)
103+
104+
105+
if __name__ == '__main__':
106+
main()

docker-compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
image: nginx
1010
ports:
1111
- "443:443"
12+
- "80:80"
1213
environment:
1314
- APPLICATION_URL=http://app:80
1415
volumes:

inventory.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
groups:
3+
- name: docker_nodes
4+
nodes: []
5+
- name: ssh_nodes
6+
nodes:
7+
- name: localhost:2222
8+
config:
9+
transport: ssh
10+
ssh:
11+
user: root
12+
password: root
13+
port: 2222
14+
host-key-check: false
15+
facts:
16+
provisioner: docker
17+
container_name: waffleimage_debian9_-2222
18+
platform: waffleimage/debian9
19+
- name: winrm_nodes
20+
nodes: []

manifests/config.pp

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
content => $config_content,
3131
}
3232

33+
packagecloud::repo { 'jarv/test':
34+
type => 'deb',
35+
}
36+
3337
# if $randrust::logfile {
3438
# file { $randrust::logfile:
3539
# ensure => file,

manifests/install.pp

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
) {
2222
if $package_manage {
2323

24+
packagecloud::repo { 'jarv/test':
25+
type => 'deb',
26+
}
27+
2428
package { $package_name:
2529
ensure => $package_ensure,
2630
}

nginx.conf

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ events {
44

55
http {
66
server {
7+
listen 80;
78
listen 443 ssl http2;
89
server_name localhost;
910

provision.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
default:
3+
provisioner: docker
4+
images: ['waffleimage/debian9']
5+
travis_deb:
6+
provisioner: docker
7+
images: ['debian:8', 'debian:9', 'ubuntu:14.04', 'ubuntu:16.04', 'ubuntu:18.04']
8+
waffle_deb:
9+
provisioner: docker
10+
images: ['waffleimage/debian8', 'waffleimage/debian9', 'waffleimage/ubuntu14.04', 'waffleimage/ubuntu16.04', 'waffleimage/ubuntu18.04']
11+
travis_el:
12+
provisioner: docker
13+
images: ['centos:6', 'centos:7', 'oraclelinux:6', 'oraclelinux:7', 'scientificlinux/sl:6', 'scientificlinux/sl:7']
14+
waffle_el:
15+
provisioner: docker
16+
images: ['waffleimage/centos6', 'waffleimage/centos7', 'waffleimage/oraclelinux6', 'waffleimage/oraclelinux7', 'waffleimage/scientificlinux6', 'waffleimage/scientificlinux7']
17+
release_checks:
18+
provisioner: vmpooler
19+
images: ['redhat-5-x86_64', 'redhat-6-x86_64', 'redhat-7-x86_64', 'redhat-8-x86_64', 'centos-5-x86_64', 'centos-6-x86_64', 'centos-7-x86_64', 'oracle-5-x86_64', 'oracle-6-x86_64', 'oracle-7-x86_64', 'scientific-6-x86_64', 'scientific-7-x86_64', 'debian-8-x86_64', 'debian-9-x86_64', 'sles-11-x86_64', 'sles-12-x86_64', 'sles-15-x86_64', 'ubuntu-1404-x86_64', 'ubuntu-1604-x86_64', 'ubuntu-1804-x86_64', 'win-2008r2-x86_64', 'win-2012r2-x86_64', 'win-2016-x86_64', 'win-10-pro-x86_64']
20+
vagrant:
21+
provisioner: vagrant
22+
images: ['centos/7', 'generic/ubuntu1804']
23+
params:
24+
hyperv_smb_username: someone
25+
hyperv_smb_password: something

requirements.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
astroid==2.3.0
2+
isort==4.3.21
3+
lazy-object-proxy==1.4.2
4+
mccabe==0.6.1
5+
nagiosplugin==1.2.4
6+
pylint==2.4.0
7+
six==1.12.0
8+
typed-ast==1.4.0
9+
wrapt==1.11.2

scripts/litmus.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
#pdk bundle exec rake 'litmus:provision_list[default]'
5+
pdk bundle exec bolt command run 'apt install wget -y' --inventoryfile inventory.yaml --targets ssh_nodes
6+
pdk bundle exec rake 'litmus:install_agent[puppet5]'
7+
pdk bundle exec rake litmus:install_module
8+
pdk bundle exec rake litmus:acceptance:parallel
9+

spec/acceptance/file_line_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'builds' do
4+
it 'does an end-to-end thing' do
5+
pp = <<-EOF
6+
require randrust
7+
EOF
8+
9+
apply_manifest(pp, :catch_failures => true)
10+
apply_manifest(pp, :catch_changes => true)
11+
12+
end
13+
14+
describe file("/etc/default/randrust") do
15+
it { is_expected.to contain "INTERFACE=0.0.0.0" }
16+
end
17+
end

spec/acceptance/test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'concat noop parameter', if: ['debian', 'redhat', 'ubuntu'].include?(os[:family]) do
4+
before(:all) do
5+
@basedir = setup_test_directory
6+
end
7+
describe 'with "/usr/bin/test -e %"' do
8+
let(:pp) do
9+
<<-MANIFEST
10+
concat_file { '#{@basedir}/file':
11+
noop => false,
12+
}
13+
concat_fragment { 'content':
14+
target => '#{@basedir}/file',
15+
content => 'content',
16+
}
17+
MANIFEST
18+
end
19+
20+
it 'applies the manifest twice with no stderr' do
21+
idempotent_apply(pp)
22+
expect(file("#{@basedir}/file")).to be_file
23+
expect(file("#{@basedir}/file").content).to contain 'content'
24+
end
25+
end
26+
end

spec/fixtures/manifests/site.pp

Whitespace-only changes.

spec/fixtures/modules/augeas_core

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 27bc5228df89cb7a3c7d3b45324ddb38c8e12063

spec/fixtures/modules/facts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 5422db897b17282289dbc6afd70d8065599e1ba8

spec/fixtures/modules/my_randrust

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/jarv/Documents/Code/randrust/spec/fixtures/my_randrust

spec/fixtures/modules/provision

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b7c622b81465ca11fd27d1893835afdeebc75b14

spec/fixtures/modules/puppet_agent

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 3aef2ffbb731cdec1baaa50d0eb48561257171e3

spec/fixtures/modules/randrust

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/jarv/Documents/Code/randrust

spec/fixtures/modules/stdlib

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit f7c9cd96f381f83669b8f0b6d8070dbb8348766c

0 commit comments

Comments
 (0)