1#!/usr/bin/env rspec
2
3require 'spec_helper'
4require File.join(File.dirname(__FILE__), '../../', 'application', 'service.rb')
5
6module MCollective
7  class Application
8    describe Service do
9      before do
10        application_file = File.join(File.dirname(__FILE__), '../../', 'application', 'service.rb')
11        @app = MCollective::Test::ApplicationTest.new('service', :application_file => application_file).plugin
12      end
13
14      describe '#application_description' do
15        it 'should have a descriptionset' do
16          @app.should have_a_description
17        end
18      end
19
20      describe '#handle_message' do
21        it 'should call the display action with the correct message' do
22          @app.expects(:print).with("Please specify service name and action")
23          @app.expects(:print).with("Action has to be one of start, stop, restart or status")
24          @app.expects(:print).with("Do you really want to operate on services unfiltered? (y/n): ")
25          @app.handle_message(:print, 1)
26          @app.handle_message(:print, 2)
27          @app.handle_message(:print, 3)
28        end
29      end
30
31      describe '#validate_configuration' do
32        before do
33          MCollective::Util.stubs(:empty_filter?).returns(true)
34          @app.stubs(:options).returns({:filter =>{}})
35          @app.stubs(:handle_message).with(:print, 3)
36          STDOUT.stubs(:flush)
37        end
38
39        it 'should ask confirmation if filter is unset and exit if not given' do
40          STDIN.stubs(:gets).returns('n')
41          @app.expects(:exit).with(1)
42          @app.validate_configuration({})
43        end
44
45        it 'should ask confirmation if filter is unset and return if given' do
46          STDIN.stubs(:gets).returns('y')
47          @app.expects(:exit).with(1).never
48          @app.validate_configuration({})
49        end
50
51        it 'should not ask confirmation if filter is unset if action is status' do
52          @app.expects(:handle_message).with(:print,3).never
53          MCollective::Util.expects(:empty_filter?).never
54          @app.validate_configuration({:action => 'status'})
55        end
56      end
57
58      describe '#post_option_parser' do
59        it 'should fail if service and action are not supplied' do
60          @app.expects(:handle_message).with(:raise, 1)
61          @app.post_option_parser({})
62        end
63
64        it 'should fail if an unknown action is supplied' do
65          @app.expects(:handle_message).with(:raise, 2)
66          ARGV << 'rspec'
67          ARGV << 'rspec'
68          @app.post_option_parser({})
69        end
70
71        it 'should parse "action" "service" correctly' do
72          config = {}
73          ARGV << 'start'
74          ARGV << 'rspec'
75          @app.post_option_parser(config)
76          config[:action].should == 'start'
77          config[:service].should == 'rspec'
78        end
79
80        it 'should parser "service" "action" correctly' do
81          config = {}
82          ARGV << 'rspec'
83          ARGV << 'start'
84          @app.post_option_parser(config)
85          config[:action].should == 'start'
86          config[:service].should == 'rspec'
87        end
88      end
89
90      describe '#main' do
91        let(:rpcclient) { mock }
92
93        before do
94          @app.expects(:rpcclient).returns(rpcclient)
95        end
96
97        it 'should display the correct output for the start action' do
98          resultset = [{:data => {:exitcode => 0,:status => 'running'},:statuscode => 0,:sender => 'rspec'}]
99          @app.configuration[:action] = 'start'
100          @app.configuration[:service] = 'rspec'
101          rpcclient.expects(:send).with('start', :service => 'rspec').returns(resultset)
102          rpcclient.stubs(:verbose).returns(false)
103          rpcclient.stubs(:stats)
104          @app.expects(:halt)
105          @app.main
106        end
107
108        it 'should display the correct output for the stop action' do
109          resultset = [{:data => {:exitcode => 0,:status => 'stopped'},:statuscode => 0,:sender => 'rspec'}]
110          @app.configuration[:action] = 'stop'
111          @app.configuration[:service] = 'rspec'
112          rpcclient.expects(:send).with('stop', :service => 'rspec').returns(resultset)
113          rpcclient.stubs(:verbose).returns(false)
114          rpcclient.stubs(:stats)
115          @app.expects(:halt)
116          @app.main
117        end
118
119        it 'should display the correct output for the status action' do
120          resultset = [{:data => {:exitcode => 0,:status => 'running'},:statuscode => 0,:sender => 'rspec'}]
121          @app.configuration[:action] = 'status'
122          @app.configuration[:service] = 'rspec'
123          rpcclient.expects(:send).with('status', :service => 'rspec').returns(resultset)
124          rpcclient.stubs(:verbose).returns(false)
125          rpcclient.stubs(:stats)
126          @app.expects(:puts).with("%8s: %s" % ['rspec', 'running'])
127          @app.expects(:halt)
128          @app.main
129        end
130
131        it 'should display the correct output when verbose is set' do
132          resultset = [{:data => {:exitcode => 0,:status => 'stopped'},:statuscode => 0,:sender => 'rspec', :statusmsg => 'OK'}]
133          @app.configuration[:action] = 'stop'
134          @app.configuration[:service] = 'rspec'
135          rpcclient.expects(:send).with('stop', :service => 'rspec').returns(resultset)
136          rpcclient.stubs(:verbose).returns(true)
137          rpcclient.stubs(:stats)
138          @app.expects(:puts).with("%8s: %s" % ['rspec', 'stopped'])
139          @app.expects(:halt)
140          @app.main
141        end
142      end
143    end
144  end
145end
146