aboutsummaryrefslogtreecommitdiffstats
path: root/switchtower/test/command_test.rb
blob: 7005ba0f2d6229dad2ad0f97288d8fbb5a22f415 (plain) (blame)
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
$:.unshift File.dirname(__FILE__) + "/../lib"

require 'stringio'
require 'test/unit'
require 'switchtower/command'

class CommandTest < Test::Unit::TestCase
  class MockSession
    def open_channel
      { :closed => true, :status => 0 }
    end
  end

  class MockActor
    attr_reader :sessions

    def initialize
      @sessions = Hash.new { |h,k| h[k] = MockSession.new }
    end
  end

  def setup
    @actor = MockActor.new
  end

  def test_command_executes_on_all_servers
    command = SwitchTower::Command.new(%w(server1 server2 server3),
      "hello", nil, {}, @actor)
    assert_equal %w(server1 server2 server3), @actor.sessions.keys.sort
  end

  def test_command_with_newlines
    command = SwitchTower::Command.new(%w(server1), "hello\nworld", nil, {},
      @actor)
    assert_equal "hello\\\nworld", command.command
  end

  def test_command_with_windows_newlines
    command = SwitchTower::Command.new(%w(server1), "hello\r\nworld", nil, {},
      @actor)
    assert_equal "hello\\\nworld", command.command
  end
end