-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36d72a8
commit 681ac60
Showing
11 changed files
with
252 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { faker } from '@faker-js/faker'; | ||
import { Status } from '../../../types/Status'; | ||
import { GrafanaConfiguration } from '../../../types/GrafanaConfiguration'; | ||
import { Grafana } from './index'; | ||
|
||
const fetchtMock = jest.fn(); | ||
jest.mock('electron-fetch', () => { | ||
return { | ||
__esModule: true, | ||
default: (...all: any) => fetchtMock(...all), | ||
}; | ||
}); | ||
|
||
describe('NewRelic', () => { | ||
describe('getState', () => { | ||
let config: GrafanaConfiguration; | ||
let observer: Grafana; | ||
|
||
let expectedUrl: string; | ||
let expectedSite: string; | ||
|
||
beforeEach(() => { | ||
fetchtMock.mockClear(); | ||
config = { | ||
type: 'grafana', | ||
url: faker.internet.url(), | ||
authToken: faker.lorem.word(), | ||
alias: faker.lorem.word(), | ||
}; | ||
expectedUrl = `${config.url}/api/v1/provisioning/alert-rules`; | ||
expectedSite = `${config.url}/alerting`; | ||
observer = new Grafana(config); | ||
}); | ||
|
||
it('shoulds return NA status if request return diferent value than 200', async () => { | ||
fetchtMock.mockResolvedValue({ | ||
json: () => Promise.resolve('kaboom'), | ||
ok: false, | ||
}); | ||
const result = await observer.getState(); | ||
expect(fetchtMock).toBeCalledWith(expectedUrl, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${config.authToken}`, | ||
}, | ||
}); | ||
expect(result).toEqual({ | ||
name: config.alias, | ||
status: Status.NA, | ||
link: expectedSite, | ||
}); | ||
}); | ||
it('shoulds return SUCCESS status if request return empty violations array', async () => { | ||
fetchtMock.mockResolvedValue({ | ||
json: () => | ||
Promise.resolve([ | ||
{ | ||
execErrState: 'Success', | ||
}, | ||
{ | ||
execErrState: 'Success', | ||
}, | ||
{ | ||
execErrState: 'Success', | ||
}, | ||
]), | ||
ok: true, | ||
}); | ||
const result = await observer.getState(); | ||
expect(fetchtMock).toBeCalledWith(expectedUrl, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${config.authToken}`, | ||
}, | ||
}); | ||
expect(result).toEqual({ | ||
name: config.alias, | ||
status: Status.SUCCESS, | ||
link: expectedSite, | ||
}); | ||
}); | ||
it('shoulds return FAILURE status if request have active alarms', async () => { | ||
fetchtMock.mockResolvedValue({ | ||
json: () => | ||
Promise.resolve([ | ||
{ | ||
execErrState: 'Error', | ||
}, | ||
{ | ||
execErrState: 'Success', | ||
}, | ||
{ | ||
execErrState: 'Success', | ||
}, | ||
]), | ||
ok: true, | ||
}); | ||
const result = await observer.getState(); | ||
expect(fetchtMock).toBeCalledWith(expectedUrl, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${config.authToken}`, | ||
}, | ||
}); | ||
expect(result).toEqual({ | ||
name: config.alias, | ||
status: Status.FAILURE, | ||
link: expectedSite, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { State } from '../../../types/State'; | ||
import { Observer } from '../../../types/Observer'; | ||
import { Status } from '../../../types/Status'; | ||
import { GrafanaConfiguration } from '../../../types/GrafanaConfiguration'; | ||
import fetch from 'electron-fetch'; | ||
|
||
export class Grafana implements Observer { | ||
private readonly url: string; | ||
private readonly site: string; | ||
private readonly alias: string; | ||
private readonly authToken: string; | ||
|
||
constructor({ url, alias, authToken }: GrafanaConfiguration) { | ||
this.url = `${url}/api/v1/provisioning/alert-rules`; | ||
this.site = `${url}/alerting`; | ||
this.alias = alias || `Grafana: ${url}`; | ||
this.authToken = authToken; | ||
} | ||
public async getState(): Promise<State> { | ||
try { | ||
const response = await fetch(this.url, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${this.authToken}`, | ||
}, | ||
}); | ||
if (!response.ok) throw new Error('response is invalid'); | ||
const alerRules = await response.json(); | ||
return { | ||
name: this.alias, | ||
status: this.getStatus(alerRules), | ||
link: this.site, | ||
}; | ||
} catch (_) { | ||
return { | ||
name: this.alias, | ||
status: Status.NA, | ||
link: this.site, | ||
}; | ||
} | ||
} | ||
|
||
private getStatus(alertRules: any[]): Status { | ||
return alertRules.some((alertRule: any) => alertRule.execErrState === 'Error') ? Status.FAILURE : Status.SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { Grafana } from './'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
jest.mock('@mui/material/TextField', () => ({ | ||
__esModule: true, | ||
default: (props: any) => <input data-testid={`textField-${props.label}`} {...props} />, | ||
})); | ||
|
||
describe('Grafana Monitor', () => { | ||
const expectedObservable = { | ||
apiKey: faker.lorem.word(), | ||
}; | ||
const expectedIndex = faker.random.numeric(); | ||
const updateFieldMock = jest.fn(); | ||
const translateMock = (val: string): string => val; | ||
beforeEach(() => { | ||
updateFieldMock.mockClear(); | ||
render( | ||
<Grafana | ||
observable={expectedObservable} | ||
index={expectedIndex} | ||
updateFieldWithValue={updateFieldMock} | ||
translate={translateMock} | ||
/> | ||
); | ||
}); | ||
describe.each([ | ||
['URL', 'url', undefined], | ||
['Authorization Token', 'authToken', 'password'], | ||
])('%s', (label: string, value: string, type: string) => { | ||
it('should have correct textfield attributes', () => { | ||
const textfield = screen.getByTestId(`textField-${label}`); | ||
expect(textfield).toHaveAttribute('label', label); | ||
expect(textfield).toHaveAttribute('variant', 'outlined'); | ||
if (type) expect(textfield).toHaveAttribute('type', type); | ||
expect(textfield).toHaveAttribute('value', (expectedObservable as any)[value]); | ||
}); | ||
|
||
it('should call update field on change event', () => { | ||
const expectedValue = faker.lorem.word(); | ||
const textfield = screen.getByTestId(`textField-${label}`); | ||
fireEvent.change(textfield, { target: { value: expectedValue } }); | ||
expect(updateFieldMock).toBeCalledWith(value, expectedIndex, expectedValue); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import TextField from '@mui/material/TextField'; | ||
|
||
export const Grafana = ({ observable, index, updateFieldWithValue, translate }: any) => ( | ||
<> | ||
<TextField | ||
label={translate('URL')} | ||
variant="outlined" | ||
value={observable.url} | ||
onChange={(event: React.ChangeEvent<HTMLInputElement>) => updateFieldWithValue('url', index, event.target.value)} | ||
/> | ||
<TextField | ||
label={translate('Authorization Token')} | ||
variant="outlined" | ||
type="password" | ||
value={observable.authToken} | ||
onChange={(event: React.ChangeEvent<HTMLInputElement>) => | ||
updateFieldWithValue('authToken', index, event.target.value) | ||
} | ||
/> | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ObserverConfiguration } from './ObserverConfiguration'; | ||
|
||
export type GrafanaConfiguration = ObserverConfiguration & { | ||
url: string; | ||
authToken: string; | ||
}; |