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

feat: add TcpCheck construct [sc-22430] #1012

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions packages/cli/e2e/__tests__/deploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ Update and Unchanged:
ApiCheck: api-check-high-freq
HeartbeatCheck: heartbeat-check-1
BrowserCheck: homepage-browser-check
TcpCheck: tcp-check
CheckGroup: my-group-1
Dashboard: dashboard-1
MaintenanceWindow: maintenance-window-1
Expand All @@ -252,6 +253,7 @@ Update and Unchanged:
HeartbeatCheck: heartbeat-check-1
BrowserCheck: homepage-browser-check
BrowserCheck: snapshot-test.test.ts
TcpCheck: tcp-check
CheckGroup: my-group-1
Dashboard: dashboard-1
MaintenanceWindow: maintenance-window-1
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/e2e/__tests__/fixtures/deploy-project/tcp.check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable no-new */
import { TcpCheck } from 'checkly/constructs'

new TcpCheck('tcp-check', {
name: 'TCP Check',
activated: false,
request: {
hostname: 'api.checklyhq.com',
port: 443,
},
degradedResponseTime: 5000,
maxResponseTime: 20000,
})
103 changes: 103 additions & 0 deletions packages/cli/src/constructs/__tests__/tcp-check.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { TcpCheck, CheckGroup, TcpRequest } from '../index'
import { Project, Session } from '../project'

const runtimes = {
'2022.10': { name: '2022.10', default: false, stage: 'CURRENT', description: 'Main updates are Playwright 1.28.0, Node.js 16.x and Typescript support. We are also dropping support for Puppeteer', dependencies: { '@playwright/test': '1.28.0', '@opentelemetry/api': '1.0.4', '@opentelemetry/sdk-trace-base': '1.0.1', '@faker-js/faker': '5.5.3', aws4: '1.11.0', axios: '0.27.2', btoa: '1.2.1', chai: '4.3.7', 'chai-string': '1.5.0', 'crypto-js': '4.1.1', expect: '29.3.1', 'form-data': '4.0.0', jsonwebtoken: '8.5.1', lodash: '4.17.21', mocha: '10.1.0', moment: '2.29.2', node: '16.x', otpauth: '9.0.2', playwright: '1.28.0', typescript: '4.8.4', uuid: '9.0.0' } },
}

const request: TcpRequest = {
hostname: 'acme.com',
port: 443,
}

describe('TcpCheck', () => {
it('should not synthesize runtime if not specified even if default runtime is set', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = '2022.02'
const apiCheck = new TcpCheck('test-check', {
name: 'Test Check',
request,
})
const payload = apiCheck.synthesize()
expect(payload.runtimeId).toBeUndefined()
delete Session.defaultRuntimeId
})

it('should synthesize runtime if specified', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = '2022.02'
const apiCheck = new TcpCheck('test-check', {
name: 'Test Check',
runtimeId: '2022.02',
request,
})
const payload = apiCheck.synthesize()
expect(payload.runtimeId).toEqual('2022.02')
delete Session.defaultRuntimeId
})

it('should apply default check settings', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.checkDefaults = { tags: ['default tags'] }
const apiCheck = new TcpCheck('test-check', {
name: 'Test Check',
request,
})
delete Session.checkDefaults
expect(apiCheck).toMatchObject({ tags: ['default tags'] })
})

it('should overwrite default check settings with check-specific config', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.checkDefaults = { tags: ['default tags'] }
const apiCheck = new TcpCheck('test-check', {
name: 'Test Check',
tags: ['test check'],
request,
})
delete Session.checkDefaults
expect(apiCheck).toMatchObject({ tags: ['test check'] })
})

it('should support setting groups with `groupId`', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const group = new CheckGroup('main-group', { name: 'Main Group', locations: [] })
const check = new TcpCheck('main-check', {
name: 'Main Check',
request,
groupId: group.ref(),
})
expect(check.synthesize()).toMatchObject({ groupId: { ref: 'main-group' } })
})

it('should support setting groups with `group`', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const group = new CheckGroup('main-group', { name: 'Main Group', locations: [] })
const check = new TcpCheck('main-check', {
name: 'Main Check',
request,
group,
})
expect(check.synthesize()).toMatchObject({ groupId: { ref: 'main-group' } })
})
})
1 change: 1 addition & 0 deletions packages/cli/src/constructs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export * from './phone-call-alert-channel'
export * from './retry-strategy'
export * from './multi-step-check'
export * from './alert-escalation-policy'
export * from './tcp-check'
77 changes: 77 additions & 0 deletions packages/cli/src/constructs/tcp-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Check, CheckProps } from './check'
import { Session } from './project'

sorccu marked this conversation as resolved.
Show resolved Hide resolved
export interface TcpRequest {
/**
* The hostname the connection should be made to.
*
* Do not include a scheme or a port in the hostname.
*/
hostname: string
/**
* The port the connection should be made to.
*/
port: number
}

export interface TcpCheckProps extends CheckProps {
/**
* Determines the request that the check is going to run.
*/
request: TcpRequest
/**
* The response time in milliseconds where a check should be considered degraded.
*/
degradedResponseTime?: number
/**
* The response time in milliseconds where a check should be considered failing.
*/
maxResponseTime?: number
}

/**
* Creates an TCP Check
*
* @remarks
*
* This class make use of the TCP Checks endpoints.
*/
export class TcpCheck extends Check {
request: TcpRequest
degradedResponseTime?: number
maxResponseTime?: number

/**
* Constructs the TCP Check instance
*
* @param logicalId unique project-scoped resource name identification
* @param props check configuration properties
*
* {@link https://checklyhq.com/docs/cli/constructs/#tcpcheck Read more in the docs}
*/

constructor (logicalId: string, props: TcpCheckProps) {
super(logicalId, props)

this.request = props.request
this.degradedResponseTime = props.degradedResponseTime
this.maxResponseTime = props.maxResponseTime

Session.registerConstruct(this)
this.addSubscriptions()
this.addPrivateLocationCheckAssignments()
}

synthesize () {
return {
...super.synthesize(),
checkType: 'TCP',
request: {
url: this.request.hostname, // Hide misleading naming from the user.
port: this.request.port,
},
degradedResponseTime: this.degradedResponseTime,
maxResponseTime: this.maxResponseTime,
}
}
}
Loading