cucumber-nagios 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +224 -0
- data/Rakefile +27 -0
- data/bin/cucumber-nagios-gen +50 -0
- data/features/creating.feature +11 -0
- data/features/installing.feature +11 -0
- data/features/steps/creating_steps.rb +23 -0
- data/features/steps/installing_steps.rb +27 -0
- data/features/steps/using_steps.rb +31 -0
- data/features/support/silent_system.rb +4 -0
- data/features/using.feature +21 -0
- data/lib/generators/project/.gitignore +9 -0
- data/lib/generators/project/Gemfile +9 -0
- data/lib/generators/project/README +166 -0
- data/lib/generators/project/bin/cucumber-nagios +31 -0
- data/lib/generators/project/bin/cucumber-nagios-gen +52 -0
- data/lib/generators/project/features/steps/result_steps.rb +19 -0
- data/lib/generators/project/features/steps/webrat_steps.rb +40 -0
- data/lib/generators/project/features/support/env.rb +18 -0
- data/lib/generators/project/features/support/nagios.rb +49 -0
- data/lib/generators/project/lib/generators/feature/%feature_name%.feature +7 -0
- data/lib/generators/project/lib/generators/feature/%feature_name%_steps.rb +1 -0
- metadata +106 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Lindsay Holmwood
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
cucumber-nagios
|
2
|
+
===============
|
3
|
+
|
4
|
+
cucumber-nagios allows you to write high-level behavioural tests of web
|
5
|
+
application, and plug the results into Nagios.
|
6
|
+
|
7
|
+
As Bradley Taylor [put it](http://bradley.is/post/82649218/testing-dash-metrics-with-cucumber):
|
8
|
+
|
9
|
+
“Instead of writing boring monitoring plugins from scratch,
|
10
|
+
you can now do behavior driven ops!
|
11
|
+
|
12
|
+
Transform from a grumpy, misanthropic sysadmin to a hipster,
|
13
|
+
agile developer instantly.”
|
14
|
+
|
15
|
+
|
16
|
+
Quickstart
|
17
|
+
==========
|
18
|
+
|
19
|
+
0. `gem sources -a http://gems.github.com`
|
20
|
+
1. `gem install auxesis-cucumber-nagios`
|
21
|
+
2. `cucumber-nagios-gen project bunch-o-tests`
|
22
|
+
3. `cd bunch-o-tests`
|
23
|
+
4. `rake deps`
|
24
|
+
5. `bin/cucumber-nagios-gen feature ebay.com.au bidding`
|
25
|
+
6. `bin/cucumber-nagios features/ebay.com.au/bidding.feature`
|
26
|
+
|
27
|
+
|
28
|
+
Setting up a project
|
29
|
+
====================
|
30
|
+
|
31
|
+
To set up a standalone `cucumber-nagios` project, run:
|
32
|
+
|
33
|
+
cucumber-nagios-gen project <project-name>
|
34
|
+
|
35
|
+
This will spit out a bunch of files in the directory specified as `<project-name>`.
|
36
|
+
|
37
|
+
Check the `README` within this directory for specific instructions for managing
|
38
|
+
the project.
|
39
|
+
|
40
|
+
|
41
|
+
Freezing
|
42
|
+
========
|
43
|
+
|
44
|
+
** This is really manky at the moment. cucumber-nagios will be switching to
|
45
|
+
wycats' bundler at the next major release! **
|
46
|
+
|
47
|
+
Freezing your dependencies into your project allows you to drop your
|
48
|
+
`cucumber-nagios` project to any machine and have it run. Its only requirement is
|
49
|
+
Ruby and Rake.
|
50
|
+
|
51
|
+
To freeze your project, within your project directory run:
|
52
|
+
|
53
|
+
$ rake deps
|
54
|
+
|
55
|
+
Redeploying
|
56
|
+
===========
|
57
|
+
|
58
|
+
Once you've copied your project around, Just run the freezer again:
|
59
|
+
|
60
|
+
$ rake deps
|
61
|
+
|
62
|
+
Writing features
|
63
|
+
================
|
64
|
+
|
65
|
+
Once you've set up a project, you can use the `bin/cucumber-nagios-gen` command
|
66
|
+
to generate new features. It takes two arguments: the site you're testing, and
|
67
|
+
feature you're testing:
|
68
|
+
|
69
|
+
bin/cucumber-nagios-gen feature gnome.org navigation
|
70
|
+
|
71
|
+
This will spit out two files:
|
72
|
+
|
73
|
+
features/gnome.org/navigation.feature
|
74
|
+
features/gnome.org/steps/navigation_steps.rb
|
75
|
+
|
76
|
+
|
77
|
+
As for writing features, you'll want to have a read of the
|
78
|
+
[Cucumber documentation](http://wiki.github.com/aslakhellesoy/cucumber), however
|
79
|
+
your tests will look something like this:
|
80
|
+
|
81
|
+
Feature: google.com.au
|
82
|
+
It should be up
|
83
|
+
And I should be able to search for things
|
84
|
+
|
85
|
+
Scenario: Searching for things
|
86
|
+
Given I visit "http://www.google.com"
|
87
|
+
When I fill in "q" with "wikipedia"
|
88
|
+
And I press "Google Search"
|
89
|
+
Then I should see "www.wikipedia.org"
|
90
|
+
|
91
|
+
There's a collection of steps that will cover most of the things you'll be
|
92
|
+
testing for in `features/steps/webrat_steps.rb`.
|
93
|
+
|
94
|
+
You can write custom steps for testing specific output and behaviour, e.g.
|
95
|
+
in `features/smh.com.au/smh.feature`:
|
96
|
+
|
97
|
+
Feature: smh.com.au
|
98
|
+
It should be up
|
99
|
+
And provide links to content
|
100
|
+
|
101
|
+
Scenario: Visiting home page
|
102
|
+
When I go to http://smh.com.au/
|
103
|
+
Then I should see site navigation
|
104
|
+
And there should be a section named "Opinion"
|
105
|
+
|
106
|
+
There aren't steps for "Then I should see site navigation", so you have to
|
107
|
+
write one yourself. :-) In `features/smh.com.au/steps/smh_steps.rb`:
|
108
|
+
|
109
|
+
Then /^I should see site navigation$/ do
|
110
|
+
doc = Nokogiri::HTML(response.body.to_s)
|
111
|
+
doc.css("ul#nav li a").size.should > 5
|
112
|
+
end
|
113
|
+
|
114
|
+
You can use Nokogiri for testing responses with XPath matchers and CSS
|
115
|
+
selectors.
|
116
|
+
|
117
|
+
I suggest you use `bin/cucumber` directly so you can get better feedback when
|
118
|
+
writing your tests:
|
119
|
+
|
120
|
+
bin/cucumber --require bin/common.rb \
|
121
|
+
--require features/
|
122
|
+
features/smh/smh.feature
|
123
|
+
|
124
|
+
This will output using the default 'pretty' formatter.
|
125
|
+
|
126
|
+
Running
|
127
|
+
=======
|
128
|
+
|
129
|
+
Invoke the Cucumber feature with the `cucumber-nagios` script:
|
130
|
+
|
131
|
+
bin/cucumber-nagios features/smh.com.au/smh.feature
|
132
|
+
|
133
|
+
`cucumber-nagios` can be run from anywhere:
|
134
|
+
|
135
|
+
/path/to/bin/cucumber-nagios /path/to/features/smh/smh.feature
|
136
|
+
|
137
|
+
It should return a standard Nagios-formatted response string:
|
138
|
+
|
139
|
+
Critical: 0, Warning: 0, 2 okay | passed=2, failed=0, total=2
|
140
|
+
|
141
|
+
Steps that fail will show up in the "Critical" total, and steps that pass
|
142
|
+
show up in the "okay" total.
|
143
|
+
|
144
|
+
The value printed at the end is in Nagios's Performance Data format, so it
|
145
|
+
can be graphed and the like.
|
146
|
+
|
147
|
+
Benchmarking
|
148
|
+
============
|
149
|
+
|
150
|
+
You can benchmark your features if you need to test response times for a set of
|
151
|
+
site interactions:
|
152
|
+
|
153
|
+
Feature: slashdot.com
|
154
|
+
To keep the geek masses satisfied
|
155
|
+
Slashdot must be responsive
|
156
|
+
|
157
|
+
Scenario: Visiting a responsive front page
|
158
|
+
Given I am benchmarking
|
159
|
+
When I go to http://slashdot.org/
|
160
|
+
Then the elapsed time should be less than 5 seconds
|
161
|
+
|
162
|
+
The elapsed time step can be reused multiple times in the same scenario if you
|
163
|
+
need fine grained testing:
|
164
|
+
|
165
|
+
Feature: slashdot.com
|
166
|
+
To keep the geek masses satisfied
|
167
|
+
Slashdot must be responsive
|
168
|
+
|
169
|
+
Scenario: Visiting news articles
|
170
|
+
Given I am benchmarking
|
171
|
+
When I go to http://slashdot.org/
|
172
|
+
Then the elapsed time should be less than 5 seconds
|
173
|
+
When I follow "Login"
|
174
|
+
Then the elapsed time should be less than 4 seconds
|
175
|
+
When I follow "Contact"
|
176
|
+
Then the elapsed time should be less than 7 seconds
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
Quirks & Caveats
|
181
|
+
================
|
182
|
+
|
183
|
+
Multiple scenarios
|
184
|
+
------------------
|
185
|
+
|
186
|
+
You may want to think about keeping to one scenario to a file, otherwise
|
187
|
+
you'll get multiple lines of output for a test:
|
188
|
+
|
189
|
+
Critical: 1, Warning: 0, 2 okay | passed=2, failed=1, total=3
|
190
|
+
Critical: 1, Warning: 0, 4 okay | passed=4, failed=1, total=5
|
191
|
+
|
192
|
+
That said, Nagios should only read the last line, so this might be an ok
|
193
|
+
behaviour when you want to test for an aggregate of failures across a site.
|
194
|
+
|
195
|
+
Failure *is* an option (exceptions are good)
|
196
|
+
--------------------------------------------
|
197
|
+
|
198
|
+
Exceptions raised within your tests will appear in the failed totals, so you
|
199
|
+
don't need to worry about trying to catch them in your own custom steps.
|
200
|
+
|
201
|
+
i.e. if you try fetching a page on a server that is down, or the page returns
|
202
|
+
a 404, the exception raised by Mechanize just gets treated by Cucumber as a
|
203
|
+
test failure.
|
204
|
+
|
205
|
+
|
206
|
+
Version control
|
207
|
+
===============
|
208
|
+
|
209
|
+
It's highly recommend that you store your cucumber-nagios projects in a version
|
210
|
+
control system!
|
211
|
+
|
212
|
+
To get up and running with git:
|
213
|
+
|
214
|
+
$ git init
|
215
|
+
$ git add .
|
216
|
+
$ git commit -m 'created cucumber-nagios project'
|
217
|
+
|
218
|
+
To get up and running with bzr:
|
219
|
+
|
220
|
+
$ bzr init
|
221
|
+
$ bzr add
|
222
|
+
$ bzr commit -m 'created cucumber-nagios project'
|
223
|
+
|
224
|
+
`.bzrignore` and `.gitignores` are created when you generate a project.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'cucumber/rake/task'
|
8
|
+
|
9
|
+
Cucumber::Rake::Task.new do |t|
|
10
|
+
t.cucumber_opts = "--require features/"
|
11
|
+
end
|
12
|
+
rescue LoadError
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
desc "build gem"
|
17
|
+
task :build do
|
18
|
+
system("gem build cucumber-nagios.gemspec")
|
19
|
+
|
20
|
+
FileUtils.mkdir_p('pkg')
|
21
|
+
puts
|
22
|
+
Dir.glob("cucumber-nagios-*.gem").each do |gem|
|
23
|
+
dest = File.join('pkg', gem)
|
24
|
+
FileUtils.mv(gem, dest)
|
25
|
+
puts "New gem in #{dest}"
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'templater'
|
5
|
+
|
6
|
+
module CucumberNagiosGenerators
|
7
|
+
|
8
|
+
extend Templater::Manifold
|
9
|
+
|
10
|
+
class ProjectGenerator < Templater::Generator
|
11
|
+
def self.source_root
|
12
|
+
File.join(File.dirname(__FILE__), '..', 'lib', 'generators', 'project')
|
13
|
+
end
|
14
|
+
|
15
|
+
def destination_root
|
16
|
+
# takes :name from first_argument
|
17
|
+
File.join(@destination_root, name)
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Generate a new self-contained cucumber-nagios project."
|
21
|
+
first_argument :name, :required => true, :desc => "Project name"
|
22
|
+
|
23
|
+
file '.gitignore', '.gitignore'
|
24
|
+
file '.bzrignore', '.bzrignore'
|
25
|
+
glob!
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Generate a cucumber-nagios project."
|
30
|
+
add :project, ProjectGenerator
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
CucumberNagiosGenerators.run_cli Dir.pwd, 'cucumber-nagios-gen', '0.1', ARGV
|
36
|
+
|
37
|
+
puts <<-README
|
38
|
+
Your new cucumber-nagios project is set up.
|
39
|
+
|
40
|
+
To get going, you'll need to bundle gems into the project:
|
41
|
+
|
42
|
+
$ gem bundle
|
43
|
+
|
44
|
+
It's highly recommended you version control your projects:
|
45
|
+
|
46
|
+
- with Git: git init ; git add .
|
47
|
+
- with Bazaar: bzr init ; bzr add
|
48
|
+
|
49
|
+
README
|
50
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Feature: Creating new project
|
2
|
+
To test websites
|
3
|
+
A cucumber-nagios projert
|
4
|
+
Must be created
|
5
|
+
|
6
|
+
Scenario: Create a project
|
7
|
+
Given cucumber-nagios is installed
|
8
|
+
When I create a new project called "great-website-tests"
|
9
|
+
And I freeze in dependencies
|
10
|
+
Then my gems directory should be populated
|
11
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Feature: Installation
|
2
|
+
To set up a cucumber-nagios project
|
3
|
+
A user
|
4
|
+
Must be able to install the gem
|
5
|
+
|
6
|
+
Scenario: Installing the gem
|
7
|
+
When I build the gem
|
8
|
+
And I install the latest gem
|
9
|
+
Then I should have cucumber-nagios-gen on my path
|
10
|
+
And I can generate a new project
|
11
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Given /^cucumber\-nagios is installed$/ do
|
2
|
+
When 'I build the gem'
|
3
|
+
And 'I install the latest gem'
|
4
|
+
Then 'I should have cucumber-nagios-gen on my path'
|
5
|
+
end
|
6
|
+
|
7
|
+
When /^I create a new project called "([^\"]*)"$/ do |project_name|
|
8
|
+
@project_name = project_name
|
9
|
+
FileUtils.rm_rf("/tmp/#{@project_name}")
|
10
|
+
|
11
|
+
silent_system("cd /tmp ; cucumber-nagios-gen project #{@project_name}").should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
When /^I freeze in dependencies$/ do
|
15
|
+
@project_name.should_not be_nil
|
16
|
+
silent_system("cd /tmp/#{@project_name} ; gem bundle").should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
Then /^my gems directory should be populated$/ do
|
20
|
+
@project_name.should_not be_nil
|
21
|
+
Dir.glob("/tmp/#{@project_name}/vendor/gems/*").size.should > 0
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
When /^I build the gem$/ do
|
2
|
+
project_root = File.join(File.dirname(__FILE__), '..', '..')
|
3
|
+
rakefile = File.join(project_root, 'Rakefile')
|
4
|
+
File.exist?(rakefile).should be_true
|
5
|
+
|
6
|
+
silent_system("rake -f #{rakefile} build").should be_true
|
7
|
+
end
|
8
|
+
|
9
|
+
When /^I install the latest gem$/ do
|
10
|
+
project_root = File.join(File.dirname(__FILE__), '..', '..')
|
11
|
+
pkg_dir = File.join(project_root, 'pkg')
|
12
|
+
pkg = File.expand_path(Dir.glob(File.join(pkg_dir, '*.gem')).last)
|
13
|
+
|
14
|
+
silent_system("gem install #{pkg} 2>&1 > /dev/null").should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
Then /^I should have cucumber\-nagios\-gen on my path$/ do
|
18
|
+
silent_system("which cucumber-nagios-gen").should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
Then /^I can generate a new project$/ do
|
22
|
+
testproj = "testproj-#{Time.now.to_i}"
|
23
|
+
FileUtils.rm_rf("/tmp/#{testproj}")
|
24
|
+
|
25
|
+
silent_system("cd /tmp ; cucumber-nagios-gen project #{testproj}").should be_true
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
Given /^a project called "([^\"]*)" is created and frozen$/ do |project_name|
|
2
|
+
@project_name = project_name
|
3
|
+
Given 'cucumber-nagios is installed'
|
4
|
+
When "I create a new project called \"#{@project_name}\""
|
5
|
+
And 'I freeze in dependencies'
|
6
|
+
Then 'my gems directory should be populated'
|
7
|
+
end
|
8
|
+
|
9
|
+
When /^I generate a new feature called "([^\"]*)" for "([^\"]*)"$/ do |feature, site|
|
10
|
+
silent_system("cd /tmp/#{@project_name} ; bin/cucumber-nagios-gen feature #{site} #{feature}")
|
11
|
+
end
|
12
|
+
|
13
|
+
Then /^a feature file should exist for "([^\"]*)" on "([^\"]*)"$/ do |feature, site|
|
14
|
+
File.exists?("/tmp/#{@project_name}/features/#{site}/#{feature}.feature").should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
Then /^the "([^\"]*)" feature on "([^\"]*)" should exit cleanly$/ do |feature, site|
|
18
|
+
silent_system("cd /tmp/#{@project_name} ; bin/cucumber-nagios features/#{site}/#{feature}.feature").should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
Then /^the "([^\"]*)" feature on "([^\"]*)" should not exit cleanly$/ do |feature, site|
|
22
|
+
silent_system("cd /tmp/#{@project_name} ; bin/cucumber-nagios features/#{site}/#{feature}.feature").should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
When /^the "([^\"]*)" feature on "([^\"]*)" checks for something preposterous$/ do |feature, site|
|
26
|
+
file_name = "/tmp/#{@project_name}/features/#{site}/#{feature}.feature"
|
27
|
+
File.open(file_name,'a') do |file|
|
28
|
+
file << " Then I should see \"supercalifragilisticexpialidocious\""
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Feature: Using features
|
2
|
+
To test websites
|
3
|
+
A cucumber feature
|
4
|
+
Must be created
|
5
|
+
|
6
|
+
Scenario: Create a feature
|
7
|
+
Given cucumber-nagios is installed
|
8
|
+
And a project called "more-great-tests" is created and frozen
|
9
|
+
When I generate a new feature called "login" for "github.com"
|
10
|
+
Then a feature file should exist for "login" on "github.com"
|
11
|
+
|
12
|
+
Scenario: Run a successful feature
|
13
|
+
Given a project called "passing-features" is created and frozen
|
14
|
+
When I generate a new feature called "homepage" for "github.com"
|
15
|
+
Then the "homepage" feature on "github.com" should exit cleanly
|
16
|
+
|
17
|
+
Scenario: Run a failing feature
|
18
|
+
Given a project called "failing-features" is created and frozen
|
19
|
+
When I generate a new feature called "profile" for "github.com"
|
20
|
+
And the "profile" feature on "github.com" checks for something preposterous
|
21
|
+
Then the "profile" feature on "github.com" should not exit cleanly
|
@@ -0,0 +1,166 @@
|
|
1
|
+
|
2
|
+
Bundling dependencies
|
3
|
+
=====================
|
4
|
+
|
5
|
+
Bundling cucumber-nagios's dependencies allows you to drop your cucumber-nagios
|
6
|
+
project to any machine and have it run. This can be useful if you want to
|
7
|
+
develop your tests on one machine, and deploy them to another (like a production
|
8
|
+
Nagios server).
|
9
|
+
|
10
|
+
You'll need to bundle your dependencies to use cucumber-nagios.
|
11
|
+
|
12
|
+
First you need to make sure the following dependencies are installed:
|
13
|
+
|
14
|
+
- RubyGems
|
15
|
+
- bundler gem (automatically pulled in by the cucumber-nagios gem)
|
16
|
+
|
17
|
+
To bundle your dependencies, within your project directory run:
|
18
|
+
|
19
|
+
$ gem bundle
|
20
|
+
|
21
|
+
|
22
|
+
Version control
|
23
|
+
===============
|
24
|
+
|
25
|
+
It's strongly recommend that you store your cucumber-nagios projects in a
|
26
|
+
version control system!
|
27
|
+
|
28
|
+
To get up and running with git:
|
29
|
+
|
30
|
+
$ git init
|
31
|
+
$ git add .
|
32
|
+
$ git commit -m 'created cucumber-nagios project'
|
33
|
+
|
34
|
+
To get up and running with bzr:
|
35
|
+
|
36
|
+
$ bzr init
|
37
|
+
$ bzr add
|
38
|
+
$ bzr commit -m 'created cucumber-nagios project'
|
39
|
+
|
40
|
+
.bzrignore and .gitignores are created when you generate a project.
|
41
|
+
|
42
|
+
|
43
|
+
Writing features
|
44
|
+
================
|
45
|
+
|
46
|
+
You can use the bin/cucumber-nagios-gen command to generate new features for
|
47
|
+
you. It takes two arguments: the site you're testing, and feature you're testing:
|
48
|
+
|
49
|
+
bin/cucumber-nagios-gen feature gnome.org navigation
|
50
|
+
|
51
|
+
This will generate two files:
|
52
|
+
|
53
|
+
features/gnome.org/navigation.feature
|
54
|
+
features/gnome.org/steps/navigation_steps.rb
|
55
|
+
|
56
|
+
|
57
|
+
As for writing features, you'll want to have a read of the Cucumber
|
58
|
+
documentation[0], however your tests will look something like this:
|
59
|
+
|
60
|
+
Feature: google.com.au
|
61
|
+
To broaden their knowledge
|
62
|
+
A user should be able
|
63
|
+
To search for things
|
64
|
+
|
65
|
+
Scenario: Searching for things
|
66
|
+
Given I visit "http://www.google.com"
|
67
|
+
When I fill in "q" with "wikipedia"
|
68
|
+
And I press "Google Search"
|
69
|
+
Then I should see "www.wikipedia.org"
|
70
|
+
|
71
|
+
|
72
|
+
There's a collection of steps that will cover most of the things you'll be
|
73
|
+
testing for in features/steps/webrat_steps.rb.
|
74
|
+
|
75
|
+
You can write custom steps for testing specific output and behaviour, e.g.
|
76
|
+
in features/smh.com.au/smh.feature:
|
77
|
+
|
78
|
+
Feature: smh.com.au
|
79
|
+
It should be up
|
80
|
+
And provide links to content
|
81
|
+
|
82
|
+
Scenario: Visiting home page
|
83
|
+
When I go to http://smh.com.au/
|
84
|
+
Then I should see site navigation
|
85
|
+
And there should be a section named "Opinion"
|
86
|
+
|
87
|
+
There aren't steps for "Then I should see site navigation", so you have to
|
88
|
+
write one yourself. :-) In features/smh.com.au/steps/smh_steps.rb:
|
89
|
+
|
90
|
+
Then /^I should see site navigation$/ do
|
91
|
+
doc = Nokogiri::HTML(response.body.to_s)
|
92
|
+
doc.css("ul#nav li a").size.should > 5
|
93
|
+
end
|
94
|
+
|
95
|
+
You can use Nokogiri for testing responses with XPath matchers and CSS
|
96
|
+
selectors.
|
97
|
+
|
98
|
+
I suggest you use bin/cucumber directly so you can get better feedback when
|
99
|
+
writing your tests:
|
100
|
+
|
101
|
+
bin/cucumber --require features/ features/smh/smh.feature
|
102
|
+
|
103
|
+
This will output using the default 'pretty' formatter.
|
104
|
+
|
105
|
+
Running
|
106
|
+
=======
|
107
|
+
|
108
|
+
Invoke the Cucumber feature with the cucumber-nagios script:
|
109
|
+
|
110
|
+
bin/cucumber-nagios features/smh.com.au/smh.feature
|
111
|
+
|
112
|
+
cucumber-nagios can be run from anywhere:
|
113
|
+
|
114
|
+
/path/to/bin/cucumber-nagios /path/to/features/smh/smh.feature
|
115
|
+
|
116
|
+
It should return a standard Nagios-formatted response string:
|
117
|
+
|
118
|
+
Critical: 0, Warning: 0, 2 okay | passed=2, failed=0, total=2
|
119
|
+
|
120
|
+
Steps that fail will show up in the "Critical" total, and steps that pass
|
121
|
+
show up in the "okay" total.
|
122
|
+
|
123
|
+
The value printed at the end is in Nagios's Performance Data format, so it
|
124
|
+
can be graphed and the like.
|
125
|
+
|
126
|
+
|
127
|
+
Quirks & Caveats
|
128
|
+
================
|
129
|
+
|
130
|
+
Multiple scenarios
|
131
|
+
------------------
|
132
|
+
|
133
|
+
You may want to think about keeping to one scenario to a file, otherwise
|
134
|
+
you'll get multiple lines of output for a test:
|
135
|
+
|
136
|
+
Critical: 1, Warning: 0, 2 okay | passed=2, failed=1, total=3
|
137
|
+
Critical: 1, Warning: 0, 4 okay | passed=4, failed=1, total=5
|
138
|
+
|
139
|
+
That said, Nagios should only read the last line, so this might be an ok
|
140
|
+
behaviour when you want to test for an aggregate of failures across a site.
|
141
|
+
|
142
|
+
|
143
|
+
Failure *is* an option (exceptions are good)
|
144
|
+
--------------------------------------------
|
145
|
+
|
146
|
+
Exceptions raised within your tests will appear in the failed totals, so you
|
147
|
+
don't need to worry about trying to catch them in your own custom steps.
|
148
|
+
|
149
|
+
i.e. if you try fetching a page on a server that is down, or the page returns
|
150
|
+
a 404, the exception raised by Mechanize just gets treated by Cucumber as a
|
151
|
+
test failure.
|
152
|
+
|
153
|
+
|
154
|
+
Deploying to production
|
155
|
+
=======================
|
156
|
+
|
157
|
+
Once you've copied your project around, just run the bundler again:
|
158
|
+
|
159
|
+
$ gem bundle
|
160
|
+
|
161
|
+
You'll need to have RubyGems and the bundler gem installed on the system
|
162
|
+
you're deploying too. I know, this is not optimal, but hopefully the bundler
|
163
|
+
gem will handle this better in the future.
|
164
|
+
|
165
|
+
|
166
|
+
[0] http://wiki.github.com/aslakhellesoy/cucumber
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
unless ARGV[0]
|
4
|
+
puts "Usage: #{__FILE__} <feature>"
|
5
|
+
exit 99
|
6
|
+
end
|
7
|
+
|
8
|
+
__DIR__ = File.expand_path(File.dirname(__FILE__))
|
9
|
+
|
10
|
+
feature = ARGV[0]
|
11
|
+
unless File.exists?(feature)
|
12
|
+
feature = File.join(__DIR__, '..', 'features', ARGV[0])
|
13
|
+
end
|
14
|
+
|
15
|
+
unless File.exist?(feature)
|
16
|
+
puts "Error: feature file doesn't exist!"
|
17
|
+
exit 98
|
18
|
+
end
|
19
|
+
|
20
|
+
command = "#{__DIR__}/cucumber"
|
21
|
+
command += " --require features/"
|
22
|
+
command += " --format Cucumber::Formatter::Nagios"
|
23
|
+
command += " #{feature}"
|
24
|
+
|
25
|
+
if ARGV[1] == "--debug"
|
26
|
+
puts command
|
27
|
+
puts
|
28
|
+
end
|
29
|
+
|
30
|
+
system(command) ? exit(0) : exit(2)
|
31
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# attempt to load up the bundled gems, and give an explanatory message if we can't
|
4
|
+
begin
|
5
|
+
require File.join(File.dirname(__FILE__), '..', 'vendor', 'gems', 'environment')
|
6
|
+
rescue LoadError
|
7
|
+
puts "You need to bundle gems into your project before you can run this."
|
8
|
+
puts
|
9
|
+
puts "To do this, make sure the 'bundler' gem is installed, and run:"
|
10
|
+
puts
|
11
|
+
puts " $ gem bundle"
|
12
|
+
puts
|
13
|
+
exit 1
|
14
|
+
end
|
15
|
+
require 'templater'
|
16
|
+
|
17
|
+
module CucumberNagiosGenerators
|
18
|
+
|
19
|
+
extend Templater::Manifold
|
20
|
+
|
21
|
+
# feature generator
|
22
|
+
class FeatureGenerator < Templater::Generator
|
23
|
+
def self.source_root
|
24
|
+
File.join(File.dirname(__FILE__), '..', 'lib', 'generators', 'feature')
|
25
|
+
end
|
26
|
+
|
27
|
+
desc <<-DESC
|
28
|
+
Generate a cucumber feature. Takes a two arguments:
|
29
|
+
bin/cucumber-nagios-gen feature <site-name> <feature-name>
|
30
|
+
DESC
|
31
|
+
|
32
|
+
first_argument :site, :required => true, :desc => "Site name"
|
33
|
+
second_argument :feature, :required => true, :desc => "Feature name"
|
34
|
+
|
35
|
+
template :feature do |template|
|
36
|
+
template.source = "%feature_name%.feature"
|
37
|
+
template.destination = "features/#{site}/#{feature}.feature"
|
38
|
+
end
|
39
|
+
|
40
|
+
template :step do |template|
|
41
|
+
template.source = "%feature_name%_steps.rb"
|
42
|
+
template.destination = "features/#{site}/steps/#{feature}_steps.rb"
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "Generators for a cucumber-nagios project"
|
48
|
+
add :feature, FeatureGenerator
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
CucumberNagiosGenerators.run_cli Dir.pwd, 'cucumber-nagios-gen', '0.5', ARGV
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Then /^I should see "(.*)"$/ do |text|
|
2
|
+
response.body.to_s.should =~ /#{text}/m
|
3
|
+
end
|
4
|
+
|
5
|
+
Then /^I should not see "(.*)"$/ do |text|
|
6
|
+
response.body.to_s.should_not =~ /#{text}/m
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /^I should see an? (\w+) message$/ do |message_type|
|
10
|
+
response.should have_xpath("//*[@class='#{message_type}']")
|
11
|
+
end
|
12
|
+
|
13
|
+
Then /^the (.*) ?request should succeed/ do |_|
|
14
|
+
success_code?.should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
Then /^the (.*) ?request should fail/ do |_|
|
18
|
+
success_code?.should be_false
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
When /^I go to (.*)$/ do |path|
|
2
|
+
visit path
|
3
|
+
end
|
4
|
+
|
5
|
+
When /^I press "(.*)"$/ do |button|
|
6
|
+
click_button(button)
|
7
|
+
end
|
8
|
+
|
9
|
+
When /^I follow "(.*)"$/ do |link|
|
10
|
+
click_link(link)
|
11
|
+
end
|
12
|
+
|
13
|
+
When /^I fill in "(.*)" with "(.*)"$/ do |field, value|
|
14
|
+
fill_in(field, :with => value)
|
15
|
+
end
|
16
|
+
|
17
|
+
When /^I select "(.*)" from "(.*)"$/ do |value, field|
|
18
|
+
select(value, :from => field)
|
19
|
+
end
|
20
|
+
|
21
|
+
When /^I check "(.*)"$/ do |field|
|
22
|
+
check(field)
|
23
|
+
end
|
24
|
+
|
25
|
+
When /^I uncheck "(.*)"$/ do |field|
|
26
|
+
uncheck(field)
|
27
|
+
end
|
28
|
+
|
29
|
+
When /^I choose "(.*)"$/ do |field|
|
30
|
+
choose(field)
|
31
|
+
end
|
32
|
+
|
33
|
+
When /^I submit the form named "(.*)"$/ do |name|
|
34
|
+
submit_form(name)
|
35
|
+
end
|
36
|
+
|
37
|
+
When /^I attach the file at "(.*)" to "(.*)" $/ do |path, field|
|
38
|
+
attach_file(field, path)
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'webrat'
|
5
|
+
require 'webrat/mechanize'
|
6
|
+
|
7
|
+
class ResponseHelper
|
8
|
+
def response
|
9
|
+
webrat_session.response
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
World do
|
14
|
+
ResponseHelper.new
|
15
|
+
Webrat::Session.new(Webrat::MechanizeAdapter.new)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'cucumber/formatter/console'
|
2
|
+
|
3
|
+
module Cucumber
|
4
|
+
module Formatter
|
5
|
+
class Nagios
|
6
|
+
|
7
|
+
def initialize(step_mother, io, options={})
|
8
|
+
@failed = []
|
9
|
+
@passed = []
|
10
|
+
@warning = []
|
11
|
+
@io = io
|
12
|
+
end
|
13
|
+
|
14
|
+
def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
|
15
|
+
case status
|
16
|
+
when :passed
|
17
|
+
@passed << step_match
|
18
|
+
when :failed
|
19
|
+
@failed << step_match
|
20
|
+
when :undefined
|
21
|
+
@warning << step_match
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def after_features(steps)
|
26
|
+
print_summary
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def print_summary
|
31
|
+
@total = @failed.size + @passed.size + @warning.size
|
32
|
+
message = ""
|
33
|
+
message += "Critical: #{@failed.size}, "
|
34
|
+
message += "Warning: #{@warning.size}, "
|
35
|
+
message += "#{@passed.size} okay"
|
36
|
+
# nagios performance data
|
37
|
+
message += " | passed=#{@passed.size}"
|
38
|
+
message += ", failed=#{@failed.size}"
|
39
|
+
message += ", nosteps=#{@warning.size}"
|
40
|
+
message += ", total=#{@total}\n"
|
41
|
+
|
42
|
+
@io.print(message)
|
43
|
+
@io.flush
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
# steps go here!
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cucumber-nagios
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lindsay Holmwood
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-12 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: templater
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.5"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.3
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.6.0
|
44
|
+
version:
|
45
|
+
description: cucumber-nagios lets you write high-level behavioural tests for your web applications that can be plugged into Nagios
|
46
|
+
email: lindsay@holmwood.id.au
|
47
|
+
executables:
|
48
|
+
- cucumber-nagios-gen
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files: []
|
52
|
+
|
53
|
+
files:
|
54
|
+
- bin/cucumber-nagios-gen
|
55
|
+
- lib/generators/project/Gemfile
|
56
|
+
- lib/generators/project/features/steps/result_steps.rb
|
57
|
+
- lib/generators/project/features/steps/webrat_steps.rb
|
58
|
+
- lib/generators/project/features/support/env.rb
|
59
|
+
- lib/generators/project/features/support/nagios.rb
|
60
|
+
- lib/generators/project/bin/cucumber-nagios
|
61
|
+
- lib/generators/project/bin/cucumber-nagios-gen
|
62
|
+
- lib/generators/project/.bzrignore
|
63
|
+
- lib/generators/project/.gitignore
|
64
|
+
- lib/generators/project/README
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/generators/project/lib/generators/feature/%feature_name%.feature
|
69
|
+
- lib/generators/project/lib/generators/feature/%feature_name%_steps.rb
|
70
|
+
- features/support/silent_system.rb
|
71
|
+
- features/using.feature
|
72
|
+
- features/steps/installing_steps.rb
|
73
|
+
- features/steps/using_steps.rb
|
74
|
+
- features/steps/creating_steps.rb
|
75
|
+
- features/creating.feature
|
76
|
+
- features/installing.feature
|
77
|
+
has_rdoc: true
|
78
|
+
homepage: http://auxesis.github.com/cucumber-nagios/
|
79
|
+
licenses: []
|
80
|
+
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: "0"
|
91
|
+
version:
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: "0"
|
97
|
+
version:
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project: cucumber-nagios
|
101
|
+
rubygems_version: 1.3.5
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: web app testing plugin for Nagios using Cucumber/Webrat/Mechanize
|
105
|
+
test_files: []
|
106
|
+
|