Information Technology Reference
In-Depth Information
tions is enough for test code. This error exception generation process happens automatically
in Serverspec. We only care about the program output being generated on stdout .
The results of running curl localhost:80 are returned to our example code as a string. We
use a feature of Ruby called a regular expression and the match RSpec matcher to search for
content in the output generated by curl . In Ruby, strings containing regular expressions are
enclosed by forward slash characters (//) instead of the usual single quotes ('') or double
quotes (“”).
A regular expression is a special string format that is used to specify a search string. In this
case, we use a regular expression to search for the string eth1 in the program output. This
seems like a reasonable and simple way to check that our website is working. It isn't likely
that the string eth1 would appear in the output otherwise. Using the string eth1 also impli-
citly checks to make sure that the vagrant box had its eth1 adapter enabled, which is another
assumption we'd like to check as well. When there are opportunities to implicitly check more
than one condition in your tests, take the opportunity to do so.
Example 16-7. chefdk/apache-test/test/integration/default/serverspec/default_spec.rb
require 'spec_helper'
describe 'web site' ddo
it 'responds on port 80' ddo
expect ( port 80 ) . to be_listening 'tcp'
end
end
it 'returns eth1 in the HTML body' ddo
expect ( command ( 'curl localhost:80' ) . stdout ) . to match /eth1/
end
end
end
end
There's a great website for learning more about regular expressions in Ruby. You can use
this website to check regular expressions against test strings. Let's use it to check our regular
expression.
First, log in to the node with kitchen login and run the same curl localhost:80 com-
mand that we will run in our test. The output is shown in the following example. Copy and
paste the program output to your clipboard from your terminal window. Then exit back out
to your host prompt:
$ kitchen login
Last login: Sun Aug 17 10:39:34 2014 from 10.0.2.2
Search WWH ::




Custom Search