summaryrefslogtreecommitdiffstats
path: root/test/other/tc_metrics.rb
blob: 9db8683e921a2a4e9a7c19e100c373c3bf470c52 (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
if __FILE__ == $0
    $:.unshift '..'
    $:.unshift '../../lib'
    $puppetbase = "../.."
end

require 'puppet/metric'
require 'puppet'
require 'puppet/type'
require 'test/unit'

# $Id$

$haverrd = true
begin
    require 'RRD'
rescue LoadError
    $haverrd = false
end

if $haverrd
    class TestMetric < Test::Unit::TestCase

        def gendata
            totalmax = 1000
            changemax = 1000
            eventmax = 10
            maxdiff = 10

            types = [Puppet::Type::PFile, Puppet::Type::Package, Puppet::Type::Service]
            data = [:total, :managed, :outofsync, :changed, :totalchanges]
            events = [:file_changed, :package_installed, :service_started]

            # if this is the first set of data points...
            typedata = Hash.new { |typehash,type|
                typehash[type] = Hash.new(0)
            }
            eventdata = Hash.new(0)
            types.each { |type|
                name = type.name
                typedata[type] = {}
                typedata[type][:total] = rand(totalmax)
                typedata[type][:managed] = rand(typedata[type][:total])
                typedata[type][:outofsync] = rand(typedata[type][:managed])
                typedata[type][:changed] = rand(typedata[type][:outofsync])
                typedata[type][:totalchanges] = rand(changemax)
            }

            events.each { |event|
                eventdata[event] = rand(eventmax)
            }

            return [typedata,eventdata]
        end

        def setup
            Puppet[:rrddir] = File.join(Puppet[:puppetvar], "rrdtesting")
            Puppet[:rrdgraph] = true
            Puppet[:loglevel] = :debug if __FILE__ == $0
        end

        def teardown
            system("rm -rf %s" % Puppet[:rrddir])
        end

        def test_fakedata
            assert_nothing_raised { Puppet::Metric.init }
            time = Time.now.to_i
            start = time
            10.times {
                assert_nothing_raised { Puppet::Metric.load(gendata) }
                assert_nothing_raised { Puppet::Metric.tally }
                assert_nothing_raised { Puppet::Metric.store(time) }
                assert_nothing_raised { Puppet::Metric.clear }
                time += 300
            }
            assert_nothing_raised { Puppet::Metric.load(gendata) }
            assert_nothing_raised { Puppet::Metric.tally }
            assert_nothing_raised { Puppet::Metric.store(time) }
            assert_nothing_raised { Puppet::Metric.graph([start,time]) }

            File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of|
                of.puts "<html><body>"
                Puppet::Metric.each { |metric|
                    of.puts "<img src=%s.png><br>" % metric.name
                }
            }
        end
    end
else
    $stderr.puts "Missing RRD library -- skipping metric tests"
end