summaryrefslogtreecommitdiffstats
path: root/test/ral/providers/port/parsed.rb
blob: d295f04e6714f633fe727109e55d771132b7e85d (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../lib/puppettest'

require 'puppettest'
#require 'puppettest/fileparsing'
#require 'puppet/type/port'
#require 'test/unit'
#require 'facter'
#
#class TestParsedPort < Test::Unit::TestCase
#	include PuppetTest
#	include PuppetTest::FileParsing
#
#    def setup
#        super
#        @provider = Puppet::Type.type(:port).provider(:parsed)
#        @oldfiletype = @provider.filetype
#    end
#
#    def teardown
#        Puppet::Util::FileType.filetype(:ram).clear
#        @provider.filetype = @oldfiletype
#        @provider.clear
#        super
#    end
#    
#    # Generate a line from a hash.  The line might include '\n'.
#    def genline(hash)
#        line = [hash[:name], "#{hash[:number]}/%s"].join("\t\t")
#        if hash[:alias]
#            line += "\t\t" + hash[:alias].join(" ")
#        end
#        if hash[:description]
#            line += "\t# " + hash[:description]
#        end
#
#        return hash[:protocols].collect { |p| line % p }.join("\n")
#    end
#
#    # Parse our sample data and make sure we regenerate it correctly.
#    def test_portsparse
#        files = fakedata("data/types/port")
#        files.each do |file|
#            oldtarget = @provider.default_target
#            cleanup do
#                @provider.default_target = oldtarget
#            end
#            @provider.default_target = file
#
#            assert_nothing_raised("failed to fetch %s" % file) {
#                @provider.prefetch
#            }
#
#            hashes = @provider.target_records(file).find_all { |i| i.is_a? Hash }
#            assert(hashes.length > 0, "Did not create any hashes")
#            dns = hashes.find { |i| i[:name] == "domain" }
#
#            assert(dns, "Did not retrieve dns record")
#            assert_equal("53", dns[:number], "dns number is wrong")
#
#            text = nil
#            assert_nothing_raised("failed to generate %s" % file) do 
#                text = @provider.to_file(@provider.target_records(file))
#            end
#
#            oldlines = File.readlines(file)
#            newlines = text.chomp.split "\n"
#            regex = /^(\S+)\s+(\d+)\/(\w+)/
#            oldlines.zip(newlines).each do |old, new|
#                if omatch = regex.match(old) 
#                    assert(newmatch = regex.match(new),
#                        "Lines were not equivalent: %s vs %s" %
#                        [old.inspect, new.inspect]
#                    )
#                    oldfields = omatch.captures and
#                    newfields = newmatch.captures
#
#                    assert_equal(oldfields, newfields,
#                        "Lines were not equivalent: %s vs %s" %
#                        [old.inspect, new.inspect]
#                    )
#                end
#            #    assert_equal(old.chomp.gsub(/\s+/, ''),
#            #        new.gsub(/\s+/, ''),
#            #        "Lines are not equal in %s" % file)
#            end
#        end
#    end
#
#    # Try parsing the different forms of lines
#    def test_parsing
#        # Each of the different possible values for each field.
#        options = {
#            :name => "service",
#            :number => "1",
#            :alias => [nil, ["null"], %w{null sink}, %w{null sink other}],
#            :description => [nil, "my description"],
#            :protocols => [%w{tcp}, %w{udp}, %w{tcp udp}]
#        }
#
#        # Now go through all of the different iterations and make sure we
#        # parse them correctly.
#        keys = options.keys
#
#        name = options[:name]
#        number = options[:number]
#        options[:alias].each do |al|
#            options[:description].each do |desc|
#                options[:protocols].each do |proto|
#                    hash = {:name => name, :number => number, :alias => al,
#                        :description => desc, :protocols => proto}
#                    line = genline(hash)
#
#                    # Try parsing it
#                    record = nil
#                    assert_nothing_raised do
#                        record = @provider.parse_line(line)
#                    end
#                    assert(record, "Did not get record returned")
#                    hash.each do |param, value|
#                        if value
#                            assert_equal(value, record[param],
#                                "did not get %s out of '%s'" % [param, line])
#                        end
#                    end
#
#                    # Now make sure it generates correctly
#                    assert_equal(line, @provider.to_line(record),
#                        "Did not generate %s correctly" % line)
#                end
#            end
#        end
#    end
#
#    # Make sure we correctly join lines by name, so that they're considered
#    # a single record.
#    def test_lines
#        result = nil
#        assert_nothing_raised do
#            result = @provider.lines(
#"smtp        25/tcp      mail
#time        37/tcp      timserver
#time        37/udp      timserver
#rlp     39/udp      resource    # resource location
#tacacs      49/tcp              # Login Host Protocol (TACACS)
#nameserver  42/tcp      name        # IEN 116
#whois       43/tcp      nicname
#tacacs      49/udp
#re-mail-ck  50/tcp              # Remote Mail Checking Protocol
#domain      53/tcp      nameserver  # name-domain server
#re-mail-ck  50/udp
#domain      53/udp      nameserver"
#            )
#        end
#
#        assert_equal([
#"smtp        25/tcp      mail",
#"time        37/tcp      timserver
#time        37/udp      timserver",
#"rlp     39/udp      resource    # resource location",
#"tacacs      49/tcp              # Login Host Protocol (TACACS)
#tacacs      49/udp",
#"nameserver  42/tcp      name        # IEN 116",
#"whois       43/tcp      nicname",
#"re-mail-ck  50/tcp              # Remote Mail Checking Protocol
#re-mail-ck  50/udp",
#"domain      53/tcp      nameserver  # name-domain server
#domain      53/udp      nameserver"
#], result)
#
#    end
#
#    # Make sure we correctly handle port merging.
#    def test_port_merge
#        fields = [:name, :number, :protocols, :alias, :description]
#        base = %w{a 1}
#
#        z = proc { |ary| h = {}; fields.zip(ary) { |p,v| h[p] = v if v }; h }
#
#        # Make sure our zipper is working
#        assert_equal({:name => "a", :number => "1", :protocols => %w{tcp udp}},
#            z.call(["a", "1", %w{tcp udp}])
#        )
#
#        # Here we go through the different options, just testing each key
#        # separately.
#        {
#            # The degenerate case - just two protocols
#            [%w{tcp udp}] => [[%w{tcp}], [%w{udp}]],
#
#            # one alias
#            [%w{tcp udp}, %w{A}] => [[%w{tcp}, %w{A}], [%w{udp}]],
#
#            # Other side
#            [%w{tcp udp}, %w{A}] => [[%w{tcp}], [%w{udp}], %w{A}],
#
#            # Both
#            [%w{tcp udp}, %w{A}] => [[%w{tcp}, %w{A}], [%w{udp}], %w{A}],
#
#            # Adding aliases
#            [%w{tcp udp}, %w{A B}] => [[%w{tcp}, %w{A}], [%w{udp}], %w{B}],
#
#            # Merging aliases
#            [%w{tcp udp}, %w{A B}] => [[%w{tcp}, %w{A B}], [%w{udp}], %w{B}],
#
#            # One description
#            [%w{tcp udp}, nil, "desc"] => [[%w{tcp}, nil, "desc"], [%w{udp}] ],
#
#            # other side
#            [%w{tcp udp}, nil, "desc"] => [[%w{tcp}], [%w{udp}, nil, "desc"] ],
#
#            # Conflicting -- first hash wins
#            [%w{tcp udp}, nil, "first"] =>
#                [[%w{tcp}, nil, "first"], [%w{udp}, nil, "desc"] ],
#        }.each do |result, hashes|
#            assert_equal(
#                z.call(base + result),
#                @provider.port_merge(
#                    z.call(base + hashes[0]),
#                    z.call(base + hashes[1])
#                ),
#                "Did not get %s out of %s + %s" % [
#                    result.inspect,
#                    hashes[0].inspect,
#                    hashes[1].inspect
#                ]
#            )
#        end
#    end
#end