summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/network_device/transport/telnet_spec.rb
blob: 7499b528e52eff23f1ab8bfaf4de5078d12590cb (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../../spec_helper'

require 'puppet/util/network_device/transport/telnet'

describe Puppet::Util::NetworkDevice::Transport::Telnet do

  before(:each) do
    @transport = Puppet::Util::NetworkDevice::Transport::Telnet.new()
  end

  it "should not handle login through the transport" do
    @transport.should_not be_handles_login
  end

  it "should connect to the given host and port" do
    Net::Telnet.expects(:new).with { |args| args["Host"] == "localhost" && args["Port"] == 23 }.returns stub_everything
    @transport.host = "localhost"
    @transport.port = 23

    @transport.connect
  end

  it "should connect and specify the default prompt" do
    @transport.default_prompt = "prompt"
    Net::Telnet.expects(:new).with { |args| args["Prompt"] == "prompt" }.returns stub_everything
    @transport.host = "localhost"
    @transport.port = 23

    @transport.connect
  end

  describe "when connected" do
    before(:each) do
      @telnet = stub_everything 'telnet'
      Net::Telnet.stubs(:new).returns(@telnet)
      @transport.connect
    end

    it "should send line to the telnet session" do
      @telnet.expects(:puts).with("line")
      @transport.send("line")
    end

    describe "when expecting output" do
      it "should waitfor output on the telnet session" do
        @telnet.expects(:waitfor).with(/regex/)
        @transport.expect(/regex/)
      end

      it "should return telnet session output" do
        @telnet.expects(:waitfor).returns("output")
        @transport.expect(/regex/).should == "output"
      end

      it "should yield telnet session output to the given block" do
        @telnet.expects(:waitfor).yields("output")
        @transport.expect(/regex/) { |out| out.should == "output" }
      end
    end
  end

  describe "when closing" do
    before(:each) do
      @telnet = stub_everything 'telnet'
      Net::Telnet.stubs(:new).returns(@telnet)
      @transport.connect
    end

    it "should close the telnet session" do
      @telnet.expects(:close)
      @transport.close
    end
  end
end