serverspec 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/Rakefile +6 -8
  4. data/WindowsSupport.md +88 -0
  5. data/bin/serverspec-init +75 -0
  6. data/lib/serverspec/backend/base.rb +31 -0
  7. data/lib/serverspec/backend/cmd.rb +35 -0
  8. data/lib/serverspec/backend/exec.rb +1 -24
  9. data/lib/serverspec/backend/powershell/command.rb +36 -0
  10. data/lib/serverspec/backend/powershell/script_helper.rb +69 -0
  11. data/lib/serverspec/backend/powershell/support/check_file_access_rules.ps1 +12 -0
  12. data/lib/serverspec/backend/powershell/support/crop_text.ps1 +11 -0
  13. data/lib/serverspec/backend/powershell/support/find_group.ps1 +8 -0
  14. data/lib/serverspec/backend/powershell/support/find_installed_application.ps1 +7 -0
  15. data/lib/serverspec/backend/powershell/support/find_service.ps1 +5 -0
  16. data/lib/serverspec/backend/powershell/support/find_user.ps1 +8 -0
  17. data/lib/serverspec/backend/powershell/support/find_usergroup.ps1 +9 -0
  18. data/lib/serverspec/backend/powershell/support/is_port_listening.ps1 +13 -0
  19. data/lib/serverspec/backend/winrm.rb +26 -0
  20. data/lib/serverspec/backend.rb +5 -0
  21. data/lib/serverspec/commands/windows.rb +211 -0
  22. data/lib/serverspec/helper/cmd.rb +15 -0
  23. data/lib/serverspec/helper/type.rb +1 -1
  24. data/lib/serverspec/helper/windows.rb +9 -0
  25. data/lib/serverspec/helper/winrm.rb +15 -0
  26. data/lib/serverspec/helper.rb +3 -0
  27. data/lib/serverspec/setup.rb +59 -83
  28. data/lib/serverspec/type/windows_registry_key.rb +21 -0
  29. data/lib/serverspec/version.rb +1 -1
  30. data/lib/serverspec.rb +3 -0
  31. data/spec/backend/cmd/configuration_spec.rb +9 -0
  32. data/spec/backend/powershell/script_helper_spec.rb +77 -0
  33. data/spec/backend/winrm/configuration_spec.rb +9 -0
  34. data/spec/spec_helper.rb +18 -26
  35. data/spec/support/powershell_command_runner.rb +52 -0
  36. data/spec/windows/file_spec.rb +161 -0
  37. data/spec/windows/group_spec.rb +29 -0
  38. data/spec/windows/port_spec.rb +31 -0
  39. data/spec/windows/user_spec.rb +44 -0
  40. metadata +37 -2
