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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#!/usr/bin/env rspec
require File.dirname(__FILE__) + '/../../../../spec_helper'
require 'puppet/util/network_device/transport/ssh'
describe Puppet::Util::NetworkDevice::Transport::Ssh, :if => Puppet.features.ssh? do
before(:each) do
@transport = Puppet::Util::NetworkDevice::Transport::Ssh.new()
end
it "should handle login through the transport" do
@transport.should be_handles_login
end
it "should connect to the given host and port" do
Net::SSH.expects(:start).with { |host, user, args| host == "localhost" && args[:port] == 22 }.returns stub_everything
@transport.host = "localhost"
@transport.port = 22
@transport.connect
end
it "should connect using the given username and password" do
Net::SSH.expects(:start).with { |host, user, args| user == "user" && args[:password] == "pass" }.returns stub_everything
@transport.user = "user"
@transport.password = "pass"
@transport.connect
end
describe "when connected" do
before(:each) do
@ssh = stub_everything 'ssh'
@channel = stub_everything 'channel'
Net::SSH.stubs(:start).returns @ssh
@ssh.stubs(:open_channel).yields(@channel)
@transport.stubs(:expect)
end
it "should open a channel" do
@ssh.expects(:open_channel)
@transport.connect
end
it "should request a pty" do
@channel.expects(:request_pty)
@transport.connect
end
it "should create a shell channel" do
@channel.expects(:send_channel_request).with("shell")
@transport.connect
end
it "should raise an error if shell channel creation fails" do
@channel.expects(:send_channel_request).with("shell").yields(@channel, false)
lambda { @transport.connect }.should raise_error
end
it "should register an on_data and on_extended_data callback" do
@channel.expects(:send_channel_request).with("shell").yields(@channel, true)
@channel.expects(:on_data)
@channel.expects(:on_extended_data)
@transport.connect
end
it "should accumulate data to the buffer on data" do
@channel.expects(:send_channel_request).with("shell").yields(@channel, true)
@channel.expects(:on_data).yields(@channel, "data")
@transport.connect
@transport.buf.should == "data"
end
it "should accumulate data to the buffer on extended data" do
@channel.expects(:send_channel_request).with("shell").yields(@channel, true)
@channel.expects(:on_extended_data).yields(@channel, 1, "data")
@transport.connect
@transport.buf.should == "data"
end
it "should mark eof on close" do
@channel.expects(:send_channel_request).with("shell").yields(@channel, true)
@channel.expects(:on_close).yields(@channel)
@transport.connect
@transport.should be_eof
end
it "should expect output to conform to the default prompt" do
@channel.expects(:send_channel_request).with("shell").yields(@channel, true)
@transport.expects(:default_prompt).returns("prompt")
@transport.expects(:expect).with("prompt")
@transport.connect
end
it "should start the ssh loop" do
@ssh.expects(:loop)
@transport.connect
end
end
describe "when closing" do
before(:each) do
@ssh = stub_everything 'ssh'
@channel = stub_everything 'channel'
Net::SSH.stubs(:start).returns @ssh
@ssh.stubs(:open_channel).yields(@channel)
@channel.stubs(:send_channel_request).with("shell").yields(@channel, true)
@transport.stubs(:expect)
@transport.connect
end
it "should close the channel" do
@channel.expects(:close)
@transport.close
end
it "should close the ssh session" do
@ssh.expects(:close)
@transport.close
end
end
describe "when sending commands" do
before(:each) do
@ssh = stub_everything 'ssh'
@channel = stub_everything 'channel'
Net::SSH.stubs(:start).returns @ssh
@ssh.stubs(:open_channel).yields(@channel)
@channel.stubs(:send_channel_request).with("shell").yields(@channel, true)
@transport.stubs(:expect)
@transport.connect
end
it "should send data to the ssh channel" do
@channel.expects(:send_data).with("data\n")
@transport.command("data")
end
it "should expect the default prompt afterward" do
@transport.expects(:default_prompt).returns("prompt")
@transport.expects(:expect).with("prompt")
@transport.command("data")
end
it "should expect the given prompt" do
@transport.expects(:expect).with("myprompt")
@transport.command("data", :prompt => "myprompt")
end
it "should yield the buffer output to given block" do
@transport.expects(:expect).yields("output")
@transport.command("data") do |out|
out.should == "output"
end
end
it "should return buffer output" do
@transport.expects(:expect).returns("output")
@transport.command("data").should == "output"
end
end
describe "when expecting output" do
before(:each) do
@connection = stub_everything 'connection'
@socket = stub_everything 'socket'
transport = stub 'transport', :socket => @socket
@ssh = stub_everything 'ssh', :transport => transport
@channel = stub_everything 'channel', :connection => @connection
@transport.ssh = @ssh
@transport.channel = @channel
end
it "should process the ssh event loop" do
IO.stubs(:select)
@transport.buf = "output"
@transport.expects(:process_ssh)
@transport.expect(/output/)
end
it "should return the output" do
IO.stubs(:select)
@transport.buf = "output"
@transport.stubs(:process_ssh)
@transport.expect(/output/).should == "output"
end
it "should return the output" do
IO.stubs(:select)
@transport.buf = "output"
@transport.stubs(:process_ssh)
@transport.expect(/output/).should == "output"
end
describe "when processing the ssh loop" do
it "should advance one tick in the ssh event loop and exit on eof" do
@transport.buf = ''
@connection.expects(:process).then.raises(EOFError)
@transport.process_ssh
end
end
end
end
|