267eea357ce36450b6df5d434444a655add18229
[unicorn.git] / test / unit / test_configurator.rb
blob267eea357ce36450b6df5d434444a655add18229
1 # -*- encoding: binary -*-
3 require 'test/unit'
4 require 'tempfile'
5 require 'unicorn'
7 TestStruct = Struct.new(
8   *(Unicorn::Configurator::DEFAULTS.keys + %w(listener_opts listeners)))
9 class TestConfigurator < Test::Unit::TestCase
11   def test_config_init
12     Unicorn::Configurator.new {}
13   end
15   def test_expand_addr
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)
38     end
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')
47   end
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)
54     end
55   end
57   def test_config_non_existent
58     tmp = Tempfile.new('unicorn_config')
59     path = tmp.path
60     tmp.close!
61     assert_raises(Errno::ENOENT) do
62       Unicorn::Configurator.new(:config_file => path)
63     end
64   end
66   def test_config_defaults
67     cfg = Unicorn::Configurator.new(:use_defaults => true)
68     test_struct = TestStruct.new
69     cfg.commit!(test_struct)
70     Unicorn::Configurator::DEFAULTS.each do |key,value|
71       assert_equal value, test_struct.__send__(key)
72     end
73   end
75   def test_config_defaults_skip
76     cfg = Unicorn::Configurator.new(:use_defaults => true)
77     skip = [ :logger ]
78     test_struct = TestStruct.new
79     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)
83     end
84     assert_nil test_struct.logger
85   end
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")
92     cfg = Unicorn::Configurator.new(:config_file => tmp.path)
93     test_struct = TestStruct.new
94     cfg.commit!(test_struct)
95     assert(listener_opts = test_struct.listener_opts)
96     assert_equal expect, listener_opts[listener]
97   end
99   def test_listen_option_bad
100     tmp = Tempfile.new('unicorn_config')
101     expect = { :sndbuf => "five" }
102     listener = "127.0.0.1:12345"
103     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
104     assert_raises(ArgumentError) do
105       Unicorn::Configurator.new(:config_file => tmp.path)
106     end
107   end
109   def test_listen_option_bad_delay
110     tmp = Tempfile.new('unicorn_config')
111     expect = { :delay => "five" }
112     listener = "127.0.0.1:12345"
113     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
114     assert_raises(ArgumentError) do
115       Unicorn::Configurator.new(:config_file => tmp.path)
116     end
117   end
119   def test_listen_option_float_delay
120     tmp = Tempfile.new('unicorn_config')
121     expect = { :delay => 0.5 }
122     listener = "127.0.0.1:12345"
123     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
124     Unicorn::Configurator.new(:config_file => tmp.path)
125   end
127   def test_listen_option_int_delay
128     tmp = Tempfile.new('unicorn_config')
129     expect = { :delay => 5 }
130     listener = "127.0.0.1:12345"
131     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
132     Unicorn::Configurator.new(:config_file => tmp.path)
133   end
135   def test_after_fork_proc
136     test_struct = TestStruct.new
137     [ proc { |a,b| }, Proc.new { |a,b| }, lambda { |a,b| } ].each do |my_proc|
138       Unicorn::Configurator.new(:after_fork => my_proc).commit!(test_struct)
139       assert_equal my_proc, test_struct.after_fork
140     end
141   end
143   def test_after_fork_wrong_arity
144     [ proc { |a| }, Proc.new { }, lambda { |a,b,c| } ].each do |my_proc|
145       assert_raises(ArgumentError) do
146         Unicorn::Configurator.new(:after_fork => my_proc)
147       end
148     end
149   end