@@ -0,0 +1,211 @@
1
+ module Serverspec
2
+ module Commands
3
+ class Windows
4
+ class NotSupportedError < Exception; end
5
+ REGISTRY_KEY_TYPES = {
6
+ :type_string => 'String',
7
+ :type_binary => 'Binary',
8
+ :type_dword => 'DWord',
9
+ :type_qword => 'QWord',
10
+ :type_multistring => 'MultiString',
11
+ :type_expandstring => 'ExpandString'
12
+ }
13
+
14
+ def method_missing method, *args
15
+ raise NotSupportedError.new "#{method} currently not supported in Windows os" if method.to_s =~ /^check_.+/
16
+ super(method, *args)
17
+ end
18
+
19
+ def check_file(file)
20
+ cmd = item_has_attribute file, 'Archive'
21
+ Backend::PowerShell::Command.new do
22
+ exec cmd
23
+ end
24
+ end
25
+
26
+ def check_file_hidden(file)
27
+ cmd = item_has_attribute file, 'Hidden'
28
+ Backend::PowerShell::Command.new do
29
+ exec cmd
30
+ end
31
+ end
32
+
33
+ def check_file_readonly(file)
34
+ cmd = item_has_attribute file, 'ReadOnly'
35
+ Backend::PowerShell::Command.new do
36
+ exec cmd
37
+ end
38
+ end
39
+
40
+ def check_file_system(file)
41
+ cmd = item_has_attribute file, 'System'
42
+ Backend::PowerShell::Command.new do
43
+ exec cmd
44
+ end
45
+ end
46
+
47
+ def check_directory(dir)
48
+ cmd = item_has_attribute dir, 'Directory'
49
+ Backend::PowerShell::Command.new do
50
+ exec cmd
51
+ end
52
+ end
53
+
54
+ def check_file_contain(file, pattern)
55
+ Backend::PowerShell::Command.new do
56
+ exec "[Io.File]::ReadAllText('#{file}') -match '#{convert_regexp(pattern)}'"
57
+ end
58
+ end
59
+
60
+ def check_file_contain_within file, pattern, from=nil, to=nil
61
+ from ||= '^'
62
+ to ||= '$'
63
+ Backend::PowerShell::Command.new do
64
+ using 'crop_text.ps1'
65
+ exec %Q[(CropText -text ([Io.File]::ReadAllText('#{file}')) -fromPattern '#{convert_regexp(from)}' -toPattern '#{convert_regexp(to)}') -match '#{pattern}']
66
+ end
67
+ end
68
+
69
+ def check_access_by_user(file, user, access)
70
+ case access
71
+ when 'r'
72
+ check_readable(file, user)
73
+ when 'w'
74
+ check_writable(file, user)
75
+ when 'x'
76
+ check_executable(file, user)
77
+ end
78
+ end
79
+
80
+ def check_readable(file, by_whom)
81
+ Backend::PowerShell::Command.new do
82
+ using 'check_file_access_rules.ps1'
83
+ exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'Read', 'ListDirectory')"
84
+ end
85
+ end
86
+
87
+ def check_writable(file, by_whom)
88
+ Backend::PowerShell::Command.new do
89
+ using 'check_file_access_rules.ps1'
90
+ exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'Write')"
91
+ end
92
+ end
93
+
94
+ def check_executable(file, by_whom)
95
+ Backend::PowerShell::Command.new do
96
+ using 'check_file_access_rules.ps1'
97
+ exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'ExecuteFile')"
98
+ end
99
+ end
100
+
101
+ def check_installed(package, version=nil)
102
+ version_selection = version.nil? ? "" : "-appVersion '#{version}'"
103
+ Backend::PowerShell::Command.new do
104
+ using 'find_installed_application.ps1'
105
+ exec "(FindInstalledApplication -appName '#{package}' #{version_selection}) -ne $null"
106
+ end
107
+ end
108
+
109
+ def check_enabled(service, level=nil)
110
+ Backend::PowerShell::Command.new do
111
+ using 'find_service.ps1'
112
+ exec "(FindService -name '#{service}').StartMode -eq 'Auto'"
113
+ end
114
+ end
115
+
116
+ def check_running(service)
117
+ Backend::PowerShell::Command.new do
118
+ using 'find_service.ps1'
119
+ exec "(FindService -name '#{service}').State -eq 'Running'"
120
+ end
121
+ end
122
+
123
+ def check_process(process)
124
+ Backend::PowerShell::Command.new do
125
+ exec "(Get-Process '#{process}') -ne $null"
126
+ end
127
+ end
128
+
129
+ def check_listening(port)
130
+ Backend::PowerShell::Command.new do
131
+ using 'is_port_listening.ps1'
132
+ exec "IsPortListening -portNumber #{port}"
133
+ end
134
+ end
135
+
136
+ def check_listening_with_protocol(port, protocol)
137
+ Backend::PowerShell::Command.new do
138
+ using 'is_port_listening.ps1'
139
+ exec "IsPortListening -portNumber #{port} -protocol '#{protocol}'"
140
+ end
141
+ end
142
+
143
+ def check_user(user)
144
+ user_id, domain = windows_account user
145
+ Backend::PowerShell::Command.new do
146
+ using 'find_user.ps1'
147
+ exec "(FindUser -userName '#{user_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
148
+ end
149
+ end
150
+
151
+ def check_group(group)
152
+ group_id, domain = windows_account group
153
+ Backend::PowerShell::Command.new do
154
+ using 'find_group.ps1'
155
+ exec "(FindGroup -groupName '#{group_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
156
+ end
157
+ end
158
+
159
+ def check_belonging_group(user, group)
160
+ user_id, user_domain = windows_account user
161
+ group_id, group_domain = windows_account group
162
+ Backend::PowerShell::Command.new do
163
+ using 'find_user.ps1'
164
+ using 'find_group.ps1'
165
+ using 'find_usergroup.ps1'
166
+ exec "(FindUserGroup -userName '#{user_id}'#{user_domain.nil? ? "" : " -userDomain '#{user_domain}'"} -groupName '#{group_id}'#{group_domain.nil? ? "" : " -groupDomain '#{group_domain}'"}) -ne $null"
167
+ end
168
+ end
169
+
170
+ def check_registry_key(key_name, key_property = {})
171
+ if key_property.empty?
172
+ cmd = "(Get-Item 'Registry::#{key_name}') -ne $null"
173
+ else
174
+ if key_property.key? :value
175
+ value = convert_key_property_value key_property
176
+ cmd = "(Compare-Object (Get-Item 'Registry::#{key_name}').GetValue('#{key_property[:name]}') #{value}) -eq $null"
177
+ else
178
+ cmd = "(Get-Item 'Registry::#{key_name}').GetValueKind('#{key_property[:name]}') -eq '#{REGISTRY_KEY_TYPES[key_property[:type]]}'"
179
+ end
180
+ end
181
+ Backend::PowerShell::Command.new { exec cmd }
182
+ end
183
+
184
+ private
185
+
186
+ def item_has_attribute item, attribute
187
+ "((Get-Item -Path '#{item}' -Force).attributes.ToString() -Split ', ') -contains '#{attribute}'"
188
+ end
189
+
190
+ def convert_key_property_value property
191
+ case property[:type]
192
+ when :type_binary
193
+ byte_array = [property[:value]].pack('H*').bytes.to_a
194
+ "([byte[]] #{byte_array.join(',')})"
195
+ when:type_dword, :type_qword
196
+ property[:value].hex
197
+ else
198
+ string_array = property[:value].split("\n").map {|s| "'#{s}'"}.reduce {|acc, s| "#{acc},#{s}"}
199
+ "@(#{string_array})"
200
+ end
201
+ end
202
+
203
+ def windows_account account
204
+ match = /((.+)\\)?(.+)/.match account
205
+ domain = match[2]
206
+ name = match[3]
207
+ [name, domain]
208
+ end
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,15 @@
1
+ module Serverspec
2
+ module Helper
3
+ module Cmd
4
+ def backend(commands_object=nil)
5
+ if ! respond_to?(:commands)
6
+ commands_object = Serverspec::Commands::Windows.new
7
+ end
8
+ instance = Serverspec::Backend::Cmd.instance
9
+ instance.set_commands(commands_object || commands)
10
+ instance
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -4,7 +4,7 @@ module Serverspec
4
4
  types = %w(
5
5
  base yumrepo service package port file cron command linux_kernel_parameter iptables host
6
6
  routing_table default_gateway selinux user group zfs ipnat ipfilter kernel_module interface php_config
7
- mail_alias
7
+ mail_alias windows_registry_key
8
8
  )
