Skip to content

Commit 47caab7

Browse files
committed
skel for integration testing:
- Framework: `test-kitchen`, `kitchen-docker`, and `serverspec` - Postgres 10 in docker container, currently handled by travis or externally bringing up a docker container with the same command. kitchen will create a link allowing it to connect with its name. At the moment its currently hard coded bad juju so this should be cleaned up later just wanted to get something out. - Basic test for `check-postgres-alive` Signed-off-by: Ben Abrams <me@benabrams.it>
1 parent 9c72709 commit 47caab7

File tree

16 files changed

+183
-9
lines changed

16 files changed

+183
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ mkmf.log
1616
.DS_Store
1717
.idea/*
1818
*.gem
19+
.kitchen/*

.kitchen.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
driver:
3+
name: docker
4+
use_sudo: false
5+
links:
6+
# TODO: multiple versions at some point
7+
- sensu-postgres-10:sensu-postgres-10
8+
9+
provisioner:
10+
name: shell
11+
data_path: .
12+
script: test/fixtures/bootstrap.sh
13+
14+
# verifier:
15+
# ruby_bindir: <%= ENV['MY_RUBY_HOME'] || '/opt/sensu/embedded' %>/bin
16+
verifier:
17+
ruby_bindir: /usr/local/bin
18+
19+
platforms:
20+
- name: debian-8
21+
22+
suites:
23+
- name: ruby-21
24+
driver:
25+
image: ruby:2.1-slim
26+
- name: ruby-22
27+
driver:
28+
image: ruby:2.2-slim
29+
- name: ruby-230
30+
driver:
31+
image: ruby:2.3.0-slim
32+
- name: ruby-241
33+
driver:
34+
image: ruby:2.4.1-slim

.travis.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
sudo: true
2+
service: docker
13
language: ruby
24
cache:
35
- bundler
6+
before_install:
7+
- sudo iptables -L DOCKER || ( echo "DOCKER iptables chain missing" ; sudo iptables -N DOCKER )
8+
- gem install bundler -v 1.15
49
install:
510
- bundle install
611
rvm:
@@ -14,8 +19,12 @@ notifications:
1419
- sensu-plugin@sensu-plugins.io
1520
on_success: change
1621
on_failure: always
22+
before_script:
23+
- docker run --name sensu-postgres-10 -e POSTGRES_PASSWORD='<REDACTED>' -d postgres
24+
1725
script:
18-
- bundle exec rake default
26+
- bundle exec rake quick
27+
- bundle exec rake kitchen:ruby-`echo $TRAVIS_RUBY_VERSION | sed -e "s/\.//g"`-debian-8
1928
- gem build sensu-plugins-postgres.gemspec
2029
- gem install sensu-plugins-postgres-*.gem
2130
deploy:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins
55

66
## [Unreleased]
77

8+
### Added
9+
- basic skel for integration testing with postgres (@majormoses)
10+
- added test for `./bin/check-postgres-alive.rb`
11+
812
## [1.4.4] - 2017-11-08
913
### Fixed
1014
- check-postgres-replication.rb: fix 9.x compatibility

Rakefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ require 'rspec/core/rake_task'
55
require 'rubocop/rake_task'
66
require 'yard'
77
require 'yard/rake/yardoc_task'
8+
require 'English'
9+
require 'kitchen/rake_tasks'
810

911
YARD::Rake::YardocTask.new do |t|
10-
OTHER_PATHS = %w().freeze
12+
OTHER_PATHS = %w[].freeze
1113
t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS]
12-
t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md)
14+
t.options = %w[--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md]
1315
end
1416

1517
RuboCop::RakeTask.new
@@ -35,4 +37,10 @@ task :check_binstubs do
3537
end
3638
end
3739

38-
task default: [:spec, :make_bin_executable, :yard, :rubocop, :check_binstubs]
40+
Kitchen::RakeTasks.new
41+
42+
desc 'Alias for kitchen:all'
43+
task integration: 'kitchen:all'
44+
45+
task default: %i[spec make_bin_executable yard rubocop check_binstubs integration]
46+
task quick: %i[make_bin_executable yard rubocop check_binstubs]

lib/sensu-plugins-postgres/pgpass.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Pgpass
22
def pgpass
33
if File.file?(config[:pgpass])
4-
pgpass = Hash[[:hostname, :port, :database, :user, :password].zip(File.readlines(config[:pgpass])[0].strip.split(':'))]
4+
pgpass = Hash[%i[hostname port database user password].zip(File.readlines(config[:pgpass])[0].strip.split(':'))]
55
pgpass[:database] = nil if pgpass[:database] == '*'
66
pgpass.each do |k, v|
77
config[k] ||= v

sensu-plugins-postgres.gemspec

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44
require 'date'
55
require_relative 'lib/sensu-plugins-postgres'
66

7-
Gem::Specification.new do |s|
7+
Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength
88
s.authors = ['Sensu-Plugins and contributors']
99

1010
s.date = Date.today.to_s
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
1616
more.'
1717
s.email = '<sensu-users@googlegroups.com>'
1818
s.executables = Dir.glob('bin/**/*.rb').map { |file| File.basename(file) }
19-
s.files = Dir.glob('{bin,lib}/**/*') + %w(LICENSE README.md CHANGELOG.md)
19+
s.files = Dir.glob('{bin,lib}/**/*') + %w[LICENSE README.md CHANGELOG.md]
2020
s.homepage = 'https://github.com/sensu-plugins/sensu-plugins-postgres'
2121
s.license = 'MIT'
2222
s.metadata = { 'maintainer' => 'sensu-plugin',
@@ -35,16 +35,23 @@ Gem::Specification.new do |s|
3535
s.version = SensuPluginsPostgres::Version::VER_STRING
3636

3737
s.add_runtime_dependency 'sensu-plugin', '~> 1.2'
38-
s.add_runtime_dependency 'pg', '0.18.3'
38+
3939
s.add_runtime_dependency 'dentaku', '2.0.4'
40+
s.add_runtime_dependency 'pg', '0.18.3'
4041

4142
s.add_development_dependency 'bundler', '~> 1.7'
4243
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
4344
s.add_development_dependency 'github-markup', '~> 1.3'
45+
s.add_development_dependency 'kitchen-docker', '~> 2.6'
46+
s.add_development_dependency 'kitchen-localhost', '~> 0.3'
47+
# locked to keep ruby 2.1 support, this is pulled in by test-kitchen
48+
s.add_development_dependency 'mixlib-shellout', ['< 2.3.0', '~> 2.2']
4449
s.add_development_dependency 'pry', '~> 0.10'
4550
s.add_development_dependency 'rake', '~> 10.0'
4651
s.add_development_dependency 'redcarpet', '~> 3.2'
4752
s.add_development_dependency 'rspec', '~> 3.1'
48-
s.add_development_dependency 'rubocop', '~> 0.40.0'
53+
s.add_development_dependency 'rubocop', '~> 0.49.0'
54+
s.add_development_dependency 'serverspec', '~> 2.36.1'
55+
s.add_development_dependency 'test-kitchen', '~> 1.16.0'
4956
s.add_development_dependency 'yard', '~> 0.8'
5057
end

test/fixtures/bootstrap.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
#
3+
# Set up a super simple web server and make it accept GET and POST requests
4+
# for Sensu plugin testing.
5+
#
6+
7+
set -e
8+
9+
# base utilities that need to exist to start bootatraping
10+
apt-get update
11+
apt-get install -y wget
12+
13+
# setup the rubies
14+
source /etc/profile
15+
DATA_DIR=/tmp/kitchen/data
16+
RUBY_HOME=${MY_RUBY_HOME}
17+
18+
# Start bootatraping
19+
20+
## install some required deps for pg_gem to install
21+
apt-get install -y libpq-dev build-essential
22+
23+
# setup postgres server
24+
## TODO: multiple postgres versions and replication the versions should probably
25+
## be matrixed but wanted to start as small as possible initially.
26+
27+
28+
# End of Actual bootatrap
29+
30+
# Install gems
31+
cd $DATA_DIR
32+
SIGN_GEM=false gem build sensu-plugins-postgres.gemspec
33+
gem install sensu-plugins-postgres-*.gem
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'shared_spec'
5+
6+
gem_path = '/usr/local/bin'
7+
check_name = 'check-postgres-alive.rb'
8+
check = "#{gem_path}/#{check_name}"
9+
pg_user = 'postgres'
10+
pg_password = '<REDACTED>'
11+
# TODO: fix up the hostname to be pulled in via the suite based on the version being tested
12+
pg_host = 'sensu-postgres-10'
13+
14+
describe 'ruby environment' do
15+
it_behaves_like 'ruby checks', check
16+
end
17+
18+
# NOTE: This ia a TERRIBLE idea and you should never specify a cleartext
19+
# password on the cli like this. you should use the pgpass or sensu tokens:
20+
# https://sensuapp.org/docs/1.1/reference/checks.html#check-token-substitution
21+
# do not use this method for anything but testing.
22+
describe command("#{check} --user #{pg_user} --password \'#{pg_password}\' --hostname #{pg_host}") do
23+
its(:exit_status) { should eq 0 }
24+
its(:stdout) { should match(/CheckPostgres OK: Server version: {"version"=>"PostgreSQL/) }
25+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
shared_examples_for 'ruby checks' do |check|
6+
describe command('which ruby') do
7+
its(:exit_status) { should eq 0 }
8+
its(:stdout) { should match(/\/usr\/local\/bin\/ruby/) }
9+
end
10+
11+
describe command('which gem') do
12+
its(:exit_status) { should eq 0 }
13+
its(:stdout) { should match(/\/usr\/local\/bin\/gem/) }
14+
end
15+
16+
describe command("which #{check}") do
17+
its(:exit_status) { should eq 0 }
18+
its(:stdout) { should match(Regexp.new(Regexp.escape(check))) }
19+
end
20+
21+
describe file(check) do
22+
it { should be_file }
23+
it { should be_executable }
24+
end
25+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
require 'serverspec'
4+
5+
set :backend, :exec
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'shared_spec'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'shared_spec'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'shared_spec'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'shared_spec'

test/spec_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
# frozen_string_literal: true
2+
13
require 'codeclimate-test-reporter'
24
CodeClimate::TestReporter.start
5+
6+
RSpec.configure do |c|
7+
c.formatter = :documentation
8+
c.color = true
9+
end

0 commit comments

Comments
 (0)