4
require 'puppet/indirector/yaml'
6
describe Puppet::Indirector::Yaml, " when choosing file location" do
8
@indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil
9
Puppet::Indirector::Indirection.expects(:instance).with(:my_yaml).returns(@indirection)
11
@store_class = class MyYaml::MyType < Puppet::Indirector::Yaml
16
@store = @store_class.new
19
@subject.singleton_class.send(:attr_accessor, :name)
23
Puppet.settings.stubs(:value).returns("fakesettingdata")
24
Puppet.settings.stubs(:value).with(:clientyamldir).returns(@dir)
25
Puppet.run_mode.stubs(:master?).returns false
27
@request = stub 'request', :key => :me, :instance => @subject
30
describe Puppet::Indirector::Yaml, " when choosing file location" do
31
it "should use the server_datadir if the run_mode is master" do
32
Puppet.run_mode.expects(:master?).returns true
33
Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir"
34
@store.path(:me).should =~ %r{^/server/yaml/dir}
37
it "should use the client yamldir if the run_mode is not master" do
38
Puppet.run_mode.expects(:master?).returns false
39
Puppet.settings.expects(:value).with(:clientyamldir).returns "/client/yaml/dir"
40
@store.path(:me).should =~ %r{^/client/yaml/dir}
43
it "should use the extension if one is specified" do
44
Puppet.run_mode.expects(:master?).returns true
45
Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir"
46
@store.path(:me,'.farfignewton').should =~ %r{\.farfignewton$}
49
it "should assume an extension of .yaml if none is specified" do
50
Puppet.run_mode.expects(:master?).returns true
51
Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir"
52
@store.path(:me).should =~ %r{\.yaml$}
55
it "should store all files in a single file root set in the Puppet defaults" do
56
@store.path(:me).should =~ %r{^#{@dir}}
59
it "should use the terminus name for choosing the subdirectory" do
60
@store.path(:me).should =~ %r{^#{@dir}/my_yaml}
63
it "should use the object's name to determine the file name" do
64
@store.path(:me).should =~ %r{me.yaml$}
68
describe Puppet::Indirector::Yaml, " when storing objects as YAML" do
69
it "should only store objects that respond to :name" do
70
@request.stubs(:instance).returns Object.new
71
proc { @store.save(@request) }.should raise_error(ArgumentError)
74
it "should convert Ruby objects to YAML and write them to disk using a write lock" do
75
yaml = @subject.to_yaml
77
path = @store.send(:path, @subject.name)
78
FileTest.expects(:exist?).with(File.dirname(path)).returns(true)
79
@store.expects(:writelock).with(path, 0660).yields(file)
80
file.expects(:print).with(yaml)
85
it "should create the indirection subdirectory if it does not exist" do
86
yaml = @subject.to_yaml
88
path = @store.send(:path, @subject.name)
89
dir = File.dirname(path)
91
FileTest.expects(:exist?).with(dir).returns(false)
92
Dir.expects(:mkdir).with(dir)
94
@store.expects(:writelock).yields(file)
95
file.expects(:print).with(yaml)
101
describe Puppet::Indirector::Yaml, " when retrieving YAML" do
102
it "should read YAML in from disk using a read lock and convert it to Ruby objects" do
103
path = @store.send(:path, @subject.name)
105
yaml = @subject.to_yaml
106
FileTest.expects(:exist?).with(path).returns(true)
108
fh = mock 'filehandle'
109
@store.expects(:readlock).with(path).yields fh
110
fh.expects(:read).returns yaml
112
@store.find(@request).instance_variable_get("@name").should == :me
115
it "should fail coherently when the stored YAML is invalid" do
116
path = @store.send(:path, @subject.name)
117
FileTest.expects(:exist?).with(path).returns(true)
119
# Something that will fail in yaml
120
yaml = "--- !ruby/object:Hash"
122
fh = mock 'filehandle'
123
@store.expects(:readlock).yields fh
124
fh.expects(:read).returns yaml
126
proc { @store.find(@request) }.should raise_error(Puppet::Error)
130
describe Puppet::Indirector::Yaml, " when searching" do
131
it "should return an array of fact instances with one instance for each file when globbing *" do
132
@request = stub 'request', :key => "*", :instance => @subject
135
@store.expects(:path).with(@request.key,'').returns :glob
136
Dir.expects(:glob).with(:glob).returns(%w{one.yaml two.yaml})
137
YAML.expects(:load_file).with("one.yaml").returns @one;
138
YAML.expects(:load_file).with("two.yaml").returns @two;
139
@store.search(@request).should == [@one, @two]
142
it "should return an array containing a single instance of fact when globbing 'one*'" do
143
@request = stub 'request', :key => "one*", :instance => @subject
145
@store.expects(:path).with(@request.key,'').returns :glob
146
Dir.expects(:glob).with(:glob).returns(%w{one.yaml})
147
YAML.expects(:load_file).with("one.yaml").returns @one;
148
@store.search(@request).should == [@one]
151
it "should return an empty array when the glob doesn't match anything" do
152
@request = stub 'request', :key => "f*ilglobcanfail*", :instance => @subject
153
@store.expects(:path).with(@request.key,'').returns :glob
154
Dir.expects(:glob).with(:glob).returns []
155
@store.search(@request).should == []