attribute を使うサンプルな Rakefile
Rake 初心者なもので読み解くのに時間はかかったのですが控えを以下に。
参考にさせて頂いたのは以下エントリです。
Rakefile の以下な記述がアレでした。
namespace :serverspec do task :all => attributes.keys.map {|key| 'serverspec:' + key } attributes.keys.each do |key| desc "Run serverspec to #{key}" Rspec::Core::RakeTask.new(key.to_sym) do |t| ENV['TARGET_SERVICE'] = key t.pattern = "spec/*/#{key}_spec.rb" end end end
こちら、attributes.yml が以下なイメージで
zabbix_server: :listen_port: 10051 (中略 mysql_server: :listen_port: 3306 (中略 httpd: :listen_port: 80 (後略
attributes.keys は [zabbix_server, mysql_server, httpd] みたいなアレだと理解。で、spec_helper.rb で
attr_set attributes[ENV['TARGET_SERVICE']]
とされることで key 毎の attribute が、という事なのか。あとは以下なカンジで使える、という事ですね。
describe "port #{attr[:listen_port]}" do it { should be_listening } end
マジックナンバーを試験な実装から除去できるのか。