fork/exec string command for self-daemonizing process, but wait for process to exit
[god.git] / test / test_process.rb
blob9e6ef39984a0d380f0bf007020cdf3a7865a9d1b
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   
76   # call_action
77   
78   def test_call_action_should_write_pid
79     # Only for start, restart
80     [:start, :restart].each do |action|
81       @p.stubs(:test).returns true
82       IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
83       @p.expects(:fork)
84       Process.expects(:waitpid)
85       File.expects(:open).with(@p.default_pid_file, 'w')
86       @p.send("#{action}=", "run")
87       @p.call_action(action)
88     end
89   end
90 end
92 ###############################################################################
94 # Daemon
96 ###############################################################################
98 class TestProcessDaemon < Test::Unit::TestCase
99   def setup
100     God.internal_init
101     @p = God::Process.new
102     @p.name = 'foo'
103     @p.pid_file = 'blah.pid'
104     @p.stubs(:test).returns true # so we don't try to mkdir_p
105     Process.stubs(:detach) # because we stub fork
106   end
107   
108   # alive?
109   
110   def test_alive_should_call_system_process_exists
111     File.expects(:read).with('blah.pid').returns('1234')
112     System::Process.any_instance.expects(:exists?).returns(false)
113     assert !@p.alive?
114   end
115   
116   def test_alive_should_return_false_if_no_such_file
117     File.expects(:read).with('blah.pid').raises(Errno::ENOENT)
118     assert !@p.alive?
119   end
120   
121   # valid?
122   
123   def test_valid_should_return_false_if_no_start
124     @p.name = 'foo'
125     @p.stop = 'baz'
126     no_stdout do
127       assert !@p.valid?
128     end
129   end
130   
131   def test_valid_should_return_false_if_self_daemonized_and_no_stop
132     @p.start = 'bar'
133     @p.pid_file = 'foo'
134     
135     no_stdout do
136       assert !@p.valid?
137     end
138   end
139   
140   # defaul_pid_file
141   
142   def test_default_pid_file
143     assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
144   end
145   
146   # call_action
147   # These actually excercise call_action in the back at this point - Kev
148   
149   def test_call_action_with_string_should_call_system
150     @p.start = "do something"
151     @p.expects(:fork)
152     Process.expects(:waitpid)
153     @p.call_action(:start)
154   end
155   
156   def test_call_action_with_lambda_should_call
157     cmd = lambda { puts "Hi" }
158     cmd.expects(:call)
159     @p.start = cmd
160     @p.call_action(:start)
161   end
162   
163   def test_call_action_with_invalid_command_class_should_raise
164     @p.start = 5
165     @p.stop = 'baz'
166     
167     assert @p.valid?
168     
169     assert_raise NotImplementedError do
170       @p.call_action(:start)
171     end
172   end
173   
174   # start!/stop!/restart!
175   
176   def test_start_stop_restart_bang
177     [:start, :stop, :restart].each do |x|
178       @p.expects(:call_action).with(x)
179       @p.send("#{x}!")
180     end
181   end