summaryrefslogtreecommitdiff
path: root/spec/unit/platform_spec.rb
blob: e8c41cdd77db21bc60067bdc15d61e28387fc6fd (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 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::Platform supports" do
  [
    :mac_os_x,
    :mac_os_x_server,
    :freebsd,
    :ubuntu,
    :debian,
    :centos,
    :fedora,
    :suse,
    :opensuse,
    :redhat,
    :oracle,
    :gentoo,
    :arch,
    :solaris,
    :mswin,
    :mingw32,
    :windows,
    :gcel,
    :ibm_powerkvm
  ].each do |platform|
    it "#{platform}" do
      Chef::Platform.platforms.should have_key(platform)
    end
  end
end

describe Chef::Platform do

  context "while testing with fake data" do

    before :all do
      @original_platform_map = Chef::Platform.platforms
    end

    after :all do ||
      Chef::Platform.platforms = @original_platform_map
    end

    before(:each) do
      Chef::Platform.platforms = {
        :darwin => {
          ">= 10.11" => {
            :file => "new_darwinian"
          },
          "9.2.2" => {
            :file => "darwinian",
            :else => "thing"
          },
          :default => {
            :file => "old school",
            :snicker => "snack"
          }
        },
        :mars_volta => {
        },
        :default => {
          :file => Chef::Provider::File,
          :pax => "brittania",
          :cat => "nice"
        }
      }
      @events = Chef::EventDispatch::Dispatcher.new
    end

    it "should allow you to look up a platform by name and version, returning the provider map for it" do
      pmap = Chef::Platform.find("Darwin", "9.2.2")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql("darwinian")
    end

    it "should allow you to look up a platform by name and version using \"greater than\" style operators" do
      pmap = Chef::Platform.find("Darwin", "11.1.0")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql("new_darwinian")
    end

    it "should use the default providers for an os if the specific version does not exist" do
      pmap = Chef::Platform.find("Darwin", "1")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql("old school")
    end

    it "should use the default providers if the os doesn't give me a default, but does exist" do
      pmap = Chef::Platform.find("mars_volta", "1")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql(Chef::Provider::File)
    end

    it "should use the default provider if the os does not exist" do
      pmap = Chef::Platform.find("AIX", "1")
      pmap.should be_a_kind_of(Hash)
      pmap[:file].should eql(Chef::Provider::File)
    end

    it "should merge the defaults for an os with the specific version" do
      pmap = Chef::Platform.find("Darwin", "9.2.2")
      pmap[:file].should eql("darwinian")
      pmap[:snicker].should eql("snack")
    end

    it "should merge the defaults for an os with the universal defaults" do
      pmap = Chef::Platform.find("Darwin", "9.2.2")
      pmap[:file].should eql("darwinian")
      pmap[:pax].should eql("brittania")
    end

    it "should allow you to look up a provider for a platform directly by symbol" do
      Chef::Platform.find_provider("Darwin", "9.2.2", :file).should eql("darwinian")
    end

    it "should raise an exception if a provider cannot be found for a resource type" do
      lambda { Chef::Platform.find_provider("Darwin", "9.2.2", :coffee) }.should raise_error(ArgumentError)
    end

    it "should look up a provider for a resource with a Chef::Resource object" do
      kitty = Chef::Resource::Cat.new("loulou")
      Chef::Platform.find_provider("Darwin", "9.2.2", kitty).should eql("nice")
    end

    it "should look up a provider with a node and a Chef::Resource object" do
      kitty = Chef::Resource::Cat.new("loulou")
      node = Chef::Node.new
      node.name("Intel")
      node.automatic_attrs[:platform] = "mac_os_x"
      node.automatic_attrs[:platform_version] = "9.2.2"
      Chef::Platform.find_provider_for_node(node, kitty).should eql("nice")
    end

    it "should not throw an exception when the platform version has an unknown format" do
      Chef::Platform.find_provider(:darwin, "bad-version", :file).should eql("old school")
    end

    it "should prefer an explicit provider" do
      kitty = Chef::Resource::Cat.new("loulou")
      kitty.stub(:provider).and_return(Chef::Provider::File)
      node = Chef::Node.new
      node.name("Intel")
      node.automatic_attrs[:platform] = "mac_os_x"
      node.automatic_attrs[:platform_version] = "9.2.2"
      Chef::Platform.find_provider_for_node(node, kitty).should eql(Chef::Provider::File)
    end

    it "should look up a provider based on the resource name if nothing else matches" do
      kitty = Chef::Resource::Cat.new("loulou")
      class Chef::Provider::Cat < Chef::Provider; end
      Chef::Platform.platforms[:default].delete(:cat)
      node = Chef::Node.new
      node.name("Intel")
      node.automatic_attrs[:platform] = "mac_os_x"
      node.automatic_attrs[:platform_version] = "8.5"
      Chef::Platform.find_provider_for_node(node, kitty).should eql(Chef::Provider::Cat)
    end

    def setup_file_resource
      node = Chef::Node.new
      node.automatic_attrs[:platform] = "mac_os_x"
      node.automatic_attrs[:platform_version] = "9.2.2"
      run_context = Chef::RunContext.new(node, {}, @events)
      [ Chef::Resource::File.new("whateva", run_context), run_context ]
    end

    it "returns a provider object given a Chef::Resource object which has a valid run context and an action" do
      file, run_context = setup_file_resource
      provider = Chef::Platform.provider_for_resource(file, :foo)
      provider.should be_an_instance_of(Chef::Provider::File)
      provider.new_resource.should equal(file)
      provider.run_context.should equal(run_context)
    end

    it "returns a provider object given a Chef::Resource object which has a valid run context without an action" do
      file, run_context = setup_file_resource
      provider = Chef::Platform.provider_for_resource(file)
      provider.should be_an_instance_of(Chef::Provider::File)
      provider.new_resource.should equal(file)
      provider.run_context.should equal(run_context)
    end

    it "raises an error when trying to find the provider for a resource with no run context" do
      file = Chef::Resource::File.new("whateva")
      lambda {Chef::Platform.provider_for_resource(file)}.should raise_error(ArgumentError)
    end

    it "does not support finding a provider by resource and node -- a run context is required" do
      lambda {Chef::Platform.provider_for_node('node', 'resource')}.should raise_error(NotImplementedError)
    end

    it "should update the provider map with map" do
      Chef::Platform.set(
           :platform => :darwin,
           :version => "9.2.2",
           :resource => :file,
           :provider => "masterful"
      )
      Chef::Platform.platforms[:darwin]["9.2.2"][:file].should eql("masterful")
      Chef::Platform.set(
           :platform => :darwin,
           :resource => :file,
           :provider => "masterful"
      )
      Chef::Platform.platforms[:darwin][:default][:file].should eql("masterful")
      Chef::Platform.set(
           :resource => :file,
           :provider => "masterful"
      )
      Chef::Platform.platforms[:default][:file].should eql("masterful")

      Chef::Platform.set(
           :platform => :hero,
           :version => "9.2.2",
           :resource => :file,
           :provider => "masterful"
      )
      Chef::Platform.platforms[:hero]["9.2.2"][:file].should eql("masterful")

      Chef::Platform.set(
           :resource => :file,
           :provider => "masterful"
      )
      Chef::Platform.platforms[:default][:file].should eql("masterful")

      Chef::Platform.platforms = {}

      Chef::Platform.set(
           :resource => :file,
           :provider => "masterful"
      )
      Chef::Platform.platforms[:default][:file].should eql("masterful")

      Chef::Platform.platforms = { :neurosis => {} }
      Chef::Platform.set(:platform => :neurosis, :resource => :package, :provider => "masterful")
      Chef::Platform.platforms[:neurosis][:default][:package].should eql("masterful")

    end

    it "does not overwrite the platform map when using :default platform" do
      Chef::Platform.set(
        :resource => :file,
        :platform => :default,
        :provider => "new school"
      )
      Chef::Platform.platforms[:default][:file].should eql("new school")
      Chef::Platform.platforms[:default][:cat].should eql("nice")
    end

  end

  context "while testing the configured platform data" do

    it "should use the solaris package provider on Solaris <11" do
      pmap = Chef::Platform.find("Solaris2", "5.9")
      pmap[:package].should eql(Chef::Provider::Package::Solaris)
    end

    it "should use the IPS package provider on Solaris 11" do
      pmap = Chef::Platform.find("Solaris2", "5.11")
      pmap[:package].should eql(Chef::Provider::Package::Ips)
    end

    it "should use the SUSE group provider on SLES11" do
      1.upto(3) do |sp|
        pmap = Chef::Platform.find("SUSE", "11.#{sp}")
        pmap[:group].should eql(Chef::Provider::Group::Suse)
      end
    end

    it "should use the Gpasswd group provider on SLES12" do
      pmap = Chef::Platform.find("SUSE", "12.0")
      pmap[:group].should eql(Chef::Provider::Group::Gpasswd)
    end
  end

end