1#!/usr/bin/env rspec
2
3require 'spec_helper'
4require File.join(File.dirname(__FILE__), '../../../', 'agent', 'service.rb')
5require File.join(File.dirname(__FILE__), '../../../', 'util', 'service', 'base.rb')
6require File.join(File.dirname(__FILE__), '../../../', 'util', 'service', 'puppetservice.rb')
7
8module MCollective
9  module Util
10    module Service
11      describe PuppetService do
12        let(:service) { PuppetService.new('rspec', {:key => "value"}) }
13        let(:svc){mock}
14
15        before do
16          PuppetService.any_instance.stubs(:require)
17          service.stubs(:service_provider).returns(svc)
18        end
19
20        describe '#stop' do
21          it 'should stop the service' do
22            svc.expects(:status).returns('running')
23            svc.expects(:stop)
24            service.expects(:properties).returns('stopped')
25            result = service.stop
26            result.should == {:status => 'stopped', :msg => nil}
27          end
28
29          it 'should return stopped and a failure message if service is already stopped' do
30            service.stubs(:status).returns('stopped')
31            service.expects(:properties).returns('stopped')
32            svc.expects(:stop).never
33            service.stop.should == {:status => 'stopped', :msg => "Could not stop 'rspec': Service is already stopped"}
34          end
35        end
36
37        describe '#start' do
38          it 'should start the service' do
39            svc.expects(:status).returns('stopped')
40            svc.expects(:start)
41            service.expects(:properties)
42            service.start
43          end
44
45          it 'should return running and a failure message if service is already running' do
46            service.stubs(:status).returns('running')
47            service.expects(:properties).returns('running')
48            svc.expects(:start).never
49            service.start.should == {:status => 'running', :msg => "Could not start 'rspec': Service is already running"}
50          end
51        end
52
53        describe '#restart' do
54          it 'should restart the service if hasrestart is true' do
55            svc.expects(:restart)
56            service.expects(:properties)
57            service.restart
58          end
59
60          it 'should raise an exception if trying to restart and hasrestart is false' do
61            expect{
62              failed_service.restart
63            }.to raise_error
64          end
65        end
66
67        describe '#status' do
68          it 'should return the status of the service' do
69            svc.expects(:status).returns('status')
70            service.status
71          end
72
73          it 'should raise an exception when calling status and hasstatus is false' do
74            expect{
75              failed_servce.status
76            }.to raise_error
77          end
78        end
79
80        describe '#service_provider' do
81          class Puppet;class Type;end;end
82          it 'should create a puppet provider once' do
83            service.unstub(:service_provider)
84
85            provider = mock
86            provider.expects(:start).twice
87
88            type = mock
89            type.expects(:new).returns(type).once
90            type.expects(:provider).returns(provider).once
91            Puppet::Type.expects(:type).returns(type).once
92
93            service.stubs(:status)
94            service.stubs(:properties)
95            service.start
96            service.start
97          end
98        end
99
100        describe '#properties' do
101          it 'should sleep for 0.5 seconds and return the status' do
102            svc.stubs(:start)
103            service.expects(:sleep).with(0.5)
104            service.stubs(:status)
105            service.start
106        end
107      end
108    end
109  end
110end
111  end
112