diff options
author | Luke Kanies <luke@madstop.com> | 2009-04-06 16:38:14 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-04-22 14:39:35 +1000 |
commit | b9c95ebf81eeb78297003de2d0ed4ca048412393 (patch) | |
tree | 184d2f5c693c6183ac59414ff339553744d110cd /spec/unit | |
parent | 8d0e9976b199a637d82d70701db6c682a89b9d6a (diff) | |
download | puppet-b9c95ebf81eeb78297003de2d0ed4ca048412393.tar.gz puppet-b9c95ebf81eeb78297003de2d0ed4ca048412393.tar.xz puppet-b9c95ebf81eeb78297003de2d0ed4ca048412393.zip |
Adding ActiveRecord terminus classes for Node and Facts.
This is most of the way to replacing standard StoreConfigs
integration with the Indirector. We still need to convert
the Catalog and then change all of the integraiton points
(which is mostly the 'store' call in the Compiler).
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'spec/unit')
-rwxr-xr-x | spec/unit/indirector/active_record.rb | 75 | ||||
-rwxr-xr-x | spec/unit/indirector/facts/active_record.rb | 103 | ||||
-rwxr-xr-x | spec/unit/indirector/node/active_record.rb | 18 | ||||
-rwxr-xr-x | spec/unit/rails/host.rb | 91 |
4 files changed, 287 insertions, 0 deletions
diff --git a/spec/unit/indirector/active_record.rb b/spec/unit/indirector/active_record.rb new file mode 100755 index 000000000..6d81b0fbe --- /dev/null +++ b/spec/unit/indirector/active_record.rb @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' +require 'puppet/indirector/active_record' + +describe Puppet::Indirector::ActiveRecord do + before do + Puppet::Rails.stubs(:init) + + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @active_record_class = Class.new(Puppet::Indirector::ActiveRecord) do + def self.to_s + "Mystuff::Testing" + end + end + + @ar_model = mock 'ar_model' + + @active_record_class.use_ar_model @ar_model + @terminus = @active_record_class.new + + @name = "me" + @instance = stub 'instance', :name => @name + + @request = stub 'request', :key => @name, :instance => @instance + end + + it "should allow declaration of an ActiveRecord model to use" do + @active_record_class.use_ar_model "foo" + @active_record_class.ar_model.should == "foo" + end + + describe "when initializing" do + it "should init Rails" do + Puppet::Rails.expects(:init) + @active_record_class.new + end + end + + describe "when finding an instance" do + it "should use the ActiveRecord model to find the instance" do + @ar_model.expects(:find_by_name).with(@name) + + @terminus.find(@request) + end + + it "should return nil if no instance is found" do + @ar_model.expects(:find_by_name).with(@name).returns nil + @terminus.find(@request).should be_nil + end + + it "should convert the instance to a Puppet object if it is found" do + instance = mock 'rails_instance' + instance.expects(:to_puppet).returns "mypuppet" + + @ar_model.expects(:find_by_name).with(@name).returns instance + @terminus.find(@request).should == "mypuppet" + end + end + + describe "when saving an instance" do + it "should use the ActiveRecord model to convert the instance into a Rails object and then save that rails object" do + rails_object = mock 'rails_object' + @ar_model.expects(:from_puppet).with(@instance).returns rails_object + + rails_object.expects(:save) + + @terminus.save(@request) + end + end +end diff --git a/spec/unit/indirector/facts/active_record.rb b/spec/unit/indirector/facts/active_record.rb new file mode 100755 index 000000000..340f2cf4c --- /dev/null +++ b/spec/unit/indirector/facts/active_record.rb @@ -0,0 +1,103 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +require 'puppet/node/facts' +require 'puppet/indirector/facts/active_record' + +describe Puppet::Node::Facts::ActiveRecord do + confine "Missing Rails" => Puppet.features.rails? + + before do + Puppet.features.stubs(:rails?).returns true + @terminus = Puppet::Node::Facts::ActiveRecord.new + end + + it "should be a subclass of the ActiveRecord terminus class" do + Puppet::Node::Facts::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + end + + it "should use Puppet::Rails::Host as its ActiveRecord model" do + Puppet::Node::Facts::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + end + + describe "when finding an instance" do + before do + @request = stub 'request', :key => "foo" + end + + it "should use the Hosts ActiveRecord class to find the host" do + Puppet::Rails::Host.expects(:find_by_name).with { |key, args| key == "foo" } + @terminus.find(@request) + end + + it "should include the fact names and values when finding the host" do + Puppet::Rails::Host.expects(:find_by_name).with { |key, args| args[:include] == {:fact_values => :fact_name} } + @terminus.find(@request) + end + + it "should return nil if no host instance can be found" do + Puppet::Rails::Host.expects(:find_by_name).returns nil + + @terminus.find(@request).should be_nil + end + + it "should convert the node's parameters into a Facts instance if a host instance is found" do + host = stub 'host', :name => "foo" + host.expects(:get_facts_hash).returns("one" => [mock("two_value", :value => "two")], "three" => [mock("three_value", :value => "four")]) + + Puppet::Rails::Host.expects(:find_by_name).returns host + + result = @terminus.find(@request) + + result.should be_instance_of(Puppet::Node::Facts) + result.name.should == "foo" + result.values.should == {"one" => "two", "three" => "four"} + end + + it "should convert all single-member arrays into non-arrays" do + host = stub 'host', :name => "foo" + host.expects(:get_facts_hash).returns("one" => [mock("two_value", :value => "two")]) + + Puppet::Rails::Host.expects(:find_by_name).returns host + + @terminus.find(@request).values["one"].should == "two" + end + end + + describe "when saving an instance" do + before do + @host = stub 'host', :name => "foo", :save => nil, :setfacts => nil + Puppet::Rails::Host.stubs(:find_by_name).returns @host + @facts = Puppet::Node::Facts.new("foo", "one" => "two", "three" => "four") + @request = stub 'request', :key => "foo", :instance => @facts + end + + it "should find the Rails host with the same name" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + + @terminus.save(@request) + end + + it "should create a new Rails host if none can be found" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns nil + + Puppet::Rails::Host.expects(:create).with(:name => "foo").returns @host + + @terminus.save(@request) + end + + it "should set the facts as facts on the Rails host instance" do + # There is other stuff added to the hash. + @host.expects(:setfacts).with { |args| args["one"] == "two" and args["three"] == "four" } + + @terminus.save(@request) + end + + it "should save the Rails host instance" do + @host.expects(:save) + + @terminus.save(@request) + end + end +end diff --git a/spec/unit/indirector/node/active_record.rb b/spec/unit/indirector/node/active_record.rb new file mode 100755 index 000000000..22a6bebaf --- /dev/null +++ b/spec/unit/indirector/node/active_record.rb @@ -0,0 +1,18 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +require 'puppet/node' +require 'puppet/indirector/node/active_record' + +describe Puppet::Node::ActiveRecord do + confine "Missing Rails" => Puppet.features.rails? + + it "should be a subclass of the ActiveRecord terminus class" do + Puppet::Node::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + end + + it "should use Puppet::Rails::Host as its ActiveRecord model" do + Puppet::Node::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + end +end diff --git a/spec/unit/rails/host.rb b/spec/unit/rails/host.rb new file mode 100755 index 000000000..882abbd5a --- /dev/null +++ b/spec/unit/rails/host.rb @@ -0,0 +1,91 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe "Puppet::Rails::Host" do + confine "Cannot test without ActiveRecord" => Puppet.features.rails? + + def column(name, type) + ActiveRecord::ConnectionAdapters::Column.new(name, nil, type, false) + end + + before do + require 'puppet/rails/host' + + # Stub this so we don't need access to the DB. + Puppet::Rails::Host.stubs(:columns).returns([column("name", "string"), column("environment", "string"), column("ip", "string")]) + + @node = Puppet::Node.new("foo") + @node.environment = "production" + @node.ipaddress = "127.0.0.1" + + @host = stub 'host', :environment= => nil, :ip= => nil + end + + describe "when converting a Puppet::Node instance into a Rails instance" do + it "should modify any existing instance in the database" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + + Puppet::Rails::Host.from_puppet(@node) + end + + it "should create a new instance in the database if none can be found" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns nil + Puppet::Rails::Host.expects(:new).with(:name => "foo").returns @host + + Puppet::Rails::Host.from_puppet(@node) + end + + it "should copy the environment from the Puppet instance" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + + @node.environment = "production" + @host.expects(:environment=).with "production" + + Puppet::Rails::Host.from_puppet(@node) + end + + it "should copy the ipaddress from the Puppet instance" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + + @node.ipaddress = "192.168.0.1" + @host.expects(:ip=).with "192.168.0.1" + + Puppet::Rails::Host.from_puppet(@node) + end + + it "should not save the Rails instance" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + + @host.expects(:save).never + + Puppet::Rails::Host.from_puppet(@node) + end + end + + describe "when converting a Puppet::Rails::Host instance into a Puppet::Node instance" do + before do + @host = Puppet::Rails::Host.new(:name => "foo", :environment => "production", :ip => "127.0.0.1") + @node = Puppet::Node.new("foo") + Puppet::Node.stubs(:new).with("foo").returns @node + end + + it "should create a new instance with the correct name" do + Puppet::Node.expects(:new).with("foo").returns @node + + @host.to_puppet + end + + it "should copy the environment from the Rails instance" do + @host.environment = "prod" + @node.expects(:environment=).with "prod" + @host.to_puppet + end + + it "should copy the ipaddress from the Rails instance" do + @host.ip = "192.168.0.1" + @node.expects(:ipaddress=).with "192.168.0.1" + @host.to_puppet + end + end +end |