Force streaming input onto apps by default
[unicorn.git] / test / unit / test_configurator.rb
blobbcdd2f51cac61950b6f5def7ff78858070825309
1 require 'test/unit'
2 require 'tempfile'
3 require 'unicorn'
5 class TestConfigurator < Test::Unit::TestCase
7   def test_config_init
8     assert_nothing_raised { Unicorn::Configurator.new {} }
9   end
11   def test_expand_addr
12     meth = Unicorn::Configurator.new.method(:expand_addr)
14     assert_equal "/var/run/unicorn.sock", meth.call("/var/run/unicorn.sock")
15     assert_equal "#{Dir.pwd}/foo/bar.sock", meth.call("unix:foo/bar.sock")
17     path = meth.call("~/foo/bar.sock")
18     assert_equal "/", path[0..0]
19     assert_match %r{/foo/bar\.sock\z}, path
21     path = meth.call("~root/foo/bar.sock")
22     assert_equal "/", path[0..0]
23     assert_match %r{/foo/bar\.sock\z}, path
25     assert_equal "1.2.3.4:2007", meth.call('1.2.3.4:2007')
26     assert_equal "0.0.0.0:2007", meth.call('0.0.0.0:2007')
27     assert_equal "0.0.0.0:2007", meth.call(':2007')
28     assert_equal "0.0.0.0:2007", meth.call('*:2007')
29     assert_equal "0.0.0.0:2007", meth.call('2007')
30     assert_equal "0.0.0.0:2007", meth.call(2007)
31     assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('1:2007')
32     assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('2:2007')
33   end
35   def test_config_invalid
36     tmp = Tempfile.new('unicorn_config')
37     tmp.syswrite(%q(asdfasdf "hello-world"))
38     assert_raises(NoMethodError) do
39       Unicorn::Configurator.new(:config_file => tmp.path)
40     end
41   end
43   def test_config_non_existent
44     tmp = Tempfile.new('unicorn_config')
45     path = tmp.path
46     tmp.close!
47     assert_raises(Errno::ENOENT) do
48       Unicorn::Configurator.new(:config_file => path)
49     end
50   end
52   def test_config_defaults
53     cfg = Unicorn::Configurator.new(:use_defaults => true)
54     assert_nothing_raised { cfg.commit!(self) }
55     Unicorn::Configurator::DEFAULTS.each do |key,value|
56       assert_equal value, instance_variable_get("@#{key.to_s}")
57     end
58   end
60   def test_config_defaults_skip
61     cfg = Unicorn::Configurator.new(:use_defaults => true)
62     skip = [ :logger ]
63     assert_nothing_raised { cfg.commit!(self, :skip => skip) }
64     @logger = nil
65     Unicorn::Configurator::DEFAULTS.each do |key,value|
66       next if skip.include?(key)
67       assert_equal value, instance_variable_get("@#{key.to_s}")
68     end
69     assert_nil @logger
70   end
72   def test_listen_options
73     tmp = Tempfile.new('unicorn_config')
74     expect = { :sndbuf => 1, :rcvbuf => 2, :backlog => 10 }.freeze
75     listener = "127.0.0.1:12345"
76     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
77     cfg = nil
78     assert_nothing_raised do
79       cfg = Unicorn::Configurator.new(:config_file => tmp.path)
80     end
81     assert_nothing_raised { cfg.commit!(self) }
82     assert(listener_opts = instance_variable_get("@listener_opts"))
83     assert_equal expect, listener_opts[listener]
84   end
86   def test_listen_option_bad
87     tmp = Tempfile.new('unicorn_config')
88     expect = { :sndbuf => "five" }
89     listener = "127.0.0.1:12345"
90     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
91     assert_raises(ArgumentError) do
92       Unicorn::Configurator.new(:config_file => tmp.path)
93     end
94   end
96   def test_after_fork_proc
97     [ proc { |a,b| }, Proc.new { |a,b| }, lambda { |a,b| } ].each do |my_proc|
98       Unicorn::Configurator.new(:after_fork => my_proc).commit!(self)
99       assert_equal my_proc, @after_fork
100     end
101   end
103   def test_after_fork_wrong_arity
104     [ proc { |a| }, Proc.new { }, lambda { |a,b,c| } ].each do |my_proc|
105       assert_raises(ArgumentError) do
106         Unicorn::Configurator.new(:after_fork => my_proc)
107       end
108     end
109   end