more tests for task, real valid? method for task
[god.git] / test / test_process.rb
blob1966a5bd60c04e386330e999a0c3e6dd70d747c1
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   def test_alive_should_return_false_if_no_such_file
96     File.expects(:read).with('blah.pid').raises(Errno::ENOENT)
97     assert !@p.alive?
98   end
99   
100   # valid?
101   
102   def test_valid_should_return_false_if_no_start
103     @p.name = 'foo'
104     @p.stop = 'baz'
105     no_stdout do
106       assert !@p.valid?
107     end
108   end
109   
110   def test_valid_should_return_false_if_self_daemonized_and_no_stop
111     @p.start = 'bar'
112     @p.pid_file = 'foo'
113     
114     no_stdout do
115       assert !@p.valid?
116     end
117   end
118   
119   def test_valid_should_return_false_if_self_daemonized_and_log
120     @p.pid_file = 'foo'
121     @p.start = 'baz'
122     @p.stop = 'qux'
123     @p.log = 'bar'
124     
125     no_stdout do
126       assert !@p.valid?
127     end
128   end
129   
130   # defaul_pid_file
131   
132   def test_default_pid_file
133     assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
134   end
135   
136   # call_action
137   # These actually excercise call_action in the back at this point - Kev
138   
139   def test_call_action_with_string_should_fork_exec 
140     @p.start = "do something"
141     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
142     @p.expects(:fork)
143     Process.expects(:waitpid)
144     @p.call_action(:start)
145   end
146   
147   def test_call_action_with_lambda_should_call
148     cmd = lambda { puts "Hi" }
149     cmd.expects(:call)
150     @p.start = cmd
151     @p.call_action(:start)
152   end
153   
154   def test_call_action_without_pid_should_write_pid
155     # Only for start, restart
156     [:start, :restart].each do |action|
157       @p = God::Process.new
158       @p.name = 'foo'
159       @p.stubs(:test).returns true
160       IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
161       @p.expects(:fork)
162       Process.expects(:waitpid)
163       File.expects(:open).with(@p.default_pid_file, 'w')
164       @p.send("#{action}=", "run")
165       @p.call_action(action)
166     end
167   end
168   
169   def test_call_action_should_not_write_pid_for_stop
170     @p.pid_file = nil
171     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
172     @p.expects(:fork)
173     Process.expects(:waitpid)
174     File.expects(:open).times(0)
175     @p.stop = "stopping"
176     @p.call_action(:stop)
177   end
178   
179   def test_call_action_with_invalid_command_class_should_raise
180     @p.start = 5
181     @p.stop = 'baz'
182     
183     assert @p.valid?
184     
185     assert_raise NotImplementedError do
186       @p.call_action(:start)
187     end
188   end
189   
190   # start!/stop!/restart!
191   
192   def test_start_stop_restart_bang
193     [:start, :stop, :restart].each do |x|
194       @p.expects(:call_action).with(x)
195       @p.send("#{x}!")
196     end
197   end