summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-03-07 15:19:20 -0800
committerNick Lewis <nick@puppetlabs.com>2011-03-08 15:23:50 -0800
commit8ce30c83ddba87ba7e2622a46f27143159132789 (patch)
tree668e6c6ec15a84d280e6c25b4b182809587f2036 /spec/unit
parente8145f91debc863b341a270e1d8cff6c43d93ef5 (diff)
downloadpuppet-8ce30c83ddba87ba7e2622a46f27143159132789.tar.gz
puppet-8ce30c83ddba87ba7e2622a46f27143159132789.tar.xz
puppet-8ce30c83ddba87ba7e2622a46f27143159132789.zip
(#6338) Add an InventoryActiveRecord terminus for Facts
So far this terminus only supports find and save. Search is forthcoming. It uses two new tables (inventory_host and inventory_facts) so that it won't interact with storedconfigs. Paired-With: Jacob Helwig
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/indirector/facts/inventory_active_record_spec.rb99
1 files changed, 99 insertions, 0 deletions
diff --git a/spec/unit/indirector/facts/inventory_active_record_spec.rb b/spec/unit/indirector/facts/inventory_active_record_spec.rb
new file mode 100644
index 000000000..b97bada19
--- /dev/null
+++ b/spec/unit/indirector/facts/inventory_active_record_spec.rb
@@ -0,0 +1,99 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+require 'sqlite3' rescue nil
+require 'tempfile'
+require 'puppet/rails'
+
+describe "Puppet::Node::Facts::InventoryActiveRecord", :if => (Puppet.features.rails? and defined? SQLite3) do
+ let(:terminus) { Puppet::Node::Facts::InventoryActiveRecord.new }
+
+ before :all do
+ require 'puppet/indirector/facts/inventory_active_record'
+ @dbfile = Tempfile.new("testdb")
+ @dbfile.close
+ end
+
+ after :all do
+ Puppet::Node::Facts.indirection.reset_terminus_class
+ @dbfile.unlink
+ end
+
+ before :each do
+ Puppet::Node::Facts.terminus_class = :inventory_active_record
+ Puppet[:dbadapter] = 'sqlite3'
+ Puppet[:dblocation] = @dbfile.path
+ Puppet[:railslog] = "/dev/null"
+ Puppet::Rails.init
+ end
+
+ after :each do
+ Puppet::Rails.teardown
+ end
+
+ describe "#save" do
+ it "should use an existing host if possible" do
+ host = Puppet::Rails::InventoryHost.new(:name => "foo", :timestamp => Time.now)
+ host.save
+ Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin").save
+
+ Puppet::Rails::InventoryHost.count.should == 1
+ Puppet::Rails::InventoryHost.first.should == host
+ end
+
+ it "should create a new host if one can't be found" do
+ # This test isn't valid if there are hosts to begin with
+ Puppet::Rails::InventoryHost.count.should == 0
+
+ Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin").save
+
+ Puppet::Rails::InventoryHost.count.should == 1
+ Puppet::Rails::InventoryHost.first.name.should == "foo"
+ end
+
+ it "should save the facts" do
+ Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin").save
+
+ Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should =~ [["uptime_days","60"],["kernel","Darwin"]]
+ end
+
+ it "should remove the previous facts for an existing host" do
+ Puppet::Node::Facts.new("foo", "uptime_days" => "30", "kernel" => "Darwin").save
+ bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "35", "kernel" => "Linux")
+ foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "is_virtual" => "false")
+ bar_facts.save
+ foo_facts.save
+
+ Puppet::Node::Facts.find("bar").should == bar_facts
+ Puppet::Node::Facts.find("foo").should == foo_facts
+ Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should_not include(["uptime_days", "30"], ["kernel", "Darwin"])
+ end
+
+ it "should not replace the node's facts if something goes wrong" do
+ end
+ end
+
+ describe "#find" do
+ before do
+ @foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin")
+ @bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "30", "kernel" => "Linux")
+ @foo_facts.save
+ @bar_facts.save
+ end
+
+ it "should identify facts by host name" do
+ Puppet::Node::Facts.find("foo").should == @foo_facts
+ end
+
+ it "should return nil if no host instance can be found" do
+ Puppet::Node::Facts.find("non-existent host").should == nil
+ end
+
+ it "should convert all single-member arrays into non-arrays" do
+ Puppet::Node::Facts.new("array", "fact1" => ["value1"]).save
+
+ Puppet::Node::Facts.find("array").values["fact1"].should == "value1"
+ end
+ end
+end
+