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