Skip to content

Commit

Permalink
chore: move examples from docs to fixtures
Browse files Browse the repository at this point in the history
* chore: move examples from docs to fixutres

* chore: add schema validation for fixtures

* chore: cleaner error handling

* chore: fix breaking test flow
  • Loading branch information
yashmehrotra authored Sep 6, 2024
1 parent 71f3b6a commit 90e0283
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 8 deletions.
4 changes: 2 additions & 2 deletions api/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ func (f Filter) String() string {

// +kubebuilder:object:generate=true
type AutoClose struct {
// How long after the health checks have been passing before, autoclosing the incident.
Timeout time.Duration `json:"timeout,omitempty"`
// How long after the health checks have been passing before, autoclosing the incident (accepts goduration format)
Timeout string `json:"timeout,omitempty"`
}

// +kubebuilder:object:generate=true
Expand Down
2 changes: 1 addition & 1 deletion config/schemas/incident-rules.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"AutoClose": {
"properties": {
"timeout": {
"type": "integer"
"type": "string"
}
},
"additionalProperties": false,
Expand Down
11 changes: 11 additions & 0 deletions fixtures/notifications/kube-cronjob-failing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: mission-control.flanksource.com/v1
kind: Notification
metadata:
name: cronjob-alerts
namespace: default
spec:
events:
- config.unhealthy
filter: config.type == 'Kubernetes::CronJob'
to:
email: alerts@acme.com
11 changes: 11 additions & 0 deletions fixtures/notifications/kube-deployment-unhealthy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: mission-control.flanksource.com/v1
kind: Notification
metadata:
name: deployment-health-alerts
spec:
events:
- config.unhealthy
- config.warning
filter: config.type == 'Kubernetes::Deployment'
to:
connection: connection://mission-control/slack
21 changes: 21 additions & 0 deletions fixtures/notifications/kube-pod-crashlooping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: mission-control.flanksource.com/v1
kind: Notification
metadata:
name: podcrashlooping-alerts
namespace: default
spec:
events:
- config.unhealthy
filter: config.type == 'Kubernetes::Pod' && config.status == 'CrashLoopBackOff'
title: "Pod {{.config.name}} in namespace {{.config.tags.namespace}} is in CrashLoopBackOff"
template: |
{{.config.tags.namespace}}/{{.config.name}}
## Reason
{{.config.config | jq '.status.containerStatuses[0].state.waiting.message' }}
### Labels:
{{range $k, $v := .config.config.metadata.labels}}
**{{$k}}**: {{$v}}
{{end}}
to:
email: alerts@acme.com
1 change: 0 additions & 1 deletion fixtures/playbooks/http.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ spec:
- name: Post a message to webhook
http:
url: https://webhook.site/9f1392a6-718a-4ef5-a8e2-bfb55b08afca
thresholdMillis: 5000
method: POST
body: |
{
Expand Down
3 changes: 1 addition & 2 deletions fixtures/playbooks/sql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ spec:
actions:
- name: Get the total event count
sql:
connection: connection://Postgres/incident-commander
database: incident_commander
connection: connection://incident-commander
driver: postgres
query: SELECT COUNT(*) FROM event_queue
# - name: Notify event count
Expand Down
4 changes: 2 additions & 2 deletions fixtures/rules/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ metadata:
labels:
a: b
c: d
priority: 1
spec:
priority: 1
filter:
status:
- unhealthy
Expand All @@ -19,7 +19,7 @@ spec:
breakOnMatch: true
autoClose:
timeout: 15m
hoursOfOperation:
hoursOfOperation: []

responders:
email:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
github.com/samber/lo v1.47.0
github.com/samber/oops v1.13.1
github.com/samber/slog-echo v1.14.4
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
github.com/slack-go/slack v0.14.0
github.com/tg123/go-htpasswd v1.2.2
github.com/timberio/go-datemath v0.1.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
Expand Down Expand Up @@ -1511,6 +1513,8 @@ github.com/samber/oops v1.13.1 h1:ZTKqsTq1F/og29+wS0tb/NJS0RENRyWE3KrIRZLjdY0=
github.com/samber/oops v1.13.1/go.mod h1:xEXk4BLyqajkvCxzpxBnqfzHzRLFk3+g4E+tOJtOPZY=
github.com/samber/slog-echo v1.14.4 h1:ZIGH03wfnYGAp8rwurWR9zGXB0q1pZhpxoZFfsFlm7I=
github.com/samber/slog-echo v1.14.4/go.mod h1:i8QlNMhE0rVr+Mjj5ZIm6DMuTQ87euvAL2jRAd5HNVY=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
Expand Down
57 changes: 57 additions & 0 deletions tests/fixtures_schema_validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tests

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/santhosh-tekuri/jsonschema/v6"
"gopkg.in/yaml.v3"
)

func validateFixtureDirWithSchema(schemaPath, dir string) {
schema, err := jsonschema.NewCompiler().Compile(schemaPath)
Expect(err).To(BeNil())

err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
yamlRaw, err := os.ReadFile(path)
if err != nil {
return err
}

var m any
err = yaml.Unmarshal(yamlRaw, &m)
if err != nil {
return err
}

if err := schema.Validate(m); err != nil {
return fmt.Errorf("schema validation failed for %s: %w", path, err)
}
}
return nil
})
Expect(err).To(BeNil())
}

var _ = Describe("Fixture schema validation", func() {
It("Notifications", func() {
schemaPath := "../config/schemas/notification.schema.json"
validateFixtureDirWithSchema(schemaPath, "../fixtures/notifications/")
})

It("Playbooks", func() {
schemaPath := "../config/schemas/playbook.schema.json"
validateFixtureDirWithSchema(schemaPath, "../fixtures/playbooks/")
})

It("Rules", func() {
schemaPath := "../config/schemas/incident-rules.schema.json"
validateFixtureDirWithSchema(schemaPath, "../fixtures/rules")
})
})

0 comments on commit 90e0283

Please sign in to comment.