9
9
 
10
10
  types.each {|type| require "serverspec/type/#{type}" }
@@ -0,0 +1,9 @@
1
+ module Serverspec
2
+ module Helper
3
+ module Windows
4
+ def commands
5
+ Serverspec::Commands::Windows.new
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Serverspec
2
+ module Helper
3
+ module WinRM
4
+ def backend(commands_object=nil)
5
+ if ! respond_to?(:commands)
6
+ commands_object = Serverspec::Commands::Windows.new
7
+ end
8
+ instance = Serverspec::Backend::WinRM.instance
9
+ instance.set_commands(commands_object || commands)
10
+ instance
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -3,6 +3,8 @@ require 'etc'
3
3
  # Backend helpers
4
4
  require 'serverspec/helper/ssh'
5
5
  require 'serverspec/helper/exec'
6
+ require 'serverspec/helper/cmd'
7
+ require 'serverspec/helper/winrm'
6
8
  require 'serverspec/helper/puppet'
7
9
 
8
10
  # Command helpers
@@ -14,6 +16,7 @@ require 'serverspec/helper/solaris10'
14
16
  require 'serverspec/helper/solaris11'
15
17
  require 'serverspec/helper/smartos'
16
18
  require 'serverspec/helper/darwin'
19
+ require 'serverspec/helper/windows'
17
20
  require 'serverspec/helper/detect_os'
18
21
 
19
22
  # Attributes helper
