pairing with luke, nagios_command provider skeleton
[vinpup.git] / spec / unit / indirector / yaml.rb
blobf217a31b533e5205fcebec1851143ecd50f61b16
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../../spec_helper'
5 require 'puppet/indirector/yaml'
7 module YamlTesting
8     def setup
9         @indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil
10         Puppet::Indirector::Indirection.stubs(:instance).with(:my_yaml).returns(@indirection)
11         @store_class = Class.new(Puppet::Indirector::Yaml) do
12             def self.to_s
13                 "MyYaml::MyType"
14             end
15         end
16         @store = @store_class.new
18         @subject = Object.new
19         @subject.metaclass.send(:attr_accessor, :name)
20         @subject.name = :me
22         @dir = "/what/ever"
23         Puppet.settings.stubs(:use)
24         Puppet.settings.stubs(:value).with(:yamldir).returns(@dir)
25     end
26 end
28 describe Puppet::Indirector::Yaml, " when choosing file location" do
29     include YamlTesting
31     it "should store all files in a single file root set in the Puppet defaults" do
32         @store.send(:path, :me).should =~ %r{^#{@dir}}
33     end
35     it "should use the terminus name for choosing the subdirectory" do
36         @store.send(:path, :me).should =~ %r{^#{@dir}/my_yaml}
37     end
39     it "should use the object's name to determine the file name" do
40         @store.send(:path, :me).should =~ %r{me.yaml$}
41     end
42 end
44 describe Puppet::Indirector::Yaml, " when storing objects as YAML" do
45     include YamlTesting
47     it "should only store objects that respond to :name" do
48         proc { @store.save(Object.new) }.should raise_error(ArgumentError)
49     end
51     it "should convert Ruby objects to YAML and write them to disk" do
52         yaml = @subject.to_yaml
53         file = mock 'file'
54         path = @store.send(:path, @subject.name)
55         FileTest.expects(:exist?).with(File.dirname(path)).returns(true)
56         File.expects(:open).with(path, "w", 0660).yields(file)
57         file.expects(:print).with(yaml)
59         @store.save(@subject)
60     end
62     it "should create the indirection subdirectory if it does not exist" do
63         yaml = @subject.to_yaml
64         file = mock 'file'
65         path = @store.send(:path, @subject.name)
66         dir = File.dirname(path)
67         FileTest.expects(:exist?).with(dir).returns(false)
68         Dir.expects(:mkdir).with(dir)
69         File.expects(:open).with(path, "w", 0660).yields(file)
70         file.expects(:print).with(yaml)
72         @store.save(@subject)
73     end
74 end
76 describe Puppet::Indirector::Yaml, " when retrieving YAML" do
77     include YamlTesting
79     it "should require the name of the object to retrieve" do
80         proc { @store.find(nil) }.should raise_error(ArgumentError)
81     end
83     it "should read YAML in from disk and convert it to Ruby objects" do
84         path = @store.send(:path, @subject.name)
86         yaml = @subject.to_yaml
87         FileTest.expects(:exist?).with(path).returns(true)
88         File.expects(:read).with(path).returns(yaml)
90         @store.find(@subject.name).instance_variable_get("@name").should == :me
91     end
93     it "should fail coherently when the stored YAML is invalid" do
94         path = @store.send(:path, @subject.name)
96         # Something that will fail in yaml
97         yaml = "--- !ruby/object:Hash"
99         FileTest.expects(:exist?).with(path).returns(true)
100         File.expects(:read).with(path).returns(yaml)
102         proc { @store.find(@subject.name) }.should raise_error(Puppet::Error)
103     end