|
| 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 |
0 commit comments