Skip to content

Commit

Permalink
Option descriptions (#33)
Browse files Browse the repository at this point in the history
* correct descriptions and crit and warn options

* handle required options

* update changelog

* set critical as default severity

* use integration testing framework
  • Loading branch information
cgeers authored and majormoses committed Mar 29, 2018
1 parent 3e1e8ea commit 22d5673
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ mkmf.log
.DS_Store
.idea/*
*.gem

# test-kitchen
.kitchen/
.kitchen.list.yml
29 changes: 29 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
driver:
name: docker
use_sudo: false

provisioner:
name: shell
data_path: .
script: test/fixtures/bootstrap.sh

verifier:
ruby_bindir: /usr/local/bin

platforms:
- name: debian-8

suites:
- name: ruby-21
driver:
image: ruby:2.1-slim
- name: ruby-22
driver:
image: ruby:2.2-slim
- name: ruby-230
driver:
image: ruby:2.3.0-slim
- name: ruby-241
driver:
image: ruby:2.4.1-slim
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
sudo: true
service: docker
language: ruby
cache:
- bundler
before_install:
- sudo iptables -L DOCKER || ( echo "DOCKER iptables chain missing" ; sudo iptables -N DOCKER )
- gem install bundler -v 1.15
install:
- bundle install
rvm:
Expand All @@ -15,9 +20,10 @@ notifications:
on_success: change
on_failure: always
script:
- bundle exec rake default
- gem build sensu-plugins-logs.gemspec
- gem install sensu-plugins-logs-*.gem
- gem build sensu-plugins-logs.gemspec
- gem install sensu-plugins-logs-*.gem
- bundle exec rake quick
- bundle exec rake kitchen:ruby-`echo $TRAVIS_RUBY_VERSION | sed -e "s/\.//g"`-debian-8
deploy:
provider: rubygems
api_key:
Expand All @@ -26,7 +32,6 @@ deploy:
on:
tags: true
all_branches: true
rvm: 2.0
rvm: 2.1
rvm: 2.2
rvm: 2.3.0
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md)

## [Unreleased]
### Fixed
- check-log.rb: pattern is now required via option config
- check-log.rb: update descriptions of crit and warn options

## [1.3.1] - 2017-10-09
### Fixed
Expand Down
29 changes: 20 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'github/markup'
require 'redcarpet'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'yard'
require 'yard/rake/yardoc_task'
require 'English'
require 'kitchen/rake_tasks'

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

RuboCop::RakeTask.new
Expand All @@ -25,14 +29,21 @@ end

desc 'Test for binstubs'
task :check_binstubs do
bin_list = Gem::Specification.load('sensu-plugins-logs.gemspec').executables
bin_list.each do |b|
`which #{ b }`
unless $CHILD_STATUS.success?
puts "#{b} was not a binstub"
exit
unless Dir.glob('bin/**/*.rb').empty?
bin_list = Gem::Specification.load('sensu-plugins-logs.gemspec').executables
bin_list.each do |b|
`which #{ b }`
unless $CHILD_STATUS.success?
puts "#{b} was not a binstub"
exit
end
end
end
end

task default: [:spec, :make_bin_executable, :yard, :rubocop, :check_binstubs]
Kitchen::RakeTasks.new
desc 'Alias for kitchen:all'
task integration: 'kitchen:all'

task default: %i[spec make_bin_executable yard rubocop check_binstubs integration]
task quick: %i[make_bin_executable yard rubocop check_binstubs]
14 changes: 8 additions & 6 deletions bin/check-log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class CheckLog < Sensu::Plugin::Check::CLI
option :pattern,
description: 'Pattern to search for',
short: '-q PAT',
long: '--pattern PAT'
long: '--pattern PAT',
required: true

# this check currently makes no attempt to try to save you from
# a bad regex such as being unbound. Regexes ending in anything like:
Expand Down Expand Up @@ -85,16 +86,17 @@ class CheckLog < Sensu::Plugin::Check::CLI
long: '--encoding ENCODING-PAGE'

option :warn,
description: 'Warning level if pattern has a group',
description: 'Warning level if pattern is found >= <warn> times',
short: '-w N',
long: '--warn N',
proc: proc(&:to_i)

option :crit,
description: 'Critical level if pattern has a group',
description: 'Critical level if pattern is found >= <crit> times. Default: 1',
short: '-c N',
long: '--crit N',
proc: proc(&:to_i)
proc: proc(&:to_i),
default: 1

option :only_warn,
description: 'Warn instead of critical on match',
Expand Down Expand Up @@ -145,7 +147,7 @@ class CheckLog < Sensu::Plugin::Check::CLI

def run
unknown 'No log file specified' unless config[:log_file] || config[:file_pattern]
unknown 'No pattern specified' unless config[:pattern]
unknown 'No thresholds specified' unless config[:crit] || config[:warn]
file_list = []
file_list << config[:log_file] if config[:log_file]
if config[:file_pattern]
Expand Down Expand Up @@ -243,7 +245,7 @@ def search_log
n_warns = n_matched
end
FileUtils.mkdir_p(File.dirname(@state_file))
File.open(@state_file, File::RDWR | File::TRUNC | File::CREAT, 0644) do |file|
File.open(@state_file, File::RDWR | File::TRUNC | File::CREAT, 0o0644) do |file|
file.flock(File::LOCK_EX) unless Gem.win_platform?
file.write(@bytes_to_skip + bytes_read)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sensu-plugins-logs/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SensuPluginsLogs
module Version
MAJOR = 1
MINOR = 3
PATCH = 1
PATCH = 2

VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
end
Expand Down
20 changes: 13 additions & 7 deletions sensu-plugins-logs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'date'
require_relative 'lib/sensu-plugins-logs'

Gem::Specification.new do |s|
Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength
s.authors = ['Sensu-Plugins and contributors']
# s.cert_chain = ['certs/sensu-plugins.pem']
s.date = Date.today.to_s
Expand All @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
regex matching. Includes various log handlers.'
s.email = '<[email protected]>'
s.executables = Dir.glob('bin/**/*.rb').map { |file| File.basename(file) }
s.files = Dir.glob('{bin,lib}/**/*') + %w(LICENSE README.md CHANGELOG.md)
s.files = Dir.glob('{bin,lib}/**/*') + %w[LICENSE README.md CHANGELOG.md]
s.homepage = 'https://github.com/sensu-plugins/sensu-plugins-logs'
s.license = 'MIT'
s.metadata = { 'maintainer' => 'sensu-plugin',
Expand All @@ -32,13 +32,19 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'sensu-plugin', '~> 1.2'

s.add_development_dependency 'bundler', '~> 1.7'
s.add_development_dependency 'bundler', '~> 1.15'
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
s.add_development_dependency 'github-markup', '~> 1.3'
s.add_development_dependency 'kitchen-docker', '~> 2.6'
s.add_development_dependency 'kitchen-localhost', '~> 0.3'
# locked to keep ruby 2.1 support, this is pulled in by test-kitchen
s.add_development_dependency 'mixlib-shellout', ['< 2.3.0', '~> 2.2']
s.add_development_dependency 'pry', '~> 0.10'
s.add_development_dependency 'rubocop', '~> 0.40.0'
s.add_development_dependency 'rspec', '~> 3.1'
s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'rake', '~> 12.0'
s.add_development_dependency 'redcarpet', '~> 3.2'
s.add_development_dependency 'yard', '~> 0.8'
s.add_development_dependency 'rspec', '~> 3.4'
s.add_development_dependency 'rubocop', '~> 0.49.0'
s.add_development_dependency 'serverspec', '~> 2.36.1'
s.add_development_dependency 'test-kitchen', '~> 1.6'
s.add_development_dependency 'yard', '~> 0.9.11'
end
28 changes: 28 additions & 0 deletions test/fixtures/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
#
# Set up a super simple web server and make it accept GET and POST requests
# for Sensu plugin testing.
#

set -e

# base utilities that need to exist to start bootatraping
apt-get update
apt-get install -y build-essential

# setup the rubies
source /etc/profile
DATA_DIR=/tmp/kitchen/data
RUBY_HOME=${MY_RUBY_HOME}

# Start bootatraping

## install some required deps for pg_gem to install


# End of Actual bootatrap

# Install gems
cd $DATA_DIR
SIGN_GEM=false gem build sensu-plugins-logs.gemspec
gem install sensu-plugins-logs-*.gem
44 changes: 44 additions & 0 deletions test/integration/helpers/serverspec/check-log_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'spec_helper'
require 'shared_spec'

gem_path = '/usr/local/bin'
check_name = 'check-log.rb'
check = "#{gem_path}/#{check_name}"
log_file = "#{File.dirname(__FILE__)}/test.log"

describe 'ruby environment' do
it_behaves_like 'ruby checks', check
end

describe command(check.to_s) do
it 'fails due to missing required parameters' do
expect(subject.exit_status).to eq 2
expect(subject.stdout).to match(/You must supply -q PAT!/)
end
end

describe command("#{check} -q anything -f /dev/null") do
it 'returns ok' do
expect(subject.exit_status).to eq 0
expect(subject.stdout).to match(/CheckLog OK: 0 warnings, 0 criticals for pattern anything./)
end
end

describe command("#{check} -q error -f #{log_file}") do
it 'returns critical' do
expect(subject.exit_status).to eq 2
expect(subject.stdout).to match(/CheckLog CRITICAL: 0 warnings, 1 criticals for pattern error./)
end
end

describe command("#{check} --warn-only --warn 1 -q error -f #{log_file}") do
before do
`rm -rf /var/cache/check-log/`
end
it 'returns warning' do
expect(subject.exit_status).to eq 1
expect(subject.stdout).to match(/CheckLog WARNING: 1 warnings, 0 criticals for pattern error./)
end
end
25 changes: 25 additions & 0 deletions test/integration/helpers/serverspec/shared_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'spec_helper'

shared_examples_for 'ruby checks' do |check|
describe command('which ruby') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/\/usr\/local\/bin\/ruby/) }
end

describe command('which gem') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/\/usr\/local\/bin\/gem/) }
end

describe command("which #{check}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(Regexp.new(Regexp.escape(check))) }
end

describe file(check) do
it { should be_file }
it { should be_executable }
end
end
5 changes: 5 additions & 0 deletions test/integration/helpers/serverspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require 'serverspec'

set :backend, :exec
5 changes: 5 additions & 0 deletions test/integration/helpers/serverspec/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
debug
info
warn
error
fatal
4 changes: 4 additions & 0 deletions test/integration/ruby-21/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'spec_helper'
require 'shared_spec'
4 changes: 4 additions & 0 deletions test/integration/ruby-22/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'spec_helper'
require 'shared_spec'
4 changes: 4 additions & 0 deletions test/integration/ruby-230/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'spec_helper'
require 'shared_spec'
4 changes: 4 additions & 0 deletions test/integration/ruby-241/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'spec_helper'
require 'shared_spec'
2 changes: 0 additions & 2 deletions test/spec_helper.rb

This file was deleted.

0 comments on commit 22d5673

Please sign in to comment.