LoginSignup
7
5

More than 5 years have passed since last update.

Serverspec で複数のサーバをテスト @ AWS EC2

Last updated at Posted at 2016-06-18

Serverspecとは

O'REILLY『Serverspec』P.5 によると以下の目的に使うサーバテストツールです。

  1. テスト駆動によるインフラコード開発
  2. サーバ構築後の確認作業の自動化
  3. 稼働しているサーバの監視
  4. サーバの再起動後の状態確認
  5. サーバのあるべき状態の抽象化

Chef, Puppet, Ansibleと合わせて使うと、TDD(テスト駆動)なインフラコード開発が可能になります。
リモートマシンのテストは、SSH接続さえできれば可能です。テスト対象マシンにrubyやServerspecのインストールは不要です。

対象読者

複数のサーバをServerspecでテストしてみたい初心者を対象としています。

サーバは、AWS EC2 Amazon Linuxを使用します。
Serverspecのインストール方法、ローカルでServerspecを実行してみる方法は、前々回の記事を参照してください。
  ● Serverspec 最初の一歩 @ AWS EC2
Serverspecを使ったリモートサーバのテスト方法は、前回の記事を参照してください。
  ● Serverspec で リモート サーバをテスト @ AWS EC2

環境の準備

■ EC2インスタンスの準備

  • EC2 Amazon Linuxインスタンス作成
    Amazon Linux のEC2インスタンスを3つ作成します。

    マシン名 IPアドレス 用途
    ec2_serverspec 172.31.24.21 Serverspecを実行するマシン
    ec2_client01 172.31.21.132 テスト対象マシン1
    ec2_client02 172.31.30.162 テスト対象マシン2

    Serverspecを実行するマシンは、前々回の記事を参照してServerspecをセットアップしておきます。
    ● Serverspec 最初の一歩 @ AWS EC2

    テスト対象マシン1、テスト対象マシン2は、前回の記事を参照してセットアップします。
    ● Serverspec で リモート サーバをテスト @ AWS EC2

    ec2_serverspecのrootの ~/.ssh/config ファイルは、以下のようになります。

    ~/.ssh/config
    Host ec2_client01
      HostName 172.31.21.132
      Port 22
      User ec2-user
      IdentityFile ~/.ssh/serverspec-key
    Host ec2_client02
      HostName 172.31.30.162
      Port 22
      User ec2-user
      IdentityFile ~/.ssh/serverspec-key
    

テストの準備

 ここでは、スペック ファイルをコピーして作成する方法と、シンボリックリンクを貼る方法を紹介します。テスト結果は、どちらも同じです。

  • コピーする方法

    [root@ip-172-31-24-21 serverspec]# cp -r spec/ec2_client01/ spec/ec2_client02/
    
  • シンボリックリンクを作成する方法

    [root@ip-172-31-24-21 serverspec]# cd spec
    [root@ip-172-31-24-21 spec]# ln -s ec2_client01 ec2_client02
    [root@ip-172-31-24-21 spec]# ls -la
    total 16
    drwxr-xr-x 3 root root 4096 Jun 18 10:38 .
    drwxr-xr-x 3 root root 4096 Jun 18 10:26 ..
    drwxr-xr-x 2 root root 4096 Jun 18 10:34 ec2_client01
    lrwxrwxrwx 1 root root   12 Jun 18 10:38 ec2_client02 -> ec2_client01
    -rw-r--r-- 1 root root  678 Jun 18 05:35 spec_helper.rb
    

テスト実行

 テストを実行するコマンドは、rake spec です。

  • テスト実行
    specディレクトリの*_spec.rbにマッチするスペックファイルがすべて実行されます。

    [root@ip-172-31-24-21 ~]# cd serverspec/
    [root@ip-172-31-24-21 serverspec]# rake spec
    /usr/bin/ruby2.0 -I/usr/local/share/ruby/gems/2.0/gems/rspec-support-3.4.1/lib:/usr/local/share/ruby/gems/2.0/gems/rspec-core-3.4.4/lib /usr/local/share/ruby/gems/2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/ec2_client01/\*_spec.rb
    
    Port "22"
      should be listening
    
    Service "ntpd"
      should be enabled
      should be running
    
    Package "openssh-server"
      should be installed
    
    Service "sshd"
      should be enabled
      should be running
    
    File "/etc/ssh/sshd_config"
      content
        should match /^#Port 22/
      content
        should not match /^PasswordAuthentication yes/
    
    Finished in 0.17563 seconds (files took 0.5126 seconds to load)
    8 examples, 0 failures
    
    /usr/bin/ruby2.0 -I/usr/local/share/ruby/gems/2.0/gems/rspec-support-3.4.1/lib:/usr/local/share/ruby/gems/2.0/gems/rspec-core-3.4.4/lib /usr/local/share/ruby/gems/2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/ec2_client02/\*_spec.rb
    
    Port "22"
      should be listening
    
    Service "ntpd"
      should be enabled
      should be running
    
    Package "openssh-server"
      should be installed
    
    Service "sshd"
      should be enabled
      should be running
    
    File "/etc/ssh/sshd_config"
      content
        should match /^#Port 22/
      content
        should not match /^PasswordAuthentication yes/
    
    Finished in 0.18081 seconds (files took 0.51155 seconds to load)
    8 examples, 0 failures
    
    [root@ip-172-31-24-21 serverspec]# cat ~/.ssh/config
    Host ec2_client01
      HostName 172.31.21.132
      Port 22
      User ec2-user
      IdentityFile ~/.ssh/serverspec-key
    
    Host ec2_client02
      HostName 172.31.30.162
      Port 22
      User ec2-user
      IdentityFile ~/.ssh/serverspec-key
    

    ec2_client01、ec2_client02 ともに8個のテストが成功しました。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5