How to write Serverspec Checks

Test File Structure

With Rakefile generated by serverspec-init, test files must be placed under the directory which name matches the target host name like this. (You can customize this structure by editing Rakefile and spec_helper.rb as you like.)

spec/target.example.jp/http_spec.rb

Test file structure would be like this.

require 'spec_helper'

describe 'RESOURCE_NAME' do
   #TEST_YOU_WANT_TO_CHECK
end

Serverspec provide many resource types. You can check yourself how to write rspec tests for each resource type as there are many you can test yourself.

bond | bridge | cgroup | command | cron | default_gateway | docker_container | docker_image | file | group | host | iis_app_pool | iis_website | interface | ip6tables | ipfilter | ipnat | iptables | kernel_module | linux_audit_system | linux_kernel_parameter | lxc | mail_alias | mysql_config | package | php_config | port | ppa | process | routing_table | selinux | selinux_module | service | user | x509_certificate | x509_private_key | windows_feature | windows_registry_key | yumrepo | zfs

Example test

For an example, I have write this rspec test to make sure mysql have been setup correctely and some user files and configurations are in place.

These rspec test can even be invoked through SSH on a remote server. On the inital setup of serverspec init we have to select SSH option and setup the remote host as the spec host to be used. for this server configurations have to be set in ~/.ssh/config accordingly.

I have serverspec-init a unix server named us-mc-staging which is in my ssh configuration and run the rake test of sample.rb and the console output  is shown below

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s