summaryrefslogtreecommitdiffstats
path: root/tests/tc_simple.rb
blob: cda25573f1f46a07c524fdefead51fcbdd68ceed (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#! /usr/bin/env ruby
# $Id$
#     
if __FILE__ == $0 # Make this library first!
    $:.unshift '../lib'
end

require 'test/unit'
require 'facter'

if __FILE__ == $0
    Facter.debugging(true)
end

class TestFacter < Test::Unit::TestCase
    def setup
        Facter.load
    end

    def teardown
        # clear out the list of facts, so we start fresh for every test
        Facter.reset
    end

    def test_version 
        #Could match /[0-9.]+/
        #Strict match: /^[0-9]+(\.[0-9]+)*$/
        #ok: 1.0.0 1.0 1
        #notok: 1..0 1. .1 1a
        assert(Facter.version =~ /^[0-9]+(\.[0-9]+)*$/ )
    end

    def test_notags_sh
        assert_nothing_raised {
            Facter["testing"].add { |fact|
                fact.code = "echo yup"
            }
        }

        assert_equal("yup", Facter["testing"].value)
    end

    def test_notags
        assert_nothing_raised {
            Facter["testing"].add { |fact|
                fact.code = proc { "foo" }
            }
        }

        assert_equal("foo", Facter["testing"].value)
    end

    def test_onetruetag
        assert_nothing_raised {
            Facter["required"].add { |fact|
                fact.code = proc { "foo" }
            }
            Facter["testing"].add { |fact|
                fact.code = proc { "bar" }
                fact.tag("required","=","foo")
            }
        }

        assert_equal("bar", Facter["testing"].value)
    end

    def test_onefalsetag
        assert_nothing_raised {
            Facter["required"].add { |fact|
                fact.code = proc { "foo" }
            }
            Facter["testing"].add { |fact|
                fact.code = proc { "bar" }
                fact.tag("required","=","bar")
            }
        }

        assert_equal(nil, Facter["testing"].value)
    end

    def test_recursivetags
        assert_nothing_raised {
            Facter["required"].add { |fact|
                fact.code = proc { "foo" }
                fact.tag("testing","=","foo")
            }
            Facter["testing"].add { |fact|
                fact.code = proc { "bar" }
                fact.tag("required","=","foo")
            }
        }

        assert_equal(nil, Facter["testing"].value)
    end

    def test_multipleresolves
        assert_nothing_raised {
            Facter["funtest"].add { |fact|
                fact.code = proc { "untagged" }
            }
            Facter["funtest"].add { |fact|
                fact.code = proc { "tagged" }
                fact.tag("operatingsystem","=", Facter["operatingsystem"].value)
            }
        }

        assert_equal("tagged", Facter["funtest"].value)
    end

	def test_osname
		assert_equal(
            %x{uname -s}.chomp,
			Facter["operatingsystem"].value
		)
	end

	def test_osrel
		assert_equal(
            %x{uname -r}.chomp,
			Facter["operatingsystemrelease"].value
		)
	end

	def test_hostname
		assert_equal(
            %x{hostname}.chomp.sub(/\..+/,''),
			Facter["hostname"].value
		)
	end

	def test_upcase
        Facter["Testing"].add { |fact|
            fact.code = proc { "foo" }
        }
		assert_equal(
			"foo",
			Facter["Testing"].value
		)
	end

	def test_doublecall
        Facter["testing"].add { |fact|
            fact.code = proc { "foo" }
        }
		assert_equal(
			Facter["testing"].value,
			Facter["testing"].value
		)
	end

	def test_downcase
        Facter["testing"].add { |fact|
            fact.code = proc { "foo" }
        }
		assert_equal(
			"foo",
			Facter["testing"].value
		)
	end

	def test_case_insensitivity
        Facter["Testing"].add { |fact|
            fact.code = proc { "foo" }
        }
        upcase = Facter["Testing"].value
        downcase = Facter["testing"].value
		assert_equal(upcase, downcase)
	end

    def test_adding
        assert_nothing_raised() {
            Facter["Funtest"].add { |obj|
                obj.code = proc { return "funtest value" }
            }
        }

        assert_equal(
            "funtest value",
            Facter["funtest"].value
        )

        assert_nothing_raised() {
            code = proc { return "yaytest value" }
            block = proc { |obj| obj.code = code }
            Facter["Yaytest"].add(&block)
        }

        assert_equal(
            "yaytest value",
            Facter["yaytest"].value
        )
    end

    def test_comparison
        assert(
            %x{uname -s}.chomp == Facter["operatingsystem"].value
        )
        assert(
            %x{hostname}.chomp.sub(/\..+/,'') == Facter["hostname"].value
        )
    end

    def test_adding2
        assert_nothing_raised() {
            Facter["bootest"].add { |obj|
                obj.tag("operatingsystem", "=", Facter["operatingsystem"].value)
                obj.code = "echo bootest"
            }
        }

        assert_equal(
            "bootest",
            Facter["bootest"].value
        )

        assert_nothing_raised() {
            Facter["bahtest"].add { |obj|
                #obj.os = Facter["operatingsystem"].value
                #obj.release = Facter["operatingsystemrelease"].value
                obj.tag("operatingsystem", "=", Facter["operatingsystem"].value)
                obj.tag("operatingsystemrelease", "=",
                    Facter["operatingsystemrelease"].value)
                obj.code = "echo bahtest"
            }
        }

        assert_equal(
            "bahtest",
            Facter["bahtest"].value
        )

        assert_nothing_raised() {
            Facter["failure"].add { |obj|
                #obj.os = Facter["operatingsystem"].value
                #obj.release = "FakeRelease"
                obj.tag("operatingsystem", "=", Facter["operatingsystem"].value)
                obj.tag("operatingsystemrelease", "=", "FakeRelease")
                obj.code = "echo failure"
            }
        }

        assert_equal(
            nil,
            Facter["failure"].value
        )
    end

    def test_distro
        if Facter["operatingsystem"] == "Linux"
            assert(Facter["Distro"])
        end
    end

    def test_each
        list = {}
        assert_nothing_raised {
            Facter.each { |name,fact|
                list[name] = fact
            }
        }

        list.each { |name,value|
            assert(value.class != Facter)
            assert(name)
            assert(value)
        }
    end
end