サーバーテストの自動化ツールとして評判の高い、Serverspecをざっくり試してみた。
テスト環境
- VPSプロバイダ: DigitalOcean (リージョン: SFO3)
- マシンタイプ: Shared CPU (1 CPU, 1 GB RAM)
- OS: CentOS 7 x64
セットアップ
Serverspecではローカルとリモートの両方のマシンのテストを行えるが、今回はローカルマシンのテストのみ行ってみた。マシンを作成したら、テスト対象としてApacheをインストールして実行しておく。
Serverspecの実行にはRubyが必要なので、Rubyをインストールしてから、serverspecのgemをインストールする。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
gitのインストール # yum install git # git --version git version 1.8.3.1 Rubyのインストール # cd /opt # git clone https://github.com/sstephenson/rbenv.git # mkdir -p /opt/rbenv/plugins # cd /opt/rbenv/plugins # git clone https://github.com/sstephenson/ruby-build.git # vi ~/.bashrc # rbenv global config export RBENV_ROOT="/opt/rbenv" export PATH="${RBENV_ROOT}/bin:${PATH}" eval "$(rbenv init -)" # yum -y install gcc make openssl-devel libffi-devel ruby-devel readline-devel rubygems sqlite-devel bzip2 # /bin/bash -lc "rbenv install 2.6.0" # /bin/bash -lc "rbenv rehash" # /bin/bash -lc "rbenv global 2.6.0" Serverspecのインストール # gem install serverspec |
Serverspecのインストールが完了したら、テストの雛形を作成する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# cd ~ # pwd /root # serverspec-init Select OS type: 1) UN*X 2) Windows Select number: 1 Select a backend type: 1) SSH 2) Exec (local) Select number: 2 + spec/ + spec/localhost/ + spec/localhost/sample_spec.rb + spec/spec_helper.rb + Rakefile + .rspec |
作成されたテストの雛形は以下の通り。予備知識無しでも内容が想像できるシンプルなコードだ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# vi spec/localhost/sample_spec.rb require 'spec_helper' describe package('httpd'), :if => os[:family] == 'redhat' do it { should be_installed } end describe package('apache2'), :if => os[:family] == 'ubuntu' do it { should be_installed } end describe service('httpd'), :if => os[:family] == 'redhat' do it { should be_enabled } it { should be_running } end describe service('apache2'), :if => os[:family] == 'ubuntu' do it { should be_enabled } it { should be_running } end describe service('org.apache.httpd'), :if => os[:family] == 'darwin' do it { should be_enabled } it { should be_running } end describe port(80) do it { should be_listening } endて |
テストを実行すると全て成功。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# rake /opt/rbenv/versions/2.6.0/bin/ruby -I/opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-support-3.12.1/lib:/opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-core-3.12.2/lib /opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-core-3.12.2/exe/rspec --pattern spec/localhost/\*_spec.rb Package "httpd" is expected to be installed Service "httpd" is expected to be enabled is expected to be running Port "80" is expected to be listening Finished in 0.13819 seconds (files took 0.56471 seconds to load) 4 examples, 0 failures |
Apacheを停止して再度テストを実行したら、今度はきちんと失敗した。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# service httpd stop # service httpd status # rake /opt/rbenv/versions/2.6.0/bin/ruby -I/opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-support-3.12.1/lib:/opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-core-3.12.2/lib /opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-core-3.12.2/exe/rspec --pattern spec/localhost/\*_spec.rb Package "httpd" is expected to be installed Service "httpd" is expected to be enabled is expected to be running (FAILED - 1) Port "80" is expected to be listening (FAILED - 2) Failures: 1) Service "httpd" is expected to be running On host `localhost' Failure/Error: it { should be_running } expected Service "httpd" to be running /bin/sh -c systemctl\ is-active\ httpd inactive # ./spec/localhost/sample_spec.rb:13:in `block (2 levels) in <top (required)>' 2) Port "80" is expected to be listening On host `localhost' Failure/Error: it { should be_listening } expected Port "80" to be listening /bin/sh -c ss\ -tunl\ \|\ grep\ -E\ --\ :80\\\ # ./spec/localhost/sample_spec.rb:27:in `block (2 levels) in <top (required)>' Finished in 0.0979 seconds (files took 0.58659 seconds to load) 4 examples, 2 failures Failed examples: rspec ./spec/localhost/sample_spec.rb:13 # Service "httpd" is expected to be running rspec ./spec/localhost/sample_spec.rb:27 # Port "80" is expected to be listening /opt/rbenv/versions/2.6.0/bin/ruby -I/opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-support-3.12.1/lib:/opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-core-3.12.2/lib /opt/rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rspec-core-3.12.2/exe/rspec --pattern spec/localhost/\*_spec.rb failed |
なかなか良さげなツールだ。ただ、自分にとってはアプリでRubyを使っていない環境に、テスト用途だけでRubyをインストールするのがちょっと重荷かもしれない。
参考サイト
Serverspec – Home
https://serverspec.org/
「Serverspec」を使ってサーバー環境を自動テストしよう | さくらのナレッジ
https://knowledge.sakura.ad.jp/2596/
2022-03-10
Serverspec のインストールと基本的な使い方 – Qiita
https://qiita.com/Tocyuki/items/5ce5250407da48886e45
2017-06-02