test/vagrant/ssh_test.rb in vagrantup-0.5.4 vs test/vagrant/ssh_test.rb in vagrantup-0.6.0

- old
+ new

@@ -1,14 +1,10 @@ require "test_helper" class SshTest < Test::Unit::TestCase def mock_ssh - @env = mock_environment do |config| - yield config if block_given? - end - - @forwarded_ports = [] + @env = vagrant_env @network_adapters = [] @vm = mock("vm") @vm.stubs(:network_adapters).returns(@network_adapters) @env.stubs(:vm).returns(mock_vm(@env)) @@ -16,18 +12,17 @@ @ssh = Vagrant::SSH.new(@env) end setup do - VirtualBox.stubs(:version).returns("3.1.4") + VirtualBox.stubs(:version).returns("3.2.4") end context "connecting to external SSH" do setup do mock_ssh @ssh.stubs(:check_key_permissions) - @ssh.stubs(:error_and_exit) Kernel.stubs(:exec) Vagrant::Util::Platform.stubs(:leopard?).returns(false) end @@ -66,32 +61,36 @@ context "on leopard" do setup do Vagrant::Util::Platform.stubs(:leopard?).returns(true) end + teardown do + Vagrant::Util::Platform.stubs(:leopard?).returns(false) + end + should "fork, exec, and wait" do pid = mock("pid") @ssh.expects(:fork).once.returns(pid) Process.expects(:wait).with(pid) @ssh.connect end end context "checking windows" do + teardown do + Mario::Platform.forced = Mario::Platform::Linux + end + should "error and exit if the platform is windows" do - Mario::Platform.expects(:windows?).returns(true) - @ssh.expects(:error_and_exit).with do |error_name, opts| - opts[:key_path] && opts[:ssh_port] - end - @ssh.connect + Mario::Platform.forced = Mario::Platform::Windows7 + assert_raises(Vagrant::Errors::SSHUnavailableWindows) { @ssh.connect } end should "not error and exit if the platform is anything other that windows" do - Mario::Platform.expects(:windows?).returns(false) - @ssh.expects(:error_and_exit).never - @ssh.connect + Mario::Platform.forced = Mario::Platform::Linux + assert_nothing_raised { @ssh.connect } end end def ssh_exec_expect(port, key_path, uname, host) Kernel.expects(:exec).with() do |arg| @@ -169,17 +168,10 @@ scp.expects(:upload!).with("foo", "bar").once Net::SCP.expects(:new).with(ssh.session).returns(scp).once @ssh.expects(:execute).yields(ssh).once @ssh.upload!("foo", "bar") end - - should "retry 5 times" do - @ssh.expects(:execute).times(5).raises(IOError) - assert_raises(IOError) { - @ssh.upload!("foo", "bar") - } - end end context "checking if host is up" do setup do mock_ssh @@ -203,11 +195,11 @@ @thread.expects(:join).with(@env.config.ssh.timeout).once @ssh.up? end should "return false if the connection is refused" do - Net::SSH.expects(:start).raises(Errno::ECONNREFUSED) + Net::SSH.expects(:start).times(5).raises(Errno::ECONNREFUSED) assert_nothing_raised { assert !@ssh.up? } end @@ -223,12 +215,11 @@ assert @ssh.up? end should "error and exit if a Net::SSH::AuthenticationFailed is raised" do @ssh.expects(:execute).raises(Net::SSH::AuthenticationFailed) - @ssh.expects(:error_and_exit).with(:vm_ssh_auth_failed).once - @ssh.up? + assert_raises(Vagrant::Errors::SSHAuthenticationFailed) { @ssh.up? } end end context "getting the ssh port" do setup do @@ -250,15 +241,19 @@ @stat = mock("stat") @stat.stubs(:owned?).returns(true) File.stubs(:stat).returns(@stat) - Mario::Platform.stubs(:windows?).returns(false) + Mario::Platform.forced = Mario::Platform::Linux end + teardown do + Mario::Platform.forced = Mario::Platform::Linux + end + should "do nothing if on windows" do - Mario::Platform.stubs(:windows?).returns(true) + Mario::Platform.forced = Mario::Platform::Windows7 File.expects(:stat).never @ssh.check_key_permissions(@key_path) end should "do nothing if the user is not the owner" do @@ -276,27 +271,24 @@ should "chmod the file if the file perms aren't 600" do perm_sequence = sequence("perm_seq") @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence) File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence) @ssh.expects(:file_perms).returns("600").in_sequence(perm_sequence) - @ssh.expects(:error_and_exit).never - @ssh.check_key_permissions(@key_path) + assert_nothing_raised { @ssh.check_key_permissions(@key_path) } end should "error and exit if the resulting chmod doesn't work" do perm_sequence = sequence("perm_seq") @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence) File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence) @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence) - @ssh.expects(:error_and_exit).once.with(:ssh_bad_permissions, :key_path => @key_path).in_sequence(perm_sequence) - @ssh.check_key_permissions(@key_path) + assert_raises(Vagrant::Errors::SSHKeyBadPermissions) { @ssh.check_key_permissions(@key_path) } end should "error and exit if a bad file perm is raised" do @ssh.expects(:file_perms).with(@key_path).returns("900") File.expects(:chmod).raises(Errno::EPERM) - @ssh.expects(:error_and_exit).once.with(:ssh_bad_permissions, :key_path => @key_path) - @ssh.check_key_permissions(@key_path) + assert_raises(Vagrant::Errors::SSHKeyBadPermissions) { @ssh.check_key_permissions(@key_path) } end end context "getting file permissions" do setup do