Recommendations for implementing test automation Serverspec


This is the Tatsuno Information Systems Tokyo Team!
Suddenly, how do you check the configuration when you build a server?
At Tatsuno System, we used [Serverspec], which is an open source that uses ruby, to test the configuration of each server.
Since it is possible to compare the output by command or to set which rule to check by application, it is highly versatile and we are introducing it.

When the number of machines increases, it takes a lot of time to check them manually, because the value of config is changed according to the scale of the customer's environment, or the running middleware and version are different.

Serverspec makes it easy to compare configs and check the installation and startup status of middleware.
If the test target is patterned to some extent, it is possible to shorten the time to check multiple units by using the test configuration file.
This time, for those who do not know serverspec yet, I will introduce what kind of tool it is.

Click here for table of contents

  1. Supported OS
  2. Items that can be checked
  3. How to install
  4. summary


Supported OS

In a word, various OS are currently used for servers.
The actual available operating systems are as follows.

* Arch Linux
* Darwin(Mac OS X)
* Debian
* Fedora/Red Hat/CentOS
* FreeBSD
* Gentoo Linux
* NixOS
* OpenBSD
* openSUSE
* Plamo Linux
* SmartOS
* Solaris
* Ubuntu
* Windows

Commonly used Linux operating systems, such as RedHat and Ubuntu, and Windows are also supported, so you can use
It is available for a variety of operating systems.

Items that can be checked

The serverspec formulaYou can see the list of testable items from
It is possible to check various things, but here are some of the items we checked in our test.


-be_file Checks for the presence of a file
-be_owned_by Checks for file owner permissions
-be_grouped_into Check the group in the file
-be_mode Check the mode of the file
-Check if there is a specified description in the content file


-be_enabled,be_running Is the service running or is it automatically started?
-be_listening Check that the port you want is in the listening state.


-be_installed Verify that the package is installed
-be_installed.with_version Check the version of the package


-have_entry Whether the target cron is registered.

Also, you can check the match between the return value of the command and the specified value, so you can use
It can be applied in various ways from the way it is confirmed by the command.


Checking the #timestamp

describe command("date") do
its(:stdout) { should match /JST/ }

Check the #swap file.

describe command("free") do
its(:stdout) { should match /Swap:/ }


How to install

This section will show you how to install serverspec for the first time.
Please install it in the environment where ruby is installed.

-ruby library installation

$ bundle
$ gem install serverspec

Serverspec initial setup

We are assuming a Unix-based environment with SSH access.

$ serverspec-init
Select OS type:

1) UN*X
2) Windows

Select number: 1

Select a backend type:

1) SSH
2) Exec (local)

Select number: 1

Vagrant instance y/n: n
Input target host name:
+ spec/
+ spec/
+ spec/
+ spec/spec_helper.rb
+ Rakefile
+ .rspec

A sample test file is generated in the following directory.
The sample test contents are
Apache is installed, running, auto-starting, and listening on port 80.
The following is a brief description of the program.

The configuration file is the following file.

How to actually run the tests on the server

% rake spec

In the initial sample file, the test is for a web server using Apache, and the
This is not a test for servers that do not use Apache.
Refer to the official documentation to find out what packages are installed on your server.
Please rewrite the sample_spec.rb and test it.


In this introduction, we have performed a simple test using a sample, but the
There are separate settings for each server and various other tests that I could not introduce.
If you are interested in trying it out, please try various ways to see if you can use it in your own environment.
Many people are using it, so you can find out how to apply it by searching.
I will also post the application when I get a chance.