summaryrefslogtreecommitdiff
path: root/test/channel_test.rb
blob: 475c59b176d1a759537f7206aea251490de8c708 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require 'common'
require 'net/ssh/multi/channel'

class ChannelTest < Minitest::Test
  def test_each_should_iterate_over_each_component_channel
    channels = [mock('channel'), mock('channel'), mock('channel')]
    channel = Net::SSH::Multi::Channel.new(mock('session'), channels)
    result = []
    channel.each { |c| result << c }
    assert_equal channels, result
  end

  def test_property_accessors
    channel = Net::SSH::Multi::Channel.new(mock('session'), [])
    channel[:foo] = "hello"
    assert_equal "hello", channel[:foo]
    channel['bar'] = "goodbye"
    assert_equal "goodbye", channel['bar']
    assert_nil channel[:bar]
    assert_nil channel['foo']
  end

  def test_exec_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:exec).with('ls -l').yields(c1)
    c2.expects(:exec).with('ls -l').yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.exec('ls -l') { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_request_pty_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:request_pty).with(:foo => 5).yields(c1)
    c2.expects(:request_pty).with(:foo => 5).yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.request_pty(:foo => 5) { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_send_data_should_delegate_to_component_channels
    c1, c2 = mock('channel'), mock('channel')
    c1.expects(:send_data).with("hello\n")
    c2.expects(:send_data).with("hello\n")
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.send_data("hello\n")
  end

  def test_active_should_be_true_if_all_component_channels_are_active
    c1, c2, c3 = stub('channel', :active? => true), stub('channel', :active? => true), stub('channel', :active? => true)
    channel = Net::SSH::Multi::Channel.new(stub('session'), [c1, c2, c3])
    assert channel.active?
  end

  def test_active_should_be_true_if_any_component_channels_are_active
    c1, c2, c3 = stub('channel', :active? => true), stub('channel', :active? => false), stub('channel', :active? => false)
    channel = Net::SSH::Multi::Channel.new(stub('session'), [c1, c2, c3])
    assert channel.active?
  end

  def test_active_should_be_false_if_no_component_channels_are_active
    c1, c2, c3 = stub('channel', :active? => false), stub('channel', :active? => false), stub('channel', :active? => false)
    channel = Net::SSH::Multi::Channel.new(stub('session'), [c1, c2, c3])
    assert !channel.active?
  end

  def test_wait_should_block_until_active_is_false
    channel = Net::SSH::Multi::Channel.new(MockSession.new, [])
    channel.expects(:active?).times(4).returns(true,true,true,false)
    assert_equal channel, channel.wait
  end

  def test_close_should_delegate_to_component_channels
    c1, c2 = mock('channel'), mock('channel')
    c1.expects(:close)
    c2.expects(:close)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.close
  end

  def test_eof_bang_should_delegate_to_component_channels
    c1, c2 = mock('channel'), mock('channel')
    c1.expects(:eof!)
    c2.expects(:eof!)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.eof!
  end

  def test_on_data_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:on_data).yields(c1)
    c2.expects(:on_data).yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.on_data { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_on_extended_data_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:on_extended_data).yields(c1)
    c2.expects(:on_extended_data).yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.on_extended_data { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_on_process_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:on_process).yields(c1)
    c2.expects(:on_process).yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.on_process { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_on_close_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:on_close).yields(c1)
    c2.expects(:on_close).yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.on_close { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_on_eof_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:on_eof).yields(c1)
    c2.expects(:on_eof).yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.on_eof { |c| results << c }
    assert_equal [c1, c2], results
  end

  def test_on_request_should_delegate_to_component_channels
    c1, c2, results = mock('channel'), mock('channel'), []
    c1.expects(:on_request).with("exit-status").yields(c1)
    c2.expects(:on_request).with("exit-status").yields(c2)
    channel = Net::SSH::Multi::Channel.new(mock('session'), [c1, c2])
    assert_equal channel, channel.on_request("exit-status") { |c| results << c }
    assert_equal [c1, c2], results
  end

  private

    class MockSession
      def loop
        while true do
          return if !yield(self)
        end
      end
    end
end