Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / spec / unit / rpc / reply_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module RPC
7     describe Reply do
8       before(:each) do
9         Cache.delete!(:ddl) rescue nil
10
11         ddl = stub
12         ddl.stubs(:action_interface).returns({:output => {}})
13         ddl.stubs(:actions).returns(["rspec"])
14         ddl.stubs(:pluginname).returns("rspec")
15
16         @reply = Reply.new("rspec", ddl)
17       end
18
19       describe "#initialize" do
20         it "should set an empty data hash" do
21           @reply.data.should == {}
22         end
23
24         it "should set statuscode to zero" do
25           @reply.statuscode.should == 0
26         end
27
28         it "should set statusmsg to OK" do
29           @reply.statusmsg.should == "OK"
30         end
31       end
32
33       describe "#initialize_data" do
34         before do
35           Log.stubs(:warn)
36           @ddl = DDL.new("rspec", :agent, false)
37         end
38
39         it "should set defaults correctly" do
40           @ddl.action :rspec, :description => "testing rspec" do
41             @ddl.output :one, :description => "rspec test", :display_as => "rspec", :default => "default"
42             @ddl.output :three, :description => "rspec test", :display_as => "rspec", :default => []
43             @ddl.output :two, :description => "rspec test", :display_as => "rspec"
44           end
45
46           reply = Reply.new(:rspec, @ddl)
47           reply.data.should == {:one => "default", :two => nil, :three => []}
48         end
49
50         it "should detect missing actions" do
51           reply = Reply.new(:rspec, @ddl)
52           expect { reply.initialize_data }.to raise_error(/No action 'rspec' defined/)
53         end
54       end
55
56       describe "#fail" do
57         it "should set statusmsg" do
58           @reply.fail "foo"
59           @reply.statusmsg.should == "foo"
60         end
61
62         it "should set statuscode to 1 by default" do
63           @reply.fail("foo")
64           @reply.statuscode.should == 1
65         end
66
67         it "should set statuscode" do
68           @reply.fail("foo", 2)
69           @reply.statuscode.should == 2
70         end
71       end
72
73       describe "#fail!" do
74         it "should set statusmsg" do
75           expect {
76             @reply.fail! "foo"
77           }.to raise_error(RPCAborted, "foo")
78
79           @reply.statusmsg.should == "foo"
80         end
81
82         it "should set statuscode to 1 by default" do
83           expect {
84             @reply.fail! "foo"
85           }.to raise_error(RPCAborted)
86         end
87
88         it "should set statuscode" do
89           expect {
90             @reply.fail! "foo", 2
91           }.to raise_error(UnknownRPCAction)
92
93           @reply.statuscode.should == 2
94         end
95
96         it "should raise RPCAborted for code 1" do
97           expect {
98             @reply.fail! "foo", 1
99           }.to raise_error(RPCAborted)
100         end
101
102         it "should raise UnknownRPCAction for code 2" do
103           expect {
104             @reply.fail! "foo", 2
105           }.to raise_error(UnknownRPCAction)
106         end
107
108         it "should raise MissingRPCData for code 3" do
109           expect {
110             @reply.fail! "foo", 3
111           }.to raise_error(MissingRPCData)
112         end
113
114         it "should raise InvalidRPCData for code 4" do
115           expect {
116             @reply.fail! "foo", 4
117           }.to raise_error(InvalidRPCData)
118         end
119
120         it "should raise UnknownRPCError for all other codes" do
121           expect {
122             @reply.fail! "foo", 5
123           }.to raise_error(UnknownRPCError)
124
125           expect {
126             @reply.fail! "foo", "x"
127           }.to raise_error(UnknownRPCError)
128         end
129       end
130
131       describe "#[]=" do
132         it "should save the correct data to the data hash" do
133           @reply[:foo] = "foo1"
134           @reply["foo"] = "foo2"
135
136           @reply.data[:foo].should == "foo1"
137           @reply.data["foo"].should == "foo2"
138         end
139       end
140
141       describe "#[]" do
142         it "should return the correct saved data" do
143           @reply[:foo] = "foo1"
144           @reply["foo"] = "foo2"
145
146           @reply[:foo].should == "foo1"
147           @reply["foo"].should == "foo2"
148         end
149       end
150
151       describe "#to_hash" do
152         it "should have the correct keys" do
153           @reply.to_hash.keys.sort.should == [:data, :statuscode, :statusmsg]
154         end
155
156         it "should have the correct statuscode" do
157           @reply.fail "meh", 2
158           @reply.to_hash[:statuscode].should == 2
159         end
160
161         it "should have the correct statusmsg" do
162           @reply.fail "meh", 2
163           @reply.to_hash[:statusmsg].should == "meh"
164         end
165
166         it "should have the correct data" do
167           @reply[:foo] = :bar
168           @reply.to_hash[:data][:foo].should == :bar
169         end
170       end
171     end
172   end
173 end