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
|
#
# Author:: Seth Falcon (<seth@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'
require 'chef/dsl/platform_introspection'
class LanguageTester
include Chef::DSL::PlatformIntrospection
end
describe Chef::DSL::PlatformIntrospection do
before(:each) do
@language = LanguageTester.new
@node = Hash.new
@language.stub!(:node).and_return(@node)
@platform_hash = {}
%w{openbsd freebsd}.each do |x|
@platform_hash[x] = {
"default" => x,
"1.2.3" => "#{x}-1.2.3"
}
end
@platform_hash["debian"] = {["5", "6"] => "debian-5/6", "default" => "debian"}
@platform_hash["default"] = "default"
@platform_family_hash = {
"debian" => "debian value",
[:rhel, :fedora] => "redhatty value",
"suse" => "suse value",
:default => "default value"
}
end
it "returns a default value when there is no known platform" do
@node = Hash.new
@language.value_for_platform(@platform_hash).should == "default"
end
it "returns a default value when there is no known platform family" do
@language.value_for_platform_family(@platform_family_hash).should == "default value"
end
it "returns a default value when the current platform doesn't match" do
@node[:platform] = "not-a-known-platform"
@language.value_for_platform(@platform_hash).should == "default"
end
it "returns a default value when current platform_family doesn't match" do
@node[:platform_family] = "ultra-derived-linux"
@language.value_for_platform_family(@platform_family_hash).should == "default value"
end
it "returns a value based on the current platform" do
@node[:platform] = "openbsd"
@language.value_for_platform(@platform_hash).should == "openbsd"
end
it "returns a value based on the current platform family" do
@node[:platform_family] = "debian"
@language.value_for_platform_family(@platform_family_hash).should == "debian value"
end
it "returns a version-specific value based on the current platform" do
@node[:platform] = "openbsd"
@node[:platform_version] = "1.2.3"
@language.value_for_platform(@platform_hash).should == "openbsd-1.2.3"
end
it "returns a value based on the current platform if version not found" do
@node[:platform] = "openbsd"
@node[:platform_version] = "0.0.0"
@language.value_for_platform(@platform_hash).should == "openbsd"
end
describe "when platform versions is an array" do
it "returns a version-specific value based on the current platform" do
@node[:platform] = "debian"
@node[:platform_version] = "6"
@language.value_for_platform(@platform_hash).should == "debian-5/6"
end
it "returns a value based on the current platform if version not found" do
@node[:platform] = "debian"
@node[:platform_version] = "0.0.0"
@language.value_for_platform(@platform_hash).should == "debian"
end
end
describe "when checking platform?" do
before(:each) do
@language = LanguageTester.new
@node = Hash.new
@language.stub!(:node).and_return(@node)
end
it "returns true if the node is a provided platform and platforms are provided as symbols" do
@node[:platform] = 'ubuntu'
@language.platform?([:redhat, :ubuntu]).should == true
end
it "returns true if the node is a provided platform and platforms are provided as strings" do
@node[:platform] = 'ubuntu'
@language.platform?(["redhat", "ubuntu"]).should == true
end
it "returns false if the node is not of the provided platforms" do
@node[:platform] = 'ubuntu'
@language.platform?(:splatlinux).should == false
end
end
describe "when checking platform_family?" do
before(:each) do
@language = LanguageTester.new
@node = Hash.new
@language.stub!(:node).and_return(@node)
end
it "returns true if the node is in a provided platform family and families are provided as symbols" do
@node[:platform_family] = 'debian'
@language.platform_family?([:rhel, :debian]).should == true
end
it "returns true if the node is a provided platform and platforms are provided as strings" do
@node[:platform_family] = 'rhel'
@language.platform_family?(["rhel", "debian"]).should == true
end
it "returns false if the node is not of the provided platforms" do
@node[:platform_family] = 'suse'
@language.platform_family?(:splatlinux).should == false
end
it "returns false if the node is not of the provided platforms and platform_family is not set" do
@language.platform_family?(:splatlinux).should == false
end
end
# NOTE: this is a regression test for bug CHEF-1514
describe "when the value is an array" do
before do
@platform_hash = {
"debian" => { "4.0" => [ :restart, :reload ], "default" => [ :restart, :reload, :status ] },
"ubuntu" => { "default" => [ :restart, :reload, :status ] },
"centos" => { "default" => [ :restart, :reload, :status ] },
"redhat" => { "default" => [ :restart, :reload, :status ] },
"fedora" => { "default" => [ :restart, :reload, :status ] },
"default" => { "default" => [:restart, :reload ] }}
end
it "returns the correct default for a given platform" do
@node[:platform] = "debian"
@node[:platform_version] = '9000'
@language.value_for_platform(@platform_hash).should == [ :restart, :reload, :status ]
end
it "returns the correct platform+version specific value " do
@node[:platform] = "debian"
@node[:platform_version] = '4.0'
@language.value_for_platform(@platform_hash).should == [:restart, :reload]
end
end
end
describe Chef::DSL::PlatformIntrospection::PlatformDependentValue do
before do
platform_hash = {
:openbsd => {:default => 'free, functional, secure'},
[:redhat, :centos, :fedora, :scientific] => {:default => '"stable"'},
:ubuntu => {'10.04' => 'using upstart more', :default => 'using init more'},
:default => 'bork da bork'
}
@platform_specific_value = Chef::DSL::PlatformIntrospection::PlatformDependentValue.new(platform_hash)
end
it "returns the default value when the platform doesn't match" do
@platform_specific_value.value_for_node(:platform => :dos).should == 'bork da bork'
end
it "returns a value for a platform set as a group" do
@platform_specific_value.value_for_node(:platform => :centos).should == '"stable"'
end
it "returns a value for the platform when it was set as a symbol but fetched as a string" do
@platform_specific_value.value_for_node(:platform => "centos").should == '"stable"'
end
it "returns a value for a specific platform version" do
node = {:platform => 'ubuntu', :platform_version => '10.04'}
@platform_specific_value.value_for_node(node).should == 'using upstart more'
end
it "returns a platform-default value if the platform version doesn't match an explicit one" do
node = {:platform => 'ubuntu', :platform_version => '9.10' }
@platform_specific_value.value_for_node(node).should == 'using init more'
end
it "returns nil if there is no default and no platforms match" do
# this matches the behavior in the original implementation.
# whether or not it's correct is another matter.
platform_specific_value = Chef::DSL::PlatformIntrospection::PlatformDependentValue.new({})
platform_specific_value.value_for_node(:platform => 'foo').should be_nil
end
it "raises an argument error if the platform hash is not correctly structured" do
bad_hash = {:ubuntu => :foo} # should be :ubuntu => {:default => 'foo'}
lambda {Chef::DSL::PlatformIntrospection::PlatformDependentValue.new(bad_hash)}.should raise_error(ArgumentError)
end
end
describe Chef::DSL::PlatformIntrospection::PlatformFamilyDependentValue do
before do
@array_values = [:stop, :start, :reload]
@platform_family_hash = {
"debian" => "debian value",
[:rhel, "fedora"] => "redhatty value",
"suse" => @array_values,
:gentoo => "gentoo value",
:default => "default value"
}
@platform_family_value = Chef::DSL::PlatformIntrospection::PlatformFamilyDependentValue.new(@platform_family_hash)
end
it "returns the default value when the platform family doesn't match" do
@platform_family_value.value_for_node(:platform_family => :os2).should == 'default value'
end
it "returns a value for the platform family when it was set as a string but fetched as a symbol" do
@platform_family_value.value_for_node(:platform_family => :debian).should == "debian value"
end
it "returns a value for the platform family when it was set as a symbol but fetched as a string" do
@platform_family_value.value_for_node(:platform_family => "gentoo").should == "gentoo value"
end
it "returns an array value stored for a platform family" do
@platform_family_value.value_for_node(:platform_family => "suse").should == @array_values
end
it "returns a value for the platform family when it was set within an array hash key as a symbol" do
@platform_family_value.value_for_node(:platform_family => :rhel).should == "redhatty value"
end
it "returns a value for the platform family when it was set within an array hash key as a string" do
@platform_family_value.value_for_node(:platform_family => "fedora").should == "redhatty value"
end
it "returns nil if there is no default and no platforms match" do
platform_specific_value = Chef::DSL::PlatformIntrospection::PlatformFamilyDependentValue.new({})
platform_specific_value.value_for_node(:platform_family => 'foo').should be_nil
end
end
|