1#!/usr/bin/env rspec
2
3require 'spec_helper'
4require File.join(File.dirname(__FILE__), '../../', 'util', 'nettest_agent.rb')
5require File.join(File.dirname(__FILE__), "../../", "agent", "nettest.rb")
6
7module MCollective
8  module Agent
9    class Nettest
10      describe "nettest agent" do
11        before do
12          agent_file = File.join(File.dirname(__FILE__), "../../", "agent", "nettest.rb")
13          @agent = MCollective::Test::LocalAgentTest.new("nettest", :agent_file => agent_file).plugin
14        end
15
16        describe "ping_action" do
17          it 'should set the rtt if ping was successful' do
18            Util::NettestAgent.expects(:get_ip_from_hostname).with('example.com').returns('1.2.3.4')
19            Nettest.expects(:testping).with('1.2.3.4').returns(1234)
20
21            result = @agent.call(:ping, :fqdn => 'example.com')
22            result.should be_successful
23            result.should have_data_items({:rtt => 1234})
24          end
25
26          it 'should fail if host cannot be reached' do
27            Util::NettestAgent.expects(:get_ip_from_hostname).with('example.com').returns('1.2.3.4')
28            Nettest.expects(:testping).with('1.2.3.4').returns(nil)
29
30            result = @agent.call(:ping, :fqdn => 'example.com')
31            result.should be_aborted_error
32          end
33
34          it 'should fail if hostname cannot be resolved' do
35            Util::NettestAgent.expects(:get_ip_from_hostname).with('example.com').returns(nil)
36
37            result = @agent.call(:ping, :fqdn => 'example.com')
38            result.should be_aborted_error
39          end
40        end
41
42        describe "connect_action" do
43          it 'should set the connect, connect_status and connect_time fields if successful' do
44            Util::NettestAgent.expects(:get_ip_from_hostname).with('example.com').returns('1.2.3.4')
45            Nettest.expects(:testconnect).with('1.2.3.4', 8080).returns([true, 'Connected', 5])
46
47            result = @agent.call(:connect, :fqdn => 'example.com', :port => 8080)
48            result.should be_successful
49            result.should have_data_items({:connect => true, :connect_status => 'Connected', :connect_time => 5})
50          end
51
52          it 'should set connect to false if connection could not be made' do
53            Util::NettestAgent.expects(:get_ip_from_hostname).with('example.com').returns('1.2.3.4')
54            Nettest.expects(:testconnect).with('1.2.3.4', 8080).returns([false, 'Connection Refused', 5])
55
56            result = @agent.call(:connect, :fqdn => 'example.com', :port => 8080)
57            result.should be_successful
58            result.should have_data_items({:connect => false, :connect_status => 'Connection Refused', :connect_time => 5})
59          end
60
61          it 'should fail if hostname cannot be resolved' do
62            Util::NettestAgent.expects(:get_ip_from_hostname).with('example.com').returns(nil)
63            result = @agent.call(:connect, :fqdn => 'example.com', :port => 8080)
64
65            result.should be_aborted_error
66          end
67        end
68
69        describe "#testping" do
70          let(:icmp) { mock }
71
72          class Net
73            class Ping
74              class ICMP
75              end
76            end
77          end
78
79          it 'should return the ping time if ping was successful' do
80            Net::Ping::ICMP.expects(:new).with('1.2.3.4').returns(icmp)
81            icmp.expects(:ping?).returns(true)
82            icmp.expects(:duration).returns(0.005)
83
84            result = Nettest.testping('1.2.3.4')
85            result.should == 5
86          end
87
88          it 'should return nil if ping was unsuccessful' do
89            Net::Ping::ICMP.expects(:new).with('1.2.3.4').returns(icmp)
90            icmp.expects(:ping?).returns(false)
91
92            result = Nettest.testping('1.2.3.4')
93            result.should == nil
94          end
95        end
96
97        describe "#testconnect" do
98          let(:socket) { mock }
99
100          it 'should return the status, status string and time for a successful connection' do
101            Time.expects(:now).twice.returns(1,2)
102            TCPSocket.stubs(:new).with('1.2.3.4', 8080).returns(socket)
103            socket.stubs(:close)
104
105            connected, connect_string, connect_time = Nettest.testconnect('1.2.3.4', 8080)
106            connected.should be_true
107            connect_string.should == "Connected"
108            connect_time.should == 1
109          end
110
111          it 'should return the status, status string and time for a refused connection' do
112            Time.expects(:now).raises('error')
113
114            connected, connect_string, connect_time = Nettest.testconnect('1.2.3.4', 8080)
115            connected.should be_false
116            connect_string.should == "Connection Refused"
117            connect_time.should == nil
118          end
119
120          it 'should return the status, status string and time for a timed out connection' do
121            Timeout.expects(:timeout).with(2).raises(Timeout::Error)
122
123            connected, connect_string, connect_time = Nettest.testconnect('1.2.3.4', 8080)
124            connected.should be_false
125            connect_string.should == "Connection Timeout"
126            connect_time.should == nil
127
128          end
129        end
130      end
131    end
132  end
133end
134