pairing with luke, nagios_command provider skeleton
[vinpup.git] / spec / unit / node.rb
blob0ce702936cbd59fc39364505d06d06bac44edbda
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../spec_helper'
5 describe Puppet::Node, " when initializing" do
6     before do
7         @node = Puppet::Node.new("testnode")
8     end
10     it "should set the node name" do
11         @node.name.should == "testnode"
12     end
14     it "should not allow nil node names" do
15         proc { Puppet::Node.new(nil) }.should raise_error(ArgumentError)
16     end
18     it "should default to an empty parameter hash" do
19         @node.parameters.should == {}
20     end
22     it "should default to an empty class array" do
23         @node.classes.should == []
24     end
26     it "should note its creation time" do
27         @node.time.should be_instance_of(Time)
28     end
30     it "should accept parameters passed in during initialization" do
31         params = {"a" => "b"}
32         @node = Puppet::Node.new("testing", :parameters => params)
33         @node.parameters.should == params
34     end
36     it "should accept classes passed in during initialization" do
37         classes = %w{one two}
38         @node = Puppet::Node.new("testing", :classes => classes)
39         @node.classes.should == classes
40     end
42     it "should always return classes as an array" do
43         @node = Puppet::Node.new("testing", :classes => "myclass")
44         @node.classes.should == ["myclass"]
45     end
47     it "should accept an environment value" do
48         Puppet.settings.stubs(:value).with(:environments).returns("myenv")
49         @node = Puppet::Node.new("testing", :environment => "myenv")
50         @node.environment.should == "myenv"
51     end
53     it "should validate the environment" do
54         Puppet.settings.stubs(:value).with(:environments).returns("myenv")
55         proc { Puppet::Node.new("testing", :environment => "other") }.should raise_error(ArgumentError)
56     end
58     it "should accept names passed in" do
59         @node = Puppet::Node.new("testing", :names => ["myenv"])
60         @node.names.should == ["myenv"]
61     end
62 end
64 describe Puppet::Node, " when returning the environment" do
65     before do
66         Puppet.settings.stubs(:value).with(:environments).returns("one,two")
67         Puppet.settings.stubs(:value).with(:environment).returns("one")
68         @node = Puppet::Node.new("testnode")
69     end
71     it "should return the 'environment' fact if present and there is no explicit environment" do
72         @node.parameters = {"environment" => "two"}
73         @node.environment.should == "two"
74     end
76     it "should use the default environment if there is no environment fact nor explicit environment" do
77         env = mock 'environment', :name => :myenv
78         Puppet::Node::Environment.expects(:new).returns(env)
79         @node.environment.should == "myenv"
80     end
82     it "should fail if the parameter environment is invalid" do
83         @node.parameters = {"environment" => "three"}
84         proc { @node.environment }.should raise_error(ArgumentError)
85     end
87     it "should fail if the parameter environment is invalid" do
88         @node.parameters = {"environment" => "three"}
89         proc { @node.environment }.should raise_error(ArgumentError)
90     end
91 end
93 describe Puppet::Node, " when merging facts" do
94     before do
95         @node = Puppet::Node.new("testnode")
96         Puppet::Node::Facts.stubs(:find).with(@node.name).returns(Puppet::Node::Facts.new(@node.name, "one" => "c", "two" => "b"))
97     end
99     it "should prefer parameters already set on the node over facts from the node" do
100         @node.parameters = {"one" => "a"}
101         @node.fact_merge
102         @node.parameters["one"].should == "a"
103     end
105     it "should add passed parameters to the parameter list" do
106         @node.parameters = {"one" => "a"}
107         @node.fact_merge
108         @node.parameters["two"].should == "b"
109     end
111     it "should accept arbitrary parameters to merge into its parameters" do
112         @node.parameters = {"one" => "a"}
113         @node.merge "two" => "three"
114         @node.parameters["two"].should == "three"
115     end
118 describe Puppet::Node, " when indirecting" do
119     it "should redirect to the indirection" do
120         @indirection = mock 'indirection'
121         Puppet::Node.stubs(:indirection).returns(@indirection)
122         @indirection.expects(:find).with(:my_node.to_s)
123         Puppet::Node.find(:my_node.to_s)
124     end
126     it "should default to the 'plain' node terminus" do
127         Puppet::Node.indirection.terminus_class.should == :plain
128     end
130     after do
131         Puppet::Indirector::Indirection.clear_cache
132     end
135 describe Puppet::Node do
136     # LAK:NOTE This is used to keep track of when a given node has connected,
137     # so we can report on nodes that do not appear to connecting to the
138     # central server.
139     it "should provide a method for noting that the node has connected"
142 describe Puppet::Node, " when searching for nodes" do
143     before do
144         @searcher = Puppet::Node
145         @facts = Puppet::Node::Facts.new("foo", "hostname" => "yay", "domain" => "domain.com")
146         @node = Puppet::Node.new("foo")
147         Puppet::Node::Facts.stubs(:find).with("foo").returns(@facts)
148     end
150     it "should return the first node found using the generated list of names" do
151         @searcher.expects(:find).with("foo").returns(nil)
152         @searcher.expects(:find).with("yay.domain.com").returns(@node)
153         @searcher.find_by_any_name("foo").should equal(@node)
154     end
156     it "should search for the node by its key first" do
157         names = []
158         @searcher.expects(:find).with do |name|
159             names << name
160             names == %w{foo}
161         end.returns(@node)
162         @searcher.find_by_any_name("foo").should equal(@node)
163     end
165     it "should search for the rest of the names inversely by length" do
166         names = []
167         @facts.values["fqdn"] = "longer.than.the.normal.fqdn.com"
168         @searcher.stubs(:find).with do |name|
169             names << name
170         end
171         @searcher.find_by_any_name("foo")
172         # Strip off the key
173         names.shift
175         # And the 'default'
176         names.pop
178         length = 100
179         names.each do |name|
180             (name.length < length).should be_true
181             length = name.length
182         end
183     end
185     it "should attempt to find a default node if no names are found" do
186         names = []
187         @searcher.stubs(:find).with do |name|
188             names << name
189         end.returns(nil)
190         @searcher.find_by_any_name("foo")
191         names[-1].should == "default"
192     end
194     it "should flush the node cache using the :filetimeout parameter" do
195         node2 = Puppet::Node.new("foo2")
196         Puppet[:filetimeout] = -1
197         # I couldn't get this to work with :expects
198         @searcher.stubs(:find).returns(@node, node2).then.raises(ArgumentError)
199         @searcher.find_by_any_name("foo").should equal(@node)
200         @searcher.find_by_any_name("foo").should equal(node2)
201     end
203     after do
204         Puppet.settings.clear
205     end