LoginSignup
3
4

More than 5 years have passed since last update.

Serverspecのディレクトリ構成について相談

Posted at

この記事について

みんなのServerspecの知見を知りたい

背景

久しぶりにServerspec
業務で真面目に導入しようという話になっている

前提

  • 案件がたくさんある
  • 案件によってアーキテクチャが全然違う
  • ミドルウェア周りの共通的なものはまとめたい

http://serverspec.org/advanced_tips.html のページの
How to use Serverspec tests as shared behaviorsを参照している

工夫できること

  • caseのディレクトリをつくって案件毎にまとめる
  • sharedのディレクトリに共通の設定をまとめる

というぐらいしかアイデアがない
みんななにを工夫をしているのか知りたい :pray:

構成

├── Rakefile
└── spec
    ├── case
    │   ├── anken_one
    │   │   └── main_spec.rb # 1インスタンスとかであればこんな感じ?
    │   └── anken_two
    │       ├── db_spec.rb # WebやDBで分かれているならこんな感じ?
    │       └── web_spec.rb
    ├── shared
    │   ├── apache
    │   │   └── init.rb
    │   ├── nginx
    │   │   └── init.rb
    │   ├── postfix
    │   │   └── init.rb
    │   └── rails
    │       └── init.rb
    └── spec_helper.rb

Rakefile

require 'rake'
require 'rspec/core/rake_task'

task :spec    => 'spec:all'
task :default => :spec

namespace :spec do
  targets = []
  Dir.glob('./spec/case/**').each do |dir| # caseディレクトリを追加
    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/case/#{original_target}/**/*_spec.rb" # ここも追加
    end
  end
end

anken_one/web_spec.rb

個別の設定

require 'spec_helper'

describe 'shared' do
  include_examples 'nginx::init'
  include_examples 'postfix::init'
end

describe process("nginx") do
  its(:user) { should eq "foo" }
end

shared/postfix/init.rb

shared_examples 'postfix::init' do
  describe port(25) do
    it { should be_listening }
  end

  describe process("master") do
    it {should be_running}
  end
end
3
4
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
3
4