summaryrefslogtreecommitdiffstats
path: root/test/network/daemon.rb
blob: e794ce24120da88ea75ea1df720bed26d7bd9fe9 (plain)
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
#!/usr/bin/env ruby

$:.unshift("../lib") if __FILE__ =~ /\.rb$/

require 'puppettest'
require 'puppet/daemon'

class TestDaemon < Test::Unit::TestCase
	include PuppetTest

    class FakeDaemon
        include Puppet::Daemon
    end

    def test_pidfile
        daemon = FakeDaemon.new

        assert_nothing_raised("removing non-existent file failed") do
            daemon.rmpidfile
        end

        Puppet[:pidfile] = tempfile()
        assert_nothing_raised "could not lock" do
            daemon.setpidfile
        end

        assert(FileTest.exists?(daemon.pidfile),
            "did not create pidfile")

        assert_nothing_raised("removing non-existent file failed") do
            daemon.rmpidfile
        end

        assert(! FileTest.exists?(daemon.pidfile),
            "did not remove pidfile")
    end

    def test_daemonize
        daemon = FakeDaemon.new
        Puppet[:pidfile] = tempfile()

        exiter = tempfile()

        assert_nothing_raised("Could not fork and daemonize") do
            fork do
                daemon.send(:daemonize)
                # Wait a max of 5 secs
                50.times do
                    if FileTest.exists?(exiter)
                        daemon.rmpidfile
                        exit(0)
                    end
                    sleep 0.1
                end
                exit(0)
            end
        end
        sleep(0.1)
        assert(FileTest.exists?(Puppet[:pidfile]),
            "did not create pidfile on daemonize")

        File.open(exiter, "w") { |f| f.puts "" }

        sleep(0.2)
        assert(! FileTest.exists?(Puppet[:pidfile]),
            "did not remove pidfile on process death")
    end
end