Skip to content

Commit 5c3f412

Browse files
committed
Moving Chef::ChecsumCache to Chef::Cache::Checksum
* Fixes bug where initialize was spelled wrong
1 parent fd8a792 commit 5c3f412

6 files changed

Lines changed: 149 additions & 101 deletions

File tree

chef/lib/chef/cache.rb

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class Cache
3131

3232
attr_reader :moneta
3333

34-
def initalize(*args)
35-
reset!(*args)
34+
def initialize(*args)
35+
self.reset!(*args)
3636
end
3737

3838
def reset!(backend=nil, options=nil)
@@ -45,55 +45,11 @@ def reset!(backend=nil, options=nil)
4545
Chef::Log.fatal("Could not load Moneta back end #{backend.inspect}")
4646
raise e
4747
end
48-
48+
4949
@moneta = Moneta.const_get(backend).new(options)
5050
end
5151

5252
end
53-
54-
class ChecksumCache < Cache
55-
56-
def self.checksum_for_file(*args)
57-
instance.checksum_for_file(*args)
58-
end
59-
60-
def checksum_for_file(file)
61-
key, fstat = filename_to_key(file), File.stat(file)
62-
lookup_checksum(key, fstat) || generate_checksum(key, file, fstat)
63-
end
64-
65-
def lookup_checksum(key, fstat)
66-
cached = moneta.fetch(key)
67-
if cached && file_unchanged?(cached, fstat)
68-
cached["checksum"]
69-
else
70-
nil
71-
end
72-
end
73-
74-
def generate_checksum(key, file, fstat)
75-
checksum = checksum_file(file)
76-
moneta.store(key, {"mtime" => fstat.mtime.to_f, "checksum" => checksum})
77-
checksum
78-
end
79-
80-
private
81-
82-
def file_unchanged?(cached, fstat)
83-
cached["mtime"].to_f == fstat.mtime.to_f
84-
end
85-
86-
def checksum_file(file)
87-
digest = Digest::SHA256.new
88-
IO.foreach(file) {|line| digest.update(line) }
89-
digest.hexdigest
90-
end
91-
92-
def filename_to_key(file)
93-
"chef-file-#{file.gsub(/(#{File::SEPARATOR}|\.)/, '-')}"
94-
end
95-
96-
end
9753
end
9854

9955
module Moneta

chef/lib/chef/cache/checksum.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Author:: Daniel DeLeo (<dan@kallistec.com>)
4+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
5+
# Copyright:: Copyright (c) 2009 Daniel DeLeo
6+
# License:: Apache License, Version 2.0
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
21+
require 'chef/cache'
22+
23+
class Chef
24+
class Cache
25+
class Checksum < Chef::Cache
26+
27+
def self.checksum_for_file(*args)
28+
instance.checksum_for_file(*args)
29+
end
30+
31+
def checksum_for_file(file)
32+
key, fstat = filename_to_key(file), File.stat(file)
33+
lookup_checksum(key, fstat) || generate_checksum(key, file, fstat)
34+
end
35+
36+
def lookup_checksum(key, fstat)
37+
cached = @moneta.fetch(key)
38+
if cached && file_unchanged?(cached, fstat)
39+
cached["checksum"]
40+
else
41+
nil
42+
end
43+
end
44+
45+
def generate_checksum(key, file, fstat)
46+
checksum = checksum_file(file)
47+
moneta.store(key, {"mtime" => fstat.mtime.to_f, "checksum" => checksum})
48+
checksum
49+
end
50+
51+
private
52+
53+
def file_unchanged?(cached, fstat)
54+
cached["mtime"].to_f == fstat.mtime.to_f
55+
end
56+
57+
def checksum_file(file)
58+
digest = Digest::SHA256.new
59+
IO.foreach(file) {|line| digest.update(line) }
60+
digest.hexdigest
61+
end
62+
63+
def filename_to_key(file)
64+
"chef-file-#{file.gsub(/(#{File::SEPARATOR}|\.)/, '-')}"
65+
end
66+
67+
end
68+
end
69+
end
70+

chef/lib/chef/mixin/checksum.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
#
1818

1919
require 'digest/sha2'
20-
require 'chef/cache'
20+
require 'chef/cache/checksum'
2121

2222
class Chef
2323
module Mixin
2424
module Checksum
2525

2626
def checksum(file)
27-
Chef::ChecksumCache.checksum_for_file(file)
27+
Chef::Cache::Checksum.checksum_for_file(file)
2828
end
2929

