summaryrefslogtreecommitdiff
path: root/spec/unit/run_context/child_run_context_spec.rb
blob: 3654125a798b2081232293073e9f6e35004f5011 (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
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
126
127
128
129
130
131
132
133
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Tim Hinderliter (<tim@opscode.com>)
# Author:: Christopher Walters (<cw@opscode.com>)
# Copyright:: Copyright (c) 2008, 2010 Opscode, Inc.
# 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 "support/lib/library_load_order"

describe Chef::RunContext::ChildRunContext do
  context "with a run context with stuff in it" do
    let(:chef_repo_path) { File.expand_path(File.join(CHEF_SPEC_DATA, "run_context", "cookbooks")) }
    let(:cookbook_collection) {
      cl = Chef::CookbookLoader.new(chef_repo_path)
      cl.load_cookbooks
      Chef::CookbookCollection.new(cl)
    }
    let(:node) {
      node = Chef::Node.new
      node.run_list << "test" << "test::one" << "test::two"
      node
    }
    let(:events) { Chef::EventDispatch::Dispatcher.new }
    let(:run_context) { Chef::RunContext.new(node, cookbook_collection, events) }

    context "and a child run context" do
      let(:child) { run_context.create_child }

      it "parent_run_context is set to the parent" do
        expect(child.parent_run_context).to eq run_context
      end

      it "audits is not the same as the parent" do
        expect(child.audits.object_id).not_to eq run_context.audits.object_id
        child.audits["hi"] = "lo"
        expect(child.audits["hi"]).to eq("lo")
        expect(run_context.audits["hi"]).not_to eq("lo")
      end

      it "resource_collection is not the same as the parent" do
        expect(child.resource_collection.object_id).not_to eq run_context.resource_collection.object_id
        f = Chef::Resource::File.new("hi", child)
        child.resource_collection.insert(f)
        expect(child.resource_collection).to include f
        expect(run_context.resource_collection).not_to include f
      end

      it "immediate_notification_collection is not the same as the parent" do
        expect(child.immediate_notification_collection.object_id).not_to eq run_context.immediate_notification_collection.object_id
        src = Chef::Resource::File.new("hi", child)
        dest = Chef::Resource::File.new("argh", child)
        notification = Chef::Resource::Notification.new(dest, :create, src)
        child.notifies_immediately(notification)
        expect(child.immediate_notification_collection["file[hi]"]).to eq([notification])
        expect(run_context.immediate_notification_collection["file[hi]"]).not_to eq([notification])
      end

      it "immediate_notifications is not the same as the parent" do
        src = Chef::Resource::File.new("hi", child)
        dest = Chef::Resource::File.new("argh", child)
        notification = Chef::Resource::Notification.new(dest, :create, src)
        child.notifies_immediately(notification)
        expect(child.immediate_notifications(src)).to eq([notification])
        expect(run_context.immediate_notifications(src)).not_to eq([notification])
      end

      it "delayed_notification_collection is not the same as the parent" do
        expect(child.delayed_notification_collection.object_id).not_to eq run_context.delayed_notification_collection.object_id
        src = Chef::Resource::File.new("hi", child)
        dest = Chef::Resource::File.new("argh", child)
        notification = Chef::Resource::Notification.new(dest, :create, src)
        child.notifies_delayed(notification)
        expect(child.delayed_notification_collection["file[hi]"]).to eq([notification])
        expect(run_context.delayed_notification_collection["file[hi]"]).not_to eq([notification])
      end

      it "delayed_notifications is not the same as the parent" do
        src = Chef::Resource::File.new("hi", child)
        dest = Chef::Resource::File.new("argh", child)
        notification = Chef::Resource::Notification.new(dest, :create, src)
        child.notifies_delayed(notification)
        expect(child.delayed_notifications(src)).to eq([notification])
        expect(run_context.delayed_notifications(src)).not_to eq([notification])
      end

      it "create_child creates a child-of-child" do
        c = child.create_child
        expect(c.parent_run_context).to eq child
      end

      context "after load('include::default')" do
        before do
          run_list = Chef::RunList.new("include::default").expand("_default")
          # TODO not sure why we had to do this to get everything to work ...
          node.automatic_attrs[:recipes] = []
          child.load(run_list)
        end

        it "load_recipe loads into the child" do
          expect(child.resource_collection).to be_empty
          child.load_recipe("include::includee")
          expect(child.resource_collection).not_to be_empty
        end

        it "include_recipe loads into the child" do
          expect(child.resource_collection).to be_empty
          child.include_recipe("include::includee")
          expect(child.resource_collection).not_to be_empty
        end

        it "load_recipe_file loads into the child" do
          expect(child.resource_collection).to be_empty
          child.load_recipe_file(File.expand_path("include/recipes/includee.rb", chef_repo_path))
          expect(child.resource_collection).not_to be_empty
        end
      end
    end
  end
end