Favor Struct members to instance variables
[unicorn.git] / test / unit / test_configurator.rb
blobaa29f61feda278aa6649087837d8ef9b23849c66
1 require 'test/unit'
2 require 'tempfile'
3 require 'unicorn'
5 TestStruct = Struct.new(
6   *(Unicorn::Configurator::DEFAULTS.keys + %w(listener_opts listeners)))
7 class TestConfigurator < Test::Unit::TestCase
9   def test_config_init
10     assert_nothing_raised { Unicorn::Configurator.new {} }
11   end
13   def test_expand_addr
14     meth = Unicorn::Configurator.new.method(:expand_addr)
16     assert_equal "/var/run/unicorn.sock", meth.call("/var/run/unicorn.sock")
17     assert_equal "#{Dir.pwd}/foo/bar.sock", meth.call("unix:foo/bar.sock")
19     path = meth.call("~/foo/bar.sock")
20     assert_equal "/", path[0..0]
21     assert_match %r{/foo/bar\.sock\z}, path
23     path = meth.call("~root/foo/bar.sock")
24     assert_equal "/", path[0..0]
25     assert_match %r{/foo/bar\.sock\z}, path
27     assert_equal "1.2.3.4:2007", meth.call('1.2.3.4:2007')
28     assert_equal "0.0.0.0:2007", meth.call('0.0.0.0: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_equal "0.0.0.0:2007", meth.call('2007')
32     assert_equal "0.0.0.0:2007", meth.call(2007)
33     assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('1:2007')
34     assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('2:2007')
35   end
37   def test_config_invalid
38     tmp = Tempfile.new('unicorn_config')
39     tmp.syswrite(%q(asdfasdf "hello-world"))
40     assert_raises(NoMethodError) do
41       Unicorn::Configurator.new(:config_file => tmp.path)
42     end
43   end
45   def test_config_non_existent
46     tmp = Tempfile.new('unicorn_config')
47     path = tmp.path
48     tmp.close!
49     assert_raises(Errno::ENOENT) do
50       Unicorn::Configurator.new(:config_file => path)
51     end
52   end
54   def test_config_defaults
55     cfg = Unicorn::Configurator.new(:use_defaults => true)
56     test_struct = TestStruct.new
57     assert_nothing_raised { cfg.commit!(test_struct) }
58     Unicorn::Configurator::DEFAULTS.each do |key,value|
59       assert_equal value, test_struct.__send__(key)
60     end
61   end
63   def test_config_defaults_skip
64     cfg = Unicorn::Configurator.new(:use_defaults => true)
65     skip = [ :logger ]
66     test_struct = TestStruct.new
67     assert_nothing_raised { cfg.commit!(test_struct, :skip => skip) }
68     Unicorn::Configurator::DEFAULTS.each do |key,value|
69       next if skip.include?(key)
70       assert_equal value, test_struct.__send__(key)
71     end
72     assert_nil test_struct.logger
73   end
75   def test_listen_options
76     tmp = Tempfile.new('unicorn_config')
77     expect = { :sndbuf => 1, :rcvbuf => 2, :backlog => 10 }.freeze
78     listener = "127.0.0.1:12345"
79     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
80     cfg = nil
81     assert_nothing_raised do
82       cfg = Unicorn::Configurator.new(:config_file => tmp.path)
83     end
84     test_struct = TestStruct.new
85     assert_nothing_raised { cfg.commit!(test_struct) }
86     assert(listener_opts = test_struct.listener_opts)
87     assert_equal expect, listener_opts[listener]
88   end
90   def test_listen_option_bad
91     tmp = Tempfile.new('unicorn_config')
92     expect = { :sndbuf => "five" }
93     listener = "127.0.0.1:12345"
94     tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
95     assert_raises(ArgumentError) do
96       Unicorn::Configurator.new(:config_file => tmp.path)
97     end
98   end
100   def test_after_fork_proc
101     test_struct = TestStruct.new
102     [ proc { |a,b| }, Proc.new { |a,b| }, lambda { |a,b| } ].each do |my_proc|
103       Unicorn::Configurator.new(:after_fork => my_proc).commit!(test_struct)
104       assert_equal my_proc, test_struct.after_fork
105     end
106   end
108   def test_after_fork_wrong_arity
109     [ proc { |a| }, Proc.new { }, lambda { |a,b,c| } ].each do |my_proc|
110       assert_raises(ArgumentError) do
111         Unicorn::Configurator.new(:after_fork => my_proc)
112       end
113     end
114   end