summaryrefslogtreecommitdiffstats
path: root/spec/unit/other/checksum.rb
blob: 6a63e833d6a04d9d87536c8b90497fa5f686db78 (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
#!/usr/bin/env ruby
#
#  Created by Luke Kanies on 2007-9-22.
#  Copyright (c) 2007. All rights reserved.

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/checksum'

describe Puppet::Checksum do
    it "should have 'Checksum' and the checksum algorithm when converted to a string" do
        inst = Puppet::Checksum.new("whatever", "md5")
        inst.to_s.should == "Checksum<{md5}#{inst.checksum}>"
    end

    it "should convert algorithm names to symbols when they are set after checksum creation" do
        sum = Puppet::Checksum.new("whatever")
        sum.algorithm = "md5"
        sum.algorithm.should == :md5
    end

    it "should return the checksum as the name" do
        sum = Puppet::Checksum.new("whatever")
        sum.checksum.should == sum.name
    end
end

describe Puppet::Checksum, " when initializing" do
    before do
        @content = "this is some content"
        @sum = Puppet::Checksum.new(@content)
    end

    it "should require content" do
        proc { Puppet::Checksum.new(nil) }.should raise_error(ArgumentError)
    end

    it "should set the content appropriately" do
        @sum.content.should == @content
    end

    it "should calculate the checksum" do
        require 'digest/md5'
        Digest::MD5.expects(:hexdigest).with(@content).returns(:mychecksum)
        @sum.checksum.should == :mychecksum
    end

    it "should not calculate the checksum until it is asked for" do
        require 'digest/md5'
        Digest::MD5.expects(:hexdigest).never
        sum = Puppet::Checksum.new(@content, :md5)
    end

    it "should remove the old checksum value if the algorithm is changed" do
        Digest::MD5.expects(:hexdigest).with(@content).returns(:oldsum)
        oldsum = @sum.checksum
        @sum.algorithm = :sha1
        Digest::SHA1.expects(:hexdigest).with(@content).returns(:newsum)
        @sum.checksum.should == :newsum
    end

    it "should default to 'md5' as the checksum algorithm if the algorithm is not in the name" do
        @sum.algorithm.should == :md5
    end

    it "should support specifying the algorithm during initialization" do
        sum = Puppet::Checksum.new(@content, :sha1)
        sum.algorithm.should == :sha1
    end

    it "should fail when an unsupported algorithm is used" do
        proc { Puppet::Checksum.new(@content, :nope) }.should raise_error(ArgumentError)
    end
end

describe Puppet::Checksum, " when using back-ends" do
    it "should redirect using Puppet::Indirector" do
        Puppet::Indirector::Indirection.instance(:checksum).model.should equal(Puppet::Checksum)
    end

    it "should have a :save instance method" do
        Puppet::Checksum.new("mysum").should respond_to(:save)
    end

    it "should respond to :find" do
        Puppet::Checksum.should respond_to(:find)
    end

    it "should respond to :destroy" do
        Puppet::Checksum.should respond_to(:destroy)
    end
end