f2c56ee5ebef678f4c55ec1ba742ff35a467b22e
[packages/precise/mcollective.git] / spec / unit / rpc / helpers_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module RPC
7     describe Helpers do
8       describe "#extract_hosts_from_json" do
9         it "should fail for non array data" do
10           expect {
11             Helpers.extract_hosts_from_json("{}")
12           }.to raise_error("JSON hosts list is not an array")
13         end
14
15         it "should fail for non hash array members" do
16           senders = [{"sender" => "sender1"}, {"sender" => "sender3"}, ""].to_json
17
18           expect {
19             Helpers.extract_hosts_from_json(senders)
20           }.to raise_error("JSON host list is not an array of Hashes")
21         end
22
23         it "should fail for hashes without senders" do
24           senders = [{"sender" => "sender1"}, {"sender" => "sender3"}, {}].to_json
25
26           expect {
27             Helpers.extract_hosts_from_json(senders)
28           }.to raise_error("JSON host list does not have senders in it")
29         end
30
31         it "should return all found unique senders" do
32           senders = [{"sender" => "sender1"}, {"sender" => "sender3"}, {"sender" => "sender1"}].to_json
33
34           Helpers.extract_hosts_from_json(senders).should == ["sender1", "sender3"]
35         end
36       end
37
38       describe "#extract_hosts_from_array" do
39         it "should support single string lists" do
40           Helpers.extract_hosts_from_array("foo").should == ["foo"]
41         end
42
43         it "should support arrays" do
44           Helpers.extract_hosts_from_array(["foo", "bar"]).should == ["foo", "bar"]
45         end
46
47         it "should fail for non string array members" do
48           expect {
49             Helpers.extract_hosts_from_array(["foo", 1])
50           }.to raise_error("1 should be a string")
51         end
52       end
53     end
54   end
55 end