fix process#alive? to not raise on no such file
[god.git] / test / test_process.rb
blob60d121a934138ab66e50dce1236cac98d45116d3
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     God.internal_init
18     @p = God::Process.new
19     @p.name = 'foo'
20     @p.stubs(:test).returns true # so we don't try to mkdir_p
21     Process.stubs(:detach) # because we stub fork
22   end
23   
24   # valid?
25   
26   def test_valid_should_return_true_if_auto_daemonized_and_log
27     @p.start = 'qux'
28     @p.log = 'bar'
29     
30     assert @p.valid?
31   end
32   
33   def test_valid_should_return_true_if_auto_daemonized_and_no_stop
34     @p.start = 'qux'
35     @p.log = 'bar'
36     
37     assert @p.valid?
38   end
39   
40   def test_valid_should_return_true_if_uid_exists
41     @p.start = 'qux'
42     @p.log = 'bar'
43     @p.uid = 'root'
44     
45     assert @p.valid?
46   end
47   
48   def test_valid_should_return_true_if_uid_does_not_exists
49     @p.start = 'qux'
50     @p.log = 'bar'
51     @p.uid = 'foobarbaz'
52     
53     no_stdout do
54       assert !@p.valid?
55     end
56   end
57   
58   def test_valid_should_return_true_if_gid_exists
59     @p.start = 'qux'
60     @p.log = 'bar'
61     @p.gid = 'wheel'
62     
63     assert @p.valid?
64   end
65   
66   def test_valid_should_return_true_if_gid_does_not_exists
67     @p.start = 'qux'
68     @p.log = 'bar'
69     @p.gid = 'foobarbaz'
70     
71     no_stdout do
72       assert !@p.valid?
73     end
74   end
75 end
77 class TestProcessDaemon < Test::Unit::TestCase
78   def setup
79     God.internal_init
80     @p = God::Process.new
81     @p.name = 'foo'
82     @p.pid_file = 'blah.pid'
83     @p.stubs(:test).returns true # so we don't try to mkdir_p
84     Process.stubs(:detach) # because we stub fork
85   end
86   
87   # alive?
88   
89   def test_alive_should_call_system_process_exists
90     File.expects(:read).with('blah.pid').returns('1234')
91     System::Process.any_instance.expects(:exists?).returns(false)
92     assert !@p.alive?
93   end
94   
95   # valid?
96   
97   def test_valid_should_return_false_if_no_name
98     @p.name = nil
99     @p.start = 'bar'
100     @p.stop = 'baz'
101     no_stdout do
102       assert !@p.valid?
103     end
104   end
105   
106   def test_valid_should_return_false_if_no_start
107     @p.name = 'foo'
108     @p.stop = 'baz'
109     no_stdout do
110       assert !@p.valid?
111     end
112   end
113   
114   def test_valid_should_return_false_if_self_daemonized_and_no_stop
115     @p.start = 'bar'
116     @p.pid_file = 'foo'
117     
118     no_stdout do
119       assert !@p.valid?
120     end
121   end
122   
123   def test_valid_should_return_false_if_self_daemonized_and_log
124     @p.pid_file = 'foo'
125     @p.start = 'baz'
126     @p.stop = 'qux'
127     @p.log = 'bar'
128     
129     no_stdout do
130       assert !@p.valid?
131     end
132   end
133   
134   # defaul_pid_file
135   
136   def test_default_pid_file
137     assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
138   end
139   
140   # call_action
141   # These actually excercise call_action in the back at this point - Kev
142   
143   def test_call_action_with_string_should_fork_exec 
144     @p.start = "do something"
145     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
146     @p.expects(:fork)
147     Process.expects(:waitpid)
148     @p.call_action(:start)
149   end
150   
151   def test_call_action_with_lambda_should_call
152     cmd = lambda { puts "Hi" }
153     cmd.expects(:call)
154     @p.start = cmd
155     @p.call_action(:start)
156   end
157   
158   def test_call_action_without_pid_should_write_pid
159     # Only for start, restart
160     [:start, :restart].each do |action|
161       @p = God::Process.new
162       @p.name = 'foo'
163       @p.stubs(:test).returns true
164       IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
165       @p.expects(:fork)
166       Process.expects(:waitpid)
167       File.expects(:open).with(@p.default_pid_file, 'w')
168       @p.send("#{action}=", "run")
169       @p.call_action(action)
170     end
171   end
172   
173   def test_call_action_should_not_write_pid_for_stop
174     @p.pid_file = nil
175     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
176     @p.expects(:fork)
177     Process.expects(:waitpid)
178     File.expects(:open).times(0)
179     @p.stop = "stopping"
180     @p.call_action(:stop)
181   end
182   
183   def test_call_action_with_invalid_command_class_should_raise
184     @p.start = 5
185     @p.stop = 'baz'
186     
187     assert @p.valid?
188     
189     assert_raise NotImplementedError do
190       @p.call_action(:start)
191     end
192   end
193   
194   # start!/stop!/restart!
195   
196   def test_start_stop_restart_bang
197     [:start, :stop, :restart].each do |x|
198       @p.expects(:call_action).with(x)
199       @p.send("#{x}!")
200     end
201   end