Foodtaster
Тестирование Chef-кода с помощью
RSpec и Vagrant
Докладчик: Михаил Лапшин
DevOps Are Coders
DevOps Are Coders...
… but without TDD/BDD
OKAY
Common DevOp Workflow
Common DevOp Workflow
Недостатки:
Poor Man’s Cookbook Testing
# cookbook/recipes/test.rb
ruby_block “cookbook test” do
block do
if !File.exists?(“...”)
raise “No config file”
end
end
end
Poor Man’s Cookbook Testing
Минусы:
Foodtaster!
DevOp Workflow 2.0
Архитектура Foodtaster
Foodtaster
+
RSpec
DRb
Vagrant
+
Foodtaster
DRb Server
VM0
VM1
VM2
vm up
vm down
vm rollback
run chef
execute command
Архитектура Foodtaster
vm0.should have_file(“/tmp/foo”)
то же самое, что
vm0.execute(“ls /tmp/foo”).should be_successful
Chef Repo (“Kitchen”) Layout
acme-infrastructure
├── cookbooks
│ ├── apt
├── site-cookbooks
│ ├── my_cookbook
├── spec
│ ├── my_cookbook_spec.rb
│ └── spec_helper.rb
├── Gemfile
└── Vagrantfile
Spec Example
describe "nginx::default" do� run_chef_on :vm0 do |c|� c.json = {}� c.add_recipe 'nginx'� end�� it "should install nginx as a daemon" do� vm0.should have_package 'nginx'� vm0.should have_user('www-data').in_group('www-data')� vm0.should listen_port(80)� end�� it "should have valid nginx config" do� result = vm0.execute("nginx -t")�� result.should be_successfull� result.stdout.should include("/etc/nginx/nginx.conf syntax is ok")� end�end
Integration Spec Example
describe "pg cookbook" do
run_chef_on :vm0 do |chef|
chef.add_recipe "test::pg_master"
end
run_chef_on :vm1 do |chef|
chef.add_recipe "test::pg_stadby"
end
def exec_sql(vm, sql)
command = %Q[env psql -p 5555 postgres -c "#{sql}"]
res = vm.execute(command)
res.stdout
end
Integration Spec Example
it "should setup hot-standby replication" do
value = Time.now.to_s
exec_sql(vm0, "create table if not exists test (num varchar)")
exec_sql(vm0, "insert into test values('#{key}')")
timeout 5 do
begin
res = exec_sql(vm1, "select value from test")
sleep 1
end while !res.include?(key)
end
end
Foodtaster Current Limitations
Links
GitHub:
mlapshin/foodtaster
mlapshin/foodtaster-example
Questions?
PM via GitHub or
E-mail: mikhail.a.lapshin@gmail.com
Questions?
FIN