summaryrefslogtreecommitdiffstats
path: root/test/providers/parsedmount.rb
blob: a4bcb753472c111de5ac16f23040cdba59042bcd (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env ruby

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

require 'puppettest'
require 'puppet'
require 'facter'

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

    def setup
        super
        @provider = Puppet.type(:mount).provider(:parsed)

        @oldfiletype = @provider.filetype
    end

    def teardown
        Puppet::FileType.filetype(:ram).clear
        @provider.filetype = @oldfiletype
        super
    end

    def mkmountargs
        mount = nil

        if defined? @pcount
            @pcount += 1
        else
            @pcount = 1
        end
        args = {
            :path => "/fspuppet%s" % @pcount,
            :device => "/dev/dsk%s" % @pcount,
        }

        @provider.fields.each do |field|
            unless args.include? field
                args[field] = "fake%s" % @pcount
            end
        end

        return args
    end

    def mkmount
        hash = mkmountargs()
        #hash[:provider] = @provider.name

        fakemodel = fakemodel(:mount, hash[:path])

        mount = @provider.new(fakemodel)
        #mount = Puppet.type(:mount).create(hash)

        hash.each do |name, val|
            fakemodel[name] = val
        end
        assert(mount, "Could not create provider mount")

        return mount
    end

    # Here we just create a fake host type that answers to all of the methods
    # but does not modify our actual system.
    def mkfaketype
        @provider.filetype = Puppet::FileType.filetype(:ram)
    end

    def test_simplemount
        mkfaketype
        assert_nothing_raised {
            assert_equal([], @provider.retrieve)
        }

        # Now create a provider
        mount = nil
        assert_nothing_raised {
            mount = mkmount
        }

        # Make sure we're still empty
        assert_nothing_raised {
            assert_equal([], @provider.retrieve)
        }

        hash = mount.model.to_hash

        # Try storing it
        assert_nothing_raised do
            mount.store(hash)
        end

        # Make sure we get the mount back
        assert_nothing_raised {
            assert_equal([hash], @provider.retrieve)
        }

        # Now remove the whole object
        assert_nothing_raised {
            mount.store({})
            assert_equal([], @provider.retrieve)
        }
    end

    unless Facter["operatingsystem"].value == "Darwin"
        def test_mountsparse
            fakedataparse(fake_fstab) do
                # Now just make we've got some mounts we know will be there
                hashes = @provider.retrieve.find_all { |i| i.is_a? Hash }
                assert(hashes.length > 0, "Did not create any hashes")
                root = hashes.find { |i| i[:path] == "/" }
                assert(root, "Could not retrieve root mount")
            end
        end

        def test_rootfs
            fs = nil
            @provider.path = fake_fstab()
            fakemodel = fakemodel(:mount, "/")
            mount = @provider.new(fakemodel)
            mount.model[:path] = "/"
            assert(mount.hash, "Could not retrieve root fs")

            assert_nothing_raised {
                assert(mount.mounted?, "Root is considered not mounted")
            }
        end
    end

    if Puppet::SUIDManager.uid == 0
    def test_mountfs
        fs = nil
        case Facter["hostname"].value
        when "culain": fs = "/ubuntu"
        when "atalanta": fs = "/mnt"
        when "figurehead": fs = "/cg4/net/depts"
        else
            $stderr.puts "No mount for mount testing; skipping"
            return
        end

        oldtext = @provider.fileobj.read

        ftype = @provider.filetype

        # Make sure the original gets reinstalled.
        if ftype == Puppet::FileType.filetype(:netinfo)
            cleanup do 
                IO.popen("niload -r /mounts .", "w") do |file|
                    file.puts oldtext
                end
            end
        else
            cleanup do 
                @provider.fileobj.write(oldtext)
            end
        end

        fakemodel = fakemodel(:mount, "/")
        obj = @provider.new(fakemodel)
        obj.model[:path] = fs

        current = nil

        assert_nothing_raised {
            current = obj.mounted?
        }

        if current
            # Make sure the original gets reinstalled.
            cleanup do
                unless obj.mounted?
                    obj.mount
                end
            end
        end

        unless current
            assert_nothing_raised {
                obj.mount
            }
        end

        assert_nothing_raised {
            obj.unmount
        }
        assert(! obj.mounted?, "FS still mounted")
        assert_nothing_raised {
            obj.mount
        }
        assert(obj.mounted?, "FS not mounted")

    end
    end

    def fake_fstab
        os = Facter['operatingsystem']
        if os == "Solaris"
            name = "solaris.fstab"
        elsif os == "FreeBSD"
            name = "freebsd.fstab"
        else
            # Catchall for other fstabs
            name = "linux.fstab"
        end
        oldpath = @provider.path
        cleanup do @provider.path = oldpath end
        return fakefile(File::join("data/types/mount", name))
    end
end

# $Id$