Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: failed request assertion not found #1340

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions src/commands/monika.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
import { Command, Errors } from '@oclif/core'
import pEvent from 'p-event'
import type { ValidatedConfig } from '../interfaces/config'
import type { Probe } from '../interfaces/probe'

import {
getValidatedConfig,
isSymonModeFrom,
initConfig,
} from '../components/config'
import { createConfig } from '../components/config/create'
import { sortProbes } from '../components/config/sort'
import { printAllLogs } from '../components/logger'
import { flush } from '../components/logger/flush'
import { closeLog } from '../components/logger/history'
Expand All @@ -43,10 +41,10 @@ import { sendMonikaStartMessage } from '../components/notification/start-message
import { printSummary } from '../components/summary'
import { getContext, setContext } from '../context'
import events from '../events'
import { type MonikaFlags, sanitizeFlags, flags } from '../flag'
import { sanitizeFlags, flags } from '../flag'
import { savePidFile } from '../jobs/summary-notification'
import initLoaders from '../loaders'
import { sanitizeProbe, startProbing } from '../looper'
import { startProbing } from '../looper'
import SymonClient from '../symon'
import { getEventEmitter } from '../utils/events'
import { log } from '../utils/pino'
Expand All @@ -57,6 +55,7 @@ import {
close as closeSentry,
flush as flushSentry,
} from '@sentry/node'
import { getProbes } from '../components/config/probe'

const em = getEventEmitter()
let symonClient: SymonClient
Expand Down Expand Up @@ -163,7 +162,7 @@ export default class Monika extends Command {

for (;;) {
const config = getValidatedConfig()
const probes = getProbes({ config, flags })
const probes = getProbes()

// emit the sanitized probe
em.emit(events.config.sanitized, probes)
Expand Down Expand Up @@ -231,19 +230,6 @@ async function logRunningInfo({ isVerbose, isSymonMode }: RunningInfoParams) {
}
}

type GetProbesParams = {
config: ValidatedConfig
flags: MonikaFlags
}

function getProbes({ config, flags }: GetProbesParams): Probe[] {
const sortedProbes = sortProbes(config.probes, flags.id)

return sortedProbes.map((probe: Probe) =>
sanitizeProbe(isSymonModeFrom(flags), probe)
)
}

function deprecationHandler(config: ValidatedConfig): ValidatedConfig {
const showDeprecateMsg: Record<'query', boolean> = {
query: false,
Expand Down
95 changes: 95 additions & 0 deletions src/components/config/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,98 @@ describe('Setup Config', () => {
).not.undefined
})
})

describe('sanitizeConfig', () => {
beforeEach(() => {
resetContext()
})

it('should sanitize probes in normal mode', () => {
// arrange
const config = {
probes: [
{
id: '1',
name: 'Test probe',
interval: 1000,
requests: [
{
url: 'https://example.com',
body: '',
followRedirects: 21,
timeout: 1000,
},
],
alerts: [],
},
],
}

// act
const result = sanitizeConfig(config)

// assert
expect(result.probes[0].alerts).to.have.lengthOf(1)
expect(result.probes[0].alerts[0]).to.include({
assertion: '',
message: 'Probe not accessible',
})
})

it('should sanitize probes in Symon mode', () => {
// arrange
setContext({
flags: sanitizeFlags({
symonKey: 'test-key',
symonUrl: 'https://example.com',
}),
})
const config = {
probes: [
{
id: '1',
name: 'Test probe',
interval: 30_000,
requests: [
{
url: 'https://example.com',
body: '',
followRedirects: 21,
timeout: 1000,
},
],
alerts: [],
},
],
}

// act
const result = sanitizeConfig(config)

// assert
expect(result.probes[0].alerts).to.have.lengthOf(0)
expect(result.probes[0].id).to.equal('1')
expect(result.probes[0].interval).to.equal(30_000)
})

it('should preserve existing config properties', () => {
// arrange
const config: Config = {
version: '1.0.0',
notifications: [{ id: 'test-1', type: 'smtp' }],
certificate: {
domains: ['example.com'],
reminder: 15,
},
probes: [],
}

// act
const result = sanitizeConfig(config)

// assert
expect(result.version).to.equal('1.0.0')
expect(result.notifications).to.deep.equal([{ id: 'test-1', type: 'smtp' }])
expect(result.certificate?.reminder).to.equal(15)
})
})
9 changes: 7 additions & 2 deletions src/components/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import type {
Config,
ValidatedConfig,
} from '../../interfaces/config'
import { sanitizeProbe } from '../../looper'
import { isSymonModeFrom } from '.'
import { getContext } from '../../context'

const DEFAULT_TLS_EXPIRY_REMINDER_DAYS = 30
const DEFAULT_STATUS_NOTIFICATION = '0 6 * * *'

export function sanitizeConfig(config: Config): ValidatedConfig {
const { certificate, notifications = [], version } = config
const sanitizedConfigWithoutVersion = {
const isSymonMode = isSymonModeFrom(getContext().flags)
const { certificate, notifications = [], version, probes } = config
const sanitizedConfigWithoutVersion: Omit<ValidatedConfig, 'version'> = {
...config,
probes: probes.map((probe) => sanitizeProbe(isSymonMode, probe)),
certificate: sanitizeCertificate(certificate),
notifications,
'status-notification':
Expand Down
7 changes: 6 additions & 1 deletion src/looper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ export const FAILED_REQUEST_ASSERTION = {
}

function addFailedRequestAssertions(assertions: ProbeAlert[]) {
// create uuid seed from FAILED_REQUEST_ASSERTION
// to make the id deterministic, consistent, and testable
const idSeed = Buffer.from(JSON.stringify(FAILED_REQUEST_ASSERTION))
return [
...assertions,
{
id: uuid(),
id: uuid({
random: idSeed,
}),
...FAILED_REQUEST_ASSERTION,
},
]
Expand Down
Loading