f5548d5f572347f4d0cba7870c6a77d5a05bec18
[packages/precise/mcollective.git] / spec / unit / ddl_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe DDL do
7     before do
8       Cache.delete!(:ddl) rescue nil
9     end
10
11     describe "#new" do
12       it "should default to agent ddls" do
13         DDL::AgentDDL.expects(:new).once
14         DDL.new("rspec")
15       end
16
17       it "should return the correct plugin ddl class" do
18         DDL.new("rspec", :agent, false).class.should == DDL::AgentDDL
19       end
20
21       it "should default to base when no specific class exist" do
22         DDL.new("rspec", :rspec, false).class.should == DDL::Base
23       end
24     end
25
26     describe "#load_and_cache" do
27       it "should setup the cache" do
28         Cache.setup(:ddl)
29
30         Cache.expects(:setup).once.returns(true)
31         DDL.load_and_cache("rspec", :agent, false)
32       end
33
34       it "should attempt to read from the cache and return found ddl" do
35         Cache.expects(:setup)
36         Cache.expects(:read).with(:ddl, "agent/rspec").returns("rspec")
37         DDL.load_and_cache("rspec", :agent, false).should == "rspec"
38       end
39
40       it "should handle cache misses then create and save a new ddl object" do
41         Cache.expects(:setup)
42         Cache.expects(:read).with(:ddl, "agent/rspec").raises("failed")
43         Cache.expects(:write).with(:ddl, "agent/rspec", kind_of(DDL::AgentDDL)).returns("rspec")
44
45         DDL.load_and_cache("rspec", :agent, false).should == "rspec"
46       end
47     end
48
49     describe "#string_to_number" do
50       it "should turn valid strings into numbers" do
51         ["1", "0", "9999"].each do |i|
52           DDL.string_to_number(i).class.should == Fixnum
53         end
54
55         ["1.1", "0.0", "9999.99"].each do |i|
56           DDL.string_to_number(i).class.should == Float
57         end
58       end
59
60       it "should raise errors for invalid values" do
61         expect { DDL.string_to_number("rspec") }.to raise_error
62       end
63     end
64
65     describe "#string_to_boolean" do
66       it "should turn valid strings into boolean" do
67         ["true", "yes", "1"].each do |t|
68           DDL.string_to_boolean(t).should == true
69           DDL.string_to_boolean(t.upcase).should == true
70         end
71
72         ["false", "no", "0"].each do |f|
73           DDL.string_to_boolean(f).should == false
74           DDL.string_to_boolean(f.upcase).should == false
75         end
76       end
77
78       it "should raise errors for invalid values" do
79         expect { DDL.string_to_boolean("rspec") }.to raise_error
80       end
81     end
82
83   end
84 end