~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to test/ral/type/filebucket.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
require File.dirname(__FILE__) + '/../../lib/puppettest'
4
 
 
5
 
require 'puppettest'
6
 
require 'puppettest/support/utils'
7
 
require 'fileutils'
8
 
 
9
 
class TestFileBucket < Test::Unit::TestCase
10
 
    include PuppetTest::Support::Utils
11
 
    include PuppetTest::FileTesting
12
 
    # hmmm
13
 
    # this is complicated, because we store references to the created
14
 
    # objects in a central store
15
 
    def mkfile(hash)
16
 
        file = nil
17
 
        assert_nothing_raised {
18
 
            file = Puppet.type(:file).create(hash)
19
 
        }
20
 
        return file
21
 
    end
22
 
 
23
 
    def mkbucket(name,path)
24
 
        bucket = nil
25
 
        assert_nothing_raised {
26
 
            bucket = Puppet.type(:filebucket).create(
27
 
                :name => name,
28
 
                :path => path
29
 
            )
30
 
        }
31
 
 
32
 
        @@tmpfiles.push path
33
 
 
34
 
        return bucket
35
 
    end
36
 
 
37
 
    def mktestfile
38
 
        # because luke's home directory is on nfs, it can't be used for testing
39
 
        # as root
40
 
        tmpfile = tempfile()
41
 
        File.open(tmpfile, "w") { |f| f.puts rand(100) }
42
 
        @@tmpfiles.push tmpfile
43
 
        mkfile(:name => tmpfile)
44
 
    end
45
 
 
46
 
    def setup
47
 
        super
48
 
        begin
49
 
            initstorage
50
 
        rescue
51
 
            system("rm -rf %s" % Puppet[:statefile])
52
 
        end
53
 
    end
54
 
 
55
 
    def initstorage
56
 
        Puppet::Util::Storage.init
57
 
        Puppet::Util::Storage.load
58
 
    end
59
 
 
60
 
    def clearstorage
61
 
        Puppet::Util::Storage.store
62
 
        Puppet::Util::Storage.clear
63
 
    end
64
 
 
65
 
    def test_simplebucket
66
 
        name = "yayness"
67
 
        bucketpath = tempfile()
68
 
        mkbucket(name, bucketpath)
69
 
 
70
 
        bucket = nil
71
 
        assert_nothing_raised {
72
 
            bucket = Puppet.type(:filebucket).bucket(name)
73
 
        }
74
 
 
75
 
        assert_instance_of(Puppet::Network::Client.dipper, bucket)
76
 
 
77
 
        md5 = nil
78
 
        newpath = tempfile()
79
 
        @@tmpfiles << newpath
80
 
        system("cp /etc/passwd %s" % newpath)
81
 
        assert_nothing_raised {
82
 
            md5 = bucket.backup(newpath)
83
 
        }
84
 
 
85
 
        assert(md5)
86
 
 
87
 
        dir, file, pathfile = Puppet::Network::Handler.filebucket.paths(bucketpath, md5)
88
 
 
89
 
        assert(FileTest.directory?(dir),
90
 
            "MD5 directory does not exist")
91
 
 
92
 
        newmd5 = nil
93
 
 
94
 
        # Just in case the file isn't writable
95
 
        File.chmod(0644, newpath)
96
 
        File.open(newpath, "w") { |f| f.puts ";lkjasdf;lkjasdflkjwerlkj134lkj" }
97
 
 
98
 
        assert_nothing_raised {
99
 
            newmd5 = bucket.backup(newpath)
100
 
        }
101
 
 
102
 
        assert(md5 != newmd5)
103
 
 
104
 
        assert_nothing_raised {
105
 
            bucket.restore(newpath, md5)
106
 
        }
107
 
 
108
 
        File.open(newpath) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }
109
 
 
110
 
        assert_equal(md5, newmd5)
111
 
    end
112
 
 
113
 
    def test_fileswithbuckets
114
 
        name = "yayness"
115
 
        mkbucket(name, tempfile())
116
 
 
117
 
        bucket = nil
118
 
        assert_nothing_raised {
119
 
            bucket = Puppet.type(:filebucket).bucket(name)
120
 
        }
121
 
 
122
 
        file = mktestfile()
123
 
        assert_nothing_raised {
124
 
            file[:backup] = name
125
 
        }
126
 
 
127
 
        opath = tempfile()
128
 
        @@tmpfiles << opath
129
 
        File.open(opath, "w") { |f| f.puts "yaytest" }
130
 
 
131
 
        origmd5 = File.open(file.name) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }
132
 
 
133
 
        file[:source] = opath
134
 
        #assert_nothing_raised {
135
 
        #    file[:backup] = true
136
 
        #}
137
 
 
138
 
        assert_apply(file)
139
 
 
140
 
        # so, we've now replaced the file with the opath file
141
 
        assert_equal(
142
 
            File.open(opath) { |f| newmd5 = Digest::MD5.hexdigest(f.read) },
143
 
            File.open(file.name) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }
144
 
        )
145
 
 
146
 
        #File.chmod(0644, file.name)
147
 
        assert_nothing_raised {
148
 
            bucket.restore(file.name, origmd5)
149
 
        }
150
 
 
151
 
        assert_equal(
152
 
            origmd5,
153
 
            File.open(file.name) { |f| newmd5 = Digest::MD5.hexdigest(f.read) }
154
 
        )
155
 
    end
156
 
end
157