summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Bradley <rick@rickbradley.com>2007-10-15 14:47:25 -0500
committerRick Bradley <rick@rickbradley.com>2007-10-15 14:47:25 -0500
commitc34efbccf1eec9957253d4fcdcb4ea9c79837ad8 (patch)
treeae04acf64b2b2a772c9d7d113fccf994fc6c71a3
parent9a179ec3a9df62c6179e7151831c4f07197cfbce (diff)
downloadpuppet-c34efbccf1eec9957253d4fcdcb4ea9c79837ad8.tar.gz
puppet-c34efbccf1eec9957253d4fcdcb4ea9c79837ad8.tar.xz
puppet-c34efbccf1eec9957253d4fcdcb4ea9c79837ad8.zip
Hooking up address/port support for the various servers w/ specs. Still need to start up a webrick server w/ address + port (this is far too incestuous with Puppet lib & Puppet.start at the moment).
-rw-r--r--lib/puppet/network/http/mongrel.rb6
-rw-r--r--lib/puppet/network/http/webrick.rb5
-rw-r--r--lib/puppet/network/server.rb6
-rw-r--r--spec/unit/network/http/mongrel.rb43
-rw-r--r--spec/unit/network/http/webrick.rb29
-rw-r--r--spec/unit/network/server.rb70
6 files changed, 119 insertions, 40 deletions
diff --git a/lib/puppet/network/http/mongrel.rb b/lib/puppet/network/http/mongrel.rb
index 49449cf59..bbba69fe3 100644
--- a/lib/puppet/network/http/mongrel.rb
+++ b/lib/puppet/network/http/mongrel.rb
@@ -6,9 +6,11 @@ class Puppet::Network::HTTP::Mongrel
end
def listen(args = {})
- raise ArgumentError if args.keys.empty?
+ raise ArgumentError, ":handlers must be specified." if !args[:handlers] or args[:handlers].keys.empty?
+ raise ArgumentError, ":address must be specified." unless args[:address]
+ raise ArgumentError, ":port must be specified." unless args[:port]
raise "Mongrel server is already listening" if listening?
- @server = Mongrel::HttpServer.new("0.0.0.0", "3000")
+ @server = Mongrel::HttpServer.new(args[:address], args[:port])
@server.run
@listening = true
end
diff --git a/lib/puppet/network/http/webrick.rb b/lib/puppet/network/http/webrick.rb
index 85a329454..c22bce938 100644
--- a/lib/puppet/network/http/webrick.rb
+++ b/lib/puppet/network/http/webrick.rb
@@ -7,8 +7,11 @@ class Puppet::Network::HTTP::WEBrick < WEBrick::HTTPServer
end
def listen(args = {})
- raise ArgumentError if args.keys.empty?
+ raise ArgumentError, ":handlers must be specified." if !args[:handlers] or args[:handlers].keys.empty?
+ raise ArgumentError, ":address must be specified." unless args[:address]
+ raise ArgumentError, ":port must be specified." unless args[:port]
raise "WEBrick server is already listening" if listening?
+
# TODO / FIXME: this should be moved out of the wacky Puppet global namespace!
Puppet.start
@listening = true
diff --git a/lib/puppet/network/server.rb b/lib/puppet/network/server.rb
index 941cb9df1..0541c1c3b 100644
--- a/lib/puppet/network/server.rb
+++ b/lib/puppet/network/server.rb
@@ -1,9 +1,13 @@
class Puppet::Network::Server
- attr_reader :server_type, :http_server_class, :protocols
+ attr_reader :server_type, :http_server_class, :protocols, :address, :port
def initialize(args = {})
@server_type = Puppet[:servertype] or raise "No servertype configuration found." # e.g., WEBrick, Mongrel, etc.
@http_server_class = http_server_class_by_type(@server_type)
+ @address = args[:address] || Puppet[:bindaddress] ||
+ raise(ArgumentError, "Must specify :address or configure Puppet :bindaddress.")
+ @port = args[:port] || Puppet[:masterport] ||
+ raise(ArgumentError, "Must specify :port or configure Puppet :masterport")
@protocols = []
@listening = false
@routes = {}
diff --git a/spec/unit/network/http/mongrel.rb b/spec/unit/network/http/mongrel.rb
index 3456a16d6..f964e6844 100644
--- a/spec/unit/network/http/mongrel.rb
+++ b/spec/unit/network/http/mongrel.rb
@@ -15,45 +15,59 @@ end
describe Puppet::Network::HTTP::Mongrel, "when turning on listening" do
before do
@server = Puppet::Network::HTTP::Mongrel.new
+ @mock_mongrel = mock('mongrel')
+ @mock_mongrel.stubs(:run)
+ Mongrel::HttpServer.stubs(:new).returns(@mock_mongrel)
+ @listen_params = { :address => "127.0.0.1", :port => 31337, :handlers => { :foo => :bar }}
end
it "should fail if already listening" do
- @server.listen(:foo => :bar)
- Proc.new { @server.listen(:foo => :bar) }.should raise_error(RuntimeError)
+ @server.listen(@listen_params)
+ Proc.new { @server.listen(@listen_params) }.should raise_error(RuntimeError)
end
it "should require at least one handler" do
- Proc.new { @server.listen }.should raise_error(ArgumentError)
+ Proc.new { @server.listen(@listen_params.delete_if {|k,v| :handlers == k}) }.should raise_error(ArgumentError)
+ end
+
+ it "should require a listening address to be specified" do
+ Proc.new { @server.listen(@listen_params.delete_if {|k,v| :address == k})}.should raise_error(ArgumentError)
+ end
+
+ it "should require a listening port to be specified" do
+ Proc.new { @server.listen(@listen_params.delete_if {|k,v| :port == k})}.should raise_error(ArgumentError)
end
it "should order a mongrel server to start" do
- mock_mongrel = mock('mongrel httpserver')
- mock_mongrel.expects(:run)
- Mongrel::HttpServer.expects(:new).returns(mock_mongrel)
- @server.listen(:foo => :bar)
+ @mock_mongrel.expects(:run)
+ @server.listen(@listen_params)
+ end
+
+ it "should tell mongrel to listen on the specified address and port" do
+ Mongrel::HttpServer.expects(:new).with("127.0.0.1", 31337).returns(@mock_mongrel)
+ @server.listen(@listen_params)
end
it "should be listening" do
mock_mongrel = mock('mongrel httpserver')
mock_mongrel.expects(:run)
Mongrel::HttpServer.expects(:new).returns(mock_mongrel)
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
@server.should be_listening
end
it "should instantiate a specific handler (mongrel+rest, e.g.) for each handler, for each protocol being served (xmlrpc, rest, etc.)"
it "should mount handlers on a mongrel path"
- it "should be able to specify the address on which mongrel will listen"
- it "should be able to specify the port on which mongrel will listen"
end
-describe Puppet::Network::HTTP::WEBrick, "when turning off listening" do
+describe Puppet::Network::HTTP::Mongrel, "when turning off listening" do
before do
@mock_mongrel = mock('mongrel httpserver')
@mock_mongrel.stubs(:run)
- @mock_mongrel.stubs(:graceful_shutdown)
Mongrel::HttpServer.stubs(:new).returns(@mock_mongrel)
+
@server = Puppet::Network::HTTP::Mongrel.new
+ @listen_params = { :address => "127.0.0.1", :port => 31337, :handlers => { :foo => :bar }}
end
it "should fail unless listening" do
@@ -61,13 +75,14 @@ describe Puppet::Network::HTTP::WEBrick, "when turning off listening" do
end
it "should order mongrel server to stop" do
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
@mock_mongrel.expects(:graceful_shutdown)
@server.unlisten
end
it "should not be listening" do
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
+ @mock_mongrel.stubs(:graceful_shutdown)
@server.unlisten
@server.should_not be_listening
end
diff --git a/spec/unit/network/http/webrick.rb b/spec/unit/network/http/webrick.rb
index b070ec0d6..9ab97e831 100644
--- a/spec/unit/network/http/webrick.rb
+++ b/spec/unit/network/http/webrick.rb
@@ -15,39 +15,48 @@ end
describe Puppet::Network::HTTP::WEBrick, "when turning on listening" do
before do
@server = Puppet::Network::HTTP::WEBrick.new
+ @listen_params = { :address => "127.0.0.1", :port => 31337, :handlers => { :foo => :bar }}
Puppet.stubs(:start)
end
it "should fail if already listening" do
- @server.listen(:foo => :bar)
- Proc.new { @server.listen(:foo => :bar) }.should raise_error(RuntimeError)
+ @server.listen(@listen_params)
+ Proc.new { @server.listen(@listen_params) }.should raise_error(RuntimeError)
end
it "should require at least one handler" do
- Proc.new { @server.listen }.should raise_error(ArgumentError)
+ Proc.new { @server.listen(@listen_params.delete_if {|k,v| :handlers == k}) }.should raise_error(ArgumentError)
end
+ it "should require a listening address to be specified" do
+ Proc.new { @server.listen(@listen_params.delete_if {|k,v| :address == k})}.should raise_error(ArgumentError)
+ end
+
+ it "should require a listening port to be specified" do
+ Proc.new { @server.listen(@listen_params.delete_if {|k,v| :port == k})}.should raise_error(ArgumentError)
+ end
+
it "should order a webrick server to start" do
Puppet.expects(:start)
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
end
+ it "should tell webrick to listen on the specified address and port"
+
it "should be listening" do
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
@server.should be_listening
end
it "should instantiate a specific handler (webrick+rest, e.g.) for each handler, for each protocol being served (xmlrpc, rest, etc.)"
it "should mount handlers on a webrick path"
-
- it "should be able to specify the address on which webrick will listen"
- it "should be able to specify the port on which webrick will listen"
end
describe Puppet::Network::HTTP::WEBrick, "when turning off listening" do
before do
@server = Puppet::Network::HTTP::WEBrick.new
@server.stubs(:shutdown)
+ @listen_params = { :address => "127.0.0.1", :port => 31337, :handlers => { :foo => :bar }}
Puppet.stubs(:start).returns(true)
end
@@ -58,12 +67,12 @@ describe Puppet::Network::HTTP::WEBrick, "when turning off listening" do
it "should order webrick server to stop" do
@server.should respond_to(:shutdown)
@server.expects(:shutdown)
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
@server.unlisten
end
it "should no longer be listening" do
- @server.listen(:foo => :bar)
+ @server.listen(@listen_params)
@server.unlisten
@server.should_not be_listening
end
diff --git a/spec/unit/network/server.rb b/spec/unit/network/server.rb
index f5d5e25d5..6802c6aaa 100644
--- a/spec/unit/network/server.rb
+++ b/spec/unit/network/server.rb
@@ -10,33 +10,71 @@ describe Puppet::Network::Server, "when initializing" do
before do
@mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.stubs(:server_class_by_type).returns(@mock_http_server_class)
+ Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
+ end
+
+ it "should allow specifying a listening address" do
+ Puppet.stubs(:[]).with(:masterport).returns('')
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1")
+ @server.address.should == "127.0.0.1"
+ end
+
+ it "should allow specifying a listening port" do
+ Puppet.stubs(:[]).with(:bindaddress).returns('')
+ @server = Puppet::Network::Server.new(:port => 31337)
+ @server.port.should == 31337
+ end
+
+ it "should use the Puppet configurator to find a default listening address" do
+ Puppet.stubs(:[]).with(:masterport).returns('')
+ Puppet.expects(:[]).with(:bindaddress).returns("10.0.0.1")
+ @server = Puppet::Network::Server.new
+ @server.address.should == "10.0.0.1"
+ end
+
+ it "should use the Puppet configurator to find a default listening port" do
+ Puppet.stubs(:[]).with(:bindaddress).returns('')
+ Puppet.expects(:[]).with(:masterport).returns(6667)
+ @server = Puppet::Network::Server.new
+ @server.port.should == 6667
+ end
+
+ it "should fail to initialize if no listening address can be found" do
+ Puppet.stubs(:[]).with(:masterport).returns(6667)
+ Puppet.stubs(:[]).with(:bindaddress).returns(nil)
+ Proc.new { Puppet::Network::Server.new }.should raise_error(ArgumentError)
end
+ it "should fail to initialize if no listening port can be found" do
+ Puppet.stubs(:[]).with(:bindaddress).returns("127.0.0.1")
+ Puppet.stubs(:[]).with(:masterport).returns(nil)
+ Proc.new { Puppet::Network::Server.new }.should raise_error(ArgumentError)
+ end
+
it "should use the Puppet configurator to determine which HTTP server will be used to provide access to clients" do
Puppet.expects(:[]).with(:servertype).returns(:suparserver)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
@server.server_type.should == :suparserver
end
it "should fail to initialize if there is no HTTP server known to the Puppet configurator" do
Puppet.expects(:[]).with(:servertype).returns(nil)
- Proc.new { Puppet::Network::Server.new }.should raise_error
+ Proc.new { Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337) }.should raise_error
end
it "should ask the Puppet::Network::HTTP class to fetch the proper HTTP server class" do
- Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.expects(:server_class_by_type).with(:suparserver).returns(mock_http_server_class)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
end
it "should allow registering indirections" do
- @server = Puppet::Network::Server.new(:handlers => [ :foo, :bar, :baz])
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337, :handlers => [ :foo, :bar, :baz])
Proc.new { @server.unregister(:foo, :bar, :baz) }.should_not raise_error
end
it "should not be listening after initialization" do
- Puppet::Network::Server.new.should_not be_listening
+ Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337).should_not be_listening
end
end
@@ -45,7 +83,7 @@ describe Puppet::Network::Server, "in general" do
@mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.stubs(:server_class_by_type).returns(@mock_http_server_class)
Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
end
it "should allow registering an indirection for client access by specifying its indirection name" do
@@ -110,7 +148,7 @@ describe Puppet::Network::Server, "in general" do
end
it "should allow for multiple configurations, each handling different indirections" do
- @server2 = Puppet::Network::Server.new
+ @server2 = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
@server.register(:foo, :bar)
@server2.register(:foo, :xyzzy)
@server.unregister(:foo, :bar)
@@ -122,6 +160,14 @@ describe Puppet::Network::Server, "in general" do
it "should provide a means of determining which style of service is being offered to clients" do
@server.protocols.should == []
end
+
+ it "should provide a means of determining the listening address" do
+ @server.address.should == "127.0.0.1"
+ end
+
+ it "should provide a means of determining the listening port" do
+ @server.port.should == 31337
+ end
end
describe Puppet::Network::Server, "when listening is off" do
@@ -129,7 +175,7 @@ describe Puppet::Network::Server, "when listening is off" do
@mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.stubs(:server_class_by_type).returns(@mock_http_server_class)
Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
@mock_http_server = mock('http server')
@mock_http_server.stubs(:listen)
@server.stubs(:http_server).returns(@mock_http_server)
@@ -154,7 +200,7 @@ describe Puppet::Network::Server, "when listening is on" do
@mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.stubs(:server_class_by_type).returns(@mock_http_server_class)
Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
@mock_http_server = mock('http server')
@mock_http_server.stubs(:listen)
@mock_http_server.stubs(:unlisten)
@@ -180,7 +226,7 @@ describe Puppet::Network::Server, "when listening is being turned on" do
@mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.stubs(:server_class_by_type).returns(@mock_http_server_class)
Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
@mock_http_server = mock('http server')
@mock_http_server.stubs(:listen)
end
@@ -204,7 +250,7 @@ describe Puppet::Network::Server, "when listening is being turned off" do
@mock_http_server_class = mock('http server class')
Puppet::Network::HTTP.stubs(:server_class_by_type).returns(@mock_http_server_class)
Puppet.stubs(:[]).with(:servertype).returns(:suparserver)
- @server = Puppet::Network::Server.new
+ @server = Puppet::Network::Server.new(:address => "127.0.0.1", :port => 31337)
@mock_http_server = mock('http server')
@mock_http_server.stubs(:listen)
@server.stubs(:http_server).returns(@mock_http_server)