3 require File.dirname(__FILE__) + '/../spec_helper'
5 describe Puppet::Node, " when initializing" do
7 @node = Puppet::Node.new("testnode")
10 it "should set the node name" do
11 @node.name.should == "testnode"
14 it "should not allow nil node names" do
15 proc { Puppet::Node.new(nil) }.should raise_error(ArgumentError)
18 it "should default to an empty parameter hash" do
19 @node.parameters.should == {}
22 it "should default to an empty class array" do
23 @node.classes.should == []
26 it "should note its creation time" do
27 @node.time.should be_instance_of(Time)
30 it "should accept parameters passed in during initialization" do
32 @node = Puppet::Node.new("testing", :parameters => params)
33 @node.parameters.should == params
36 it "should accept classes passed in during initialization" do
38 @node = Puppet::Node.new("testing", :classes => classes)
39 @node.classes.should == classes
42 it "should always return classes as an array" do
43 @node = Puppet::Node.new("testing", :classes => "myclass")
44 @node.classes.should == ["myclass"]
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"
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)
58 it "should accept names passed in" do
59 @node = Puppet::Node.new("testing", :names => ["myenv"])
60 @node.names.should == ["myenv"]
64 describe Puppet::Node, " when returning the environment" 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")
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"
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"
82 it "should fail if the parameter environment is invalid" do
83 @node.parameters = {"environment" => "three"}
84 proc { @node.environment }.should raise_error(ArgumentError)
87 it "should fail if the parameter environment is invalid" do
88 @node.parameters = {"environment" => "three"}
89 proc { @node.environment }.should raise_error(ArgumentError)
93 describe Puppet::Node, " when merging facts" 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"))
99 it "should prefer parameters already set on the node over facts from the node" do
100 @node.parameters = {"one" => "a"}
102 @node.parameters["one"].should == "a"
105 it "should add passed parameters to the parameter list" do
106 @node.parameters = {"one" => "a"}
108 @node.parameters["two"].should == "b"
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"
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)
126 it "should default to the 'plain' node terminus" do
127 Puppet::Node.indirection.terminus_class.should == :plain
131 Puppet::Indirector::Indirection.clear_cache
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
139 it "should provide a method for noting that the node has connected"
142 describe Puppet::Node, " when searching for nodes" 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)
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)
156 it "should search for the node by its key first" do
158 @searcher.expects(:find).with do |name|
162 @searcher.find_by_any_name("foo").should equal(@node)
165 it "should search for the rest of the names inversely by length" do
167 @facts.values["fqdn"] = "longer.than.the.normal.fqdn.com"
168 @searcher.stubs(:find).with do |name|
171 @searcher.find_by_any_name("foo")
180 (name.length < length).should be_true
185 it "should attempt to find a default node if no names are found" do
187 @searcher.stubs(:find).with do |name|
190 @searcher.find_by_any_name("foo")
191 names[-1].should == "default"
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)
204 Puppet.settings.clear