File: storage.rb

package info (click to toggle)
puppet 0.20.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,072 kB
  • ctags: 3,365
  • sloc: ruby: 47,009; sh: 496; lisp: 143; xml: 122; makefile: 67
file content (103 lines) | stat: -rwxr-xr-x 2,450 bytes parent folder | download
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
#!/usr/bin/env ruby

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

require 'puppet'
require 'puppettest'

class TestStorage < Test::Unit::TestCase
	include PuppetTest

    def mkfile
        path = tempfile()
        File.open(path, "w") { |f| f.puts :yayness }

        f = Puppet.type(:file).create(
            :name => path,
            :check => %w{checksum type}
        )

        return f
    end

    def test_storeandretrieve
        path = tempfile()

        f = mkfile()

        # Load first, since that's what we do in the code base; this creates
        # all of the necessary directories.
        assert_nothing_raised {
            Puppet::Storage.load
        }

        hash = {:a => :b, :c => :d}

        state = nil
        assert_nothing_raised {
            state = Puppet::Storage.cache(f)
        }

        assert(!state.include?("name"))

        assert_nothing_raised {
            state["name"] = hash
        }

        assert_nothing_raised {
            Puppet::Storage.store
        }
        assert_nothing_raised {
            Puppet::Storage.clear
        }
        assert_nothing_raised {
            Puppet::Storage.load
        }

        # Reset it
        state = nil
        assert_nothing_raised {
            state = Puppet::Storage.cache(f)
        }

        assert_equal(state["name"], hash)
    end

    # we're getting corrupt files, probably because multiple processes
    # are reading or writing the file at once
    # so we need to test that
    def test_multiwrite
        f = mkfile()

        value = {:a => :b}
        threads = []
        9.times { |a|
            threads << Thread.new {
                9.times { |b|
                    assert_nothing_raised {
                        Puppet::Storage.load
                        state = Puppet::Storage.cache(f)
                        value.each { |k,v| state[k] = v }
                        state[:e] = rand(100)
                        Puppet::Storage.store
                    }
                }
            }
        }
        threads.each { |th| th.join }
    end

    def test_emptyrestore
        Puppet::Storage.load
        Puppet::Storage.store
        Puppet::Storage.clear
        Puppet::Storage.load

        f = mkfile()
        state = Puppet::Storage.cache(f)
        assert_same Hash, state.class
        assert_equal 0, state.size
    end
end

# $Id: storage.rb 1793 2006-10-16 22:01:40Z luke $