/
lock_helper_spec.rb
125 lines (104 loc) · 3.38 KB
/
lock_helper_spec.rb
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
# Copyright (c) 2009-2012 VMware, Inc.
require File.expand_path("../../spec_helper", __FILE__)
describe Bosh::Director::LockHelper do
class TestClass
include BD::LockHelper
end
before(:each) do
@test_instance = TestClass.new
end
describe :with_deployment_lock do
it "should support a deployment model or plan" do
deployment = mock(:deployment)
deployment.stub(:name).and_return("foo")
lock = mock(:lock)
BD::Lock.stub!(:new).with("lock:deployment:foo", {:timeout=>10}).
and_return(lock)
lock.should_receive(:lock).and_yield
called = false
@test_instance.with_deployment_lock(deployment) do
called = true
end
called.should be_true
end
it "should support a deployment name" do
lock = mock(:lock)
BD::Lock.stub!(:new).with("lock:deployment:bar", {:timeout=>5}).
and_return(lock)
lock.should_receive(:lock).and_yield
called = false
@test_instance.with_deployment_lock("bar", :timeout => 5) do
called = true
end
called.should be_true
end
it "should fail for other types" do
expect { @test_instance.with_deployment_lock(nil, :timeout => 5) }.
to raise_error(ArgumentError)
end
end
describe :with_release_lock do
it "should support a release name" do
lock = mock(:lock)
BD::Lock.stub!(:new).with("lock:release:bar", {:timeout=>5}).
and_return(lock)
lock.should_receive(:lock).and_yield
called = false
@test_instance.with_release_lock("bar", :timeout => 5) do
called = true
end
called.should be_true
end
end
describe :with_release_locks do
it "should support a deployment plan" do
deployment_plan = mock(:deployment_plan)
release_a = mock(:release_a)
release_a.stub!(:name).and_return("a")
release_b = mock(:release_b)
release_b.stub!(:name).and_return("b")
deployment_plan.stub!(:releases).and_return([release_a, release_b])
lock_a = mock(:lock_a)
BD::Lock.stub!(:new).with("lock:release:a", {:timeout=>5}).
and_return(lock_a)
lock_b = mock(:lock_b)
BD::Lock.stub!(:new).with("lock:release:b", {:timeout=>5}).
and_return(lock_b)
lock_a.should_receive(:lock).ordered
lock_b.should_receive(:lock).ordered
lock_b.should_receive(:release).ordered
lock_a.should_receive(:release).ordered
called = false
@test_instance.with_release_locks(deployment_plan, :timeout => 5) do
called = true
end
called.should be_true
end
end
describe :with_stemcell_lock do
it "should support a stemcell name and version" do
lock = mock(:lock)
BD::Lock.stub!(:new).with("lock:stemcells:foo:1.0", {:timeout=>5}).
and_return(lock)
lock.should_receive(:lock).and_yield
called = false
@test_instance.with_stemcell_lock("foo", "1.0", :timeout => 5) do
called = true
end
called.should be_true
end
end
describe :with_compile_lock do
it "should support a package and stemcell id" do
lock = mock(:lock)
BD::Lock.stub!(:new).with("lock:compile:3:4", {:timeout=>900}).
and_return(lock)
lock.should_receive(:lock).and_yield
called = false
@test_instance.with_compile_lock(3, 4) do
called = true
end
called.should be_true
end
end
end