Skip to content

Commit

Permalink
#2 support ansible_ssh_port
Browse files Browse the repository at this point in the history
  • Loading branch information
volanja committed Jan 4, 2015
1 parent f57c25e commit e0bb99c
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 46 deletions.
63 changes: 51 additions & 12 deletions lib/ansible_spec/load_ansible.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
module AnsibleSpec
# param: inventory file of Ansible
# return: Hash {"active_group_name" => ["192.168.0.1","192.168.0.2"]}
# return: Hash {"group" => ["192.168.0.1","192.168.0.2"]}
# return: Hash {"group" => [{"name" => "192.168.0.1","uri" => "192.168.0.1", "port" => 22},...]}
def self.load_targets(file)
hosts = File.open(file).read
active_group = Hash.new
active_group_name = ''
hosts.each_line{|line|
f = File.open(file).read
res = Hash.new
group = ''
f.each_line{|line|
line = line.chomp
next if line.start_with?('#')
# skip
next if line.start_with?('#') #comment
next if line.empty? == true #null

# get group
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
group = line.gsub('[','').gsub(']','')
res["#{group}"] = Array.new
next
end

#get host
if group.empty? == false
host = Hash.new
host['name'] = line
if line.split.count == 1
if line.include?(":") # 192.168.0.1:22
host['uri'] = line.split(":")[0]
host['port'] = line.split(":")[1].to_i
else # 192.168.0.1
res["#{group}"] << line
next
end
else
# 192.168.0.1 ansible_ssh_port=22
line.split.each{|v|
if v.include?("=")
host = host.merge(get_inventory_param(v))
else
host['uri'] = v
end
}
end
res["#{group}"] << host
end
}
return active_group
return res
end

# param ansible_ssh_port=22
# return: hash
def self.get_inventory_param(str)
res = Hash.new
if str.include?("=")
key,value = str.split("=")
res['port'] = value.to_i if key == "ansible_ssh_port"
end
return res
end

# param: none
Expand Down
16 changes: 12 additions & 4 deletions lib/src/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ require 'yaml'
require 'ansible_spec'

properties = AnsibleSpec.get_properties
# {"name"=>"Ansible-Sample-TDD", "hosts"=>["192.168.0.103"], "user"=>"root", "roles"=>["nginx", "mariadb"]}
# {"name"=>"Ansible-Sample-TDD", "hosts"=>["192.168.0.103","192.168.0.103"], "user"=>"root", "roles"=>["nginx", "mariadb"]}
# {"name"=>"Ansible-Sample-TDD", "hosts"=>[{"name" => "192.168.0.103:22","uri"=>"192.168.0.103","port"=>22}], "user"=>"root", "roles"=>["nginx", "mariadb"]}

namespace :serverspec do
properties.each do |property|
property["hosts"].each do |host|
desc "Run serverspec for #{property["name"]}"
RSpec::Core::RakeTask.new(property["name"].to_sym) do |t|
puts "Run serverspec for #{property["name"]} to #{host}"
ENV['TARGET_HOST'] = host
ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
ENV['TARGET_USER'] = property["user"]
if host.instance_of?(Hash)
ENV['TARGET_HOST'] = host["uri"]
ENV['TARGET_PORT'] = host["port"].to_s
ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
ENV['TARGET_USER'] = property["user"]
else
ENV['TARGET_HOST'] = host
ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
ENV['TARGET_USER'] = property["user"]
end
t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/src/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
options = Net::SSH::Config.for(host)

options[:user] ||= ENV['TARGET_USER']
options[:port] ||= ENV['TARGET_PORT']
options[:keys] ||= ENV['TARGET_PRIVATE_KEY']

set :host, options[:host_name] || host
Expand Down
161 changes: 161 additions & 0 deletions spec/inventory_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# coding: utf-8
require 'ansible_spec'

def create_file(name,content)
File.open(name, 'w') do |f|
f.puts content
end
end

def ready_test
tmp_hosts = 'hosts'
tmp_ansiblespec = '.ansiblespec'
tmp_playbook = 'site.yml'

content = <<'EOF'
---
-
playbook: site.yml
inventory: hosts
EOF

content_p = <<'EOF'
- name: Ansible-Sample-TDD
hosts: normal
user: root
roles:
- nginx
- mariadb
EOF

content_h = <<'EOF'
[normal]
192.168.0.1
192.168.0.2 ansible_ssh_port=22
192.168.0.3:5309
#[variables]
#192.168.0.4
#[variables:vars]
#ansible_ssh_port=22
#ansible_ssh_user=root
#[alias]
#jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
EOF
create_file(tmp_ansiblespec,content)
create_file(tmp_playbook,content_p)
create_file(tmp_hosts,content_h)
end

describe "load_targetsの実行" do
context '正常系:複数グループ:変数' do
tmp_hosts = 'hosts'
before(:all) do
ready_test
@res = AnsibleSpec.load_targets(tmp_hosts)
end

it 'res is hash' do
expect(@res.instance_of?(Hash)).to be_truthy
end

it 'exist 1 group' do
expect(@res.length).to eq 1
end

it 'exist group' do
expect(@res.key?('normal')).to be_truthy
#expect(@res.key?('variables')).to be_truthy
#expect(@res.key?('variables:vars')).not_to be_truthy
#expect(@res.key?('alias')).to be_truthy
end

it 'normal 192.168.0.1' do
expect(@res['normal'][0].instance_of?(String)).to be_truthy
expect(@res['normal'][0]).to eq '192.168.0.1'
end
it 'normal 192.168.0.2 ansible_ssh_port=22' do
expect(@res['normal'][1].instance_of?(Hash)).to be_truthy
expect(@res['normal'][1]['uri']).to eq '192.168.0.2'
expect(@res['normal'][1]['port']).to eq 22
end
it 'normal 192.168.0.3:5309' do
expect(@res['normal'][2].instance_of?(Hash)).to be_truthy
expect(@res['normal'][2]['uri']).to eq '192.168.0.3'
expect(@res['normal'][2]['port']).to eq 5309
end

after(:all) do
File.delete(tmp_hosts)
end
end
end

describe "get_propertiesの実行" do
require 'yaml'
tmp_ansiblespec = '.ansiblespec'
tmp_playbook = 'site.yml'
tmp_hosts = 'hosts'

before(:all) do
ready_test
@res = AnsibleSpec.get_properties
end

context '正常系' do
it 'res is array' do
expect(@res.instance_of?(Array)).to be_truthy
end

it 'res[0] is hash' do
expect(@res[0].instance_of?(Hash)).to be_truthy
end

it 'check 1 group' do
expect(@res[0].length).to eq 4
end

it 'exist name' do
expect(@res[0].key?('name')).to be_truthy
expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
end

it 'normal 192.168.0.1' do
expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
expect(@res[0]['hosts'][0]).to eq '192.168.0.1'
end

it 'normal 192.168.0.2 ansible_ssh_port=22' do
expect(@res[0]['hosts'][1].instance_of?(Hash)).to be_truthy
expect(@res[0]['hosts'][1]['uri']).to eq '192.168.0.2'
expect(@res[0]['hosts'][1]['port']).to eq 22
end

it 'normal 192.168.0.3:5309' do
expect(@res[0]['hosts'][2].instance_of?(Hash)).to be_truthy
expect(@res[0]['hosts'][2]['uri']).to eq '192.168.0.3'
expect(@res[0]['hosts'][2]['port']).to eq 5309
end

it 'exist user' do
expect(@res[0].key?('user')).to be_truthy
expect(@res[0]['user']).to eq 'root'
end

it 'exist roles' do
expect(@res[0].key?('roles')).to be_truthy
expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
expect(@res[0]['roles'][0]).to eq 'nginx'
expect(@res[0]['roles'][1]).to eq 'mariadb'
end

after(:all) do
File.delete(tmp_ansiblespec)
File.delete(tmp_playbook)
File.delete(tmp_hosts)
end
end
end
85 changes: 55 additions & 30 deletions spec/ssh_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
require 'spec_helper'
require 'ansible_spec'
require 'yaml'

set :backend, :ssh

describe 'ssh' do
context 'with root user' do
before do
create_normality
properties = AnsibleSpec.get_properties
@h = Hash.new
n = 0
properties.each do |property|
property["hosts"].each do |host|
#ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
#t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
set :host, host
set :ssh_options, :user => property["user"]
@ssh = double(:ssh)
allow(@ssh).to receive(:host).and_return(Specinfra.configuration.host)
allow(@ssh).to receive(:user).and_return(Specinfra.configuration.ssh_options[:user])
@h["task_#{n}"] = @ssh
n += 1
create_normality
properties = AnsibleSpec.get_properties
@h = Hash.new
n = 0
properties.each do |property|
property["hosts"].each do |host|
#ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
#t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
@ssh = double(:ssh)
if host.instance_of?(Hash)
set :host, host["uri"]
set :ssh_options, :user => property["user"], :port => host["port"]
allow(@ssh).to receive(:port).and_return(Specinfra.configuration.ssh_options[:port])
else
set :host, host
set :ssh_options, :user => property["user"]
end
allow(@ssh).to receive(:host).and_return(Specinfra.configuration.host)
allow(@ssh).to receive(:user).and_return(Specinfra.configuration.ssh_options[:user])
@h["task_#{n}"] = @ssh
n += 1
end
end
it 'should not prepend sudo' do
@h.each{|k,v|
if k == "task_0"
expect(v.user).to eq 'root'
expect(v.host).to eq '192.168.0.103'
elsif k == "task_1"
expect(v.user).to eq 'root'
expect(v.host).to eq '192.168.0.104'
end
}
end
end

it '192.168.0.1' do
v = @h["task_0"]
expect(v.user).to eq 'root'
expect(v.host).to eq '192.168.0.1'
end
it '192.168.0.2:22' do
v = @h["task_1"]
expect(v.user).to eq 'root'
expect(v.host).to eq '192.168.0.2'
expect(v.port).to eq 22
end
it '192.168.0.3 ansible_ssh_port=22' do
v = @h["task_2"]
expect(v.user).to eq 'root'
expect(v.host).to eq '192.168.0.3'
expect(v.port).to eq 5309
end
after do
delete_normality
end
Expand All @@ -55,17 +69,28 @@ def create_normality

content_p = <<'EOF'
- name: Ansible-Sample-TDD
hosts: server
hosts: normal
user: root
roles:
- nginx
- mariadb
EOF

content_h = <<'EOF'
[server]
192.168.0.103
192.168.0.104
[normal]
192.168.0.1
192.168.0.2 ansible_ssh_port=22
192.168.0.3:5309
#[variables]
#192.168.0.4
#[variables:vars]
#ansible_ssh_port=22
#ansible_ssh_user=root
#[alias]
#jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
EOF

File.open(tmp_ansiblespec, 'w') do |f|
Expand Down

0 comments on commit e0bb99c

Please sign in to comment.