summaryrefslogtreecommitdiff
path: root/spec/unit/run_list/versioned_recipe_list_spec.rb
blob: 209ac37fc14fd7818f724d83d4915855ec30704c (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
#
# Author:: Stephen Delano (<stephen@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::VersionedRecipeList do

  describe "initialize" do
    it "should create an empty array" do
      l = Chef::RunList::VersionedRecipeList.new
      expect(l).to eq([])
    end
  end

  describe "add_recipe" do
    before(:each) do
      @list = Chef::RunList::VersionedRecipeList.new
      @list << "apt"
      @list << "god"
      @list << "apache2"
    end

    it "should append the recipe to the end of the list" do
      @list.add_recipe "rails"
      expect(@list).to eq(["apt", "god", "apache2", "rails"])
    end

    it "should not duplicate entries" do
      @list.add_recipe "apt"
      expect(@list).to eq(["apt", "god", "apache2"])
    end

    it "should allow you to specify a version" do
      @list.add_recipe "rails", "1.0.0"
      expect(@list).to eq(["apt", "god", "apache2", "rails"])
      expect(@list.with_versions).to include({:name => "rails", :version => "1.0.0"})
    end

    it "should allow you to specify a version for a recipe that already exists" do
      @list.add_recipe "apt", "1.2.3"
      expect(@list).to eq(["apt", "god", "apache2"])
      expect(@list.with_versions).to include({:name => "apt", :version => "1.2.3"})
    end

    it "should allow you to specify the same version of a recipe twice" do
      @list.add_recipe "rails", "1.0.0"
      @list.add_recipe "rails", "1.0.0"
      expect(@list.with_versions).to include({:name => "rails", :version => "1.0.0"})
    end

    it "should allow you to spcify no version, even when a version already exists" do
      @list.add_recipe "rails", "1.0.0"
      @list.add_recipe "rails"
      expect(@list.with_versions).to include({:name => "rails", :version => "1.0.0"})
    end

    it "should not allow multiple versions of the same recipe" do
      @list.add_recipe "rails", "1.0.0"
      expect {@list.add_recipe "rails", "0.1.0"}.to raise_error Chef::Exceptions::CookbookVersionConflict
    end
  end

  describe "with_versions" do
    before(:each) do
      @recipes = [
        {:name => "apt", :version => "1.0.0"},
        {:name => "god", :version => nil},
        {:name => "apache2", :version => "0.0.1"}
      ]
      @list = Chef::RunList::VersionedRecipeList.new
      @recipes.each {|i| @list.add_recipe i[:name], i[:version]}
    end

    it "should return an array of hashes with :name and :version" do
      expect(@list.with_versions).to eq(@recipes)
    end

    it "should retain the same order as the version-less list" do
      with_versions = @list.with_versions
      @list.each_with_index do |item, index|
        expect(with_versions[index][:name]).to eq(item)
      end
    end
  end

  describe "with_version_constraints" do
    before(:each) do
      @recipes = [
                  {:name => "apt", :version => "~> 1.2.0"},
                  {:name => "god", :version => nil},
                  {:name => "apache2", :version => "0.0.1"}
                 ]
      @list = Chef::RunList::VersionedRecipeList.new
      @recipes.each {|i| @list.add_recipe i[:name], i[:version]}
      @constraints = @recipes.map do |x|
        { :name => x[:name],
          :version_constraint => Chef::VersionConstraint.new(x[:version])
        }
      end
    end

    it "should return an array of hashes with :name and :version_constraint" do
      @list.with_version_constraints.each do |x|
        expect(x).to have_key :name
        expect(x[:version_constraint]).not_to be nil
      end
    end
  end
end