3 require File.dirname(__FILE__) + '/../../spec_helper'
5 require 'puppet/indirector/yaml'
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
16 @store = @store_class.new
19 @subject.metaclass.send(:attr_accessor, :name)
23 Puppet.settings.stubs(:use)
24 Puppet.settings.stubs(:value).with(:yamldir).returns(@dir)
28 describe Puppet::Indirector::Yaml, " when choosing file location" do
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}}
35 it "should use the terminus name for choosing the subdirectory" do
36 @store.send(:path, :me).should =~ %r{^#{@dir}/my_yaml}
39 it "should use the object's name to determine the file name" do
40 @store.send(:path, :me).should =~ %r{me.yaml$}
44 describe Puppet::Indirector::Yaml, " when storing objects as YAML" do
47 it "should only store objects that respond to :name" do
48 proc { @store.save(Object.new) }.should raise_error(ArgumentError)
51 it "should convert Ruby objects to YAML and write them to disk" do
52 yaml = @subject.to_yaml
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)
62 it "should create the indirection subdirectory if it does not exist" do
63 yaml = @subject.to_yaml
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)
76 describe Puppet::Indirector::Yaml, " when retrieving YAML" do
79 it "should require the name of the object to retrieve" do
80 proc { @store.find(nil) }.should raise_error(ArgumentError)
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
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)