lib/rspec-system/node_set/base.rb in rspec-system-1.0.0 vs lib/rspec-system/node_set/base.rb in rspec-system-1.1.0
- old
+ new
@@ -58,7 +58,81 @@
end
else
return nodes[dn]
end
end
+
+ # Return a random string of chars, used for temp dir creation
+ #
+ # @return [String] string of 50 random characters A-Z and a-z
+ def random_string(length = 50)
+ o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
+ (0...length).map{ o[rand(o.length)] }.join
+ end
+
+ # Generates a random string for use in remote transfers.
+ #
+ # @return [String] a random path
+ # @todo Very Linux dependant, probably need to consider OS X and Windows at
+ # least.
+ def tmppath
+ '/tmp/' + random_string
+ end
+
+ # Execute command via SSH.
+ #
+ # A special version of exec! from Net::SSH that returns exit code and exit
+ # signal as well. This method is blocking.
+ #
+ # @param ssh [Net::SSH::Connection::Session] an active ssh session
+ # @param command [String] command to execute
+ # @return [Hash] a hash of results
+ def ssh_exec!(ssh, command)
+ r = {
+ :stdout => '',
+ :stderr => '',
+ :exit_code => nil,
+ :exit_signal => nil,
+ }
+ ssh.open_channel do |channel|
+ channel.exec(command) do |ch, success|
+ unless success
+ abort "FAILED: couldn't execute command (ssh.channel.exec)"
+ end
+ channel.on_data do |ch,data|
+ d = data
+ print d
+ r[:stdout]+=d
+ end
+
+ channel.on_extended_data do |ch,type,data|
+ d = data
+ print d
+ r[:stderr]+=d
+ end
+
+ channel.on_request("exit-status") do |ch,data|
+ c = data.read_long
+ puts "Exit code: #{c}"
+ r[:exit_code] = c
+ end
+
+ channel.on_request("exit-signal") do |ch, data|
+ s = data.read_string
+ puts "Exit signal: #{s}"
+ r[:exit_signal] = s
+ end
+ end
+ end
+ ssh.loop
+
+ r
+ end
+
+ # Return a random mac address
+ #
+ # @return [String] a random mac address
+ def randmac
+ "080027" + (1..3).map{"%0.2X"%rand(256)}.join
+ end
end
end