dctl 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'dctl/daemon'
5
+
6
+ class DaemonTest < Test::Unit::TestCase
7
+
8
+ def teardown
9
+ Dir['*.pid'].each { |f| File::delete f }
10
+ end
11
+
12
+ def setup
13
+ teardown
14
+ end
15
+
16
+ def test_which
17
+ assert_equal '/usr/bin/env', Dctl::Daemon.which('env')
18
+ assert_nil Dctl::Daemon.which('idontexist')
19
+ end
20
+
21
+ def test_run
22
+ assert_raise(Errno::ENOENT) { Dctl::Daemon.new('idontexist', '.').run }
23
+ assert_raise(Errno::EACCES) { Dctl::Daemon.new('/bin', '.').run }
24
+ assert_raise(Errno::EACCES) { Dctl::Daemon.new('/etc/passwd', '.').run }
25
+ end
26
+
27
+ def test_stop
28
+ fork { Dctl::Daemon.new('test/infinite.sh', '.').start }
29
+ sleep 1
30
+ assert_nothing_thrown { Dctl::Daemon.new('test/infinite.sh', '.').stop }
31
+ assert Dir['*.pid'].empty?
32
+ assert_raise(Errno::ENOENT) { Dctl::Daemon.new('test/infinite.sh', '.').stop }
33
+ end
34
+
35
+ def test_concurrent
36
+ puts '1' ; fork { Dctl::Daemon.new('test/infinite.sh', '.').start }
37
+ puts '2' ; assert_raise(Errno::EEXIST) { Dctl::Daemon.new('test/infinite.sh', '.').run }
38
+ puts '3' ; assert_nothing_thrown { Dctl::Daemon.new('test/infinite.sh', '.').stop }
39
+ assert Dir['*.pid'].empty?
40
+ end
41
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'dctl/daemonize'
5
+
6
+ class DaemonizeTest < Test::Unit::TestCase
7
+ include Daemonize
8
+
9
+ Input = 'in'
10
+ In_message = 'This is stdin.'
11
+ Output = 'out'
12
+ Out_message = 'This is stdout.'
13
+ Error = 'err'
14
+ Err_message = 'This is stderr.'
15
+ Pid_file = File.expand_path 'test/pidfile.pid'
16
+
17
+ def setup
18
+ File.open(Input, "w") { |f| f << In_message }
19
+ File.open(Output, "w").close
20
+ File.open(Error, "w").close
21
+ end
22
+
23
+ def teardown
24
+ File::delete Input
25
+ File::delete Output
26
+ File::delete Error
27
+ end
28
+
29
+ def test_ruby_daemonize_redirection
30
+ fork {
31
+ daemonize(Input, Output, Error)
32
+ # stdin redirection does not work.
33
+ # print gets
34
+ print "This is stdout."
35
+ $stderr.print "This is stderr."
36
+ exit
37
+ }
38
+ sleep 1
39
+
40
+ inp = File.open(Input) { |f| f.read }
41
+ out = File.open(Output) { |f| f.read }
42
+ err = File.open(Error) { |f| f.read }
43
+
44
+ assert_equal(In_message, inp)
45
+ assert_equal(Out_message, out)
46
+ assert_equal(Err_message, err)
47
+ end
48
+
49
+ def test_executable_daemonize_redirection
50
+ fork {
51
+ executable = File.expand_path 'test/redirection_test.sh'
52
+ daemonize(Input, Output, Error)
53
+ exec executable
54
+ }
55
+ sleep 1
56
+
57
+ inp = File.open(Input) { |f| f.read }
58
+ out = File.open(Output) { |f| f.read }
59
+ err = File.open(Error) { |f| f.read.chomp }
60
+
61
+ assert_equal(In_message, inp)
62
+ assert_equal(In_message, out)
63
+ assert_equal(Err_message, err)
64
+ end
65
+
66
+ def test_new_daemon
67
+ fork {
68
+ Daemonize
69
+ File.open(Pid_file, 'w') { |f| f << $$ }
70
+ loop {}
71
+ }
72
+ sleep 3
73
+ pid = File.open(Pid_file) { |f| Integer(f.read) }
74
+ puts(system("ps -ax | grep #{pid}"))
75
+ assert_nothing_thrown { Process.kill("SIGTERM", pid) }
76
+ File::delete Pid_file
77
+ end
78
+
79
+ end
data/test/dctl_test.rb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+
5
+ require 'test/pidfile_test'
6
+ require 'test/daemon_test'
7
+ require 'test/daemonize_test'
data/test/infinite.sh ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ while [ /bin/true ] ; do
4
+ /bin/echo -n "."
5
+ sleep 1
6
+ done
7
+
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'dctl/pidfile'
5
+
6
+ class PidFileTest < Test::Unit::TestCase
7
+ include Daemonize
8
+ Path = '.'
9
+ Name = 'testfile'
10
+
11
+ def teardown
12
+ PidFile.new(Path, Name).delete rescue nil
13
+ end
14
+
15
+ def test_no_such_path1
16
+ assert_raise(Errno::ENOENT) { PidFile.new('/no/such/path', Name) }
17
+ end
18
+
19
+ def test_no_such_path2
20
+ assert_raise(Errno::ENOTDIR) { PidFile.new('/usr/bin/env', Name) }
21
+ end
22
+
23
+ def test_writable_path
24
+ assert_raise(Errno::EACCES) { PidFile.new('/etc', Name) }
25
+ end
26
+
27
+ def test_no_such_file1
28
+ pf1 = PidFile.new Path, Name
29
+ assert_raise(Errno::ENOENT) { pf1.read }
30
+ end
31
+
32
+ def test_no_such_file2
33
+ pf1 = PidFile.new Path, Name
34
+ assert_raise(Errno::ENOENT) { pf1.delete }
35
+ end
36
+
37
+ def test_unique_instance
38
+ pf1 = PidFile.new Path, Name
39
+ pf2 = PidFile.new Path, Name
40
+ pf1.write
41
+ assert_raise(Errno::EEXIST) { pf2.write }
42
+ end
43
+
44
+ def test_readable_file
45
+ pf1 = PidFile.new Path, Name
46
+ pf1.write
47
+ pid = nil
48
+ assert_nothing_thrown { pid = pf1.read }
49
+ assert_instance_of Fixnum, pid
50
+ assert_equal $$, pid
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ cat
4
+ echo "This is stderr." >&2
5
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: dctl
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2005-04-07
8
+ summary: "dctl - a daemon controller written in Ruby"
9
+ require_paths:
10
+ - lib
11
+ email: bcarnazzi@gmail.com
12
+ homepage: http://rubyforge.org/project/dctl/
13
+ rubyforge_project: dctl
14
+ description: "dctl is a small script for providing daemon functionnalty (start, stop, ...) to
15
+ non-daemon process or script, with automatic handling of pidfiles."
16
+ autorequire: dctl
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ -
23
+ - ">"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.0
26
+ version:
27
+ platform: ruby
28
+ authors:
29
+ - Bruno Carnazzi
30
+ files:
31
+ - Rakefile
32
+ - setup.rb
33
+ - README
34
+ - TODO
35
+ - CHANGELOG
36
+ - lib/dctl
37
+ - lib/dctl.rb
38
+ - lib/dctl/cmdparser.rb
39
+ - lib/dctl/command.rb
40
+ - lib/dctl/daemon.rb
41
+ - lib/dctl/daemonize.rb
42
+ - lib/dctl/defaults.rb
43
+ - lib/dctl/metainf.rb
44
+ - lib/dctl/pidfile.rb
45
+ - bin/dctl
46
+ - test/daemon_test.rb
47
+ - test/daemonize_test.rb
48
+ - test/dctl_test.rb
49
+ - test/infinite.sh
50
+ - test/pidfile_test.rb
51
+ - test/redirection_test.sh
52
+ test_files: []
53
+ rdoc_options:
54
+ - "--main"
55
+ - README
56
+ extra_rdoc_files:
57
+ - README
58
+ - TODO
59
+ - CHANGELOG
60
+ executables:
61
+ - dctl
62
+ extensions: []
63
+ requirements: []
64
+ dependencies:
65
+ - !ruby/object:Gem::Dependency
66
+ name: cmdparse
67
+ version_requirement:
68
+ version_requirements: !ruby/object:Gem::Version::Requirement
69
+ requirements:
70
+ -
71
+ - "="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.0.0
74
+ version: