-
Notifications
You must be signed in to change notification settings - Fork 1
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
899a381
commit 7e2d021
Showing
10 changed files
with
317 additions
and
28 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
77 changes: 77 additions & 0 deletions
77
apps/server/test/api/routes/v1/satellite/satellite.restartAllPlugins.test.ts
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,77 @@ | ||
import Docker from '@friday-ai/docker'; | ||
import { expect } from 'chai'; | ||
import { Container } from 'dockerode'; | ||
import Plugin from '../../../../../src/core/plugin/plugin'; | ||
import { admin, guest, habitant } from '../../../../utils/apiToken'; | ||
import server from '../../../../utils/request'; | ||
|
||
let plugin: Plugin; | ||
let docker: Docker; | ||
let container: Container; | ||
|
||
describe('PATCH /api/v1/satellite/restart/plugins/:id', () => { | ||
// Create a fake container and save docker id on plugin | ||
before(async function before() { | ||
plugin = global.FRIDAY.plugin; | ||
docker = global.FRIDAY.docker; | ||
|
||
container = await docker.createContainer({ | ||
Image: 'alpine', | ||
AttachStdin: false, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Tty: true, | ||
OpenStdin: false, | ||
StdinOnce: false, | ||
}); | ||
|
||
await container.start(); | ||
}); | ||
|
||
// Only update container id before each hook to prevent multiple useless container created | ||
beforeEach(async function beforeEach() { | ||
this.timeout(15000); | ||
await plugin.update('88b48273-15e6-4729-9199-0682677475f4', { | ||
dockerId: container.id, | ||
}); | ||
}); | ||
|
||
after(async function after() { | ||
this.timeout(15000); | ||
await container.remove({ force: true }); | ||
}); | ||
|
||
it('should restart all plugins of a satellite', async function restart() { | ||
this.timeout(15000); | ||
await server | ||
.patch('/api/v1/satellite/restart/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1') | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.body.success).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('admin should have access to restart all plugins of a satellite', async function restart() { | ||
this.timeout(15000); | ||
await server | ||
.patch('/api/v1/satellite/restart/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1', admin) | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.body.success).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('PATCH /api/v1/satellite/restart/plugins/:id', () => { | ||
it("habitant should't have access to restart all plugins of a satellite", async () => { | ||
await server.patch('/api/v1/satellite/restart/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1', habitant).expect(403); | ||
}); | ||
|
||
it("guest should't have access to restart all plugins of a satellite", async () => { | ||
await server.patch('/api/v1/satellite/restart/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1', guest).expect(403); | ||
}); | ||
|
||
it('should not found a satellite', async () => { | ||
await server.patch('/api/v1/satellite/restart/plugins/5801badb-55d7-4bcd-9bf0-37a6cffe0bb1').expect(404); | ||
}); | ||
}); |
77 changes: 77 additions & 0 deletions
77
apps/server/test/api/routes/v1/satellite/satellite.stopAllPlugins.test.ts
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,77 @@ | ||
import Docker from '@friday-ai/docker'; | ||
import { expect } from 'chai'; | ||
import { Container } from 'dockerode'; | ||
import Plugin from '../../../../../src/core/plugin/plugin'; | ||
import { admin, guest, habitant } from '../../../../utils/apiToken'; | ||
import server from '../../../../utils/request'; | ||
|
||
let plugin: Plugin; | ||
let docker: Docker; | ||
let container: Container; | ||
|
||
describe('PATCH /api/v1/satellite/stop/plugins/:id', () => { | ||
// Create a fake container and save docker id on plugin | ||
before(async function before() { | ||
plugin = global.FRIDAY.plugin; | ||
docker = global.FRIDAY.docker; | ||
|
||
container = await docker.createContainer({ | ||
Image: 'alpine', | ||
AttachStdin: false, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Tty: true, | ||
OpenStdin: false, | ||
StdinOnce: false, | ||
}); | ||
|
||
await container.start(); | ||
}); | ||
|
||
// Only update container id before each hook to prevent multiple useless container created | ||
beforeEach(async function beforeEach() { | ||
this.timeout(15000); | ||
await plugin.update('88b48273-15e6-4729-9199-0682677475f4', { | ||
dockerId: container.id, | ||
}); | ||
}); | ||
|
||
after(async function after() { | ||
this.timeout(15000); | ||
await container.remove({ force: true }); | ||
}); | ||
|
||
it('should stop all plugins of a satellite', async function restart() { | ||
this.timeout(15000); | ||
await server | ||
.patch('/api/v1/satellite/stop/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1') | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.body.success).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('admin should have access to stop all plugins of a satellite', async function restart() { | ||
this.timeout(15000); | ||
await server | ||
.patch('/api/v1/satellite/stop/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1', admin) | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.body.success).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('PATCH /api/v1/satellite/stop/plugins/:id', () => { | ||
it("habitant should't have access to stop all plugins of a satellite", async () => { | ||
await server.patch('/api/v1/satellite/stop/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1', habitant).expect(403); | ||
}); | ||
|
||
it("guest should't have access to stop all plugins of a satellite", async () => { | ||
await server.patch('/api/v1/satellite/stop/plugins/4801badb-55d7-4bcd-9bf0-37a6cffe0bb1', guest).expect(403); | ||
}); | ||
|
||
it('should not found a satellite', async () => { | ||
await server.patch('/api/v1/satellite/stop/plugins/5801badb-55d7-4bcd-9bf0-37a6cffe0bb1').expect(404); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
apps/server/test/core/satellite/satellite.restartAllPlugins.test.ts
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,59 @@ | ||
import { AvailableState, EventsType } from '@friday-ai/shared'; | ||
import { assert, expect } from 'chai'; | ||
import { Container } from 'dockerode'; | ||
import sinon from 'sinon'; | ||
import Satellite from '../../../src/core/satellite/satellite'; | ||
import { NotFoundError } from '../../../src/utils/decorators/error'; | ||
|
||
let satellite: Satellite; | ||
let container: Container; | ||
|
||
describe('Satellite.restartAllPlugins', () => { | ||
before(async () => { | ||
satellite = global.FRIDAY.satellite; | ||
|
||
// Create a fake container and save docker id on plugin | ||
container = await global.FRIDAY.docker.createContainer({ | ||
Image: 'alpine', | ||
AttachStdin: false, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Tty: true, | ||
OpenStdin: false, | ||
StdinOnce: false, | ||
}); | ||
|
||
await container.start(); | ||
}); | ||
|
||
// Only update container id before each hook to prevent multiple useless container created | ||
beforeEach(async function beforeEach() { | ||
this.timeout(15000); | ||
await global.FRIDAY.plugin.update('88b48273-15e6-4729-9199-0682677475f4', { | ||
dockerId: container.id, | ||
}); | ||
}); | ||
|
||
after(async function after() { | ||
this.timeout(15000); | ||
await container.remove({ force: true }); | ||
}); | ||
|
||
it('should restart all plugins of a satellite', async function stop() { | ||
this.timeout(15000); | ||
|
||
const listener = sinon.spy(); | ||
global.FRIDAY.event.on(EventsType.WEBSOCKET_SEND_ALL, listener); | ||
|
||
const promise = satellite.restartAllPlugins('4801badb-55d7-4bcd-9bf0-37a6cffe0bb1'); | ||
|
||
await assert.isFulfilled(promise); | ||
expect(listener.called).equal(true); | ||
expect(listener.args[0][0].type).to.equal(AvailableState.PLUGIN_STOPPED); | ||
}); | ||
|
||
it('should not found satellite', async () => { | ||
const promise = satellite.update('4017c89a-8d02-4d9b-9aec-1e1bcb93a3a7', {}); | ||
await assert.isRejected(promise, NotFoundError); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
apps/server/test/core/satellite/satellite.stopAllPlugins.test.ts
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,59 @@ | ||
import { AvailableState, EventsType } from '@friday-ai/shared'; | ||
import { assert, expect } from 'chai'; | ||
import { Container } from 'dockerode'; | ||
import sinon from 'sinon'; | ||
import Satellite from '../../../src/core/satellite/satellite'; | ||
import { NotFoundError } from '../../../src/utils/decorators/error'; | ||
|
||
let satellite: Satellite; | ||
let container: Container; | ||
|
||
describe('Satellite.stopAllPlugins', () => { | ||
before(async () => { | ||
satellite = global.FRIDAY.satellite; | ||
|
||
// Create a fake container and save docker id on plugin | ||
container = await global.FRIDAY.docker.createContainer({ | ||
Image: 'alpine', | ||
AttachStdin: false, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Tty: true, | ||
OpenStdin: false, | ||
StdinOnce: false, | ||
}); | ||
|
||
await container.start(); | ||
}); | ||
|
||
// Only update container id before each hook to prevent multiple useless container created | ||
beforeEach(async function beforeEach() { | ||
this.timeout(15000); | ||
await global.FRIDAY.plugin.update('88b48273-15e6-4729-9199-0682677475f4', { | ||
dockerId: container.id, | ||
}); | ||
}); | ||
|
||
after(async function after() { | ||
this.timeout(15000); | ||
await container.remove({ force: true }); | ||
}); | ||
|
||
it('should stop all plugins of a satellite', async function stop() { | ||
this.timeout(15000); | ||
|
||
const listener = sinon.spy(); | ||
global.FRIDAY.event.on(EventsType.WEBSOCKET_SEND_ALL, listener); | ||
|
||
const promise = satellite.stopAllPlugins('4801badb-55d7-4bcd-9bf0-37a6cffe0bb1'); | ||
|
||
await assert.isFulfilled(promise); | ||
expect(listener.called).equal(true); | ||
expect(listener.args[0][0].type).to.equal(AvailableState.PLUGIN_STOPPED); | ||
}); | ||
|
||
it('should not found satellite', async () => { | ||
const promise = satellite.update('4017c89a-8d02-4d9b-9aec-1e1bcb93a3a7', {}); | ||
await assert.isRejected(promise, NotFoundError); | ||
}); | ||
}); |
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
Oops, something went wrong.