default stop for auto-daemonized; bug fixes
[god.git] / test / test_process.rb
blobe7947f3a2feccd7b61c46dbedd2becff5a80a7f2
1 require File.dirname(__FILE__) + '/helper'
3 module God
4   class Process
5     def fork
6       raise "You forgot to stub fork"
7     end
8     
9     def exec(*args)
10       raise "You forgot to stub exec"
11     end
12   end
13 end
15 class TestProcessChild < Test::Unit::TestCase
16   def setup
17     @p = God::Process.new(:name => 'foo')
18     @p.stubs(:test).returns true # so we don't try to mkdir_p
19     Process.stubs(:detach) # because we stub fork
20   end
21   
22   # valid?
23   
24   def test_valid_should_return_true_if_auto_daemonized_and_log
25     @p.start = 'qux'
26     @p.log = 'bar'
27     
28     assert @p.valid?
29   end
30   
31   def test_valid_should_return_true_if_auto_daemonized_and_no_stop
32     @p.start = 'qux'
33     @p.log = 'bar'
34     
35     assert @p.valid?
36   end
37   
38   def test_valid_should_return_true_if_uid_exists
39     @p.start = 'qux'
40     @p.log = 'bar'
41     @p.uid = 'root'
42     
43     assert @p.valid?
44   end
45   
46   def test_valid_should_return_true_if_uid_does_not_exists
47     @p.start = 'qux'
48     @p.log = 'bar'
49     @p.uid = 'foobarbaz'
50     
51     no_stdout do
52       assert !@p.valid?
53     end
54   end
55   
56   def test_valid_should_return_true_if_gid_exists
57     @p.start = 'qux'
58     @p.log = 'bar'
59     @p.gid = 'wheel'
60     
61     assert @p.valid?
62   end
63   
64   def test_valid_should_return_true_if_gid_does_not_exists
65     @p.start = 'qux'
66     @p.log = 'bar'
67     @p.gid = 'foobarbaz'
68     
69     no_stdout do
70       assert !@p.valid?
71     end
72   end
73 end
75 class TestProcessDaemon < Test::Unit::TestCase
76   def setup
77     @p = God::Process.new(:name => 'foo', :pid_file => 'blah.pid')
78     @p.stubs(:test).returns true # so we don't try to mkdir_p
79     Process.stubs(:detach) # because we stub fork
80   end
81   
82   # valid?
83   
84   def test_valid_should_return_false_if_no_start
85     no_stdout do
86       assert !@p.valid?
87     end
88   end
89   
90   def test_valid_should_return_false_if_self_daemonized_and_no_stop
91     @p.pid_file = 'foo'
92     
93     no_stdout do
94       assert !@p.valid?
95     end
96   end
97   
98   def test_valid_should_return_false_if_self_daemonized_and_log
99     @p.pid_file = 'foo'
100     @p.log = 'bar'
101     
102     no_stdout do
103       assert !@p.valid?
104     end
105   end
106   
107   # defaul_pid_file
108   
109   def test_default_pid_file
110     assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
111   end
112   
113   # call_action
114   # These actually excercise call_action in the back at this point - Kev
115   
116   def test_call_action_with_string_should_fork_exec 
117     @p.start = "do something"
118     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
119     @p.expects(:fork)
120     Process.expects(:waitpid)
121     @p.call_action(:start)
122   end
123   
124   def test_call_action_with_lambda_should_call
125     cmd = lambda { puts "Hi" }
126     cmd.expects(:call)
127     @p.start = cmd
128     @p.call_action(:start)
129   end
130   
131   def test_call_action_without_pid_should_write_pid
132     # Only for start, restart
133     [:start, :restart].each do |action|
134       @p = God::Process.new(:name => 'foo')
135       @p.stubs(:test).returns true
136       IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
137       @p.expects(:fork)
138       Process.expects(:waitpid)
139       File.expects(:open).with(@p.default_pid_file, 'w')
140       @p.send("#{action}=", "run")
141       @p.call_action(action)
142     end
143   end
144   
145   def test_call_action_should_not_write_pid_for_stop
146     @p.pid_file = nil
147     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
148     @p.expects(:fork)
149     Process.expects(:waitpid)
150     File.expects(:open).times(0)
151     @p.stop = "stopping"
152     @p.call_action(:stop)
153   end
154   
155   def test_call_action_should_mkdir_p_if_pid_file_dir_existence_test_fails
156     @p.pid_file = nil
157     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
158     @p.expects(:fork)
159     Process.expects(:waitpid)
160     @p.expects(:test).returns(false, true)
161     FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
162     File.expects(:open)
163     @p.start = "starting"
164     @p.call_action(:start)
165   end
166   
167   # start!/stop!/restart!
168   
169   def test_start_stop_restart_bang
170     [:start, :stop, :restart].each do |x|
171       @p.expects(:call_action).with(x)
172       @p.send("#{x}!")
173     end
174   end