filter exception messages with control characters
[unicorn.git] / test / unit / test_configurator.rb
blobc19c42771c929cd76f134455045d088570802906
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     assert_nothing_raised { 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     assert_nothing_raised { 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     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)
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 = nil
93     assert_nothing_raised do
94       cfg = Unicorn::Configurator.new(:config_file => tmp.path)
95     end
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]
100   end
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)
109     end
110   end
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)
119     end
120   end
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)
129     end
130   end
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)
139     end
140   end
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
147     end
148   end
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)
154       end
155     end
156   end