summaryrefslogtreecommitdiff
path: root/spec/unit/resource_platform_map_spec.rb
blob: 99673d868f4bdc8b63d54adaf76fa80067e7aa48 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#
# Author:: Seth Chisamore (<schisamo@opscode.com>)
# Copyright:: Copyright (c) 2011 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::Resource::PlatformMap do

  before(:each) do
    @platform_map = Chef::Resource::PlatformMap.new({
      :windows => {
        "6.1" => {
          :file => "softiefile",
          :else => "thing"
        },
        :default => {
          :file => Chef::Resource::File,
          :ping => "pong",
          :cat => "nice"
        }
      },
      :pop_tron => {
      },
      :default => {
        :soundwave => "lazerbeak",
        :directory => Chef::Resource::Directory,
      }
    })
  end

  describe 'filtering the map' do
    it "returns resources for platform and version" do
      pmap = @platform_map.filter("Windows", "6.1")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql("softiefile")
    end

    it "returns platform default resources if version does not exist" do
      pmap = @platform_map.filter("windows", "1")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql(Chef::Resource::File)
    end

    it "returns global default resources if none exist for plaform" do
      pmap = @platform_map.filter("pop_tron", "1")
      pmap.should be_a_kind_of(Hash)
      pmap[:directory].should eql(Chef::Resource::Directory)
    end

    it "returns global default resources if platform does not exist" do
      pmap = @platform_map.filter("BeOS", "1")
      pmap.should be_a_kind_of(Hash)
      pmap[:soundwave].should eql("lazerbeak")
    end

    it "returns a merged map of platform version and plaform default resources" do
      pmap = @platform_map.filter("Windows", "6.1")
      pmap[:file].should eql("softiefile")
      pmap[:ping].should eql("pong")
    end

    it "returns a merged map of platform specific version and global defaults" do
      pmap = @platform_map.filter("Windows", "6.1")
      pmap[:file].should eql("softiefile")
      pmap[:soundwave].should eql("lazerbeak")
    end
  end

  describe 'finding a resource' do
    it "returns a resource for a platform directly by short name" do
      @platform_map.get(:file, "windows", "6.1").should eql("softiefile")
    end

    it "returns a default resource if platform and version don't exist" do
      @platform_map.get(:remote_file).should eql(Chef::Resource::RemoteFile)
    end

    it "raises an exception if a resource cannot be found" do
      lambda { @platform_map.get(:coffee, "windows", "6.1")}.should raise_error(NameError)
    end

    it "returns a resource with a Chef::Resource object" do
      kitty = Chef::Resource::Cat.new("loulou")
      @platform_map.get(kitty, "windows", "6.1").should eql("nice")
    end
  end

  describe 'building the map' do
    it "allows passing of a resource map at creation time" do
      @new_map = Chef::Resource::PlatformMap.new({:the_dude => {:default => 'abides'}})
      @new_map.map[:the_dude][:default].should eql("abides")
    end

    it "defaults to a resource map with :default key" do
      @new_map = Chef::Resource::PlatformMap.new
      @new_map.map.has_key?(:default)
    end

    it "updates the resource map with a map" do
      @platform_map.set(
       :platform => :darwin,
       :version => "9.2.2",
       :short_name => :file,
       :resource => "masterful"
      )
      @platform_map.map[:darwin]["9.2.2"][:file].should eql("masterful")

      @platform_map.set(
       :platform => :darwin,
       :short_name => :file,
       :resource => "masterful"
      )
      @platform_map.map[:darwin][:default][:file].should eql("masterful")

      @platform_map.set(
       :short_name => :file,
       :resource => "masterful"
      )
      @platform_map.map[:default][:file].should eql("masterful")

      @platform_map.set(
       :platform => :hero,
       :version => "9.2.2",
       :short_name => :file,
       :resource => "masterful"
      )
      @platform_map.map[:hero]["9.2.2"][:file].should eql("masterful")

      @platform_map.set(
        :short_name => :file,
        :resource => "masterful"
      )
      @platform_map.map[:default][:file].should eql("masterful")

      @platform_map.set(
       :short_name => :file,
       :resource => "masterful"
      )
      @platform_map.map[:default][:file].should eql("masterful")

      @platform_map.set(
        :platform => :neurosis,
        :short_name => :package,
        :resource => "masterful"
      )
      @platform_map.map[:neurosis][:default][:package].should eql("masterful")
    end
  end

end