Skip to content

Commit

Permalink
Add test to validate example configs (#773)
Browse files Browse the repository at this point in the history
* add monika-configuration.json

* add checking for notifications

* add explicit requirement for data:url

* add more offline test cases

* ran prettier

* update schema filename

* add body and headers

* ran formatter

* cleanup test file

* remove array body and headers

* update filename

* free form body

* add url validation

* add test, fix body validation

* oops..removed *.only test debug

* update variable name from PR review

* use of const instead of enum

* test sample configs

* use latest schema, remove test.only

Co-authored-by: Gregory Sukanto <[email protected]>
Co-authored-by: Nico Prananta <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2022
1 parent eceb577 commit a50b64c
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 23 deletions.
1 change: 1 addition & 0 deletions config_sample/config.desktop.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
3 changes: 2 additions & 1 deletion config_sample/config.desktop.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
Expand Down
3 changes: 2 additions & 1 deletion config_sample/config.mailgun.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
"id": "unique-id-mailgun",
"type": "mailgun",
"data": {
"recipients": ["RECIPIENT_EMAIL_ADDRESS"],
"recipients": ["RECIPIENT_EMAIL_ADDRESS", "RECIPIENT_EMAIL_ADDRESS2"],
"apiKey": "YOUR_API_KEY",
"domain": "YOUR_DOMAIN"
}
}
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
5 changes: 3 additions & 2 deletions config_sample/config.mailgun.example.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
- id: unique-id-mailgun
type: mailgun
data:
recipients: [RECIPIENT_EMAIL_ADDRESS]
recipients: [RECIPIENT_EMAIL_ADDRESS, RECIPIENT_EMAIL_ADDRESS2]
apiKey: YOUR_API_KEY
domain: YOUR_DOMAIN
3 changes: 2 additions & 1 deletion config_sample/config.monika-whatsapp.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"id": "monika-webhook",
"type": "monika-notif",
"data": {
"url": "YOUR_MONIKA_WHATSAPP_WEBHOOK"
"url": "https://YOUR_MONIKA_WHATSAPP_WEBHOOK"
}
}
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
7 changes: 4 additions & 3 deletions config_sample/config.monika-whatsapp.example.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
probes:
- requests:
- url: http://github.com
- id: probe-01
requests:
- url: https://github.com

notifications:
- id: monika-webhook
type: monika-notif
data:
url: YOUR_MONIKA_WHATSAPP_WEBHOOK
url: https://YOUR_MONIKA_WHATSAPP_WEBHOOK
1 change: 1 addition & 0 deletions config_sample/config.sendgrid.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
5 changes: 3 additions & 2 deletions config_sample/config.sendgrid.example.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
- id: unique-id-mailgun
type: mailgun
type: sendgrid
data:
recipients: [RECIPIENT_EMAIL_ADDRESS]
apiKey: YOUR_API_KEY
Expand Down
1 change: 1 addition & 0 deletions config_sample/config.slack.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
3 changes: 2 additions & 1 deletion config_sample/config.slack.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
Expand Down
1 change: 1 addition & 0 deletions config_sample/config.smtp-gmail.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
3 changes: 2 additions & 1 deletion config_sample/config.smtp-gmail.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
Expand Down
1 change: 1 addition & 0 deletions config_sample/config.teams.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
5 changes: 3 additions & 2 deletions config_sample/config.teams.example.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
- id: unique-id-teams
type: type
type: teams
data:
url: https://YOUR_TEAMS_WEBHOOK_URL
1 change: 1 addition & 0 deletions config_sample/config.telegram.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
3 changes: 2 additions & 1 deletion config_sample/config.telegram.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
Expand Down
1 change: 1 addition & 0 deletions config_sample/config.webhook.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
3 changes: 2 additions & 1 deletion config_sample/config.webhook.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
Expand Down
1 change: 1 addition & 0 deletions config_sample/config.whatsapp.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
7 changes: 4 additions & 3 deletions config_sample/config.whatsapp.example.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
- id: unique-id-webhook
type: webhook
type: whatsapp
data:
recipients: [628123456789]
recipients: ['628123456789']
url: https://yourwhatsappapiserver.com
username: whatsappusername
password: whatsapppassword
1 change: 1 addition & 0 deletions config_sample/config.workplace.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"probes": [
{
"id": "probe-01",
"requests": [
{
"url": "https://github.com"
Expand Down
9 changes: 5 additions & 4 deletions config_sample/config.workplace.example.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
probes:
- requests:
- id: probe-01
requests:
- url: http://github.com

notifications:
- id: unique-id-webhook
type: webhook
- id: unique-id-workplace
type: workplace
data:
thread_id: 12345678910
thread_id: 'abcdef12345678910'
access_token: your_custom_integration_access_token
85 changes: 85 additions & 0 deletions test/json-schema/validate-sample-configs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**********************************************************************************
* MIT License *
* *
* Copyright (c) 2021 Hyperjump Technology *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**********************************************************************************/

import yaml from 'js-yaml'
import fs from 'fs'
import path from 'path'
import { expect } from 'chai'
import Ajv from 'ajv'

const ajv = new Ajv()

import mySchema from '../../monika-config-schema.json'

ajv.addVocabulary(['name', 'fileMatch', 'url']) // add custom Scheme Store keywords, reference: https://www.schemastore.org/json/

const validate = ajv.compile(mySchema)

const getAllFiles = function (dirPath: string, arrayOfFiles: string[]) {
const files = fs.readdirSync(dirPath)

arrayOfFiles = arrayOfFiles || []

// files.forEach(function(file) {
for (const file of files) {
if (fs.statSync(dirPath + '/' + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(dirPath, '/', file))
}
}

return arrayOfFiles
}

describe('validate example configs', () => {
it('should detect examples that does not conform to the schema', () => {
const files = getAllFiles('./config_sample', [])

let sampleFile: any

for (const file of files) {
switch (path.extname(file)) {
case '.yml':
case '.yaml':
sampleFile = yaml.load(fs.readFileSync(file, 'utf8'))
break
case '.json':
sampleFile = JSON.parse(fs.readFileSync(file, 'utf8'))
break
default:
sampleFile = null
continue // skip for other file.extension
}

const isValid = validate(sampleFile)
if (isValid === false) {
console.error('file:', file, 'validity:', isValid)
console.error('error:', validate.errors)
}

expect(isValid).to.be.true
}
})
})

0 comments on commit a50b64c

Please sign in to comment.