Permalink
Browse files

Add tests by Serverspec

  • Loading branch information...
akagisho committed Dec 24, 2014
1 parent 61a58f1 commit 2386a515e80f7114d0e7111eaa9c38742a0b4801
View
@@ -0,0 +1,4 @@
---
-
playbook: site.yml
inventory: hosts
View
@@ -2,3 +2,8 @@
production/inventory
production/group_vars/all
tmp
/.bundle
/hosts
Gemfile.lock
vendor/bundle
View
@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'rake'
gem 'serverspec'
gem 'ansible_spec'
View
@@ -55,6 +55,15 @@ Check installation.
ns1.example.com. admin.example.com. 2014052907 10800 7200 604800 10800
ns1.example.com. admin.example.com. 2014052907 10800 7200 604800 10800
## test
You can test server's configurations by using Serverspec.
$ bundle install --path vendor/bundle
$ rm -f hosts && ln -s <environment>/inventory hosts
$ bundle exec rake serverspec:master
$ bundle exec rake serverspec:slave
## use
Access PowerDNS on Rails interface with admin@example.com/secret.
View
@@ -0,0 +1,88 @@
require 'rake'
require 'rspec/core/rake_task'
require 'yaml'
# param: none
# return: @playbook, @inventoryfile
def load_ansiblespec()
f = '.ansiblespec'
if File.exist?(f)
y = YAML.load_file(f)
@playbook = y[0]['playbook']
@inventoryfile = y[0]['inventory']
else
@playbook = 'site.yml'
@inventoryfile = 'hosts'
end
if File.exist?(@playbook) == false
puts 'Error: ' + @playbook + ' is not Found. create site.yml or /.ansiblespec See https://github.com/volanja/ansible_spec'
exit 1
elsif File.exist?(@inventoryfile) == false
puts 'Error: ' + @inventoryfile + ' is not Found. create hosts or /.ansiblespec See https://github.com/volanja/ansible_spec'
exit 1
end
end
# param: inventory file of Ansible
# return: Hash {"active_group_name" => ["192.168.0.1","192.168.0.2"]}
def load_host(file)
hosts = File.open(file).read
active_group = Hash.new
active_group_name = ''
hosts.each_line{|line|
line = line.chomp
next if line.start_with?('#')
if line.start_with?('[') && line.end_with?(']')
active_group_name = line.gsub('[','').gsub(']','')
active_group["#{active_group_name}"] = Array.new
elsif active_group_name.empty? == false
next if line.empty? == true
active_group["#{active_group_name}"] << line
end
}
return active_group
end
# main
load_ansiblespec
load_file = YAML.load_file(@playbook)
# e.g. comment-out
if load_file === false
puts 'Error: No data in site.yml'
exit
end
properties = Array.new
load_file.each do |site|
if site.has_key?("include")
properties.push YAML.load_file(site["include"])[0]
else
properties.push site
end
end
#load inventry file
hosts = load_host(@inventoryfile)
properties.each do |var|
if hosts.has_key?("#{var["hosts"]}")
var["hosts"] = hosts["#{var["hosts"]}"]
end
end
namespace :serverspec do
properties.each do |var|
var["hosts"].each do |host|
desc "Run serverspec for #{var["name"]}"
RSpec::Core::RakeTask.new(var["name"].to_sym) do |t|
puts "Run serverspec for #{var["name"]} to #{host}"
ENV['TARGET_HOST'] = host
ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
ENV['TARGET_USER'] = var["user"]
t.pattern = 'roles/{' + var["roles"].join(',') + '}/spec/*_spec.rb'
end
end
end
end
@@ -0,0 +1,18 @@
require 'spec_helper'
describe package('mysql-server') do
it { should be_installed }
end
describe package('mysql-client') do
it { should be_installed }
end
describe service('mysql') do
it { should be_enabled }
it { should be_running }
end
describe port(3306) do
it { should be_listening }
end
@@ -0,0 +1,14 @@
require 'spec_helper'
describe package('nginx') do
it { should be_installed }
end
describe service('nginx') do
it { should be_enabled }
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
@@ -0,0 +1,14 @@
require 'spec_helper'
describe package('pdns-server') do
it { should be_installed }
end
describe service('pdns') do
it { should be_enabled }
it { should be_running }
end
describe port(53) do
it { should be_listening }
end
@@ -0,0 +1,6 @@
require 'spec_helper'
describe service('unicorn') do
it { should be_enabled }
it { should be_running }
end
@@ -0,0 +1,14 @@
require 'spec_helper'
describe package('bind9') do
it { should be_installed }
end
describe service('bind9') do
it { should be_enabled }
it { should be_running }
end
describe port(53) do
it { should be_listening }
end
View
@@ -1,16 +1,19 @@
---
- hosts: local
- name: local
hosts: local
sudo: no
roles:
- local
- hosts: master
- name: master
hosts: master
sudo: yes
roles:
- common
- master
- hosts: slave
- name: slave
hosts: slave
sudo: yes
roles:
- common
View
@@ -0,0 +1,34 @@
require 'serverspec'
require 'net/ssh'
set :backend, :ssh
if ENV['ASK_SUDO_PASSWORD']
begin
require 'highline/import'
rescue LoadError
fail "highline is not available. Try installing it."
end
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
else
set :sudo_password, ENV['SUDO_PASSWORD']
end
host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)
options[:user] ||= Etc.getlogin
set :host, options[:host_name] || host
set :ssh_options, options
# Disable sudo
# set :disable_sudo, true
# Set environment variables
# set :env, :LANG => 'C', :LC_MESSAGES => 'C'
# Set PATH
# set :path, '/sbin:/usr/local/sbin:$PATH'

0 comments on commit 2386a51

Please sign in to comment.