summaryrefslogtreecommitdiffstats
path: root/test/util/metrics.rb
blob: b0ac1e2f5f49da25ccac2ef2f558f6b52ea109f9 (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
#!/usr/bin/env ruby

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

require 'puppet'
require 'puppet/util/metric'
require 'puppettest'
require 'puppet/type'

class TestMetric < PuppetTest::TestCase
    confine "Missing RRDtool library" =>  Puppet.features.rrd?
    include PuppetTest

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

        types = [Puppet.type(:file), Puppet.type(:package), Puppet.type(:package)]
        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)
        typedata = {}
        typedata[:total] = rand(totalmax)
        typedata[:managed] = rand(typedata[:total])
        typedata[:outofsync] = rand(typedata[:managed])
        typedata[:changed] = rand(typedata[:outofsync])
        typedata[:totalchanges] = rand(changemax)

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

        return {:typedata => typedata, :eventdata => eventdata}
    end

    def rundata(report, time)
        assert_nothing_raised {
            gendata.each do |name, data|
                report.newmetric(name, data)
            end
            report.metrics.each { |n, m| m.store(time) }
        }
    end

    def setup
        super
        Puppet[:rrdgraph] = true
    end

    def test_fakedata
        report = Puppet::Transaction::Report.new
        time = Time.now.to_i
        start = time
        10.times {
            rundata(report, time)
            time += 300
        }
        rundata(report, time)

        report.metrics.each do |n, m| m.graph end

        File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of|
            of.puts "<html><body>"
            report.metrics.each { |name, metric|
                of.puts "<img src=%s.png><br>" % metric.name
            }
        }
    end
end