summaryrefslogtreecommitdiff
path: root/chef/spec/unit/checksum_spec.rb
blob: c8b405194f9bc838379b3d976b2d4605ad0157f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Copyright:: Copyright (c) 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'chef/checksum'

describe Chef::Checksum do

  before do
    Chef::Log.logger = Logger.new(StringIO.new)

    @now = Time.now

    Time.stub!(:now).and_return(@now)

    @checksum_of_the_file = "3fafecfb15585ede6b840158cbc2f399"
    @checksum = Chef::Checksum.new(@checksum_of_the_file)
  end

  it "has no original committed file location" do
    @checksum.original_committed_file_location.should be_nil
  end

  it "has the MD5 checksum of the file it represents" do
    @checksum.checksum.should == @checksum_of_the_file
  end

  it "stores the time it was created" do
    @checksum.create_time.should == @now.iso8601
  end

  it "has the path to the file in the checksum repo" do
    @checksum.file_location.should == "/var/chef/checksums/3f/3fafecfb15585ede6b840158cbc2f399"
  end

  it "has the path the the file's subdirectory in the checksum repo" do
    @checksum.checksum_repo_directory.should == "/var/chef/checksums/3f"
  end

  it "commits a sandbox file from a given location to the checksum repo location" do
    File.should_receive(:rename).with("/tmp/arbitrary_file_location", @checksum.file_location)
    FileUtils.should_receive(:mkdir_p).with("/var/chef/checksums/3f")
    
    @checksum.should_receive(:cdb_save)
    @checksum.commit_sandbox_file("/tmp/arbitrary_file_location")
    @checksum.original_committed_file_location.should == "/tmp/arbitrary_file_location"
  end

  it "reverts committing a sandbox file" do
    File.should_receive(:rename).with("/tmp/arbitrary_file_location", @checksum.file_location)
    FileUtils.should_receive(:mkdir_p).with("/var/chef/checksums/3f")
    @checksum.should_receive(:cdb_save)
    @checksum.commit_sandbox_file("/tmp/arbitrary_file_location")
    @checksum.original_committed_file_location.should == "/tmp/arbitrary_file_location"

    File.should_receive(:rename).with(@checksum.file_location, "/tmp/arbitrary_file_location")
    @checksum.should_receive(:cdb_destroy)
    @checksum.revert_sandbox_file_commit
  end

  it "raises an error when trying to revert a checksum that was not previously committed" do
    lambda {@checksum.revert_sandbox_file_commit}.should raise_error(Chef::Exceptions::IllegalChecksumRevert)
  end

  it "deletes the file and its document from couchdb" do
    @checksum.should_receive(:cdb_destroy)
    FileUtils.should_receive(:rm).with(@checksum.file_location)
    @checksum.purge
  end

  it "successfully purges even if its file has been deleted from the repo" do
    @checksum.should_receive(:cdb_destroy)
    FileUtils.should_receive(:rm).with(@checksum.file_location).and_raise(Errno::ENOENT)
    lambda {@checksum.purge}.should_not raise_error
  end

  describe "when converted to json" do
    before do
      @checksum_as_json = @checksum.to_json
      @checksum_as_hash_from_json = Chef::JSON.from_json(@checksum_as_json, :create_additions => false)
    end

    it "contains the file's MD5 checksum" do
      @checksum_as_hash_from_json["checksum"].should == @checksum_of_the_file
    end

    it "contains the creation time" do
      @checksum_as_hash_from_json["create_time"].should == @now.iso8601
    end

    it "uses the file's MD5 checksum for its 'name' property" do
      @checksum_as_hash_from_json["name"].should == @checksum_of_the_file
    end
  end

end