以下のコマンドでServerspecのテストスクリプトが実行される。結果は標準出力に出力されるため、teeコマンドを利用し、ファイルにも保存している。
単一ホストを実行したい場合
$ rake spec:tissvv096 | tee tissvv096_test.log
全ホストを実行したい場合
$ rake spec | tee all_host_test.log
実行時のディレクトリのローカルに存在するRakefileを参照し、実行対象のテストスクリプトが選択、実行される。デフォルトで生成されるRakefileは以下の通り。上記コマンドの例のように、spec:[ホスト名]と指定すると、単一ホストのみが実行される。省略した場合は、./spec直下のホスト名を抽出し、1ホストごとに順番にテストスクリプトを実行するようになっている。
require 'rake' require 'rspec/core/rake_task' task :spec => 'spec:all' task :default => :spec namespace :spec do targets = [] Dir.glob('./spec/*').each do |dir| next unless File.directory?(dir) target = File.basename(dir) target = "_#{target}" if target == "default" targets << target end task :all => targets task :default => :all targets.each do |target| original_target = target == "_default" ? target[1..-1] : target desc "Run Serverspec tests to #{original_target}" RSpec::Core::RakeTask.new(target.to_sym) do |t| ENV['TARGET_HOST'] = original_target t.pattern = "spec/#{original_target}/*_spec.rb" end end end
テストスクリプトの実行により、以下の結果が出力される。
/usr/bin/ruby -I/usr/local/share/gems/gems/rspec-core-3.4.4/lib:/usr/local/share/gems/gems/rspec-support-3.4.1/lib /usr/local/share/gems/gems/rspec-core-3.4.4/exe/rspec --pattern spec/tissvv096/\*_spec.rb Command "yum check-update" exit_status should eq 0 Package "php" should be installed Package "php-mysql" should be installed Package "mariadb-server" should be installed with version "5.5" Service "mariadb" should be enabled should be running Port "3306" should be listening Command "mysqlshow -u root -pFM11AD2+ mysql" stdout should contain "Database" Command "mysqlshow" stdout should contain "Database" Command "mysqladmin ping" stdout should contain "mysqld is alive" Command "logrotate -vd /etc/logrotate.d/mariadb" stdout should contain "log does not need rotating" File "/var/www/wordpress/readme.html" should contain "Version 4.5.2" Command "mysqlshow -u wp_admin -pHB-F1XDJ WordPress" stdout should contain "WordPress" File "/var/www/wordpress/wp-config.php" should exist File "/var/www/wordpress/wp-config.php" should contain "define('DB_NAME',.*'WordPress');" should contain "define('DB_USER',.*'wp_admin');" should contain "define('DB_PASSWORD',.*'HB-F1XDJ');" should contain "define('AUTH_KEY',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('SECURE_AUTH_KEY',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('LOGGED_IN_KEY',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('NONCE_KEY',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('AUTH_SALT',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('SECURE_AUTH_SALT',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('LOGGED_IN_SALT',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" should contain "define('NONCE_SALT',.*'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');" Command "find /var/www/wordpress -not -user root -or -not -group root | wc -l | tr -d ' '" stdout should contain "^0$" File "/etc/httpd/conf.d/wordpress.conf" should exist File "/etc/httpd/conf.d/wordpress.conf" should contain "<VirtualHost *:80>\n ServerName tissvv096\n DocumentRoot /var/www/wordpress\n <Directory \"/var/www/wordpress\">\n AllowOverride All\n Options -Indexes\n </Directory>\n\n <Files wp-config.php>\n order allow,deny\n deny from all\n </Files>\n</VirtualHost>\n" Package "httpd" should be installed File "/etc/httpd/conf/httpd.conf.bak" should exist File "/etc/httpd/conf/httpd.conf" should contain "ServerName tissvv096" Service "httpd" should be enabled should be running Port "80" should be listening Command "firewall-cmd --list-all --zone=public" stdout should contain "services:.*ssh" stdout should contain "services:.*http" stdout should contain "services:.*mysql" stdout should contain "ports: " Command "curl http://tissvv096/wp-admin/install.php" stdout should contain "WordPress" Finished in 1.65 seconds (files took 0.58705 seconds to load) 39 examples, 0 failures
これだけのテスト項目が、わずか2秒弱で完了している。
Copyright © ITmedia, Inc. All Rights Reserved.