07cb7011f73f3625981326d290a5ea45cc2513ff
[packages/precise/mcollective.git] / spec / unit / registration / base_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module Registration
7     describe Base do
8       before do
9         @config = mock
10         @config.stubs(:identity).returns("rspec")
11         @config.stubs(:main_collective).returns("main_collective")
12         Config.stubs(:instance).returns(@config)
13
14         @reg = Base.new
15       end
16
17       describe "#config" do
18         it "should provide access the main configuration class" do
19           @reg.config.should == @config
20         end
21
22       end
23
24       describe "#identity" do
25         it "should return the correct identity" do
26           @reg.config.identity.should == "rspec"
27         end
28       end
29
30       describe "#msg_filter" do
31         it "should target the registration agent" do
32           @reg.msg_filter["agent"].should == ["registration"]
33         end
34       end
35
36       describe "#target_collective" do
37         it "should return the configured registration_collective" do
38           @config.expects(:registration_collective).returns("registration").once
39           @config.expects(:collectives).returns(["main_collective", "registration"]).once
40           @reg.target_collective.should == "registration"
41         end
42
43         it "should use the main collective if registration collective is not valid" do
44           @config.expects(:registration_collective).returns("registration").once
45           @config.expects(:collectives).returns(["main_collective"]).once
46
47           Log.expects(:warn).with("Sending registration to main_collective: registration is not a valid collective").once
48
49           @reg.target_collective.should == "main_collective"
50         end
51       end
52
53       describe "#publish" do
54         it "should skip registration for empty messages" do
55           Log.expects(:debug).with("Skipping registration due to nil body")
56           @reg.publish(nil)
57         end
58
59         it "should publish via the message object" do
60           message = mock
61           message.expects(:encode!)
62           message.expects(:publish)
63           message.expects(:requestid).returns("123")
64           message.expects(:collective).returns("mcollective")
65
66           Message.expects(:new).returns(message)
67
68           Log.expects(:debug).with("Sending registration 123 to collective mcollective")
69
70           @reg.expects(:target_collective).returns("mcollective")
71
72           @reg.publish("message")
73         end
74       end
75     end
76   end
77 end