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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/network/formats'
class PsonTest
attr_accessor :string
def ==(other)
string == other.string
end
def self.from_data_hash(data)
new(data)
end
def initialize(string)
@string = string
end
def to_pson(*args)
{
'type' => self.class.name,
'data' => @string
}.to_pson(*args)
end
end
describe "Puppet Network Format" do
it "should include a msgpack format", :if => Puppet.features.msgpack? do
Puppet::Network::FormatHandler.format(:msgpack).should_not be_nil
end
describe "msgpack", :if => Puppet.features.msgpack? do
before do
@msgpack = Puppet::Network::FormatHandler.format(:msgpack)
end
it "should have its mime type set to application/x-msgpack" do
@msgpack.mime.should == "application/x-msgpack"
end
it "should have a weight of 20" do
@msgpack.weight.should == 20
end
it "should fail when one element does not have a from_data_hash" do
expect do
@msgpack.intern_multiple(Hash, MessagePack.pack(["foo"]))
end.to raise_error(NoMethodError)
end
it "should be able to serialize a catalog" do
cat = Puppet::Resource::Catalog.new('foo', Puppet::Node::Environment.create(:testing, []))
cat.add_resource(Puppet::Resource.new(:file, 'my_file'))
catunpack = MessagePack.unpack(cat.to_msgpack)
catunpack.should include(
"tags"=>[],
"name"=>"foo",
"version"=>nil,
"environment"=>"testing",
"edges"=>[],
"classes"=>[]
)
catunpack["resources"][0].should include(
"type"=>"File",
"title"=>"my_file",
"exported"=>false
)
catunpack["resources"][0]["tags"].should include(
"file",
"my_file"
)
end
end
it "should include a yaml format" do
Puppet::Network::FormatHandler.format(:yaml).should_not be_nil
end
describe "yaml" do
before do
@yaml = Puppet::Network::FormatHandler.format(:yaml)
end
it "should have its mime type set to text/yaml" do
@yaml.mime.should == "text/yaml"
end
it "should be supported on Strings" do
@yaml.should be_supported(String)
end
it "should render by calling 'to_yaml' on the instance" do
instance = mock 'instance'
instance.expects(:to_yaml).returns "foo"
@yaml.render(instance).should == "foo"
end
it "should render multiple instances by calling 'to_yaml' on the array" do
instances = [mock('instance')]
instances.expects(:to_yaml).returns "foo"
@yaml.render_multiple(instances).should == "foo"
end
it "should deserialize YAML" do
@yaml.intern(String, YAML.dump("foo")).should == "foo"
end
it "should deserialize symbols as strings" do
@yaml.intern(String, YAML.dump(:foo)).should == "foo"
end
it "should load from yaml when deserializing an array" do
text = YAML.dump(["foo"])
@yaml.intern_multiple(String, text).should == ["foo"]
end
it "fails intelligibly instead of calling to_pson with something other than a hash" do
expect do
@yaml.intern(Puppet::Node, '')
end.to raise_error(Puppet::Network::FormatHandler::FormatError, /did not contain a valid instance/)
end
it "fails intelligibly when intern_multiple is called and yaml doesn't decode to an array" do
expect do
@yaml.intern_multiple(Puppet::Node, '')
end.to raise_error(Puppet::Network::FormatHandler::FormatError, /did not contain a collection/)
end
it "fails intelligibly instead of calling to_pson with something other than a hash when interning multiple" do
expect do
@yaml.intern_multiple(Puppet::Node, YAML.dump(["hello"]))
end.to raise_error(Puppet::Network::FormatHandler::FormatError, /did not contain a valid instance/)
end
end
describe "base64 compressed yaml", :if => Puppet.features.zlib? do
before do
@yaml = Puppet::Network::FormatHandler.format(:b64_zlib_yaml)
end
it "should have its mime type set to text/b64_zlib_yaml" do
@yaml.mime.should == "text/b64_zlib_yaml"
end
it "should render by calling 'to_yaml' on the instance" do
instance = mock 'instance'
instance.expects(:to_yaml).returns "foo"
@yaml.render(instance)
end
it "should encode generated yaml on render" do
instance = mock 'instance', :to_yaml => "foo"
@yaml.expects(:encode).with("foo").returns "bar"
@yaml.render(instance).should == "bar"
end
it "should render multiple instances by calling 'to_yaml' on the array" do
instances = [mock('instance')]
instances.expects(:to_yaml).returns "foo"
@yaml.render_multiple(instances)
end
it "should encode generated yaml on render" do
instances = [mock('instance')]
instances.stubs(:to_yaml).returns "foo"
@yaml.expects(:encode).with("foo").returns "bar"
@yaml.render(instances).should == "bar"
end
it "should round trip data" do
@yaml.intern(String, @yaml.encode("foo")).should == "foo"
end
it "should round trip multiple data elements" do
data = @yaml.render_multiple(["foo", "bar"])
@yaml.intern_multiple(String, data).should == ["foo", "bar"]
end
it "should intern by base64 decoding, uncompressing and safely Yaml loading" do
input = Base64.encode64(Zlib::Deflate.deflate(YAML.dump("data in")))
@yaml.intern(String, input).should == "data in"
end
it "should render by compressing and base64 encoding" do
output = @yaml.render("foo")
YAML.load(Zlib::Inflate.inflate(Base64.decode64(output))).should == "foo"
end
describe "when zlib is disabled" do
before do
Puppet[:zlib] = false
end
it "use_zlib? should return false" do
@yaml.use_zlib?.should == false
end
it "should refuse to encode" do
expect { @yaml.render("foo") }.to raise_error(Puppet::Error, /zlib library is not installed/)
end
it "should refuse to decode" do
expect { @yaml.intern(String, "foo") }.to raise_error(Puppet::Error, /zlib library is not installed/)
end
end
describe "when zlib is not installed" do
it "use_zlib? should return false" do
Puppet[:zlib] = true
Puppet.features.expects(:zlib?).returns(false)
@yaml.use_zlib?.should == false
end
end
end
describe "plaintext" do
before do
@text = Puppet::Network::FormatHandler.format(:s)
end
it "should have its mimetype set to text/plain" do
@text.mime.should == "text/plain"
end
it "should use 'txt' as its extension" do
@text.extension.should == "txt"
end
end
describe "dot" do
before do
@dot = Puppet::Network::FormatHandler.format(:dot)
end
it "should have its mimetype set to text/dot" do
@dot.mime.should == "text/dot"
end
end
describe Puppet::Network::FormatHandler.format(:raw) do
before do
@format = Puppet::Network::FormatHandler.format(:raw)
end
it "should exist" do
@format.should_not be_nil
end
it "should have its mimetype set to application/x-raw" do
@format.mime.should == "application/x-raw"
end
it "should always be supported" do
@format.should be_supported(String)
end
it "should fail if its multiple_render method is used" do
lambda { @format.render_multiple("foo") }.should raise_error(NotImplementedError)
end
it "should fail if its multiple_intern method is used" do
lambda { @format.intern_multiple(String, "foo") }.should raise_error(NotImplementedError)
end
it "should have a weight of 1" do
@format.weight.should == 1
end
end
it "should include a pson format" do
Puppet::Network::FormatHandler.format(:pson).should_not be_nil
end
describe "pson" do
before do
@pson = Puppet::Network::FormatHandler.format(:pson)
end
it "should have its mime type set to text/pson" do
Puppet::Network::FormatHandler.format(:pson).mime.should == "text/pson"
end
it "should require the :render_method" do
Puppet::Network::FormatHandler.format(:pson).required_methods.should be_include(:render_method)
end
it "should require the :intern_method" do
Puppet::Network::FormatHandler.format(:pson).required_methods.should be_include(:intern_method)
end
it "should have a weight of 10" do
@pson.weight.should == 10
end
describe "when supported" do
it "should render by calling 'to_pson' on the instance" do
instance = PsonTest.new("foo")
instance.expects(:to_pson).returns "foo"
@pson.render(instance).should == "foo"
end
it "should render multiple instances by calling 'to_pson' on the array" do
instances = [mock('instance')]
instances.expects(:to_pson).returns "foo"
@pson.render_multiple(instances).should == "foo"
end
it "should intern by calling 'PSON.parse' on the text and then using from_data_hash to convert the data into an instance" do
text = "foo"
PSON.expects(:parse).with("foo").returns("type" => "PsonTest", "data" => "foo")
PsonTest.expects(:from_data_hash).with("foo").returns "parsed_pson"
@pson.intern(PsonTest, text).should == "parsed_pson"
end
it "should not render twice if 'PSON.parse' creates the appropriate instance" do
text = "foo"
instance = PsonTest.new("foo")
PSON.expects(:parse).with("foo").returns(instance)
PsonTest.expects(:from_data_hash).never
@pson.intern(PsonTest, text).should equal(instance)
end
it "should intern by calling 'PSON.parse' on the text and then using from_data_hash to convert the actual into an instance if the pson has no class/data separation" do
text = "foo"
PSON.expects(:parse).with("foo").returns("foo")
PsonTest.expects(:from_data_hash).with("foo").returns "parsed_pson"
@pson.intern(PsonTest, text).should == "parsed_pson"
end
it "should intern multiples by parsing the text and using 'class.intern' on each resulting data structure" do
text = "foo"
PSON.expects(:parse).with("foo").returns ["bar", "baz"]
PsonTest.expects(:from_data_hash).with("bar").returns "BAR"
PsonTest.expects(:from_data_hash).with("baz").returns "BAZ"
@pson.intern_multiple(PsonTest, text).should == %w{BAR BAZ}
end
it "fails intelligibly when given invalid data" do
expect do
@pson.intern(Puppet::Node, '')
end.to raise_error(PSON::ParserError, /source did not contain any PSON/)
end
end
end
describe ":console format" do
subject { Puppet::Network::FormatHandler.format(:console) }
it { should be_an_instance_of Puppet::Network::Format }
let :json do Puppet::Network::FormatHandler.format(:pson) end
[:intern, :intern_multiple].each do |method|
it "should not implement #{method}" do
expect { subject.send(method, String, 'blah') }.to raise_error NotImplementedError
end
end
["hello", 1, 1.0].each do |input|
it "should just return a #{input.inspect}" do
subject.render(input).should == input
end
end
[[1, 2], ["one"], [{ 1 => 1 }]].each do |input|
it "should render #{input.inspect} as one item per line" do
subject.render(input).should == input.collect { |item| item.to_s + "\n" }.join('')
end
end
it "should render empty hashes as empty strings" do
subject.render({}).should == ''
end
it "should render a non-trivially-keyed Hash as JSON" do
hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 }
subject.render(hash).should == json.render(hash).chomp
end
it "should render a {String,Numeric}-keyed Hash into a table" do
object = Object.new
hash = { "one" => 1, "two" => [], "three" => {}, "four" => object,
5 => 5, 6.0 => 6 }
# Gotta love ASCII-betical sort order. Hope your objects are better
# structured for display than my test one is. --daniel 2011-04-18
subject.render(hash).should == <<EOT
5 5
6.0 6
four #{json.render(object).chomp}
one 1
three {}
two []
EOT
end
it "should render a hash nicely with a multi-line value" do
pending "Moving to PSON rather than PP makes this unsupportable."
hash = {
"number" => { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 },
"text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 }
}
subject.render(hash).should == <<EOT
number {"1"=>"1111111111111111111111111111111111111111",
"2"=>"2222222222222222222222222222222222222222",
"3"=>"3333333333333333333333333333333333333333"}
text {"a"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"b"=>"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"c"=>"cccccccccccccccccccccccccccccccccccccccc"}
EOT
end
end
end
|