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
|
require 'puppettest'
module PuppetTest
# A baseclass for the faketypes.
class FakeModel
include Puppet::Util
class << self
attr_accessor :name, :realresource
@name = :fakeresource
end
def self.namevar
@realresource.namevar
end
def self.validproperties
Puppet::Type.type(@name).validproperties
end
def self.validproperty?(name)
Puppet::Type.type(@name).validproperty?(name)
end
def self.to_s
"Fake%s" % @name.to_s.capitalize
end
def [](param)
if @realresource.attrtype(param) == :property
@is[param]
else
@params[param]
end
end
def []=(param, value)
param = symbolize(param)
unless @realresource.validattr?(param)
raise Puppet::DevError, "Invalid attribute %s for %s" %
[param, @realresource.name]
end
if @realresource.attrtype(param) == :property
@should[param] = value
else
@params[param] = value
end
end
def initialize(name)
@realresource = Puppet::Type.type(self.class.name)
raise "Could not find type #{self.class.name}" unless @realresource
@is = {}
@should = {}
@params = {}
self[@realresource.namevar] = name
end
def inspect
"%s(%s)" % [self.class.to_s.sub(/.+::/, ''), super()]
end
def is(param)
@is[param]
end
def should(param)
@should[param]
end
def to_hash
hash = @params.dup
[@is, @should].each do |h|
h.each do |p, v|
hash[p] = v
end
end
hash
end
def name
self[:name]
end
end
class FakeProvider
attr_accessor :resource
class << self
attr_accessor :name, :resource_type, :methods
end
# A very low number, so these never show up as defaults via the standard
# algorithms.
def self.defaultnum
-50
end
# Set up methods to fake things
def self.apimethods(*ary)
@resource_type.validproperties.each do |property|
ary << property unless ary.include? property
end
attr_accessor(*ary)
@methods = ary
end
def self.default?
false
end
def self.initvars
@calls = Hash.new do |hash, key|
hash[key] = 0
end
end
def self.supports_parameter?(param)
true
end
def self.suitable?
true
end
def clear
@resource = nil
end
def initialize(resource)
@resource = resource
end
end
class FakeParsedProvider < FakeProvider
def hash
ret = {}
instance_variables.each do |v|
v = v.sub("@", '')
if val = self.send(v)
ret[v.intern] = val
end
end
return ret
end
def store(hash)
hash.each do |n, v|
method = n.to_s + "="
if respond_to? method
send(method, v)
end
end
end
end
@@fakeresources = {}
@@fakeproviders = {}
def fakeresource(type, name, options = {})
type = type.intern if type.is_a? String
unless @@fakeresources.include? type
@@fakeresources[type] = Class.new(FakeModel)
@@fakeresources[type].name = type
resource = Puppet::Type.type(type)
raise("Could not find type %s" % type) unless resource
@@fakeresources[type].realresource = resource
end
obj = @@fakeresources[type].new(name)
options.each do |name, val|
obj[name] = val
end
obj
end
module_function :fakeresource
def fakeprovider(type, resource)
type = type.intern if type.is_a? String
unless @@fakeproviders.include? type
@@fakeproviders[type] = Class.new(FakeModel) do
@name = type
end
end
@@fakeproviders[type].new(resource)
end
module_function :fakeprovider
end
# $Id$
|