File: helper.rb

package info (click to toggle)
ruby-sshkit 1.21.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 700 kB
  • sloc: ruby: 3,522; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 2,459 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'tempfile'
require 'minitest/autorun'
require 'minitest/reporters'
require 'mocha/minitest'
require 'json'

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'sshkit'

Dir[File.expand_path('test/support/*.rb')].each { |file| require file }

class UnitTest < Minitest::Test

  def setup
    SSHKit.reset_configuration!
  end

  SSHKit::Backend::ConnectionPool.class_eval do
    alias_method :old_flush_connections, :flush_connections
    def flush_connections
      Thread.current[:sshkit_pool] = {}
    end
  end
end

class FunctionalTest < Minitest::Test

  def setup
    unless VagrantWrapper.running?
      warn "Vagrant VMs are not running. Please, start it manually with `vagrant up`"
    end
  end

  private

  def create_user_with_key(username, password = :secret)
    username, password = username.to_s, password.to_s

    keys = VagrantWrapper.hosts.collect do |_name, host|
      Net::SSH.start(host.hostname, host.user, port: host.port, password: host.password) do |ssh|

        # Remove the user, make it again, force-generate a key for him
        # short keys save us a few microseconds
        ssh.exec!("sudo userdel -rf #{username}; true") # The `rescue nil` of the shell world
        ssh.exec!("sudo useradd -m #{username}")
        ssh.exec!("sudo echo y | ssh-keygen -b 1024 -f #{username} -N ''")
        ssh.exec!("sudo chown vagrant:vagrant #{username}*")
        ssh.exec!("sudo echo #{username}:#{password} | chpasswd")

        # Make the .ssh directory, change the ownership and the
        ssh.exec!("sudo mkdir -p /home/#{username}/.ssh")
        ssh.exec!("sudo chown #{username}:#{username} /home/#{username}/.ssh")
        ssh.exec!("sudo chmod 700 /home/#{username}/.ssh")

        # Move the key to authorized keys and chown and chmod it
        ssh.exec!("sudo cat #{username}.pub > /home/#{username}/.ssh/authorized_keys")
        ssh.exec!("sudo chown #{username}:#{username} /home/#{username}/.ssh/authorized_keys")
        ssh.exec!("sudo chmod 600 /home/#{username}/.ssh/authorized_keys")

        key = ssh.exec!("cat /home/vagrant/#{username}")

        # Clean Up Files
        ssh.exec!("sudo rm #{username} #{username}.pub")

        key
      end
    end

    Hash[VagrantWrapper.hosts.collect { |n, _h| n.to_sym }.zip(keys)]
  end

end

#
# Force colours in Autotest
#
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new