1 # -*- encoding: binary -*-
7 TestStruct = Struct.new(
8 *(Unicorn::Configurator::DEFAULTS.keys + %w(listener_opts listeners)))
9 class TestConfigurator < Test::Unit::TestCase
12 assert_nothing_raised { Unicorn::Configurator.new {} }
16 meth = Unicorn::Configurator.new.method(:expand_addr)
18 assert_equal "/var/run/unicorn.sock", meth.call("/var/run/unicorn.sock")
19 assert_equal "#{Dir.pwd}/foo/bar.sock", meth.call("unix:foo/bar.sock")
21 path = meth.call("~/foo/bar.sock")
22 assert_equal "/", path[0..0]
23 assert_match %r{/foo/bar\.sock\z}, path
25 path = meth.call("~root/foo/bar.sock")
26 assert_equal "/", path[0..0]
27 assert_match %r{/foo/bar\.sock\z}, path
29 assert_equal "1.2.3.4:2007", meth.call('1.2.3.4:2007')
30 assert_equal "0.0.0.0:2007", meth.call('0.0.0.0:2007')
31 assert_equal "0.0.0.0:2007", meth.call(':2007')
32 assert_equal "0.0.0.0:2007", meth.call('*:2007')
33 assert_equal "0.0.0.0:2007", meth.call('2007')
34 assert_equal "0.0.0.0:2007", meth.call(2007)
36 %w([::1]:2007 [::]:2007).each do |addr|
37 assert_equal addr, meth.call(addr.dup)
40 # for Rainbows! users only
41 assert_equal "[::]:80", meth.call("[::]")
42 assert_equal "127.6.6.6:80", meth.call("127.6.6.6")
44 # the next two aren't portable, consider them unsupported for now
45 # assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('1:2007')
46 # assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('2:2007')
49 def test_config_invalid
50 tmp = Tempfile.new('unicorn_config')
51 tmp.syswrite(%q(asdfasdf "hello-world"))
52 assert_raises(NoMethodError) do
53 Unicorn::Configurator.new(:config_file => tmp.path)
57 def test_config_non_existent
58 tmp = Tempfile.new('unicorn_config')
61 assert_raises(Errno::ENOENT) do
62 Unicorn::Configurator.new(:config_file => path)
66 def test_config_defaults
67 cfg = Unicorn::Configurator.new(:use_defaults => true)
68 test_struct = TestStruct.new
69 assert_nothing_raised { cfg.commit!(test_struct) }
70 Unicorn::Configurator::DEFAULTS.each do |key,value|
71 assert_equal value, test_struct.__send__(key)
75 def test_config_defaults_skip
76 cfg = Unicorn::Configurator.new(:use_defaults => true)
78 test_struct = TestStruct.new
79 assert_nothing_raised { cfg.commit!(test_struct, :skip => skip) }
80 Unicorn::Configurator::DEFAULTS.each do |key,value|
81 next if skip.include?(key)
82 assert_equal value, test_struct.__send__(key)
84 assert_nil test_struct.logger
87 def test_listen_options
88 tmp = Tempfile.new('unicorn_config')
89 expect = { :sndbuf => 1, :rcvbuf => 2, :backlog => 10 }.freeze
90 listener = "127.0.0.1:12345"
91 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
93 assert_nothing_raised do
94 cfg = Unicorn::Configurator.new(:config_file => tmp.path)
96 test_struct = TestStruct.new
97 assert_nothing_raised { cfg.commit!(test_struct) }
98 assert(listener_opts = test_struct.listener_opts)
99 assert_equal expect, listener_opts[listener]
102 def test_listen_option_bad
103 tmp = Tempfile.new('unicorn_config')
104 expect = { :sndbuf => "five" }
105 listener = "127.0.0.1:12345"
106 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
107 assert_raises(ArgumentError) do
108 Unicorn::Configurator.new(:config_file => tmp.path)
112 def test_listen_option_bad_delay
113 tmp = Tempfile.new('unicorn_config')
114 expect = { :delay => "five" }
115 listener = "127.0.0.1:12345"
116 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
117 assert_raises(ArgumentError) do
118 Unicorn::Configurator.new(:config_file => tmp.path)
122 def test_listen_option_float_delay
123 tmp = Tempfile.new('unicorn_config')
124 expect = { :delay => 0.5 }
125 listener = "127.0.0.1:12345"
126 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
127 assert_nothing_raised do
128 Unicorn::Configurator.new(:config_file => tmp.path)
132 def test_listen_option_int_delay
133 tmp = Tempfile.new('unicorn_config')
134 expect = { :delay => 5 }
135 listener = "127.0.0.1:12345"
136 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
137 assert_nothing_raised do
138 Unicorn::Configurator.new(:config_file => tmp.path)
142 def test_after_fork_proc
143 test_struct = TestStruct.new
144 [ proc { |a,b| }, Proc.new { |a,b| }, lambda { |a,b| } ].each do |my_proc|
145 Unicorn::Configurator.new(:after_fork => my_proc).commit!(test_struct)
146 assert_equal my_proc, test_struct.after_fork
150 def test_after_fork_wrong_arity
151 [ proc { |a| }, Proc.new { }, lambda { |a,b,c| } ].each do |my_proc|
152 assert_raises(ArgumentError) do
153 Unicorn::Configurator.new(:after_fork => my_proc)