summaryrefslogtreecommitdiff
path: root/spec/functional/root_alias_spec.rb
blob: 7091615d1200fa70532f4e7dd5983b0abd25e2a8 (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
#
# Copyright:: Copyright 2017, Noah Kantrowitz
# 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"

describe "root aliases" do
  let(:chef_repo_path) { File.expand_path(File.join(CHEF_SPEC_DATA, "root_alias_cookbooks")) }
  let(:cookbook_collection) do
    cl = Chef::CookbookLoader.new(chef_repo_path)
    cl.load_cookbooks
    Chef::CookbookCollection.new(cl)
  end
  let(:node) do
    node = Chef::Node.new
    node.automatic[:recipes] = []
    node
  end
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, cookbook_collection, events) }
  before do
    node.run_context = run_context
  end

  describe "attributes root aliases" do
    it "should load attributes.rb when included directly" do
      node.include_attribute("simple")
      expect(node["aliased"]["attr"]).to eq "value"
    end

    it "should load attributes.rb when loading a cookbook" do
      node.run_list << "simple"
      run_context.load(node.run_list.expand("_default"))
      expect(node["aliased"]["attr"]).to eq "value"
    end

    context "with both an attributes.rb and attributes/default.rb" do
      it "should log an error and ignore attributes/default.rb" do
        expect(Chef::Log).to receive(:error).with("Cookbook dup_attr contains both attributes.rb and and attributes/default.rb, ignoring attributes/default.rb")
        node.run_list << "dup_attr"
        run_context.load(node.run_list.expand("_default"))
        expect(node["aliased"]["attr"]).to eq "value"
      end
    end
  end

  describe "recipe root aliased" do
    it "should load recipe.rb" do
      node.run_list << "simple"
      run_context.load(node.run_list.expand("_default"))
      run_context.include_recipe("simple")
      expect(run_context.resource_collection.map(&:to_s)).to eq ["ruby_block[root alias]"]
    end

    context "with both an recipe.rb and recipes/default.rb" do
      it "should log an error and ignore recipes/default.rb" do
        expect(Chef::Log).to receive(:error).with("Cookbook dup_recipe contains both recipe.rb and and recipes/default.rb, ignoring recipes/default.rb")
        node.run_list << "dup_recipe"
        run_context.load(node.run_list.expand("_default"))
        run_context.include_recipe("dup_recipe")
        expect(run_context.resource_collection.map(&:to_s)).to eq ["ruby_block[root alias]"]
      end
    end
  end
end