summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-06-15 19:12:57 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-06-15 19:12:57 -0700
commitebc1b392fe7e8f0fbabc305c299b4d365d2b4d9b (patch)
tree1d8882eee26e9ca7f039366c77973d79af1affb3 /spec
parent14ca7fdc284eeaac9c66abd5878d7b24bd439180 (diff)
downloadchef-ebc1b392fe7e8f0fbabc305c299b4d365d2b4d9b.tar.gz
Moving chef-server into lib, reworking specs, adding storieschef-server-package
Diffstat (limited to 'spec')
-rw-r--r--spec/chef_server/controllers/log/merb_test.log4
-rw-r--r--spec/chef_server/controllers/nodes_spec.rb214
-rw-r--r--spec/chef_server/controllers/openid_consumer_spec.rb29
-rw-r--r--spec/chef_server/controllers/openid_register_spec.rb93
-rw-r--r--spec/chef_server/log/merb_test.log4
-rw-r--r--spec/chef_server/spec.opts4
-rw-r--r--spec/chef_server/spec_helper.rb16
-rw-r--r--spec/unit/compile_spec.rb1
8 files changed, 365 insertions, 0 deletions
diff --git a/spec/chef_server/controllers/log/merb_test.log b/spec/chef_server/controllers/log/merb_test.log
new file mode 100644
index 0000000000..86fbe4b9cb
--- /dev/null
+++ b/spec/chef_server/controllers/log/merb_test.log
@@ -0,0 +1,4 @@
+Tue, 27 May 2008 00:52:34 GMT ~ info ~ Logfile created
+ ~ Not Using Sessions
+ ~ Not Using Sessions
+ ~ Not Using Sessions
diff --git a/spec/chef_server/controllers/nodes_spec.rb b/spec/chef_server/controllers/nodes_spec.rb
new file mode 100644
index 0000000000..a86fa4138c
--- /dev/null
+++ b/spec/chef_server/controllers/nodes_spec.rb
@@ -0,0 +1,214 @@
+require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
+
+describe Nodes, "index action" do
+ it "should get a list of all the nodes" do
+ Chef::Node.should_receive(:list).and_return(["one"])
+ dispatch_to(Nodes, :index) do |c|
+ c.stub!(:display)
+ end
+ end
+
+ it "should send a list of nodes to display" do
+ Chef::Node.stub!(:list).and_return(["one"])
+ dispatch_to(Nodes, :index) do |c|
+ c.should_receive(:display).with(["one"])
+ end
+ end
+end
+
+describe Nodes, "show action" do
+ it "should load a node from the filestore based on the id" do
+ node = stub("Node", :null_object => true)
+ Chef::Node.should_receive(:load).with("bond").once.and_return(node)
+ dispatch_to(Nodes, :show, { :id => "bond" }) do |c|
+ c.should_receive(:display).with(node).once.and_return(true)
+ end
+ end
+
+ it "should return 200 on a well formed request" do
+ node = stub("Node", :null_object => true)
+ Chef::Node.should_receive(:load).with("bond").once.and_return(node)
+ controller = dispatch_to(Nodes, :show, { :id => "bond" }) do |c|
+ c.stub!(:display)
+ end
+ controller.status.should eql(200)
+ end
+
+ it "should raise a BadRequest if the id is not found" do
+ Chef::Node.should_receive(:load).with("bond").once.and_raise(RuntimeError)
+ lambda {
+ dispatch_to(Nodes, :show, { :id => "bond" })
+ }.should raise_error(Merb::ControllerExceptions::BadRequest)
+ end
+end
+
+describe Nodes, "create action" do
+ it "should create a node from an inflated object" do
+ mnode = mock("Node", :null_object => true)
+ mnode.stub!(:name).and_return("bond")
+ mnode.should_receive(:save).once.and_return(true)
+ controller = dispatch_to(Nodes, :create) do |c|
+ c.stub!(:params).and_return({ "inflated_object" => mnode })
+ c.stub!(:session).and_return({
+ :openid => 'http://localhost/openid/server/node/bond',
+ :level => :node,
+ :node_name => "bond",
+ })
+ c.stub!(:display)
+ end
+ controller.status.should eql(202)
+ end
+
+ it "should raise an exception if it cannot inflate an object" do
+ lambda {
+ dispatch_to(Nodes, :create) do |c|
+ c.stub!(:params).and_return({ })
+ end
+ }.should raise_error(Merb::Controller::BadRequest)
+ end
+end
+
+describe Nodes, "update action" do
+ it "should update a node from an inflated object" do
+ mnode = mock("Node", :null_object => true)
+ mnode.stub!(:name).and_return("one")
+ Chef::FileStore.should_receive(:store).with("node", "one", mnode).once.and_return(true)
+ controller = dispatch_to(Nodes, :update, { :id => "one" }) do |c|
+ c.stub!(:session).and_return({
+ :openid => 'http://localhost/openid/server/node/one',
+ :level => :node,
+ :node_name => "one",
+ })
+ c.stub!(:params).and_return({ "inflated_object" => mnode })
+ c.stub!(:display)
+ end
+ controller.status.should eql(202)
+ end
+
+ it "should raise an exception if it cannot inflate an object" do
+ lambda { dispatch_to(Nodes, :update) }.should raise_error(Merb::Controller::BadRequest)
+ end
+end
+
+describe Nodes, "destroy action" do
+ def do_destroy
+ dispatch_to(Nodes, :destroy, { :id => "one" }) do |c|
+ c.stub!(:display)
+ end
+ end
+
+ it "should load the node it's about to destroy from the filestore" do
+ mnode = stub("Node", :null_object => true)
+ Chef::FileStore.should_receive(:load).with("node", "one").once.and_return(mnode)
+ Chef::FileStore.stub!(:delete)
+ do_destroy
+ end
+
+ it "should raise an exception if it cannot find the node to destroy" do
+ Chef::FileStore.should_receive(:load).with("node", "one").once.and_raise(RuntimeError)
+ lambda { do_destroy }.should raise_error(Merb::Controller::BadRequest)
+ end
+
+ it "should remove the node from the filestore" do
+ mnode = stub("Node", :null_object => true)
+ Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
+ Chef::FileStore.should_receive(:delete).with("node", "one")
+ do_destroy
+ end
+
+ it "should remove the node from the search index" do
+ mnode = stub("Node", :null_object => true)
+ Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
+ Chef::FileStore.stub!(:delete)
+ do_destroy
+ end
+
+ it "should return the node it just deleted" do
+ mnode = stub("Node", :null_object => true)
+ Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
+ Chef::FileStore.stub!(:delete)
+ dispatch_to(Nodes, :destroy, { :id => "one" }) do |c|
+ c.should_receive(:display).once.with(mnode)
+ end
+ end
+
+ it "should return a status of 202" do
+ mnode = stub("Node", :null_object => true)
+ Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
+ Chef::FileStore.stub!(:delete)
+ controller = do_destroy
+ controller.status.should eql(202)
+ end
+end
+
+describe Nodes, "compile action" do
+ before(:each) do
+ @compile = stub("Compile", :null_object => true)
+ @node = stub("Node", :null_object => true)
+ @node.stub!(:[]).and_return(true)
+ @node.stub!(:[]=).and_return(true)
+ @node.stub!(:recipes).and_return([])
+ @compile.stub!(:load_definitions).and_return(true)
+ @compile.stub!(:load_recipes).and_return(true)
+ @compile.stub!(:collection).and_return([])
+ @compile.stub!(:node, @node)
+ @compile.stub!(:load_node).and_return(true)
+ @stored_node = stub("Stored Node", :null_object => true)
+ end
+
+ def do_compile
+ Chef::FileStore.stub!(:store).and_return(true)
+ Chef::FileStore.stub!(:load).and_return(@stored_node)
+ Chef::Compile.stub!(:new).and_return(@compile)
+ dispatch_to(Nodes, :compile, { :id => "one" }) do |c|
+ c.stub!(:display)
+ end
+ end
+
+ it "should load the node from the node resource" do
+ @compile.should_receive(:load_node).with("one").and_return(true)
+ do_compile
+ end
+
+ it "should merge the data with the currently stored node" do
+ node1 = Chef::Node.new
+ node1.name "adam"
+ node1.music "crowe"
+ node1.recipes << "monkey"
+ @compile.stub!(:node).and_return(node1)
+ @stored_node = Chef::Node.new
+ @stored_node.name "adam"
+ @stored_node.music "crown"
+ @stored_node.woot "woot"
+ @stored_node.recipes << "monkeysoup"
+ do_compile
+ node1.name.should eql("adam")
+ node1.music.should eql("crown")
+ node1.woot.should eql("woot")
+ node1.recipes.should eql([ "monkey", "monkeysoup" ])
+ end
+
+ it "should load definitions" do
+ @compile.should_receive(:load_definitions)
+ do_compile
+ end
+
+ it "should load recipes" do
+ @compile.should_receive(:load_recipes)
+ do_compile
+ end
+
+ it "should display the collection and node object" do
+ Chef::FileStore.stub!(:load).and_return(@stored_node)
+ Chef::Compile.stub!(:new).and_return(@compile)
+ dispatch_to(Nodes, :compile, { :id => "one" }) do |c|
+ c.should_receive(:display).with({ :collection => [], :node => nil })
+ end
+ end
+
+ it "should return 200" do
+ controller = do_compile
+ controller.status.should eql(200)
+ end
+
+end \ No newline at end of file
diff --git a/spec/chef_server/controllers/openid_consumer_spec.rb b/spec/chef_server/controllers/openid_consumer_spec.rb
new file mode 100644
index 0000000000..a93cad4191
--- /dev/null
+++ b/spec/chef_server/controllers/openid_consumer_spec.rb
@@ -0,0 +1,29 @@
+require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
+
+describe OpenidConsumer, "check_valid_openid_provider method" do
+ it "should confirm the openid provider if the openid_providers config is nil" do
+ Chef::Config[:openid_providers] = nil
+ c = OpenidConsumer.new(true)
+ c.send(:check_valid_openid_provider, "monkeyid").should eql(true)
+ end
+
+ it "should return true if the openid provider is in openid_providers list" do
+ Chef::Config[:openid_providers] = [ 'monkeyid' ]
+ c = OpenidConsumer.new(true)
+ c.send(:check_valid_openid_provider, "monkeyid").should eql(true)
+ end
+
+ it "should return true if the openid provider is in openid_providers list with http://" do
+ Chef::Config[:openid_providers] = [ 'monkeyid' ]
+ c = OpenidConsumer.new(true)
+ c.send(:check_valid_openid_provider, "http://monkeyid").should eql(true)
+ end
+
+ it "should raise an exception if the openid provider is not in openid_providers list" do
+ Chef::Config[:openid_providers] = [ 'monkeyid' ]
+ c = OpenidConsumer.new(true)
+ lambda {
+ c.send(:check_valid_openid_provider, "monkey")
+ }.should raise_error(Merb::Controller::Unauthorized)
+ end
+end \ No newline at end of file
diff --git a/spec/chef_server/controllers/openid_register_spec.rb b/spec/chef_server/controllers/openid_register_spec.rb
new file mode 100644
index 0000000000..6822722868
--- /dev/null
+++ b/spec/chef_server/controllers/openid_register_spec.rb
@@ -0,0 +1,93 @@
+require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
+
+describe OpenidRegister, "index action" do
+ it "should get a list of all registered nodes" do
+ Chef::OpenIDRegistration.should_receive(:list).with(true).and_return(["one"])
+ dispatch_to(OpenidRegister, :index) do |c|
+ c.stub!(:display)
+ end
+ end
+end
+
+describe OpenidRegister, "show action" do
+ it "should raise a 404 if the nodes registration is not found" do
+ Chef::OpenIDRegistration.should_receive(:load).with("foo").and_raise(RuntimeError)
+ lambda {
+ dispatch_to(OpenidRegister, :show, { :id => "foo" })
+ }.should raise_error(Merb::ControllerExceptions::NotFound)
+ end
+
+ it "should call display on the node registration" do
+ Chef::OpenIDRegistration.stub!(:load).and_return(true)
+ dispatch_to(OpenidRegister, :show, { :id => "foo" }) do |c|
+ c.should_receive(:display).with(true)
+ end
+ end
+end
+
+describe OpenidRegister, "create action" do
+ def do_create
+ dispatch_to(OpenidRegister, :create, { :id => "foo", :password => "beck" }) do |c|
+ c.stub!(:display)
+ end
+ end
+
+ it "should require an id to register" do
+ lambda {
+ dispatch_to(OpenidRegister, :create, { :password => "beck" })
+ }.should raise_error(Merb::ControllerExceptions::BadRequest)
+ end
+
+ it "should require a password to register" do
+ lambda {
+ dispatch_to(OpenidRegister, :create, { :id => "foo" })
+ }.should raise_error(Merb::ControllerExceptions::BadRequest)
+ end
+
+ it "should return 400 if a node is already registered" do
+ Chef::OpenIDRegistration.should_receive(:has_key?).with("foo").and_return(true)
+ lambda {
+ dispatch_to(OpenidRegister, :create, { :id => "foo", :password => "beck" })
+ }.should raise_error(Merb::ControllerExceptions::BadRequest)
+ end
+
+ it "should store the registration in a new Chef::OpenIDRegistration" do
+ mock_reg = mock("Chef::OpenIDRegistration", :null_object => true)
+ mock_reg.should_receive(:name=).with("foo").and_return(true)
+ mock_reg.should_receive(:set_password).with("beck").and_return(true)
+ mock_reg.should_receive(:save).and_return(true)
+ Chef::OpenIDRegistration.stub!(:has_key?).and_return(false)
+ Chef::OpenIDRegistration.should_receive(:new).and_return(mock_reg)
+ do_create
+ end
+end
+
+describe OpenidRegister, "update action" do
+ it "should raise a 400 error" do
+ lambda {
+ dispatch_to(OpenidRegister, :update)
+ }
+ end
+end
+
+describe OpenidRegister, "destroy action" do
+ def do_destroy
+ dispatch_to(OpenidRegister, :destroy, { :id => "foo" }) do |c|
+ c.stub!(:display)
+ end
+ end
+
+ it "should return 400 if it cannot find the registration" do
+ Chef::OpenIDRegistration.should_receive(:load).and_raise(ArgumentError)
+ lambda {
+ do_destroy
+ }.should raise_error(Merb::ControllerExceptions::BadRequest)
+ end
+
+ it "should delete the registration from the store" do
+ mock_reg = mock("OpenIDRegistration")
+ mock_reg.should_receive(:destroy).and_return(true)
+ Chef::OpenIDRegistration.should_receive(:load).with("foo").and_return(mock_reg)
+ do_destroy
+ end
+end \ No newline at end of file
diff --git a/spec/chef_server/log/merb_test.log b/spec/chef_server/log/merb_test.log
new file mode 100644
index 0000000000..b2aa15df9a
--- /dev/null
+++ b/spec/chef_server/log/merb_test.log
@@ -0,0 +1,4 @@
+Mon, 16 Jun 2008 02:08:02 GMT ~ info ~ Logfile created
+ ~ Not Using Sessions
+ ~ Not Using Sessions
+ ~ Not Using Sessions
diff --git a/spec/chef_server/spec.opts b/spec/chef_server/spec.opts
new file mode 100644
index 0000000000..3a2ff550a2
--- /dev/null
+++ b/spec/chef_server/spec.opts
@@ -0,0 +1,4 @@
+--color
+--loadby
+mtime
+
diff --git a/spec/chef_server/spec_helper.rb b/spec/chef_server/spec_helper.rb
new file mode 100644
index 0000000000..90c1e0af7a
--- /dev/null
+++ b/spec/chef_server/spec_helper.rb
@@ -0,0 +1,16 @@
+require 'rubygems'
+require 'merb-core'
+require 'spec' # Satiates Autotest and anyone else not using the Rake tasks
+
+Merb.start_environment(
+ :testing => true,
+ :adapter => 'runner',
+ :environment => ENV['MERB_ENV'] || 'test',
+ :init_file => File.join(File.dirname(__FILE__), "..", "..", "lib", "chef_server", "init.rb")
+)
+
+Spec::Runner.configure do |config|
+ config.include(Merb::Test::ViewHelper)
+ config.include(Merb::Test::RouteHelper)
+ config.include(Merb::Test::ControllerHelper)
+end
diff --git a/spec/unit/compile_spec.rb b/spec/unit/compile_spec.rb
index d042837606..3ae69dad2d 100644
--- a/spec/unit/compile_spec.rb
+++ b/spec/unit/compile_spec.rb
@@ -19,6 +19,7 @@
#
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
+
describe Chef::Compile do
before(:each) do
Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "compile", "nodes"))