Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / spec / unit / client_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe Client do
7     before do
8       @security = mock
9       @security.stubs(:initiated_by=)
10       @connector = mock
11       @connector.stubs(:connect)
12       @connector.stubs(:subscribe)
13       @connector.stubs(:unsubscribe)
14       @ddl = mock
15       @ddl.stubs(:meta).returns({:timeout => 1})
16       @discoverer = mock
17
18       Discovery.expects(:new).returns(@discoverer)
19
20       Config.instance.instance_variable_set("@configured", true)
21       PluginManager.expects("[]").with("connector_plugin").returns(@connector)
22       PluginManager.expects("[]").with("security_plugin").returns(@security)
23
24       @client = Client.new("/nonexisting")
25       @client.options = Util.default_options
26     end
27
28     describe "#sendreq" do
29       it "should send the supplied message" do
30         message = Message.new("rspec", nil, {:agent => "rspec", :type => :request, :collective => "mcollective", :filter => Util.empty_filter, :options => Util.default_options})
31
32         message.expects(:encode!)
33         @client.expects(:subscribe).with("rspec", :reply)
34         message.expects(:publish)
35         message.expects(:requestid).twice
36         @client.sendreq(message, "rspec")
37       end
38
39       it "should not subscribe to a reply queue for a message with a reply-to" do
40         message = Message.new("rspec", nil, {:agent => "rspec", :type => :request, :collective => "mcollective", :filter => Util.empty_filter, :options => Util.default_options})
41         message.reply_to = "rspec"
42
43         message.expects(:encode!)
44         @client.expects(:subscribe).never
45         message.expects(:publish)
46         message.expects(:requestid).twice
47         @client.sendreq(message, "rspec")
48       end
49     end
50     describe "#req" do
51       it "should record the requestid" do
52         message = Message.new("rspec", nil, {:agent => "rspec", :type => :request, :collective => "mcollective", :filter => Util.empty_filter, :options => Util.default_options})
53         message.discovered_hosts = ["rspec"]
54
55         reply = mock
56         reply.stubs("payload").returns("rspec payload")
57
58         @client.expects(:sendreq).returns("823a3419a0975c3facbde121f72ab61f")
59         @client.expects(:receive).returns(reply)
60
61         @discoverer.expects(:discovery_timeout).with(message.options[:timeout], message.options[:filter]).returns(0)
62
63         Time.stubs(:now).returns(Time.at(1340621250), Time.at(1340621251))
64
65         @client.req(message){}.should == {:blocktime => 1.0, :discoverytime => 0, :noresponsefrom => [],
66                                           :requestid => "823a3419a0975c3facbde121f72ab61f", :responses => 1,
67                                           :starttime => 1340621250.0, :totaltime => 1.0}
68       end
69     end
70
71     describe "#discover" do
72       it "should delegate to the discovery plugins" do
73         @discoverer.expects(:discover).with({}, 1, 0).returns([])
74         @client.discover({}, 1).should == []
75       end
76     end
77   end
78 end