Serverspecでインフラテスト › wp-content › uploads › 2018 › 04 ›...

16
Serverspecでインフラテスト 2018420株式会社フルマークス 大久保和彦

Transcript of Serverspecでインフラテスト › wp-content › uploads › 2018 › 04 ›...

Serverspecでインフラテスト

2018年4月20日

株式会社フルマークス 大久保和彦

目次

copyright©2018 FULL MARKS, Inc. 2

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. まとめと課題

1. Serverspecとは

copyright©2018 FULL MARKS, Inc. 3

1.1. Serverspecとは

Rubyで実装されたサーバの状態をテストするためのフレームワーク

RubyのテストフレームワークであるRSpecの書き方に準拠

2018/4/19時点での最新バージョンは、2.41.3

1. Serverspecとは

copyright©2018 FULL MARKS, Inc. 4

1.2. Serverspecで出来る事 意図した設定や処理がサーバーに適切に反映されていることを確認できます。

ホスト名が正しく設定されているか

サーバにあるパッケージがインストールされているか

ミドルウェアが起動しているか

自動起動の設定はできているか

TCPの何番ポートがLISTEN状態になっているか

などなど。これにより、ChefやAnsible等で構築したサーバーが正常に稼働していることを保証できます。

また、サーバーの設定変更を行った後でテストを実行することで既存の設定に問題が発生していないかどうかも確認することができます。

2. セットアップ

copyright©2018 FULL MARKS, Inc. 5

2.1. 必要なもの 以下をServerspecの実行環境にインストールします

Ruby

RubyGems

Bundler

※今回は、全てchefdk-2.4.17-1-x64.msiに同梱のものを使用しました

$ ruby -v ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32]

$ gem -v 2.6.14

$ bundler -v Bundler version 1.16.1

2. セットアップ

copyright©2018 FULL MARKS, Inc. 6

2.2. インストール

Gemfileを書いてBundlerでインストールします。

Gemfileの内容

インストール

$ mkdir -p /d/work/git/pri/serverspec-sample/serverspec/ $ cd /d/work/git/pri/serverspec-sample/serverspec/ $ vim Gemfile

source "https://rubygems.org" gem 'serverspec' gem 'rake' gem 'highline'

$ bundle install --path vendor/bundle

3. デモ

copyright©2018 FULL MARKS, Inc. 7

3.1. デモ環境について

ServerSpecの実行環境

Windows (AWS Workspaces)

テスト対象としてRHEL7のWEBサーバーを2台用意

dev-web01

live-web01

3. デモ

copyright©2018 FULL MARKS, Inc. 8

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

3. デモ

copyright©2018 FULL MARKS, Inc. 9

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 (つづく)

3. デモ

copyright©2018 FULL MARKS, Inc. 10

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

3. デモ

copyright©2018 FULL MARKS, Inc. 11

3.4.サンプルテストの実行

rakeコマンドを使用してサンプルテストを実行してみます。

4 failuresとなっていて、失敗してます。

$ 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

3. デモ

copyright©2018 FULL MARKS, Inc. 12

3.5.エラーを解消する

サーバー側で設定を行い、エラーを解消してみます。

httpdのインストール

httpdサービスを起動

httpdサービスの自動起動を有効化

# yum install -y httpd

# systemctl start httpd.service

# systemctl enable httpd.service

3. デモ

copyright©2018 FULL MARKS, Inc. 13

3.6. 再度サンプルテストの実行

再びサンプルテストを実行します。

0 failuresとなっていて、成功するようになりました。

無事対応完了です。

$ 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

3. デモ

copyright©2018 FULL MARKS, Inc. 14

3.7. テスト対象サーバーの追加

テスト対象のサーバー、live-web01を登録します。

dev-web01の時と同じように以下を実施してテストが全て成功するようにします。

テスト対象サーバーの登録

サーバー設定

httpdのインストール

httpdサービスを起動

httpdサービスの自動起動を有効化(enabled)

サンプルテストの実行

3. デモ

copyright©2018 FULL MARKS, Inc. 15

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

4. まとめ

copyright©2018 FULL MARKS, Inc. 16

まとめ

ServerSpec実行環境から、2台のLinuxサーバーに対して自動テストを実行しました。

今後の課題

サーバー単位でテストを実行したい(このままだと全サーバーのテストが実行されてしまう)

環境(DEV/STG/LIVE)や、サーバー単位の固有情報の扱いをどうするか

同じテストケースはサーバーや環境をまたいで共有したい