基础设施行为测试框架
Infrastructure Behavior Testing Framework.
First, create Gemfile:
source 'https://rubygems.org'
gem 'infrataster'Install gems:
$ bundle installInstall Vagrant: Official Docs
Create Vagrantfile:
# Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.define :proxy do |c|
c.vm.network "private_network", ip: "192.168.33.10"
c.vm.network "private_network", ip: "172.16.33.10", virtualbox__intnet: "infrataster-example"
end
config.vm.define :app do |c|
c.vm.network "private_network", ip: "172.16.33.11", virtualbox__intnet: "infrataster-example"
end
endStart VMs:
$ vagrant upInitialize rspec directory:
$ rspec --init
create spec/spec_helper.rb
create .rspecrequire 'infrataster/rspec' and define target servers for testing in spec/spec_helper.rb:
# spec/spec_helper.rb
require 'infrataster/rspec'
Infrataster::Server.define(
:proxy, # name
'192.168.0.0/16', # ip address
vagrant: true # for vagrant VM
)
Infrataster::Server.define(
:app, # name
'172.16.0.0/16', # ip address
vagrant: true, # for vagrant VM
from: :proxy # access to this machine via SSH port forwarding from proxy
)
# Code generated by `rspec --init` is following...Or
# spec/spec_helper.rb
require 'infrataster/rspec'
Infrataster::Server.define(:proxy) do |server|
server.address = '192.168.0.0/16'
server.vagrant = true
end
Infrataster::Server.define(:app) do |server|
server.address = '172.16.0.0/16'
server.vagrant = true
server.from = :proxy
end
# Code generated by `rspec --init` is following...Then, you can write spec files:
# spec/example_spec.rb
require 'spec_helper'
describe server(:app) do
describe http('http://app') do
it "responds content including 'Hello Sinatra'" do
expect(response.body).to include('Hello Sinatra')
end
it "responds as 'text/html'" do
expect(response.headers['content-type']).to eq("text/html")
end
end
endRun tests:
$ bundle exec rspec
2 examples, 2 failuresCurrently, the tests failed because the VM doesn't respond to HTTP request.
It's time to write provisioning instruction like Chef's cookbooks or Puppet's manifests!
"Server" is a server you tests. This supports Vagrant, which is very useful to run servers for testing. Of course, you can test real servers.
You should define servers in spec_helper.rb like the following:
…You can specify SSH configuration manually too:
Infrataster::Server.define(
# ...
ssh: {host_name: 'hostname', user: 'testuser', keys: ['/path/to/id_rsa']}
)Infrataster has "fuzzy IP address" feature. You can pass IP address which has netmask (= CIDR) to Infrataster::Server#define. This needs vagrant option or ssh option which has host_name because this fetches all IP address via SSH and find the matching one.
Infrataster::Server.define(
:app,
# find IP address matching 172.16.0.0 ~ 172.16.255.255
'172.16.0.0/16',
)Of course, you can set fully-specified IP address too.
Infrataster::Server.define(
:app,
'172.16.11.22',
# or
'172.16.11.22/32',
)You can execute a command on the server like the following:
describe server(:proxy) do
let(:time) { Time.now }
before do
current_server.ssh_exec "echo 'Hello' > /tmp/test-#{time.to_i}"
end
it "executes a command on the current server" do
result = current_server.ssh_exec("cat /tmp/test-#{time.to_i}")
expect(result.chomp).to eq('Hello')
end
endThis is useful to test cases which depends on the status of the server.
"Resource" is what you test by Infrataster. For instance, the following code describes http resource.
describe server(:app) do
describe http('http://example.com') do
it "responds content including 'Hello Sinatra'" do
expect(response.body).to include('Hello Sinatra')
end
end
endhttp resourcehttp resource tests HTTP response when sending HTTP request.
It accepts method, params and header as options.
…capybara resourcecapybara resource tests your web application by simulating real user's interaction.
describe server(:app) do
describe capybara('http://app.example.com') do
it 'shows food list' do
visit '/'
click_link 'Foods'
expect(page).to have_content 'Yummy Soup'
end
end
endmysql_query resourcemysql_query resource is now in infrataster-plugin-mysql.
pgsql_query resourcepgsql_query resource sends a query to PostgreSQL server.
pgsql_query is provided by infrataster-plugin-pgsql by @SnehaM.
dns resourcedns resource sends a query to DNS server.
dns is provided by infrataster-plugin-dns by @otahi.
memcached resourcememcached resource sends a query to memcached server.
memcached is provided by infrataster-plugin-memecached by @rahulkhengare.
redis resourceredis resource sends a query to redis server.
redis is provided by infrataster-plugin-redis by @rahulkhengare.
firewall resourcefirewall resource tests your firewalls.
firewall is provided by infrataster-plugin-firewall by @otahi.
Unit tests are under spec/unit directory.
$ bundle exec rake spec:unitIntegration tests are under spec/integration directory.
$ bundle exec rake spec:integration:prepare
$ bundle exec rake spec:integrationhttps://speakerdeck.com/ryotarai/infrataster-infra-behavior-testing-framework-number-oedo04
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)暂无开放 Issues,或尚未同步最近议题。