serverspec 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require 'etc'
2
2
 
3
3
  module Serverspec
4
4
  module SshHelper
5
- def ssh_exec(cmd, opt={})
5
+ def do_check(cmd, opt={})
6
6
  cmd = "sudo #{cmd}" if not RSpec.configuration.ssh.options[:user] == 'root'
7
7
  ssh_exec!(cmd)
8
8
  end
@@ -41,7 +41,16 @@ module Serverspec
41
41
  ssh.loop
42
42
  { :stdout => stdout_data, :stderr => stderr_data, :exit_code => exit_code, :exit_signal => exit_signal }
43
43
  end
44
+ end
44
45
 
46
+ module ExecHelper
47
+ def do_check(cmd, opts={})
48
+ stdout = `#{cmd} 2>&1`
49
+ # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
50
+ #stdout, stderr, status = Open3.capture3(cmd)
51
+ { :stdout => stdout, :stderr => nil,
52
+ :exit_code => $?, :exit_signal => nil }
53
+ end
45
54
  end
46
55
 
47
56
  module RedHatHelper
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_directory do
2
2
  match do |actual|
3
- ret = ssh_exec(commands.check_directory(actual))
3
+ ret = do_check(commands.check_directory(actual))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_enabled do
2
2
  match do |actual|
3
- ret = ssh_exec(commands.check_enabled(actual))
3
+ ret = do_check(commands.check_enabled(actual))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_file do
2
2
  match do |actual|
3
- ret = ssh_exec(commands.check_file(actual))
3
+ ret = do_check(commands.check_file(actual))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_group do
2
2
  match do |actual|
3
- ret = ssh_exec(commands.check_group(actual))
3
+ ret = do_check(commands.check_group(actual))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_grouped_into do |group|
2
2
  match do |file|
3
- ret = ssh_exec(commands.check_grouped(file, group))
3
+ ret = do_check(commands.check_grouped(file, group))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_installed do
2
2
  match do |actual|
3
- ret = ssh_exec(commands.check_installed(actual))
3
+ ret = do_check(commands.check_installed(actual))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_installed_by_gem do
2
2
  match do |name|
3
- ret = ssh_exec(commands.check_installed_by_gem(name))
3
+ ret = do_check(commands.check_installed_by_gem(name))
4
4
  res = ret[:exit_code] == 0
5
5
  if res && @version
