80a150438fe8cc7ee68dd68c3dc570e999bbb095
[packages/precise/mcollective.git] / spec / unit / data / base_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module Data
7     describe Base do
8       before do
9         @ddl = mock
10         @ddl.stubs(:dataquery_interface).returns({:output => {'rspec' => {}}})
11         @ddl.stubs(:meta).returns({:timeout => 1})
12       end
13
14       describe "#initialize" do
15         it "should set the plugin name, ddl and timeout and call the startup hook" do
16           DDL.stubs(:new).returns(@ddl)
17           Base.any_instance.expects(:startup_hook).once
18           plugin = Base.new
19           plugin.name.should == "base"
20           plugin.timeout.should == 1
21           plugin.result.class.should == Result
22         end
23       end
24
25       describe "#lookup" do
26         before do
27           DDL.stubs(:new).returns(@ddl)
28           @plugin = Base.new
29         end
30
31         it "should validate the request" do
32           @plugin.expects(:ddl_validate).with("hello world").returns(true)
33           @plugin.stubs(:query_data)
34           @plugin.lookup("hello world")
35         end
36
37         it "should query the plugin" do
38           @plugin.stubs(:ddl_validate)
39           @plugin.expects(:query_data).with("hello world")
40           @plugin.lookup("hello world").class.should == Result
41         end
42
43         it "should raise MsgTTLExpired errors for Timeout errors" do
44           @plugin.stubs(:ddl_validate)
45           @plugin.expects(:query_data).raises(Timeout::Error)
46
47           msg = "Data plugin base timed out on query 'hello world'"
48           Log.expects(:error).with(msg)
49           expect { @plugin.lookup("hello world") }.to raise_error(msg)
50         end
51       end
52
53       describe "#query" do
54         it "should create a new method" do
55           class Rspec_data<Base; end
56           Rspec_data.query { "rspec test" }
57
58           DDL.stubs(:new).returns(@ddl)
59
60           data = Rspec_data.new
61           data.query_data.should == "rspec test"
62         end
63       end
64
65       describe "#ddl_validate" do
66         it "should validate the request using the Data class" do
67           DDL.stubs(:new).returns(@ddl)
68           plugin = Base.new
69           Data.expects(:ddl_validate).with(@ddl, "rspec")
70           plugin.ddl_validate("rspec")
71         end
72       end
73
74       describe "#activate_when" do
75         it "should create a new activate? method" do
76           class Rspec_data<Base;end
77
78           Rspec_data.activate_when { raise "rspec" }
79           DDL.stubs(:new).returns(@ddl)
80           expect { Rspec_data.activate? }.to raise_error("rspec")
81         end
82       end
83
84       describe "#activate?" do
85         it "should default to true" do
86           Base.activate?.should == true
87         end
88       end
89     end
90   end
91 end