Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

serverspec | SSH経由でのテスト実行

概要

SSH経由でのテスト実行

前提

・テスト実行環境とテスト対象環境を仮想環境として用意します
ホストはWindows7(作業はcygwinを利用)
ゲストは双方Ubuntu1204

ゲストはそれぞれclient,serverとする。
ipはclientが192.168.33.10
ipはserverが192.168.33.11
serverはgitをインストール
clientはrbenv, ruby2.0.0.p247, git, serverspecをインストール

手順

テスト実行環境Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

$script = <<SCRIPT
echo 'start shell'
su - vagrant -c '
  PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

  echo "start install basic"
  sudo apt-get update
  sudo apt-get install build-essential bison git-core libreadline6-dev curl zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt1-dev autoconf libncurses5-dev -y
  echo "end   install basic"

  echo "start install rbenv"
  git clone https://github.com/sstephenson/rbenv.git /home/vagrant/.rbenv

  echo "export PATH=/home/vagrant/.rbenv/bin:$PATH" >> /home/vagrant/.bash_profile
'
echo \'eval "$(rbenv init -)"\' >> /home/vagrant/.bash_profile
source /home/vagrant/.bash_profile
echo "end   install rbenv"

su - vagrant -c '
  echo "start install ruby 2.0.0-p247"
  git clone https://github.com/sstephenson/ruby-build.git /home/vagrant/ruby-build
  cd /home/vagrant/ruby-build
  sudo ./install.sh
  cd /home/vagrant
  rbenv install 2.0.0-p247
  rbenv global 2.0.0-p247
  rbenv rehash
  echo "end   install ruby 2.0.0-p247"

  echo "start install bundler"
  rbenv exec gem i bundler
  echo "end   install bundler"

  echo "start install serverspec"
  mkdir /home/vagrant/work
  cd /home/vagrant/work
'

echo "source 'https://rubygems.org'" >> /home/vagrant/work/Gemfile
echo "" >> /home/vagrant/work/Gemfile
echo "gem 'serverspec', '~> 0.10.10'" >> /home/vagrant/work/Gemfile

su - vagrant -c '
  cd /home/vagrant/work
  bundle
  echo "end   install serverspec"
  echo "end   shell"
'
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu-12.04-x64"
  config.vm.network :private_network, ip: "192.168.33.10"

  config.ssh.username = "vagrant"
  config.vm.provision "shell", inline: $script
end
テスト環境起動,ソフトのインストール確認
$ vagrant up
$ vagrant provision
$ vagrant ssh
vagrant@precise64:~$ cd ./work
vagrant@precise64:~$ vagrant@precise64:~/work$ rbenv -v
vagrant@precise64:~$ rbenv 0.4.0-67-g3300587
vagrant@precise64:~$ vagrant@precise64:~/work$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
vagrant@precise64:~$ vagrant@precise64:~/work$ bundle -v
Bundler version 1.3.5
vagrant@precise64:~$ bundle exec gem list | grep serverspec
serverspec (0.10.10)
テスト対象環境Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

$script = <<SCRIPT
sudo apt-get install git -y
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu-12.04-x64"
  config.vm.network :private_network, ip: "192.168.33.11"

  config.vm.provision "shell", inline: $script
end
テスト対象環境起動,ソフトのインストール確認
vagrant@precise64:~$ vagrant up
vagrant@precise64:~$ vagrant provision
vagrant@precise64:~$ vagrant ssh
vagrant@precise64:~$ git --version
git version 1.7.9.5
vagrant@precise64:~$ echo hoge > hoge.txt # 後ほど検証で使用する
SSH公開鍵認証の設定

設定については下記参照
vagrant検証用に仮想環境を2環境作成して公開鍵認証によるSSH接続確認を行う
http://d.hatena.ne.jp/tbpg/20131025/1382717749

テスト実行環境でテストケース編集+実行
$ vagrant ssh
vagrant@precise64:~$ cd work
vagrant@precise64:~/work$ 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: 192.168.33.11
 + spec/
 + spec/192.168.33.11/
 + spec/192.168.33.11/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile
cd spec/192.168.33.11
vi httpd_spec.rb # 編集内容は後述

httpd_spec.rb

require 'spec_helper'

describe package('git') do
  it { should be_installed }
end

describe file('/home/vagrant/hoge.txt') do
  it { should be_file }
  it { should contain "hoge" }
end

テスト実行

vagrant@precise64:~/work$ rake spec
/home/vagrant/.rbenv/versions/2.0.0-p247/bin/ruby -S rspec spec/192.168.33.11/httpd_spec.rb
...

Finished in 0.12878 seconds
3 examples, 0 failures

備考

/etc/environmentについて
vagrantのshell provisioning中は/etc/environmentが読み込まれない。
そのため、インストール中に/etc/environmentに設定されている内容を取得したければ
擬似的にExpertしてやる必要がある

Vagrantfileのスクリプトの中より抜粋

PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games