diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-05-19 01:44:05 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-05-19 01:44:05 -0700 |
commit | 4ce7356d6d08cf02dde23b6e2fe2066a26ec45b2 (patch) | |
tree | 9e9953b6240efe270603a19151d91c9c8a0508dc /spec/unit | |
parent | a125c8a450c4368bfdb1668679dd7153d53757c6 (diff) | |
download | chef-4ce7356d6d08cf02dde23b6e2fe2066a26ec45b2.tar.gz |
Working on network layer
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/config_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/file_store_spec.rb | 95 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 25 | ||||
-rw-r--r-- | spec/unit/queue_spec.rb | 107 | ||||
-rw-r--r-- | spec/unit/resource_collection_spec.rb | 14 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 12 | ||||
-rw-r--r-- | spec/unit/search_index_spec.rb | 57 |
7 files changed, 308 insertions, 8 deletions
diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 5d74e83249..543a7caa13 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -84,4 +84,10 @@ describe Chef::Config do lambda { Chef::Config[:snob_hobbery] }.should raise_error(ArgumentError) end + it "should return true or false with has_key?" do + Chef::Config.has_key?(:monkey).should eql(false) + Chef::Config[:monkey] = "gotcha" + Chef::Config.has_key?(:monkey).should eql(true) + end + end
\ No newline at end of file diff --git a/spec/unit/file_store_spec.rb b/spec/unit/file_store_spec.rb new file mode 100644 index 0000000000..f2f1070f2d --- /dev/null +++ b/spec/unit/file_store_spec.rb @@ -0,0 +1,95 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) + +class Fakestore + attr_accessor :name + + def to_json(*a) + { :name => @name }.to_json(*a) + end + + def self.json_create(o) + new_fakestore = new + new_fakestore.name = o[:name] + new_fakestore + end +end + +describe Chef::FileStore do + before(:each) do + Chef::Config[:file_store_path] = "/tmp/chef-test" + @fakestore = Fakestore.new + @fakestore.name = "Landslide" + @fakestore_digest = "a56a428bddac69e505731708ba206da0bb75e8de883bb4d5ef6be9b327da556a" + end + + it "should return a path to a file given a type and key" do + Dir.stub!(:mkdir).and_return(true) + File.stub!(:directory?).and_return(true) + path = Chef::FileStore.create_store_path("fakestore", @fakestore.name) + path.should eql("/tmp/chef-test/fakestore/a/56a/Landslide") + end + + it "should create directories for the path if needed" do + File.stub!(:directory?).and_return(false) + Dir.should_receive(:mkdir).exactly(4).times.and_return(true) + Chef::FileStore.create_store_path("fakestore", @fakestore.name) + end + + it "should store an object with a type and key" do + Chef::FileStore.should_receive(:create_store_path).with("fakestore", @fakestore.name).and_return("/monkey") + File.stub!(:directory?).and_return(true) + ioobj = mock("IO", :null_object => true) + ioobj.should_receive(:puts).with(@fakestore.to_json) + ioobj.should_receive(:close).once.and_return(true) + File.should_receive(:open).with("/monkey", "w").and_return(ioobj) + Chef::FileStore.store("fakestore", @fakestore.name, @fakestore) + end + + it "should load an object from the store with type and key" do + Chef::FileStore.should_receive(:create_store_path).with("fakestore", @fakestore.name).and_return("/monkey") + File.stub!(:exists?).and_return(true) + IO.should_receive(:read).once.and_return(true) + JSON.should_receive(:parse).and_return(true) + Chef::FileStore.load("fakestore", @fakestore.name) + end + + it "should through an exception if it cannot load a file from the store" do + Chef::FileStore.should_receive(:create_store_path).and_return("/tmp") + File.stub!(:exists?).and_return(false) + lambda { Chef::FileStore.load("fakestore", @fakestore.name) }.should raise_error(RuntimeError) + end + + it "should delete a file from the store if it exists" do + Chef::FileStore.should_receive(:create_store_path).with("node", "nothing").and_return("/tmp/foolio") + File.stub!(:exists?).and_return(true) + File.should_receive(:unlink).with("/tmp/foolio").and_return(1) + Chef::FileStore.delete("node", "nothing") + end + + it "should list all the keys of a particular type" do + Dir.should_receive(:[]).with("/tmp/chef-test/node/**/*").and_return(["pool"]) + File.should_receive(:file?).with("pool").and_return(true) + Chef::FileStore.list("node").should eql(["pool"]) + end + +end
\ No newline at end of file diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 0ea4d864d4..753d056404 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -153,14 +153,23 @@ describe Chef::Node do it "should serialize itself as json" do node = Chef::Node.find("test.example.com") - json = node.to_json - result = JSON.load(json) - result["name"].should == "test.example.com" - result["type"].should == "Chef::Node" - result["attributes"]["something"].should == "else" - result["attributes"]["sunshine"].should == "in" - result["recipes"].detect { |r| r == "operations-master" }.should == "operations-master" - result["recipes"].detect { |r| r == "operations-monitoring" }.should == "operations-monitoring" + json = node.to_json() + json.should =~ /json_class/ + json.should =~ /name/ + json.should =~ /attributes/ + json.should =~ /recipes/ + end + + it "should deserialize itself from json" do + original_node = Chef::Node.find("test.example.com") + json = original_node.to_json + serialized_node = JSON.parse(json) + serialized_node.should be_a_kind_of(Chef::Node) + serialized_node.name.should eql(original_node.name) + original_node.each_attribute do |k,v| + serialized_node[k].should eql(v) + end + serialized_node.recipes.should eql(original_node.recipes) end it "should return a list of node names based on which files are in the node_path" do diff --git a/spec/unit/queue_spec.rb b/spec/unit/queue_spec.rb new file mode 100644 index 0000000000..9a199c9852 --- /dev/null +++ b/spec/unit/queue_spec.rb @@ -0,0 +1,107 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) + +describe Chef::Queue do + + it "should connect to a stomp server on localhost and 61613" do + Stomp::Connection.should_receive(:open).with("", "", "localhost", 61613, false).once + Chef::Queue.connect + end + + it "should allow config options to override defaults on connect" do + Chef::Config[:queue_user] = "monkey" + Chef::Config[:queue_password] = "password" + Chef::Config[:queue_host] = "10.10.10.10" + Chef::Config[:queue_port] = 61614 + Stomp::Connection.should_receive(:open).with("monkey", "password", "10.10.10.10", 61614, false).once + Chef::Queue.connect + end + + it "should make a url based on type and name" do + Chef::Queue.make_url("topic", "goal").should eql("/topic/chef/goal") + Chef::Queue.make_url("queue", "pool").should eql("/queue/chef/pool") + end + + it "should allow you to subscribe to a queue" do + queue = mock("Queue", :null_object => true) + queue.should_receive(:subscribe).with(Chef::Queue.make_url(:topic, :node)).once + Stomp::Connection.stub!(:open).and_return(queue) + Chef::Queue.connect + Chef::Queue.subscribe(:topic, :node) + end + + it "should allow you to send a message" do + message = mock("Message", :null_object => true) + message.should_receive(:to_json).once.and_return("some json") + connection = mock("Connection", :null_object => true) + connection.should_receive(:send).with(Chef::Queue.make_url(:queue, :node), "some json").once.and_return(true) + Stomp::Connection.stub!(:open).and_return(connection) + Chef::Queue.connect + Chef::Queue.send_msg(:queue, :node, message) + end + + it "should receive a message with receive_msg" do + raw_msg = mock("Stomp Message", :null_object => true) + raw_msg.should_receive(:body).twice.and_return("the body") + connection = mock("Connection", :null_object => true) + connection.should_receive(:receive).once.and_return(raw_msg) + JSON.should_receive(:parse).with("the body").and_return("the body") + Stomp::Connection.stub!(:open).and_return(connection) + Chef::Queue.connect + Chef::Queue.receive_msg.should eql([ "the body", raw_msg ]) + end + + it "should poll for a message with poll_msg, returning a message if there is one" do + raw_msg = mock("Stomp Message", :null_object => true) + raw_msg.should_receive(:body).once.and_return("the body") + connection = mock("Connection", :null_object => true) + connection.should_receive(:poll).once.and_return(raw_msg) + JSON.should_receive(:parse).with("the body").and_return("the body") + Stomp::Connection.stub!(:open).and_return(connection) + Chef::Queue.connect + Chef::Queue.poll_msg.should eql("the body") + end + + it "should poll for a message with poll_msg, returning nil if there is not a message" do + connection = mock("Connection", :null_object => true) + connection.should_receive(:poll).once.and_return(nil) + JSON.should_not_receive(:parse).with(nil) + Stomp::Connection.stub!(:open).and_return(connection) + Chef::Queue.connect + Chef::Queue.poll_msg.should eql(nil) + end + + it "should raise an exception if you disconnect without a connection" do + Stomp::Connection.stub!(:open).and_return(nil) + Chef::Queue.connect + lambda { Chef::Queue.disconnect }.should raise_error(ArgumentError) + end + + it "should disconnect an active connection" do + connection = mock("Connection", :null_object => true) + connection.should_receive(:disconnect).once.and_return(true) + Stomp::Connection.stub!(:open).and_return(connection) + Chef::Queue.connect + Chef::Queue.disconnect + end + +end
\ No newline at end of file diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb index 10f251aaac..ac83e019c0 100644 --- a/spec/unit/resource_collection_spec.rb +++ b/spec/unit/resource_collection_spec.rb @@ -171,6 +171,20 @@ describe Chef::ResourceCollection do it "should raise an exception if you pass something other than a string or hash to resource" do lambda { @rc.resources([Array.new]) }.should raise_error(ArgumentError) end + + it "should serialize to json" do + json = @rc.to_json + json.should =~ /json_class/ + json.should =~ /instance_vars/ + end + + it "should deserialize itself from json" do + @rc << @resource + json = @rc.to_json + s_rc = JSON.parse(json) + s_rc.should be_a_kind_of(Chef::ResourceCollection) + s_rc[0].name.should eql(@resource.name) + end def check_by_names(results, *names) names.each do |res_name| diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index be9a7cc3aa..6be2932967 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -130,6 +130,18 @@ describe Chef::Resource do @resource.noop.should eql(true) end + it "should serialize to json" do + json = @resource.to_json + json.should =~ /json_class/ + json.should =~ /instance_vars/ + end + + it "should deserialize itself from json" do + json = @resource.to_json + serialized_node = JSON.parse(json) + serialized_node.should be_a_kind_of(Chef::Resource) + serialized_node.name.should eql(@resource.name) + end # it "should serialize to yaml" do # yaml_output = <<-DESC diff --git a/spec/unit/search_index_spec.rb b/spec/unit/search_index_spec.rb new file mode 100644 index 0000000000..2957f9c774 --- /dev/null +++ b/spec/unit/search_index_spec.rb @@ -0,0 +1,57 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) + +describe Chef::SearchIndex do + before(:each) do + @fake_indexer = stub("Indexer", :null_object => true) + Ferret::Index::Index.stub!(:new).and_return(@fake_indexer) + @sindex = Chef::SearchIndex.new() + @node = Chef::Node.new + @node.name "adam.foo.com" + @node.fqdn "adam.foo.com" + @node.mars "volta" + @node.recipes "one", "two" + end + + it "should index a node object with add" do + @sindex.should_receive(:_prepare_node).with(@node).and_return("my value") + @fake_indexer.should_receive(:add_document).with("my value") + @sindex.add(@node) + end + + it "should remove a node from the index with delete" do + @sindex.should_receive(:_prepare_node).with(@node).and_return({ :id => "node-my value" }) + @fake_indexer.should_receive(:delete).with(:id => "node-my value") + @sindex.delete(@node) + end + + it "should prepare a node by creating a proper hash" do + node_hash = @sindex.send(:_prepare_node, @node) + node_hash[:id].should eql("node-adam.foo.com") + node_hash[:type].should eql("node") + node_hash[:name].should eql("adam.foo.com") + node_hash[:fqdn].should eql("adam.foo.com") + node_hash[:mars].should eql("volta") + node_hash[:recipe].should eql(["one", "two"]) + end + +end
\ No newline at end of file |