3030
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Author:: Daniel DeLeo (<dan@kallistec.com>)
4+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
5+
# Copyright:: Copyright (c) 2009 Daniel DeLeo
6+
# License:: Apache License, Version 2.0
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
21+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
22+
23+
describe Chef::Cache::Checksum do
24+
25+
before do
26+
@cache = Chef::Cache::Checksum.instance
27+
@cache.reset!("Memory", {})
28+
end
29+
30+
it "proxies the class method checksum_for_file to the instance" do
31+
@cache.should_receive(:checksum_for_file).with("a_file_or_a_fail")
32+
Chef::Cache::Checksum.checksum_for_file("a_file_or_a_fail")
33+
end
34+
35+
it "returns a cached checksum value" do
36+
@cache.moneta["chef-file-riseofthemachines"] = {"mtime" => "12345", "checksum" => "123abc"}
37+
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(12345))
38+
File.should_receive(:stat).with("riseofthemachines").and_return(fstat)
39+
@cache.checksum_for_file("riseofthemachines").should == "123abc"
40+
end
41+
42+
it "gives nil for a cache miss" do
43+
@cache.moneta["chef-file-riseofthemachines"] = {"mtime" => "12345", "checksum" => "123abc"}
44+
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(555555))
45+
@cache.lookup_checksum("chef-file-riseofthemachines", fstat).should be_nil
46+
end
47+
48+
it "treats a non-matching mtime as a cache miss" do
49+
@cache.moneta["chef-file-riseofthemachines"] = {"mtime" => "12345", "checksum" => "123abc"}
50+
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(555555))
51+
@cache.lookup_checksum("chef-file-riseofthemachines", fstat).should be_nil
52+
end
53+
54+
it "computes a checksum of a file" do
55+
fixture_file = File.dirname(__FILE__) + "/../../data/checksum/random.txt"
56+
expected = "09ee9c8cc70501763563bcf9c218d71b2fbf4186bf8e1e0da07f0f42c80a3394"
57+
@cache.send(:checksum_file, fixture_file).should == expected
58+
end
59+
60+
it "computes a checksum and stores it in the cache" do
61+
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(555555))
62+
@cache.should_receive(:checksum_file).with("riseofthemachines").and_return("ohai2uChefz")
63+
@cache.generate_checksum("chef-file-riseofthemachines", "riseofthemachines", fstat).should == "ohai2uChefz"
64+
@cache.lookup_checksum("chef-file-riseofthemachines", fstat).should == "ohai2uChefz"
65+
end
66+
67+
it "returns a generated checksum if there is no cached value" do
68+
fixture_file = File.dirname(__FILE__) + "/../../data/checksum/random.txt"
69+
expected = "09ee9c8cc70501763563bcf9c218d71b2fbf4186bf8e1e0da07f0f42c80a3394"
70+
@cache.checksum_for_file(fixture_file).should == expected
71+
end
72+
73+
end

chef/spec/unit/cache_spec.rb

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,3 @@
4747

4848
end
4949

50-
describe Chef::ChecksumCache do
51-
52-
before do
53-
@cache = Chef::ChecksumCache.instance
54-
@cache.reset!("Memory", {})
55-
end
56-
57-
it "proxies the class method checksum_for_file to the instance" do
58-
@cache.should_receive(:checksum_for_file).with("a_file_or_a_fail")
59-
Chef::ChecksumCache.checksum_for_file("a_file_or_a_fail")
60-
end
61-
62-
it "returns a cached checksum value" do
63-
@cache.moneta["chef-file-riseofthemachines"] = {"mtime" => "12345", "checksum" => "123abc"}
64-
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(12345))
65-
File.should_receive(:stat).with("riseofthemachines").and_return(fstat)
66-
@cache.checksum_for_file("riseofthemachines").should == "123abc"
67-
end
68-
69-
it "gives nil for a cache miss" do
70-
@cache.moneta["chef-file-riseofthemachines"] = {"mtime" => "12345", "checksum" => "123abc"}
71-
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(555555))
72-
@cache.lookup_checksum("chef-file-riseofthemachines", fstat).should be_nil
73-
end
74-
75-
it "treats a non-matching mtime as a cache miss" do
76-
@cache.moneta["chef-file-riseofthemachines"] = {"mtime" => "12345", "checksum" => "123abc"}
77-
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(555555))
78-
@cache.lookup_checksum("chef-file-riseofthemachines", fstat).should be_nil
79-
end
80-
81-
it "computes a checksum of a file" do
82-
fixture_file = File.dirname(__FILE__) + "/../data/checksum/random.txt"
83-
expected = "09ee9c8cc70501763563bcf9c218d71b2fbf4186bf8e1e0da07f0f42c80a3394"
84-
@cache.send(:checksum_file, fixture_file).should == expected
85-
end
86-
87-
it "computes a checksum and stores it in the cache" do
88-
fstat = mock("File.stat('riseofthemachines')", :mtime => Time.at(555555))
89-
@cache.should_receive(:checksum_file).with("riseofthemachines").and_return("ohai2uChefz")
90-
@cache.generate_checksum("chef-file-riseofthemachines", "riseofthemachines", fstat).should == "ohai2uChefz"
91-
@cache.lookup_checksum("chef-file-riseofthemachines", fstat).should == "ohai2uChefz"
92-
end
93-
94-
it "returns a generated checksum if there is no cached value" do
95-
fixture_file = File.dirname(__FILE__) + "/../data/checksum/random.txt"
96-
expected = "09ee9c8cc70501763563bcf9c218d71b2fbf4186bf8e1e0da07f0f42c80a3394"
97-
@cache.checksum_for_file(fixture_file).should == expected
98-
end
99-
100-
end

chef/spec/unit/mixin/checksum_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Chef::CMCCheck
2727
describe Chef::Mixin::Checksum do
2828
before(:each) do
2929
@checksum_user = Chef::CMCCheck.new
30-
@cache = Chef::ChecksumCache.instance
30+
@cache = Chef::Cache::Checksum.instance
3131
@file = File.dirname(__FILE__) + "/../../data/checksum/random.txt"
3232
@stat = mock("File::Stat", { :mtime => Time.at(0) })
3333
File.stub!(:stat).and_return(@stat)

0 commit comments

Comments
 (0)