blob: 7c74e6738c256c070ff48aa16192d0c29a1cca5c (
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
|
#
# Author:: Ranjib Dey (<ranjib@linux.com>)
#
# Copyright:: Copyright (c) 2015 Ranjib Dey
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
require "chef/event_dispatch/dsl"
describe Chef::EventDispatch::DSL do
let(:events) do
Chef::EventDispatch::Dispatcher.new
end
let(:run_context) do
Chef::RunContext.new(Chef::Node.new, nil, events)
end
before do
Chef.set_run_context(run_context)
end
subject{ described_class.new("test") }
it "set handler name" do
subject.on(:run_started) {}
expect(events.subscribers.first.name).to eq("test")
end
it "raise error when invalid event type is supplied" do
expect do
subject.on(:foo_bar) {}
end.to raise_error(Chef::Exceptions::InvalidEventType)
end
it "register user hooks against valid event type" do
subject.on(:run_failed) {"testhook"}
expect(events.subscribers.first.run_failed).to eq("testhook")
end
it "preserve state across event hooks" do
calls = []
Chef.event_handler do
on :resource_updated do
calls << :updated
end
on :resource_action_start do
calls << :started
end
end
resource = Chef::Resource::RubyBlock.new("foo", run_context)
resource.block { }
resource.run_action(:run)
expect(calls).to eq([:started, :updated])
end
it "preserve instance variables across handler callbacks" do
Chef.event_handler do
on :resource_action_start do
@ivar = [1]
end
on :resource_updated do
@ivar << 2
end
end
resource = Chef::Resource::RubyBlock.new("foo", run_context)
resource.block { }
resource.run_action(:run)
expect(events.subscribers.first.instance_variable_get(:@ivar)).to eq([1, 2])
end
end
|