summaryrefslogtreecommitdiff
path: root/spec/unit/event_dispatch/dsl_spec.rb
blob: 2896ce461012a69395454827dd845927d19230b2 (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 2015-2016, 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) do  end
    expect(events.subscribers.first.name).to eq("test")
  end

  it "raise error when invalid event type is supplied" do
    expect {
      subject.on(:foo_bar) {}
    }.to raise_error(Chef::Exceptions::InvalidEventType)
  end

  it "register user hooks against valid event type" do
    subject.on(:run_failed) do "testhook" end
    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 do end
    resource.run_action(:run)
    expect(calls).to eq(%i{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 do end
    resource.run_action(:run)
    expect(events.subscribers.first.instance_variable_get(:@ivar)).to eq([1, 2])
  end
end