|
2 | 2 | require 'spec_helper'
|
3 | 3 |
|
4 | 4 | require 'yaml'
|
| 5 | +require 'fileutils' |
5 | 6 | require 'puppet/util/storage'
|
6 | 7 |
|
7 | 8 | describe Puppet::Util::Storage do
|
8 | 9 | include PuppetSpec::Files
|
9 | 10 |
|
10 |
| - before(:all) do |
11 |
| - @basepath = make_absolute("/somepath") |
12 |
| - Puppet[:statedir] = tmpdir("statedir") |
13 |
| - end |
14 |
| - |
15 |
| - after(:all) do |
16 |
| - Puppet.settings.clear |
17 |
| - end |
18 |
| - |
19 | 11 | before(:each) do
|
20 |
| - Puppet::Util::Storage.clear |
| 12 | + @basepath = File.expand_path("/somepath") |
21 | 13 | end
|
22 | 14 |
|
23 | 15 | describe "when caching a symbol" do
|
|
40 | 32 | end
|
41 | 33 |
|
42 | 34 | describe "when caching a Puppet::Type" do
|
43 |
| - before(:all) do |
| 35 | + before(:each) do |
44 | 36 | @file_test = Puppet::Type.type(:file).new(:name => @basepath+"/yayness", :audit => %w{checksum type})
|
45 | 37 | @exec_test = Puppet::Type.type(:exec).new(:name => @basepath+"/bin/ls /yayness")
|
46 | 38 | end
|
|
81 | 73 |
|
82 | 74 | describe "when the state file/directory does not exist" do
|
83 | 75 | before(:each) do
|
84 |
| - transient = Tempfile.new('storage_test') |
85 |
| - @path = transient.path() |
86 |
| - transient.close!() |
| 76 | + @path = tmpfile('storage_test') |
87 | 77 | end
|
88 | 78 |
|
89 |
| - it "should not fail to load()" do |
| 79 | + it "should not fail to load" do |
90 | 80 | FileTest.exists?(@path).should be_false
|
91 | 81 | Puppet[:statedir] = @path
|
92 |
| - expect { Puppet::Util::Storage.load }.to_not raise_error |
| 82 | + Puppet::Util::Storage.load |
93 | 83 | Puppet[:statefile] = @path
|
94 |
| - expect { Puppet::Util::Storage.load }.to_not raise_error |
| 84 | + Puppet::Util::Storage.load |
95 | 85 | end
|
96 | 86 |
|
97 | 87 | it "should not lose its internal state when load() is called" do
|
|
109 | 99 |
|
110 | 100 | describe "when the state file/directory exists" do
|
111 | 101 | before(:each) do
|
112 |
| - @state_file = Tempfile.new('storage_test') |
113 |
| - @saved_statefile = Puppet[:statefile] |
114 |
| - Puppet[:statefile] = @state_file.path |
| 102 | + @state_file = tmpfile('storage_test') |
| 103 | + FileUtils.touch(@state_file) |
| 104 | + Puppet[:statefile] = @state_file |
| 105 | + end |
| 106 | + |
| 107 | + def write_state_file(contents) |
| 108 | + File.open(@state_file, 'w') { |f| f.write(contents) } |
115 | 109 | end
|
116 | 110 |
|
117 | 111 | it "should overwrite its internal state if load() is called" do
|
|
126 | 120 |
|
127 | 121 | it "should restore its internal state if the state file contains valid YAML" do
|
128 | 122 | test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}}
|
129 |
| - @state_file.write(test_yaml.to_yaml) |
130 |
| - @state_file.flush |
| 123 | + write_state_file(test_yaml.to_yaml) |
131 | 124 |
|
132 | 125 | Puppet::Util::Storage.load
|
133 | 126 |
|
134 | 127 | Puppet::Util::Storage.state.should == test_yaml
|
135 | 128 | end
|
136 | 129 |
|
137 | 130 | it "should initialize with a clear internal state if the state file does not contain valid YAML" do
|
138 |
| - @state_file.write('{ invalid') |
139 |
| - @state_file.flush |
| 131 | + write_state_file('{ invalid') |
140 | 132 |
|
141 | 133 | Puppet::Util::Storage.load
|
142 | 134 |
|
143 | 135 | Puppet::Util::Storage.state.should == {}
|
144 | 136 | end
|
145 | 137 |
|
146 | 138 | it "should initialize with a clear internal state if the state file does not contain a hash of data" do
|
147 |
| - @state_file.write("not_a_hash") |
148 |
| - @state_file.flush |
| 139 | + write_state_file("not_a_hash") |
149 | 140 |
|
150 | 141 | Puppet::Util::Storage.load
|
151 | 142 |
|
152 | 143 | Puppet::Util::Storage.state.should == {}
|
153 | 144 | end
|
154 | 145 |
|
155 | 146 | it "should raise an error if the state file does not contain valid YAML and cannot be renamed" do
|
156 |
| - @state_file.write('{ invalid') |
157 |
| - @state_file.flush |
| 147 | + write_state_file('{ invalid') |
158 | 148 |
|
159 | 149 | File.expects(:rename).raises(SystemCallError)
|
160 | 150 |
|
161 | 151 | expect { Puppet::Util::Storage.load }.to raise_error(Puppet::Error, /Could not rename/)
|
162 | 152 | end
|
163 | 153 |
|
164 | 154 | it "should attempt to rename the state file if the file is corrupted" do
|
165 |
| - @state_file.write('{ invalid') |
166 |
| - @state_file.flush |
| 155 | + write_state_file('{ invalid') |
167 | 156 |
|
168 | 157 | File.expects(:rename).at_least_once
|
169 | 158 |
|
170 | 159 | Puppet::Util::Storage.load
|
171 | 160 | end
|
172 | 161 |
|
173 | 162 | it "should fail gracefully on load() if the state file is not a regular file" do
|
174 |
| - @state_file.close!() |
175 |
| - Dir.mkdir(Puppet[:statefile]) |
| 163 | + FileUtils.rm_f(@state_file) |
| 164 | + Dir.mkdir(@state_file) |
176 | 165 |
|
177 | 166 | Puppet::Util::Storage.load
|
178 |
| - |
179 |
| - Dir.rmdir(Puppet[:statefile]) |
180 |
| - end |
181 |
| - |
182 |
| - after(:each) do |
183 |
| - @state_file.close!() |
184 |
| - Puppet[:statefile] = @saved_statefile |
185 | 167 | end
|
186 | 168 | end
|
187 | 169 | end
|
|
0 commit comments