Serverspec でインフラテスト 2018 年 4 月 20 日株式会社フルマークス大久保和彦
目次 1. Serverspec とは 1.1. Serverspecとは 1.2. Serverspecで出来る事 2. セットアップ 2.1. 必要なもの 2.2. インストール 3. デモ 3.1. デモ環境について 3.2. テスト対象サーバーの登録 3.3. サンプルテストの中身 3.4. サンプルテストの実行 3.5. エラーを解消する 3.6. 再度サンプルテストの実行 3.7. テスト対象サーバーの追加 4. まとめと課題 2
1. Serverspec とは 1.1. Serverspec とは Ruby で実装されたサーバの状態をテストするためのフレームワーク Ruby のテストフレームワークである RSpec の書き方に準拠 2018/4/19 時点での最新バージョンは 2.41.3 3
1. Serverspec とは 1.2. Serverspec で出来る事意図した設定や処理がサーバーに適切に反映されていることを確認できます ホスト名が正しく設定されているかサーバにあるパッケージがインストールされているかミドルウェアが起動しているか自動起動の設定はできているか TCPの何番ポートがLISTEN 状態になっているか などなど これにより Chef や Ansible 等で構築したサーバーが正常に稼働していることを保証できます また サーバーの設定変更を行った後でテストを実行することで既存の設定に問題が発生していないかどうかも確認することができます 4
2. セットアップ 2.1. 必要なもの 以下を Serverspec の実行環境にインストールします Ruby $ ruby -v ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32] RubyGems $ gem -v 2.6.14 Bundler $ bundler -v Bundler version 1.16.1 今回は 全て chefdk-2.4.17-1-x64.msi に同梱のものを使用しました 5
2. セットアップ 2.2. インストール Gemfile を書いて Bundler でインストールします $ mkdir -p /d/work/git/pri/serverspec-sample/serverspec/ $ cd /d/work/git/pri/serverspec-sample/serverspec/ $ vim Gemfile Gemfile の内容 source "https://rubygems.org" gem 'serverspec' gem 'rake' gem 'highline' インストール $ bundle install --path vendor/bundle 6
3.1. デモ環境について ServerSpec の実行環境 Windows (AWS Workspaces) テスト対象として RHEL7 の WEB サーバーを 2 台用意 dev-web01 live-web01 7
3.2. テスト対象サーバーの登録 serverspec-init を実行してテスト対象のサーバー dev-web01 を登録します $ bundle exec 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: dev-web01 + spec/dev-web01/ + spec/dev-web01/sample_spec.rb 8
3.3. サンプルテストの中身 httpd(apache) のテストが書かれてます $ cat spec/dev-web01/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 ( つづく ) 9
3.3. サンプルテストの中身 ( つづき ) 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 10
3.4. サンプルテストの実行 rake コマンドを使用してサンプルテストを実行してみます $ bundle exec rake spec Package "httpd" should be installed (FAILED - 1) Service "httpd" should be enabled (FAILED - 2) should be running (FAILED - 3) Port "80" should be listening (FAILED - 4) ( 途中省略 ) Finished in 0.49597 seconds (files took 2.71 seconds to load) 4 examples, 4 failures 4 failures となっていて 失敗してます 11
3.5. エラーを解消する サーバー側で設定を行い エラーを解消してみます httpdのインストール # yum install -y httpd httpd サービスを起動 # systemctl start httpd.service httpd サービスの自動起動を有効化 # systemctl enable httpd.service 12
3.6. 再度サンプルテストの実行 再びサンプルテストを実行します $ bundle exec rake spec Package "httpd" should be installed Service "httpd" should be enabled should be running Port "80" should be listening Finished in 0.42497 seconds (files took 2.68 seconds to load) 4 examples, 0 failures 0 failures となっていて 成功するようになりました 無事対応完了です 13
3.7. テスト対象サーバーの追加 テスト対象のサーバー live-web01 を登録します dev-web01 の時と同じように以下を実施してテストが全て成功するようにします テスト対象サーバーの登録 サーバー設定 httpdのインストール httpdサービスを起動 httpdサービスの自動起動を有効化 (enabled) サンプルテストの実行 14
3.7. テスト対象サーバーの追加 ( テストの実行結果 ) テストを実行すると 2 台のサーバーのテストが実行され成功します $ bundle exec rake spec Package "httpd" should be installed Service "httpd" should be enabled should be running Port "80" should be listening Finished in 0.51004 seconds (files took 7.37 seconds to load) 4 examples, 0 failures Package "httpd" should be installed Service "httpd" should be enabled should be running Port "80" should be listening Finished in 0.69802 seconds (files took 4.12 seconds to load) 4 examples, 0 failures 15
4. まとめ まとめ ServerSpec 実行環境から 2 台の Linux サーバーに対して自動テストを実行しました 今後の課題 サーバー単位でテストを実行したい ( このままだと全サーバーのテストが実行されてしまう ) 環境 (DEV/STG/LIVE) や サーバー単位の固有情報の扱いをどうするか 同じテストケースはサーバーや環境をまたいで共有したい 16