Skip to content

Commit

Permalink
Merge pull request #5 from kevinjqiu/more-descriptive-test-names
Browse files Browse the repository at this point in the history
Friendlier test names
  • Loading branch information
kevinjqiu authored Jun 20, 2018
2 parents 03f44d6 + c3f3fd6 commit 1cf3c77
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 16 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ Top level attributes
--------------------

* `name` - The name of the test case
* [`rules`](#Rules) - The rule definitions that are under test
* [`fixtures`](#Fixtures) - The fixture setup for the tests
* [`assertions`](#Assertions) - The test assertions
* [`rules`](#rules) - The rule definitions that are under test
* [`fixtures`](#fixtures) - The fixture setup for the tests
* [`assertions`](#assertions) - The test assertions

Rules
-----
Expand Down Expand Up @@ -198,9 +198,10 @@ assertions:
Run the test:

```bash
$ ./pat test/test.yaml
=== RUN Test_HTTP_Requests_too_low_alert_0
--- PASS: Test_HTTP_Requests_too_low_alert_0 (0.00s)
=== RUN Test_HTTP_Requests_too_low_alert_1
--- PASS: Test_HTTP_Requests_too_low_alert_1 (0.00s)
$ ./pat examples/test.yaml
=== RUN Test_HTTP_Requests_too_low_alert_at_0m
--- PASS: Test_HTTP_Requests_too_low_alert_at_0m (0.00s)
=== RUN Test_HTTP_Requests_too_low_alert_at_5m
--- PASS: Test_HTTP_Requests_too_low_alert_at_5m (0.00s)
PASS
```
4 changes: 4 additions & 0 deletions examples/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ assertions:
instance: "1"
job: app-server
severity: critical
comment: |-
At 0m, the alerts met the threshold but has not met the duration requirement. Expect the alert to be pending
- at: 5m
expected:
- alertname: HTTPRequestRateLow
Expand All @@ -35,3 +37,5 @@ assertions:
instance: "1"
job: app-server
severity: critical
comment: |-
At 5m, the alerts should be firing because the duration requirement is met.
8 changes: 4 additions & 4 deletions pkg/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/ruleloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (ruleLoader RuleLoader) Load() ([]*rules.Group, error) {
return nil, fmt.Errorf("Must specify a rule loading strategy")
}

retval := []*rules.Group{}
var retval []*rules.Group
for _, rg := range ruleGroups.Groups {
rls := make([]rules.Rule, 0, len(rg.Rules))
for _, r := range rg.Rules {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ruletest.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func (prt PromRuleTest) getTestCaseName(assertionIdx int) string {
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%s_%d", reg.ReplaceAllString(prt.Name, "_"), assertionIdx)
assertion := prt.Assertions[assertionIdx]
return fmt.Sprintf("%s_at_%s", reg.ReplaceAllString(prt.Name, "_"), assertion.At)
}

func (prt PromRuleTest) newTestCase(idx int, assertion Assertion, resultAlerts []map[string]string) TestCase {
Expand Down
15 changes: 13 additions & 2 deletions pkg/ruletest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ import (
)

func TestGetTestCaseName(t *testing.T) {
prt := PromRuleTest{Name: "Test HTTP Requests too low alert"}
assert.Equal(t, "Test_HTTP_Requests_too_low_alert_1", prt.getTestCaseName(1))
prt := PromRuleTest{
Name: "Test HTTP Requests too low alert",
Assertions: []Assertion{
{
At: "5m",
},
{
At: "10m",
},
},
}
assert.Equal(t, "Test_HTTP_Requests_too_low_alert_at_5m", prt.getTestCaseName(0))
assert.Equal(t, "Test_HTTP_Requests_too_low_alert_at_10m", prt.getTestCaseName(1))
}

func TestNewPromRuleTestFromFile(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
"additionalProperties": false,
"definitions": {
"Assertion": {
"additionalProperties": false,
"properties": {
"at": {
"$ref": "#/definitions/Duration"
},
"comment": {
"type": "string"
},
"expected": {
"items": {
"$ref": "#/definitions/Expectation"
},
"type": "array"
}
},
"required": [
"at",
"expected"
],
"type": "object"
},
"Duration": {
Expand Down
6 changes: 6 additions & 0 deletions pkg/schema/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ definitions:
type: array
items:
$ref: "#/definitions/Expectation"
comment:
type: string
required:
- at
- expected
additionalProperties: false
Duration:
type: string
pattern: "^\\d+(ns|us|ms|s|m|h)$"
Expand Down

0 comments on commit 1cf3c77

Please sign in to comment.