Skip to content

Commit 9f838c2

Browse files
committed
Add some proper testing
1 parent 23c82f9 commit 9f838c2

File tree

9 files changed

+66
-6
lines changed

9 files changed

+66
-6
lines changed

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ AllCops:
66
- 'Rakefile'
77
- 'vendor/**/*'
88
NewCops: enable
9+
SuggestExtensions: false
910

1011
# Don't enforce documentation
1112
Style/Documentation:

lib/grafana_matrix/renderer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Renderer
1212
}.freeze
1313

1414
def render(data, rule, template)
15-
erb = ERB.new template, 0, '-'
15+
erb = ERB.new template, trim_mode: '-'
1616
erb.result(binding)
1717
end
1818

lib/grafana_matrix/templates/html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<%- if data['imageUrl'] && rule.details? && data['state'] != 'ok' -%>
33
<details><summary>
44
<%- unless data.fetch('message', '').empty? -%>
5-
<%= data['message'] %>
5+
<%= data['message'] %><br/>
66
<%- end -%>
77
<%- else -%>
88
<%- unless data.fetch('message', '').empty? -%>

test/fixtures/message-details.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h4><a href="https://grafana.example.com/panel-url" style="font-weight: bold; font-style: italic"><font color="#ed2e18">[Alerting] IO wait alert</font></a></h4>
2+
<details><summary>
3+
IO wait percentage averaged above 10% the last five minutes<br/>
4+
<b>node.example.com</b>: 14.45<br/>
5+
<b>node2.example.com</b>: 11.22<br/>
6+
</summary>
7+
<a href="https://grafana.example.com/panel-url"><img src="https://grafana.example.com/image.png" alt="" /></a>
8+
</details>

test/fixtures/message.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h4><a href="https://grafana.example.com/panel-url" style="font-weight: bold; font-style: italic"><font color="#ed2e18">[Alerting] IO wait alert</font></a></h4>
2+
<p>IO wait percentage averaged above 10% the last five minutes</p>
3+
<b>node.example.com</b>: 14.45<br/>
4+
<b>node2.example.com</b>: 11.22<br/>
5+
<br/>
6+
<a href="https://grafana.example.com/panel-url"><img src="https://grafana.example.com/image.png" alt="" /></a>

test/fixtures/message.plain

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Alerting] IO wait alert
2+
IO wait percentage averaged above 10% the last five minutes
3+
4+
node.example.com: 14.45
5+
node2.example.com: 11.22
6+
7+
Image: https://grafana.example.com/image.png
8+
URL: https://grafana.example.com/panel-url

test/grafana_matrix_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "test_helper"
1+
require 'test_helper'
22

33
class GrafanaMatrixTest < Minitest::Test
44
def test_that_it_has_a_version_number

test/renderer_test.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'test_helper'
2+
3+
class RendererTest < Minitest::Test
4+
ALERTDATA = {
5+
'title' => '[Alerting] IO wait alert',
6+
'message' => 'IO wait percentage averaged above 10% the last five minutes',
7+
'state' => 'alerting',
8+
'evalMatches' => [
9+
{ 'metric' => 'node.example.com', 'value' => 14.45 },
10+
{ 'metric' => 'node2.example.com', 'value' => 11.22 },
11+
],
12+
'imageUrl' => 'https://grafana.example.com/image.png',
13+
'ruleUrl' => 'https://grafana.example.com/panel-url'
14+
}.freeze
15+
ALERTRULE = GrafanaMatrix::Config::Rule.new(nil, {}).freeze
16+
ALERTRULE_DETAILS = GrafanaMatrix::Config::Rule.new(nil, { details_tag: true }).freeze
17+
18+
def setup
19+
@renderer = GrafanaMatrix::Renderer.new
20+
end
21+
22+
def test_that_it_renders_plain
23+
assert_equal load_fixture('message.plain').read, @renderer.render_plain(ALERTDATA, ALERTRULE)
24+
end
25+
26+
def test_that_it_renders_html
27+
assert_equal load_fixture('message.html').read, @renderer.render_html(ALERTDATA, ALERTRULE)
28+
end
29+
30+
def test_that_it_renders_html_with_details
31+
assert_equal load_fixture('message-details.html').read, @renderer.render_html(ALERTDATA, ALERTRULE_DETAILS)
32+
end
33+
end

test/test_helper.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2-
require "grafana_matrix"
1+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2+
require 'grafana_matrix'
33

4-
require "minitest/autorun"
4+
require 'minitest/autorun'
5+
6+
def load_fixture(filename)
7+
File.open(File.join('test', 'fixtures', filename))
8+
end

0 commit comments

Comments
 (0)