summaryrefslogtreecommitdiffstats
path: root/test/ral/types/filebucket.rb
blob: 094a5da2bcf281d8b6036ea7f0e2d0030aa330eb (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env ruby

$:.unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppettest'
require 'fileutils'

class TestFileBucket < Test::Unit::TestCase
    include PuppetTest::FileTesting
    # hmmm
    # this is complicated, because we store references to the created
    # objects in a central store
    def mkfile(hash)
        file = nil
        assert_nothing_raised {
            file = Puppet.type(:file).create(hash)
        }
        return file
    end

    def mkbucket(name,path)
        bucket = nil
        assert_nothing_raised {
            bucket = Puppet.type(:filebucket).create(
                :name => name,
                :path => path
            )
        }

        @@tmpfiles.push path

        return bucket
    end

    def mktestfile
        # because luke's home directory is on nfs, it can't be used for testing
        # as root
        tmpfile = tempfile()
        File.open(tmpfile, "w") { |f| f.puts rand(100) }
        @@tmpfiles.push tmpfile
        mkfile(:name => tmpfile)
    end

    def setup
        super
        begin
            initstorage
        rescue
            system("rm -rf %s" % Puppet[:statefile])
        end
    end

    def initstorage
        Puppet::Util::Storage.init
        Puppet::Util::Storage.load
    end

    def clearstorage
        Puppet::Util::Storage.store
        Puppet::Util::Storage.clear
    end

    def test_simplebucket
        name = "yayness"
        bucketpath = tempfile()
        mkbucket(name, bucketpath)

        bucket = nil
        assert_nothing_raised {
            bucket = Puppet.type(:filebucket).bucket(name)
        }

        assert_instance_of(Puppet::Network::Client.dipper, bucket)

        md5 = nil
        newpath = tempfile()
        @@tmpfiles << newpath
        system("cp /etc/passwd %s" % newpath)
        assert_nothing_raised {
            md5 = bucket.backup(newpath)
        }

        assert(md5)

        dir, file, pathfile = Puppet::Network::Handler.filebucket.paths(bucketpath, md5)

        assert(FileTest.directory?(dir),
            "MD5 directory does not exist")

        newmd5 = nil

        # Just in case the file isn't writable
        File.chmod(0644, newpath)
        File.open(newpath, "w") { |f| f.puts ";lkjasdf;lkjasdflkjwerlkj134lkj" }

        assert_nothing_raised {
            newmd5 = bucket.backup(newpath)
        }

        assert(md5 != newmd5)

        assert_nothing_raised {
            bucket.restore(newpath, md5)
        }

        File.open(newpath) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }

        assert_equal(md5, newmd5)
    end

    def test_fileswithbuckets
        name = "yayness"
        mkbucket(name, tempfile())

        bucket = nil
        assert_nothing_raised {
            bucket = Puppet.type(:filebucket).bucket(name)
        }

        file = mktestfile()
        assert_nothing_raised {
            file[:backup] = name
        }

        opath = tempfile()
        @@tmpfiles << opath
        File.open(opath, "w") { |f| f.puts "yaytest" }

        origmd5 = File.open(file.name) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }

        file[:source] = opath
        #assert_nothing_raised {
        #    file[:backup] = true
        #}

        assert_apply(file)

        # so, we've now replaced the file with the opath file
        assert_equal(
            File.open(opath) { |f| newmd5 = Digest::MD5.hexdigest(f.read) },
            File.open(file.name) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }
        )

        #File.chmod(0644, file.name)
        assert_nothing_raised {
            bucket.restore(file.name, origmd5)
        }

        assert_equal(
            origmd5,
            File.open(file.name) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }
        )
    end
end