rspec-system 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  require 'fileutils'
2
2
  require 'systemu'
3
+ require 'net/ssh'
3
4
 
4
5
  module RSpecSystem
5
6
  # A NodeSet implementation for Vagrant.
@@ -29,6 +30,16 @@ module RSpecSystem
29
30
 
30
31
  log.info "[Vagrant#setup] Running 'vagrant up'"
31
32
  vagrant("up")
33
+
34
+ # Establish ssh connectivity
35
+ ssh_channels = {}
36
+ nodes.each do |k,v|
37
+ log.info "[Vagrant#setup] establishing Net::SSH channel with #{k}"
38
+ chan = Net::SSH.start(k, 'vagrant', :config => ssh_config)
39
+ ssh_channels[k] = chan
40
+ end
41
+ RSpec.configuration.ssh_channels = ssh_channels
42
+
32
43
  nil
33
44
  end
34
45
 
@@ -36,6 +47,11 @@ module RSpecSystem
36
47
  #
37
48
  # @return [void]
38
49
  def teardown
50
+ log.info "[Vagrant#teardown] closing all ssh channels"
51
+ RSpec.configuration.ssh_channels.each do |k,v|
52
+ v.close unless v.closed?
53
+ end
54
+
39
55
  log.info "[Vagrant#teardown] Running 'vagrant destroy'"
40
56
  vagrant("destroy --force")
41
57
  nil
@@ -51,19 +67,8 @@ module RSpecSystem
51
67
  dest = opts[:n].name
52
68
  cmd = opts[:c]
53
69
 
54
- r = nil
55
- Dir.chdir(@vagrant_path) do
56
- ex = "vagrant ssh #{dest} --command \"cd /tmp && sudo sh -c '#{cmd}'\""
57
- log.debug("[vagrant#run] Running command: #{ex}")
58
- r = systemu ex
59
- log.debug("[Vagrant#run] Finished running command: #{ex}.")
60
- end
61
-
62
- {
63
- :exit_code => r[0].exitstatus,
64
- :stdout => r[1],
65
- :stderr => r[2]
66
- }
70
+ ssh_channels = RSpec.configuration.ssh_channels
71
+ ssh_exec!(ssh_channels[dest], "cd /tmp && sudo sh -c '#{cmd}'")
67
72
  end
68
73
 
69
74
  # Transfer files to a host in the NodeSet.
@@ -207,5 +212,48 @@ module RSpecSystem
207
212
  def randmac
208
213
  "080027" + (1..3).map{"%0.2X"%rand(256)}.join
209
214
  end
215
+
216
+ # Execute command via SSH.
217
+ #
218
+ # A special version of exec! from Net::SSH that returns exit code and exit
219
+ # signal as well. This method is blocking.
220
+ #
221
+ # @api private
222
+ # @param ssh [Net::SSH::Connection::Session] an active ssh session
223
+ # @param command [String] command to execute
224
+ # @return [Hash] a hash of results
225
+ def ssh_exec!(ssh, command)
226
+ r = {
227
+ :stdout => '',
228
+ :stderr => '',
229
+ :exit_code => nil,
230
+ :exit_signal => nil,
231
+ }
232
+ ssh.open_channel do |channel|
233
+ channel.exec(command) do |ch, success|
234
+ unless success
235
+ abort "FAILED: couldn't execute command (ssh.channel.exec)"
236
+ end
237
+ channel.on_data do |ch,data|
238
+ r[:stdout]+=data
239
+ end
240
+
241
+ channel.on_extended_data do |ch,type,data|
242
+ r[:stderr]+=data
243
+ end
244
+
245
+ channel.on_request("exit-status") do |ch,data|
246
+ r[:exit_code] = data.read_long
247
+ end
248
+
249
+ channel.on_request("exit-signal") do |ch, data|
250
+ r[:exit_signal] = data.read_string
251
+ end
252
+ end
253
+ end
254
+ ssh.loop
255
+
256
+ r
257
+ end
210
258
  end
211
259
  end
@@ -14,6 +14,8 @@ RSpec.configure do |c|
14
14
  c.add_setting :system_tmp
15
15
  # Block to execute for environment setup
16
16
  c.add_setting :system_setup_block
17
+ # Storage for ssh channels
18
+ c.add_setting :ssh_channels, :default => {}
17
19
 
18
20
  def nodeset
19
21
  Pathname.new(File.join(File.basename(__FILE__), '..', '.nodeset.yml'))
@@ -2,7 +2,7 @@
2
2
  Gem::Specification.new do |s|
3
3
  # Metadata
4
4
  s.name = "rspec-system"
5
- s.version = "0.3.4"
5
+ s.version = "0.3.5"
6
6
  s.authors = ["Ken Barber"]
7
7
  s.email = ["ken@bob.sh"]
8
8
  s.homepage = "https://github.com/kbarber/rspec-system"
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
19
19
  s.add_runtime_dependency "rspec"
20
20
  s.add_runtime_dependency "kwalify"
21
21
  s.add_runtime_dependency "systemu"
22
+ s.add_runtime_dependency "net-ssh", '~>2.6'
22
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-system
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: net-ssh
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.6'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.6'
62
78
  description:
63
79
  email:
64
80
  - ken@bob.sh