6
6
  res = false if not ret[:stdout].match(/\(#{@version}\)/)
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_linked_to do |target|
2
2
  match do |link|
3
- ret = ssh_exec(commands.check_link(link, target))
3
+ ret = do_check(commands.check_link(link, target))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,7 +1,7 @@
1
1
  RSpec::Matchers.define :be_listening do
2
2
  match do |actual|
3
3
  port = actual.gsub(/port\s+/, '')
4
- ret = ssh_exec(commands.check_listening(port))
4
+ ret = do_check(commands.check_listening(port))
5
5
  ret[:exit_code] == 0
6
6
  end
7
7
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_mode do |mode|
2
2
  match do |file|
3
- ret = ssh_exec(commands.check_mode(file, mode))
3
+ ret = do_check(commands.check_mode(file, mode))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_owned_by do |owner|
2
2
  match do |file|
3
- ret = ssh_exec(commands.check_owner(file, owner))
3
+ ret = do_check(commands.check_owner(file, owner))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,8 +1,8 @@
1
1
  RSpec::Matchers.define :be_running do
2
2
  match do |process|
3
- ret = ssh_exec(commands.check_running(process))
3
+ ret = do_check(commands.check_running(process))
4
4
  if ret[:exit_code] == 1 || ret[:stdout] =~ /stopped/
5
- ret = ssh_exec(commands.check_process(process))
5
+ ret = do_check(commands.check_process(process))
6
6
  end
7
7
  ret[:exit_code] == 0
8
8
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_user do
2
2
  match do |actual|
3
- ret = ssh_exec(commands.check_user(actual))
3
+ ret = do_check(commands.check_user(actual))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :belong_to_group do |group|
2
2
  match do |user|
3
- ret = ssh_exec(commands.check_belonging_group(user, group))
3
+ ret = do_check(commands.check_belonging_group(user, group))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -5,7 +5,7 @@ RSpec::Matchers.define :contain do |pattern|
5
5
  else
6
6
  cmd = commands.check_file_contain_within(file, pattern, @from, @to)
7
7
  end
8
- ret = ssh_exec(cmd)
8
+ ret = do_check(cmd)
9
9
  ret[:exit_code] == 0
10
10
  end
11
11
  # for contain(pattern).from(/A/).to(/B/)
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :get_stdout do |expected|
2
2
  match do |command|
3
- ret = ssh_exec(command)
3
+ ret = do_check(command)
4
4
  ret[:stdout] =~ /#{expected}/
5
5
  end
6
6
  end
@@ -1,7 +1,7 @@
1
1
  RSpec::Matchers.define :have_cron_entry do |entry|
2
2
  match do |actual|
3
3
  @user ||= 'root'
4
- ret = ssh_exec(commands.check_cron_entry(@user, entry))
4
+ ret = do_check(commands.check_cron_entry(@user, entry))
5
5
  ret[:exit_code] == 0
6
6
  end
7
7
  chain :with_user do |user|
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :have_iptables_rule do |rule|
2
2
  match do |iptables|
3
- ret = ssh_exec(commands.check_iptables_rule(rule, @table, @chain))
3
+ ret = do_check(commands.check_iptables_rule(rule, @table, @chain))
4
4
  ret[:exit_code] == 0
5
5
  end
6
6
  chain :with_table do |table|
@@ -3,8 +3,25 @@ require 'fileutils'
3
3
  module Serverspec
4
4
  class Setup
5
5
  def self.run
6
- print "Input target host name: "
7
- @hostname = gets.chomp
6
+ prompt = <<-EOF
7
+ Select a backend type:
8
+
9
+ 1) SSH
10
+ 2) Exec (local)
11
+
12
+ Select number:
13
+ EOF
14
+ print prompt.chop
15
+ num = gets.to_i - 1
16
+ puts
17
+
18
+ @backend_type = [ 'Ssh', 'Exec' ][num]
19
+ if @backend_type == 'Ssh'
20
+ print "Input target host name: "
21
+ @hostname = gets.chomp
22
+ else
23
+ @hostname = 'localhost'
24
+ end
8
25
 
9
26
  prompt = <<-EOF
10
27
 
@@ -82,8 +99,16 @@ require 'pathname'
82
99
  require 'net/ssh'
83
100
 
84
101
  RSpec.configure do |c|
102
+ ### include backend helper ###
85
103
  ### include os helper ###
86
- c.before do
104
+ ### include backend conf ###
105
+ end
106
+ EOF
107
+
108
+ if not @backend_type.nil?
109
+ content.gsub!(/### include backend helper ###/, "c.include(Serverspec::#{@backend_type}Helper)")
110
+ if @backend_type == 'Ssh'
111
+ content.gsub!(/### include backend conf ###/, "c.before do
87
112
  host = File.basename(Pathname.new(example.metadata[:location]).dirname)
88
113
  if c.host != host
89
114
  c.ssh.close if c.ssh
@@ -93,9 +118,9 @@ RSpec.configure do |c|
93
118
  c.ssh = Net::SSH.start(c.host, user, options)
94
119
  end
95
120
  end
96
- end
97
- EOF
98
-
121
+ ")
122
+ end
123
+ end
99
124
  if not @os_type.nil?
100
125
  content.gsub!(/### include os helper ###/, "c.include(Serverspec::#{@os_type}Helper)")
101
126
  end
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,7 @@ Dir[PROJECT_ROOT.join("spec/support/**/*.rb")].each { |file| require(file) }
7
7
 
8
8
  module Serverspec
9
9
  module SshHelper
10
- def ssh_exec(cmd)
10
+ def do_check(cmd)
11
11
  if cmd =~ /invalid/
12
12
  { :stdout => '', :stderr => '', :exit_code => 1, :exit_signal => nil }
13
13
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000 Z
12
+ date: 2013-04-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh