tighten up Hub and add comments
[god.git] / test / test_process.rb
blob0c7d77a948fc163f7cec090b0993f51350d82800
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   # defaul_pid_file
148   
149   def test_default_pid_file
150     assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
151   end
152   
153   # call_action
154   # These actually excercise call_action in the back at this point - Kev
155   
156   def test_call_action_with_string_should_call_system
157     @p.start = "do something"
158     @p.expects(:fork)
159     Process.expects(:waitpid)
160     @p.call_action(:start)
161   end
162   
163   def test_call_action_with_lambda_should_call
164     cmd = lambda { puts "Hi" }
165     cmd.expects(:call)
166     @p.start = cmd
167     @p.call_action(:start)
168   end
169   
170   def test_call_action_with_invalid_command_class_should_raise
171     @p.start = 5
172     @p.stop = 'baz'
173     
174     assert @p.valid?
175     
176     assert_raise NotImplementedError do
177       @p.call_action(:start)
178     end
179   end
180   
181   # start!/stop!/restart!
182   
183   def test_start_stop_restart_bang
184     [:start, :stop, :restart].each do |x|
185       @p.expects(:call_action).with(x)
186       @p.send("#{x}!")
187     end
188   end