@@ -1,21 +1,18 @@
1
1
  require 'fileutils'
2
+ require 'erb'
2
3
 
3
4
  module Serverspec
4
5
  class Setup
5
6
  def self.run
6
- prompt = <<-EOF
7
- Select a backend type:
8
7
 
9
- 1) SSH
10
- 2) Exec (local)
8
+ ask_os_type
11
9
 
12
- Select number:
13
- EOF
14
- print prompt.chop
15
- num = gets.to_i - 1
16
- puts
10
+ if @os_type == 'UN*X'
11
+ ask_unix_backend
12
+ else
13
+ ask_windows_backend
14
+ end
17
15
 
18
- @backend_type = [ 'Ssh', 'Exec' ][num]
19
16
  if @backend_type == 'Ssh'
20
17
  print "Vagrant instance y/n: "
21
18
  @vagrant = gets.chomp
@@ -37,14 +34,63 @@ EOF
37
34
  else
38
35
  @hostname = 'localhost'
39
36
  end
37
+
40
38
  [ 'spec', "spec/#{@hostname}" ].each { |dir| safe_mkdir(dir) }
41
39
  safe_create_spec
42
40
  safe_create_spec_helper
43
41
  safe_create_rakefile
44
42
  end
45
43
 
46
- def self.safe_create_spec
44
+ def self.ask_os_type
45
+ prompt = <<-EOF
46
+ Select OS type:
47
+
48
+ 1) UN*X
49
+ 2) Windows
50
+
51
+ Select number:
52
+ EOF
53
+
54
+ print prompt.chop
55
+ num = gets.to_i - 1
56
+ puts
57
+
58
+ @os_type = [ 'UN*X', 'Windows' ][num] || 'UN*X'
59
+ end
60
+
61
+ def self.ask_unix_backend
62
+ prompt = <<-EOF
63
+ Select a backend type:
64
+
65
+ 1) SSH
66
+ 2) Exec (local)
67
+
68
+ Select number:
69
+ EOF
70
+ print prompt.chop
71
+ num = gets.to_i - 1
72
+ puts
73
+
74
+ @backend_type = [ 'Ssh', 'Exec' ][num] || 'Exec'
75
+ end
76
+
77
+ def self.ask_windows_backend
78
+ prompt = <<-EOF
79
+ Select a backend type:
80
+
81
+ 1) WinRM
82
+ 2) Cmd (local)
83
+
84
+ Select number:
85
+ EOF
86
+ print prompt.chop
87
+ num = gets.to_i - 1
88
+ puts
89
+
90
+ @backend_type = [ 'WinRM', 'Cmd' ][num] || 'Exec'
91
+ end
47
92
 
93
+ def self.safe_create_spec
48
94
  content = <<-EOF
49
95
  require 'spec_helper'
50
96
 
@@ -92,77 +138,8 @@ EOF
92
138
  end
93
139
 
94
140
  def self.safe_create_spec_helper
