summaryrefslogtreecommitdiffstats
path: root/spec/unit/agent/locker.rb
blob: 1477c824e4b68c1bc1e8f6f09b6def433bf9be6f (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
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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/agent'
require 'puppet/agent/locker'

class LockerTester
    include Puppet::Agent::Locker
end

describe Puppet::Agent::Locker do
    before do
        @locker = LockerTester.new
        @locker.stubs(:lockfile_path).returns "/my/lock"
    end

    it "should use a Pidlock instance as its lockfile" do
        @locker.lockfile.should be_instance_of(Puppet::Util::Pidlock)
    end

    it "should use 'lockfile_path' to determine its lockfile path" do
        @locker.expects(:lockfile_path).returns "/my/lock"
        lock = Puppet::Util::Pidlock.new("/my/lock")
        Puppet::Util::Pidlock.expects(:new).with("/my/lock").returns lock

        @locker.lockfile
    end

    it "should reuse the same lock file each time" do
        @locker.lockfile.should equal(@locker.lockfile)
    end

    it "should use the lock file to anonymously lock the process when disabled" do
        @locker.lockfile.expects(:lock).with(:anonymous => true)

        @locker.disable
    end

    it "should use the lock file to anonymously unlock the process when enabled" do
        @locker.lockfile.expects(:unlock).with(:anonymous => true)

        @locker.enable
    end

    it "should have a method that yields when a lock is attained" do
        @locker.lockfile.expects(:lock).returns true

        yielded = false
        @locker.lock do
            yielded = true
        end
        yielded.should be_true
    end

    it "should return true when the lock method successfully locked" do
        @locker.lockfile.expects(:lock).returns true

        @locker.lock {}.should be_true
    end

    it "should return true when the lock method does not receive the lock" do
        @locker.lockfile.expects(:lock).returns false

        @locker.lock {}.should be_false
    end

    it "should not yield when the lock method does not receive the lock" do
        @locker.lockfile.expects(:lock).returns false

        yielded = false
        @locker.lock { yielded = true }
        yielded.should be_false
    end

    it "should not unlock when a lock was not received" do
        @locker.lockfile.expects(:lock).returns false
        @locker.lockfile.expects(:unlock).never

        @locker.lock {}
    end

    it "should unlock after yielding upon obtaining a lock" do
        @locker.lockfile.stubs(:lock).returns true
        @locker.lockfile.expects(:unlock)

        @locker.lock {}
    end

    it "should unlock after yielding upon obtaining a lock, even if the block throws an exception" do
        @locker.lockfile.stubs(:lock).returns true
        @locker.lockfile.expects(:unlock)

        lambda { @locker.lock { raise "foo" } }.should raise_error(RuntimeError)
    end

    it "should be considered running if the lockfile is locked" do
        @locker.lockfile.expects(:locked?).returns true
        @locker.should be_running
    end
end