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