95
- content = <<-EOF
96
- require 'serverspec'
97
- require 'pathname'
98
- ### include requirements ###
99
-
100
- ### include backend helper ###
101
- include Serverspec::Helper::DetectOS
102
-
103
- RSpec.configure do |c|
104
- if ENV['ASK_SUDO_PASSWORD']
105
- require 'highline/import'
106
- c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
107
- else
108
- c.sudo_password = ENV['SUDO_PASSWORD']
109
- end
110
- ### include backend conf ###
111
- end
112
- EOF
113
-
114
- if not @backend_type.nil?
115
- content.gsub!(/### include backend helper ###/, "include Serverspec::Helper::#{@backend_type}")
116
- case @backend_type
117
- when 'Ssh'
118
- content.gsub!(/### include requirements ###/, "require 'net/ssh'")
119
- content.gsub!(/### include backend conf ###/, "c.before :all do
120
- block = self.class.metadata[:example_group_block]
121
- if RUBY_VERSION.start_with?('1.8')
122
- file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
123
- else
124
- file = block.source_location.first
125
- end
126
- host = File.basename(Pathname.new(file).dirname)
127
- if c.host != host
128
- c.ssh.close if c.ssh
129
- c.host = host
130
- options = Net::SSH::Config.for(c.host)
131
- user = options[:user] || Etc.getlogin
132
- ### include vagrant conf ###
133
- c.ssh = Net::SSH.start(c.host, user, options)
134
- end
135
- end")
136
- if @vagrant
137
- content.gsub!(/### include vagrant conf ###/,"
138
- vagrant_up = `vagrant up #{@hostname}`
139
- config = `vagrant ssh-config #{@hostname}`
140
- if config != ''
141
- config.each_line do |line|
142
- if match = /HostName (.*)/.match(line)
143
- c.host = match[1]
144
- elsif match = /User (.*)/.match(line)
145
- user = match[1]
146
- elsif match = /IdentityFile (.*)/.match(line)
147
- options[:keys] = [match[1].gsub(/\"/,'')]
148
- elsif match = /Port (.*)/.match(line)
149
- options[:port] = match[1]
150
- end
151
- end
152
- end
153
- ")
154
- else
155
- content.gsub!(/### include vagrant conf ###/,'')
156
- end
157
- when 'Exec'
158
- content.gsub!(/### include backend conf ###/, "c.before :all do
159
- end")
160
- when 'Puppet'
161
- content.gsub!(/### include requirements ###/, "require 'puppet'\nrequire 'serverspec/backend/puppet'
162
- ")
163
- end
164
- end
165
-
141
+ requirements = []
142
+ content = ERB.new(DATA.read, nil, '-').result(binding)
166
143
  if File.exists? 'spec/spec_helper.rb'
167
144
  old_content = File.read('spec/spec_helper.rb')
168
145
  if old_content != content
@@ -233,6 +210,5 @@ EOF
233
210
  exit 1
234
211
  end
235
212
  end
236
-
237
213
  end
238
214
  end
@@ -0,0 +1,21 @@
1
+ module Serverspec
2
+ module Type
3
+ class WindowsRegistryKey < Base
4
+ def exists?
5
+ backend.check_registry_key(@name)
6
+ end
7
+
8
+ def has_property?(property_name, property_type = :type_string)
9
+ backend.check_registry_key(@name, {:name => property_name, :type => property_type})
10
+ end
11
+
12
+ def has_value?(value)
13
+ backend.check_registry_key(@name, {:name => '', :type => :type_string, :value => value})
14
+ end
15
+
16
+ def has_property_value?(property_name, property_type, value)
17
+ backend.check_registry_key(@name, {:name => property_name, :type => property_type, :value => value})
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.8.1"
2
+ VERSION = "0.9.0"
3
3
  end
data/lib/serverspec.rb CHANGED
@@ -16,6 +16,7 @@ require 'serverspec/commands/solaris10'
16
16
  require 'serverspec/commands/solaris11'
17
17
  require 'serverspec/commands/smartos'
18
18
  require 'serverspec/commands/darwin'
19
+ require 'serverspec/commands/windows'
19
20
  require 'serverspec/configuration'
20
21
  require 'rspec/core/formatters/base_formatter'
21
22
 
@@ -39,10 +40,12 @@ RSpec.configure do |c|
39
40
  c.include(Serverspec::Helper::Solaris11, :os => :solaris11)
40
41
  c.include(Serverspec::Helper::SmartOS, :os => :smartos)
41
42
  c.include(Serverspec::Helper::Darwin, :os => :darwin)
43
+ c.include(Serverspec::Helper::Windows, :os => :windows)
42
44
  c.add_setting :os, :default => nil
43
45
  c.add_setting :host, :default => nil
44
46
  c.add_setting :ssh, :default => nil
45
47
  c.add_setting :sudo_password, :default => nil
48
+ c.add_setting :winrm, :default => nil
46
49
  Serverspec.configuration.defaults.each { |k, v| c.add_setting k, :default => v }
47
50
  c.before :each do
48
51
  backend.set_example(example)
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'support/powershell_command_runner'
3
+
4
+ include Serverspec::Helper::Cmd
5
+ include Serverspec::Helper::Windows
6
+
7
+ describe "Cmd" do
8
+ it_behaves_like "a powershell command runner"
9
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ include Serverspec::Backend::PowerShell::ScriptHelper
4
+
5
+ describe 'build command with path' do
6
+ before :each do
7
+ RSpec.configure do |c|
8
+ c.path = 'c:/test/path/bin'
9
+ end
10
+ end
11
+
12
+ it "should prefix the command with the path instruction" do
13
+ cmd = build_command('run_script -f param')
14
+ cmd.should eq <<-eof
15
+ $env:path = "c:/test/path/bin;$env:path"
16
+ run_script -f param
17
+ eof
18
+ end
19
+
20
+ after :each do
21
+ RSpec.configure do |c|
22
+ c.path = nil
23
+ end
24
+ end
25
+ end
26
+
27
+ describe 'add pre-command' do
28
+ before :each do
29
+ Serverspec.configuration.pre_command = 'test_pre_command'
30
+ end
31
+
32
+ it "should add the test for pre_command before the command" do
33
+ cmd = add_pre_command('run_script -f param')
34
+ cmd.should eq <<-eof
35
+ if (test_pre_command)
36
+ {
37
+ run_script -f param
38
+ }
39
+ eof
40
+ end
41
+
42
+ context "with path" do
43
+ before :each do
44
+ RSpec.configure do |c|
45
+ c.path = 'c:/test/path/bin'
46
+ end
47
+ end
48
+
49
+ it "should add the path instruction and the test for pre_command before the command" do
50
+ cmd = add_pre_command('run_script -f param')
51
+ cmd.should eq <<-eof
52
+ $env:path = "c:/test/path/bin;$env:path"
53
+ if (test_pre_command)
54
+ {
55
+ run_script -f param
56
+ }
57
+ eof
58
+ end
59
+
60
+ after :each do
61
+ RSpec.configure do |c|
62
+ c.path = nil
63
+ end
64
+ end
65
+ end
66
+
67
+ after :each do
68
+ Serverspec.configuration.pre_command = nil
69
+ end
70
+ end
71
+
72
+ describe "script encoding" do
73
+ it "should encode the given script" do
74
+ script = encode_script("test_powershell_script")
75
+ script.should == "dABlAHMAdABfAHAAbwB3AGUAcgBzAGgAZQBsAGwAXwBzAGMAcgBpAHAAdAA="
76
+ end
77
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'support/powershell_command_runner'
3
+
4
+ include Serverspec::Helper::WinRM
5
+ include Serverspec::Helper::Windows
6
+
7
+ describe "WinRM" do
8
+ it_behaves_like "a powershell command runner"
9
+ end
data/spec/spec_helper.rb CHANGED
@@ -11,10 +11,8 @@ Dir[PROJECT_ROOT.join("spec/support/**/*.rb")].each { |file| require(file) }
11
11
 
12
12
  module Serverspec
13
13
  module Backend
14
- class Exec
15
- def run_command(cmd)
16
- cmd = build_command(cmd)
17
- cmd = add_pre_command(cmd)
14
+ module TestCommandRunner
15
+ def do_run cmd
18
16
  if @example
19
17
  @example.metadata[:subject].set_command(cmd)
20
18
  end
@@ -36,29 +34,23 @@ module Serverspec
36
34
  end
37
35
  end
38
36
  end
39
-
40
- class Ssh
41
- def run_command(cmd)
42
- cmd = build_command(cmd)
43
- cmd = add_pre_command(cmd)
44
- if @example
45
- @example.metadata[:subject].set_command(cmd)
37
+ [Exec, Ssh].each do |clz|
38
+ clz.class_eval do
39
+ include TestCommandRunner
40
+ def run_command(cmd)
41
+ cmd = build_command(cmd)
42
+ cmd = add_pre_command(cmd)
43
+ do_run cmd
46
44
  end
47
-
48
- if cmd =~ /invalid/
49
- {
50
- :stdout => ::Serverspec.configuration.stdout,
51
- :stderr => ::Serverspec.configuration.stderr,
52
- :exit_status => 1,
53
- :exit_signal => nil
54
- }
55
- else
56
- {
57
- :stdout => ::Serverspec.configuration.stdout,
58
- :stderr => ::Serverspec.configuration.stderr,
59
- :exit_status => 0,
60
- :exit_signal => nil
61
- }
45
+ end
46
+ end
47
+ [Cmd, WinRM].each do |clz|
48
+ clz.class_eval do
49
+ include TestCommandRunner
50
+ def run_command(cmd)
51
+ cmd = build_command(cmd.script)
52
+ cmd = add_pre_command(cmd)
53
+ do_run cmd
62
54
  end
63
55
  end
64
56
  end