summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit')
-rwxr-xr-xspec/unit/indirector/facts/yaml.rb62
-rwxr-xr-xspec/unit/indirector/indirector.rb79
-rwxr-xr-xspec/unit/node/facts.rb25
-rwxr-xr-xspec/unit/node/node.rb116
4 files changed, 282 insertions, 0 deletions
diff --git a/spec/unit/indirector/facts/yaml.rb b/spec/unit/indirector/facts/yaml.rb
new file mode 100755
index 000000000..45c079a69
--- /dev/null
+++ b/spec/unit/indirector/facts/yaml.rb
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/indirector'
+require 'puppet/node/facts'
+require 'puppettest'
+
+describe Puppet::Indirector.terminus(:facts, :yaml), " when managing facts" do
+ # For cleanup mechanisms.
+ include PuppetTest
+
+ # LAK:FIXME It seems like I really do have to hit the filesystem
+ # here, since, like, that's what I'm testing. Is there another/better
+ # way to do this?
+ before do
+ @store = Puppet::Indirector.terminus(:facts, :yaml).new
+ setup # Grr, stupid rspec
+ Puppet[:yamlfactdir] = tempfile
+ Dir.mkdir(Puppet[:yamlfactdir])
+ end
+
+ it "should store facts in YAML in the yamlfactdir" do
+ values = {"one" => "two", "three" => "four"}
+ facts = Puppet::Node::Facts.new("node", values)
+ @store.put(facts)
+
+ # Make sure the file exists
+ path = File.join(Puppet[:yamlfactdir], facts.name) + ".yaml"
+ File.exists?(path).should be_true
+
+ # And make sure it's right
+ newvals = YAML.load(File.read(path))
+
+ # We iterate over them, because the store might add extra values.
+ values.each do |name, value|
+ newvals[name].should == value
+ end
+ end
+
+ it "should retrieve values from disk" do
+ values = {"one" => "two", "three" => "four"}
+
+ # Create the file.
+ path = File.join(Puppet[:yamlfactdir], "node") + ".yaml"
+ File.open(path, "w") do |f|
+ f.print values.to_yaml
+ end
+
+ facts = Puppet::Node::Facts.get('node')
+ facts.should be_instance_of(Puppet::Node::Facts)
+
+ # We iterate over them, because the store might add extra values.
+ values.each do |name, value|
+ facts.values[name].should == value
+ end
+ end
+
+ after do
+ teardown
+ end
+end
diff --git a/spec/unit/indirector/indirector.rb b/spec/unit/indirector/indirector.rb
new file mode 100755
index 000000000..c4221febb
--- /dev/null
+++ b/spec/unit/indirector/indirector.rb
@@ -0,0 +1,79 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/defaults'
+require 'puppet/indirector'
+
+describe Puppet::Indirector, " when managing indirections" do
+ before do
+ @indirector = Object.new
+ @indirector.send(:extend, Puppet::Indirector)
+ end
+
+ # LAK:FIXME This seems like multiple tests, but I don't really know how to test one at a time.
+ it "should accept specification of an indirection terminus via a configuration parameter" do
+ @indirector.indirects :test, :to => :node_source
+ Puppet[:node_source] = "test_source"
+ klass = mock 'terminus_class'
+ terminus = mock 'terminus'
+ klass.expects(:new).returns terminus
+ Puppet::Indirector.expects(:terminus).with(:test, :test_source).returns(klass)
+ @indirector.send(:terminus).should equal(terminus)
+ end
+
+ it "should not allow more than one indirection in the same object" do
+ @indirector.indirects :test
+ proc { @indirector.indirects :else }.should raise_error(ArgumentError)
+ end
+
+ it "should allow multiple classes to use the same indirection" do
+ @indirector.indirects :test
+ other = Object.new
+ other.send(:extend, Puppet::Indirector)
+ proc { other.indirects :test }.should_not raise_error
+ end
+end
+
+describe Puppet::Indirector, " when managing termini" do
+ before do
+ @indirector = Object.new
+ @indirector.send(:extend, Puppet::Indirector)
+ end
+
+ it "should should autoload termini from disk" do
+ Puppet::Indirector.expects(:instance_load).with(:test, "puppet/indirector/test")
+ @indirector.indirects :test
+ end
+end
+
+describe Puppet::Indirector, " when performing indirections" do
+ before do
+ @indirector = Object.new
+ @indirector.send(:extend, Puppet::Indirector)
+ @indirector.indirects :test, :to => :node_source
+
+ # Set up a fake terminus class that will just be used to spit out
+ # mock terminus objects.
+ @terminus_class = mock 'terminus_class'
+ Puppet::Indirector.stubs(:terminus).with(:test, :test_source).returns(@terminus_class)
+ Puppet[:node_source] = "test_source"
+ end
+
+ it "should redirect http methods to the default terminus" do
+ terminus = mock 'terminus'
+ terminus.expects(:put).with("myargument")
+ @terminus_class.expects(:new).returns(terminus)
+ @indirector.put("myargument")
+ end
+
+ # Make sure it caches the terminus.
+ it "should use the same terminus for all indirections" do
+ terminus = mock 'terminus'
+ terminus.expects(:put).with("myargument")
+ terminus.expects(:get).with("other_argument")
+ @terminus_class.expects(:new).returns(terminus)
+ @indirector.put("myargument")
+ @indirector.get("other_argument")
+ end
+end
diff --git a/spec/unit/node/facts.rb b/spec/unit/node/facts.rb
new file mode 100755
index 000000000..c7fc65f38
--- /dev/null
+++ b/spec/unit/node/facts.rb
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/node/facts'
+
+describe Puppet::Node::Facts, " when indirecting" do
+ before do
+ Puppet[:fact_store] = "test_store"
+ @terminus_class = mock 'terminus_class'
+ @terminus = mock 'terminus'
+ @terminus_class.expects(:new).returns(@terminus)
+ Puppet::Indirector.expects(:terminus).with(:facts, :test_store).returns(@terminus_class)
+ end
+
+ it "should redirect to the specified fact store for retrieval" do
+ @terminus.expects(:get).with(:my_facts)
+ Puppet::Node::Facts.get(:my_facts)
+ end
+
+ it "should redirect to the specified fact store for storage" do
+ @terminus.expects(:put).with(:my_facts)
+ Puppet::Node::Facts.put(:my_facts)
+ end
+end
diff --git a/spec/unit/node/node.rb b/spec/unit/node/node.rb
new file mode 100755
index 000000000..a6cc1e301
--- /dev/null
+++ b/spec/unit/node/node.rb
@@ -0,0 +1,116 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe Puppet::Node, " when initializing" do
+ before do
+ @node = Puppet::Node.new("testnode")
+ end
+
+ it "should set the node name" do
+ @node.name.should == "testnode"
+ end
+
+ it "should default to an empty parameter hash" do
+ @node.parameters.should == {}
+ end
+
+ it "should default to an empty class array" do
+ @node.classes.should == []
+ end
+
+ it "should note its creation time" do
+ @node.time.should be_instance_of(Time)
+ end
+
+ it "should accept parameters passed in during initialization" do
+ params = {"a" => "b"}
+ @node = Puppet::Node.new("testing", :parameters => params)
+ @node.parameters.should == params
+ end
+
+ it "should accept classes passed in during initialization" do
+ classes = %w{one two}
+ @node = Puppet::Node.new("testing", :classes => classes)
+ @node.classes.should == classes
+ end
+
+ it "should always return classes as an array" do
+ @node = Puppet::Node.new("testing", :classes => "myclass")
+ @node.classes.should == ["myclass"]
+ end
+
+ it "should accept the environment during initialization" do
+ @node = Puppet::Node.new("testing", :environment => "myenv")
+ @node.environment.should == "myenv"
+ end
+
+ it "should accept names passed in" do
+ @node = Puppet::Node.new("testing", :names => ["myenv"])
+ @node.names.should == ["myenv"]
+ end
+end
+
+describe Puppet::Node, " when returning the environment" do
+ before do
+ @node = Puppet::Node.new("testnode")
+ end
+
+ it "should return the 'environment' fact if present and there is no explicit environment" do
+ @node.parameters = {"environment" => "myenv"}
+ @node.environment.should == "myenv"
+ end
+
+ it "should return the central environment if there is no environment fact nor explicit environment" do
+ Puppet.config.expects(:[]).with(:environment).returns(:centralenv)
+ @node.environment.should == :centralenv
+ end
+
+ it "should not use an explicit environment that is an empty string" do
+ @node.environment == ""
+ @node.environment.should be_nil
+ end
+
+ it "should not use an environment fact that is an empty string" do
+ @node.parameters = {"environment" => ""}
+ @node.environment.should be_nil
+ end
+
+ it "should not use an explicit environment that is an empty string" do
+ Puppet.config.expects(:[]).with(:environment).returns(nil)
+ @node.environment.should be_nil
+ end
+end
+
+describe Puppet::Node, " when merging facts" do
+ before do
+ @node = Puppet::Node.new("testnode")
+ end
+
+ it "should prefer parameters already set on the node over facts from the node" do
+ @node.parameters = {"one" => "a"}
+ @node.fact_merge("one" => "c")
+ @node.parameters["one"].should == "a"
+ end
+
+ it "should add passed parameters to the parameter list" do
+ @node.parameters = {"one" => "a"}
+ @node.fact_merge("two" => "b")
+ @node.parameters["two"].should == "b"
+ end
+end
+
+describe Puppet::Node, " when indirecting" do
+ before do
+ Puppet[:node_source] = :test_source
+ @terminus_class = mock 'terminus_class'
+ @terminus = mock 'terminus'
+ @terminus_class.expects(:new).returns(@terminus)
+ Puppet::Indirector.expects(:terminus).with(:node, :test_source).returns(@terminus_class)
+ end
+
+ it "should redirect to the specified node source" do
+ @terminus.expects(:get).with(:my_node)
+ Puppet::Node.get(:my_node)
+ end
+end