summaryrefslogtreecommitdiff
path: root/spec/unit/run_list/run_list_expansion_spec.rb
blob: 927257875e0e1fe6c3fc4b031132e5d705695b54 (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
#
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Copyright:: Copyright (c) 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'

describe Chef::RunList::RunListExpansion do
  before do
    @run_list = Chef::RunList.new
    @run_list << 'recipe[lobster]' << 'role[rage]' << 'recipe[fist]'
    @expansion = Chef::RunList::RunListExpansion.new("_default", @run_list.run_list_items)
  end

  describe "before expanding the run list" do
    it "has an array of run list items" do
      expect(@expansion.run_list_items).to eq(@run_list.run_list_items)
    end

    it "has default_attrs" do
      expect(@expansion.default_attrs).to eq(Mash.new)
    end

    it "has override attrs" do
      expect(@expansion.override_attrs).to eq(Mash.new)
    end

    it "it has an empty list of recipes" do
      expect(@expansion.recipes.size).to eq(0)
    end

    it "has not applied its roles" do
      expect(@expansion.applied_role?('rage')).to be_falsey
    end
  end

  describe "after applying a role with environment-specific run lists" do
    before do
      @rage_role = Chef::Role.new.tap do |r|
        r.name("rage")
        r.env_run_lists('_default' => [], "prod" => ["recipe[prod-only]"])
      end
      @expansion = Chef::RunList::RunListExpansion.new("prod", @run_list.run_list_items)
      expect(@expansion).to receive(:fetch_role).and_return(@rage_role)
      @expansion.expand
    end

    it "has the correct list of recipes for the given environment" do
      expect(@expansion.recipes).to eq(["lobster", "prod-only", "fist"])
    end

  end

  describe "after applying a role" do
    before do
      allow(@expansion).to receive(:fetch_role).and_return(Chef::Role.new)
      @expansion.inflate_role('rage', "role[base]")
    end

    it "tracks the applied role" do
      expect(@expansion.applied_role?('rage')).to be_truthy
    end

    it "does not inflate the role again" do
      expect(@expansion.inflate_role('rage', "role[base]")).to be_falsey
    end
  end

  describe "after expanding a run list" do
    before do
      @first_role = Chef::Role.new
      @first_role.run_list('role[mollusk]')
      @first_role.default_attributes({'foo' => 'bar'})
      @first_role.override_attributes({'baz' => 'qux'})
      @second_role = Chef::Role.new
      @second_role.run_list('recipe[crabrevenge]')
      @second_role.default_attributes({'foo' => 'boo'})
      @second_role.override_attributes({'baz' => 'bux'})
      allow(@expansion).to receive(:fetch_role).and_return(@first_role, @second_role)
      @expansion.expand
    end

    it "has the ordered list of recipes" do
      expect(@expansion.recipes).to eq(['lobster', 'crabrevenge', 'fist'])
    end

    it "has the merged attributes from the roles with outer roles overridding inner" do
      expect(@expansion.default_attrs).to eq({'foo' => 'bar'})
      expect(@expansion.override_attrs).to eq({'baz' => 'qux'})
    end

    it "has the list of all roles applied" do
      # this is the correct order, but 1.8 hash order is not stable
      expect(@expansion.roles).to match_array(['rage', 'mollusk'])
    end

  end

  describe "after expanding a run list with a non existant role" do
    before do
      allow(@expansion).to receive(:fetch_role) { @expansion.role_not_found('crabrevenge', "role[base]") }
      @expansion.expand
    end

    it "is invalid" do
      expect(@expansion).to be_invalid
      expect(@expansion.errors?).to be_truthy # aliases
    end

    it "has a list of invalid role names" do
      expect(@expansion.errors).to include('crabrevenge')
    end

  end

end