|
| 1 | +// +build !integration |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + dto "github.com/prometheus/client_model/go" |
| 7 | + . "gopkg.in/check.v1" |
| 8 | +) |
| 9 | + |
| 10 | +type PgSettingSuite struct{} |
| 11 | + |
| 12 | +var _ = Suite(&PgSettingSuite{}) |
| 13 | + |
| 14 | +var fixtures = []fixture{ |
| 15 | + fixture{ |
| 16 | + p: pgSetting{ |
| 17 | + name: "seconds_fixture_metric", |
| 18 | + setting: "5", |
| 19 | + unit: "s", |
| 20 | + shortDesc: "Foo foo foo", |
| 21 | + vartype: "integer", |
| 22 | + }, |
| 23 | + n: normalised{ |
| 24 | + val: 5, |
| 25 | + unit: "seconds", |
| 26 | + err: "", |
| 27 | + }, |
| 28 | + d: "Desc{fqName: \"pg_settings_seconds_fixture_metric_seconds\", help: \"Foo foo foo [Units converted to seconds.]\", constLabels: {}, variableLabels: []}", |
| 29 | + v: 5, |
| 30 | + }, |
| 31 | + fixture{ |
| 32 | + p: pgSetting{ |
| 33 | + name: "milliseconds_fixture_metric", |
| 34 | + setting: "5000", |
| 35 | + unit: "ms", |
| 36 | + shortDesc: "Foo foo foo", |
| 37 | + vartype: "integer", |
| 38 | + }, |
| 39 | + n: normalised{ |
| 40 | + val: 5, |
| 41 | + unit: "seconds", |
| 42 | + err: "", |
| 43 | + }, |
| 44 | + d: "Desc{fqName: \"pg_settings_milliseconds_fixture_metric_seconds\", help: \"Foo foo foo [Units converted to seconds.]\", constLabels: {}, variableLabels: []}", |
| 45 | + v: 5, |
| 46 | + }, |
| 47 | + fixture{ |
| 48 | + p: pgSetting{ |
| 49 | + name: "eight_kb_fixture_metric", |
| 50 | + setting: "17", |
| 51 | + unit: "8kB", |
| 52 | + shortDesc: "Foo foo foo", |
| 53 | + vartype: "integer", |
| 54 | + }, |
| 55 | + n: normalised{ |
| 56 | + val: 139264, |
| 57 | + unit: "bytes", |
| 58 | + err: "", |
| 59 | + }, |
| 60 | + d: "Desc{fqName: \"pg_settings_eight_kb_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}", |
| 61 | + v: 139264, |
| 62 | + }, |
| 63 | + fixture{ |
| 64 | + p: pgSetting{ |
| 65 | + name: "16_mb_real_fixture_metric", |
| 66 | + setting: "3.0", |
| 67 | + unit: "16MB", |
| 68 | + shortDesc: "Foo foo foo", |
| 69 | + vartype: "real", |
| 70 | + }, |
| 71 | + n: normalised{ |
| 72 | + val: 5.0331648e+07, |
| 73 | + unit: "bytes", |
| 74 | + err: "", |
| 75 | + }, |
| 76 | + d: "Desc{fqName: \"pg_settings_16_mb_real_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}", |
| 77 | + v: 5.0331648e+07, |
| 78 | + }, |
| 79 | + fixture{ |
| 80 | + p: pgSetting{ |
| 81 | + name: "bool_on_fixture_metric", |
| 82 | + setting: "on", |
| 83 | + unit: "", |
| 84 | + shortDesc: "Foo foo foo", |
| 85 | + vartype: "bool", |
| 86 | + }, |
| 87 | + n: normalised{ |
| 88 | + val: 1, |
| 89 | + unit: "", |
| 90 | + err: "", |
| 91 | + }, |
| 92 | + d: "Desc{fqName: \"pg_settings_bool_on_fixture_metric\", help: \"Foo foo foo\", constLabels: {}, variableLabels: []}", |
| 93 | + v: 1, |
| 94 | + }, |
| 95 | + fixture{ |
| 96 | + p: pgSetting{ |
| 97 | + name: "bool_off_fixture_metric", |
| 98 | + setting: "off", |
| 99 | + unit: "", |
| 100 | + shortDesc: "Foo foo foo", |
| 101 | + vartype: "bool", |
| 102 | + }, |
| 103 | + n: normalised{ |
| 104 | + val: 0, |
| 105 | + unit: "", |
| 106 | + err: "", |
| 107 | + }, |
| 108 | + d: "Desc{fqName: \"pg_settings_bool_off_fixture_metric\", help: \"Foo foo foo\", constLabels: {}, variableLabels: []}", |
| 109 | + v: 0, |
| 110 | + }, |
| 111 | + fixture{ |
| 112 | + p: pgSetting{ |
| 113 | + name: "special_minus_one_value", |
| 114 | + setting: "-1", |
| 115 | + unit: "d", |
| 116 | + shortDesc: "foo foo foo", |
| 117 | + vartype: "integer", |
| 118 | + }, |
| 119 | + n: normalised{ |
| 120 | + val: -1, |
| 121 | + unit: "seconds", |
| 122 | + err: "", |
| 123 | + }, |
| 124 | + d: "Desc{fqName: \"pg_settings_special_minus_one_value_seconds\", help: \"foo foo foo [Units converted to seconds.]\", constLabels: {}, variableLabels: []}", |
| 125 | + v: -1, |
| 126 | + }, |
| 127 | + fixture{ |
| 128 | + p: pgSetting{ |
| 129 | + name: "unknown_unit", |
| 130 | + setting: "10", |
| 131 | + unit: "nonexistent", |
| 132 | + shortDesc: "foo foo foo", |
| 133 | + vartype: "integer", |
| 134 | + }, |
| 135 | + n: normalised{ |
| 136 | + val: 10, |
| 137 | + unit: "", |
| 138 | + err: `Unknown unit for runtime variable: "nonexistent"`, |
| 139 | + }, |
| 140 | + }, |
| 141 | +} |
| 142 | + |
| 143 | +func (s *PgSettingSuite) TestNormaliseUnit(c *C) { |
| 144 | + for _, f := range fixtures { |
| 145 | + switch f.p.vartype { |
| 146 | + case "integer", "real": |
| 147 | + val, unit, err := f.p.normaliseUnit() |
| 148 | + |
| 149 | + c.Check(val, Equals, f.n.val) |
| 150 | + c.Check(unit, Equals, f.n.unit) |
| 151 | + |
| 152 | + if err == nil { |
| 153 | + c.Check("", Equals, f.n.err) |
| 154 | + } else { |
| 155 | + c.Check(err.Error(), Equals, f.n.err) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func (s *PgSettingSuite) TestMetric(c *C) { |
| 162 | + defer func() { |
| 163 | + if r := recover(); r != nil { |
| 164 | + if r.(error).Error() != `Unknown unit for runtime variable: "nonexistent"` { |
| 165 | + panic(r) |
| 166 | + } |
| 167 | + } |
| 168 | + }() |
| 169 | + |
| 170 | + for _, f := range fixtures { |
| 171 | + d := &dto.Metric{} |
| 172 | + m := f.p.metric() |
| 173 | + m.Write(d) |
| 174 | + |
| 175 | + c.Check(m.Desc().String(), Equals, f.d) |
| 176 | + c.Check(d.GetGauge().GetValue(), Equals, f.v) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +type normalised struct { |
| 181 | + val float64 |
| 182 | + unit string |
| 183 | + err string |
| 184 | +} |
| 185 | + |
| 186 | +type fixture struct { |
| 187 | + p pgSetting |
| 188 | + n normalised |
| 189 | + d string |
| 190 | + v float64 |
| 191 | +} |
0 commit comments