summaryrefslogtreecommitdiffstats
path: root/test/lib/support/utils.rb
blob: 9aea05affeed39c902110572309dec3ad02720ac (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
module PuppetTestSupport    
    module Utils
        def gcdebug(type)
            Puppet.warning "%s: %s" % [type, ObjectSpace.each_object(type) { |o| }]
        end

        #
        # TODO: I think this method needs to be renamed to something a little more explanatory.
        #

        def newobj(type, name, hash)
            transport = Puppet::TransObject.new(name, "file")
            transport[:path] = path
            transport[:ensure] = "file"
            assert_nothing_raised {
                file = transport.to_type
            }
            end

        # stop any services that might be hanging around
        def stopservices
            if stype = Puppet::Type.type(:service)
                stype.each { |service|
                    service[:ensure] = :stopped
                    service.evaluate
                }
                    end
        end

        # TODO: rewrite this to use the 'etc' module.

        def setme
            # retrieve the user name
            id = %x{id}.chomp
            if id =~ /uid=\d+\(([^\)]+)\)/
                @me = $1
            else
                puts id
            end
            unless defined? @me
                raise "Could not retrieve user name; 'id' did not work"
            end
        end

        def run_events(type, trans, events, msg)
            case type
            when :evaluate, :rollback: # things are hunky-dory
            else
                raise Puppet::DevError, "Incorrect run_events type"
            end

            method = type

            newevents = nil
            assert_nothing_raised("Transaction %s %s failed" % [type, msg]) {
                newevents = trans.send(method).reject { |e| e.nil? }.collect { |e|
                e.event
            }
            }

            assert_equal(events, newevents, "Incorrect %s %s events" % [type, msg])

            return trans
        end

        # If there are any fake data files, retrieve them
        def fakedata(dir)
            ary = [$puppetbase, "test"]
            ary += dir.split("/")
            dir = File.join(ary)

            unless FileTest.exists?(dir)
                raise Puppet::DevError, "No fakedata dir %s" % dir
            end
            files = Dir.entries(dir).reject { |f| f =~ /^\./ }.collect { |f|
                File.join(dir, f)
            }

            return files
        end

        def fakefile(name)
            ary = [$puppetbase, "test"]
            ary += name.split("/")
            file = File.join(ary)
            unless FileTest.exists?(file)
                raise Puppet::DevError, "No fakedata file %s" % file
            end
            return file
        end

        # wrap how to retrieve the masked mode
        def filemode(file)
            File.stat(file).mode & 007777
        end

        def memory
            Puppet::Util.memory
        end

        # a list of files that we can parse for testing
        def textfiles
            textdir = File.join($puppetbase,"examples","code", "snippets")
            Dir.entries(textdir).reject { |f|
                f =~ /^\./ or f =~ /fail/
            }.each { |f|
                    yield File.join(textdir, f)
                }
        end

        def failers
            textdir = File.join($puppetbase,"examples","code", "failers")
            # only parse this one file now
            files = Dir.entries(textdir).reject { |file|
                file =~ %r{\.swp}
            }.reject { |file|
                    file =~ %r{\.disabled}
                }.collect { |file|
                        File.join(textdir,file)
                    }.find_all { |file|
                        FileTest.file?(file)
                    }.sort.each { |file|
                        Puppet.debug "Processing %s" % file
                        yield file
                    }
        end

        def newcomp(*ary)
            name = nil
            if ary[0].is_a?(String)
                name = ary.shift
            else
                name = ary[0].title
            end

            comp = Puppet.type(:component).create(
                                                  :name => name
                                                 )
                                                 ary.each { |item|
                                                     comp.push item
                                                 }

                                                 return comp
        end
    end
end