1#!/usr/bin/env rspec
2require 'spec_helper'
3
4module Mcollective
5  describe "nettest application" do
6    before do
7      application_file = File.join(File.dirname(__FILE__), "../../", "application","nettest.rb")
8      @util = MCollective::Test::ApplicationTest.new("nettest", :application_file => application_file)
9      @app = @util.plugin
10    end
11
12    describe "#application_description" do
13      it "should have a description" do
14        @app.should have_a_description
15      end
16    end
17
18    describe '#raise_message' do
19      it 'should print the correct message if 1 is passed' do
20        expect{
21          @app.raise_message(:raise, 1)
22        }.to raise_error("Please specify an action and optional arguments")
23      end
24
25      it 'should print the correct message if 2 is passed' do
26        expect{
27          @app.raise_message(:raise, 2)
28        }.to raise_error("Action can only to be ping or connect")
29      end
30
31      it 'should print the correct message if 3 is passed' do
32        expect{
33          @app.raise_message(:raise, 3)
34        }.to raise_error("Do you really want to perform network tests unfiltered? (y/n): ")
35      end
36    end
37
38    describe "#post_option_parser" do
39      it "should raise an exception for no arguments" do
40        @app.expects(:raise_message).with(:raise, 1)
41        @app.post_option_parser({})
42      end
43
44      it "should raise an exception for unknown actions" do
45        ARGV << "action"
46        ARGV << "rspec"
47
48        @app.expects(:raise_message).with(:raise, 2)
49        @app.post_option_parser({})
50      end
51
52      it "should set fqdn and port correctly in the configuration" do
53        ARGV << "ping"
54        ARGV << "rspec"
55
56        configuration = {}
57        @app.post_option_parser(configuration)
58
59        configuration.should == {:action=>"ping", :arguments=>{:fqdn=>"rspec"}}
60
61        ARGV << "connect"
62        ARGV << "rspec"
63        ARGV << "80"
64
65        configuration = {}
66        @app.post_option_parser(configuration)
67
68        configuration.should == {:action=>"connect", :arguments=>{:port=>80, :fqdn=>"rspec"}}
69      end
70    end
71
72    describe "#validate_configuration" do
73      it "should check if no filter is supplied and ask confirmation expecting y or yes" do
74        MCollective::Util.expects("empty_filter?").returns(true).twice
75
76        @app.expects(:raise_message).with(:print, 3)
77        @app.expects(:raise_message).with(:print, 3)
78        @app.expects(:options).returns({}).twice
79        STDIN.expects(:gets).returns("y")
80        @app.expects("exit").with(1).never
81        @app.validate_configuration({})
82
83        STDIN.expects(:gets).returns("yes")
84        @app.validate_configuration({})
85      end
86
87      it "should exit unless y or yes is supplied" do
88        MCollective::Util.expects("empty_filter?").returns(true)
89
90        @app.expects(:print).with("Do you really want to perform network tests unfiltered? (y/n): ")
91        @app.expects(:options).returns({})
92
93        @app.expects("exit").with(1)
94        STDIN.expects(:gets).returns("n")
95        @app.validate_configuration({})
96      end
97    end
98
99    describe "#main" do
100      let(:rpcclient) {mock}
101
102      before do
103        @app.expects(:rpcclient).returns(rpcclient)
104      end
105
106      it 'should output the correct results for the ping command' do
107        resultset = [{:data => {:exitcode => 0,:rtt => '1.000'},:statuscode => 0,:sender => 'rspec1'}]
108        @app.configuration[:action] = 'ping'
109        @app.configuration[:arguments] = {:fqdn => 'www.rspec.com'}
110        rpcclient.expects(:send).with('ping', :fqdn => 'www.rspec.com').returns(resultset)
111        rpcclient.stubs(:verbose).returns(false)
112        rpcclient.expects(:stats).returns('stats')
113        @app.expects(:halt).with('stats')
114
115        @app.expects(:puts).with("%-40s time = %s" % ['rspec1', '1.000'])
116        @app.expects(:printrpcstats)
117
118        @app.main
119      end
120
121      it 'should output the correct results for the connect command' do
122        resultset = [{:data => {:exitcode => 0,:connect => 'Connected'}, :statuscode => 0, :sender => 'rspec1'}]
123        @app.configuration[:action] = 'connect'
124        @app.configuration[:arguments] = {:fqdn => 'www.rspec.com', :port => 80}
125        rpcclient.expects(:send).with('connect', {:fqdn => 'www.rspec.com', :port => 80}).returns(resultset)
126        rpcclient.stubs(:verbose).returns(false)
127        @app.expects(:puts).with("%-40s status = %s" % ['rspec1', 'Connected'])
128        @app.expects(:printrpcstats)
129        rpcclient.expects(:stats).returns('stats')
130        @app.expects(:halt).with('stats')
131
132        @app.main
133      end
134    end
135  end
136end
137