File: test_process.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (253 lines) | stat: -rw-r--r-- 5,700 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
require File.dirname(__FILE__) + '/helper'

module God
  class Process
    # def fork
    #   raise "You forgot to stub fork"
    # end

    def exec(*args)
      raise "You forgot to stub exec"
    end
  end
end

class TestProcessChild < Test::Unit::TestCase
  def setup
    God.internal_init
    @p = God::Process.new
    @p.name = 'foo'
    @p.stubs(:test).returns true # so we don't try to mkdir_p
    Process.stubs(:detach) # because we stub fork

    ::Process::Sys.stubs(:setuid).returns(true)
    ::Process::Sys.stubs(:setgid).returns(true)
  end

  # valid?

  def test_valid_should_return_true_if_auto_daemonized_and_log
    @p.start = 'qux'
    @p.log = 'bar'

    assert @p.valid?
  end

  def test_valid_should_return_true_if_auto_daemonized_and_no_stop
    @p.start = 'qux'
    @p.log = 'bar'

    assert @p.valid?
  end

  def test_valid_should_return_true_if_uid_exists
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.uid = 'root'

    assert @p.valid?
  end

  def test_valid_should_return_true_if_uid_does_not_exists
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.uid = 'foobarbaz'

    assert !@p.valid?
  end

  def test_valid_should_return_true_if_gid_exists
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.gid = 'wheel'

    assert @p.valid?
  end

  def test_valid_should_return_false_if_gid_does_not_exists
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.gid = 'foobarbaz'

    assert !@p.valid?
  end

  def test_valid_should_return_true_if_dir_exists
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.dir = '/tmp'

    assert @p.valid?
  end

  def test_valid_should_return_false_if_dir_does_not_exists
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.dir = '/tmp/doesnotexist'

    assert !@p.valid?
  end

  def test_valid_should_return_false_if_dir_is_not_a_dir
    @p.start = 'qux'
    @p.log = '/tmp/foo.log'
    @p.dir = '/etc/passwd'

    assert !@p.valid?
  end

  def test_valid_should_return_false_with_bogus_chroot
    @p.chroot = '/bogusroot'

    assert !@p.valid?
  end

  def test_valid_should_return_true_with_chroot_and_valid_log
    @p.start = 'qux'
    @p.chroot = '/tmp'
    @p.log = '/tmp/foo.log'

    File.expects(:exist?).with('/tmp').returns(true)
    File.expects(:exist?).with('/tmp/foo.log').returns(true)
    File.expects(:exist?).with('/tmp/dev/null').returns(true)

    assert @p.valid?
  end

  # call_action

  def test_call_action_should_write_pid
    # Only for start, restart
    [:start, :restart].each do |action|
      @p.stubs(:test).returns true
      IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
      @p.expects(:fork)
      Process.expects(:waitpid)
      File.expects(:open).with(@p.default_pid_file, 'w')
      @p.send("#{action}=", "run")
      @p.call_action(action)
    end
  end
end

###############################################################################
#
# Daemon
#
###############################################################################

class TestProcessDaemon < Test::Unit::TestCase
  def setup
    God.internal_init
    @p = God::Process.new
    @p.name = 'foo'
    @p.pid_file = 'blah.pid'
    @p.stubs(:test).returns true # so we don't try to mkdir_p
    Process.stubs(:detach) # because we stub fork
  end

  # alive?

  def test_alive_should_call_system_process_exists
    File.expects(:read).with('blah.pid').times(2).returns('1234')
    System::Process.any_instance.expects(:exists?).returns(false)
    assert !@p.alive?
  end

  def test_alive_should_return_false_if_no_such_file
    File.expects(:read).with('blah.pid').raises(Errno::ENOENT)
    assert !@p.alive?
  end

  # valid?

  def test_valid_should_return_false_if_no_start
    @p.name = 'foo'
    @p.stop = 'baz'
    assert !@p.valid?
  end

  # pid

  def test_pid_should_return_integer_for_valid_pid_files
    File.stubs(:read).returns("123")
    assert_equal 123, @p.pid
  end

  def test_pid_should_return_nil_for_missing_files
    @p.pid_file = ''
    assert_equal nil, @p.pid
  end

  def test_pid_should_return_nil_for_invalid_pid_files
    File.stubs(:read).returns("four score and seven years ago")
    assert_equal nil, @p.pid
  end

  def test_pid_should_retain_last_pid_value_if_pid_file_is_removed
    File.stubs(:read).returns("123")
    assert_equal 123, @p.pid

    File.stubs(:read).raises(Errno::ENOENT)
    assert_equal 123, @p.pid

    File.stubs(:read).returns("246")
    assert_equal 246, @p.pid
  end

  # default_pid_file

  def test_default_pid_file
    assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
  end

  # unix socket

  def test_unix_socket_should_return_path_specified
    @p.unix_socket = '/path/to-socket'
    assert_equal '/path/to-socket', @p.unix_socket
  end

  # umask
  def test_umask_should_return_umask_specified
    @p.umask = 002
    assert_equal 002, @p.umask
  end

  # call_action
  # These actually excercise call_action in the back at this point - Kev

  def test_call_action_with_string_should_call_system
    @p.start = "do something"
    @p.expects(:fork)
    Process.expects(:waitpid2).returns([123, 0])
    @p.call_action(:start)
  end

  def test_call_action_with_lambda_should_call
    cmd = lambda { puts "Hi" }
    cmd.expects(:call)
    @p.start = cmd
    @p.call_action(:start)
  end

  def test_call_action_with_invalid_command_class_should_raise
    @p.start = 5
    @p.stop = 'baz'

    assert @p.valid?

    assert_raise NotImplementedError do
      @p.call_action(:start)
    end
  end

  # start!/stop!/restart!

  def test_start_stop_restart_bang
    [:start, :stop, :restart].each do |x|
      @p.expects(:call_action).with(x)
      @p.send("#{x}!")
    end
  end
end