2020-01-21 21:21:19 +01:00
|
|
|
import { assert, spy, use } from 'chai'
|
|
|
|
import spies from 'chai-spies'
|
|
|
|
|
|
|
|
import { Ocean } from '../../src/ocean/Ocean'
|
|
|
|
import config from '../config'
|
|
|
|
import { Account } from '../../src/squid'
|
2020-01-28 18:00:06 +01:00
|
|
|
import { OceanCompute, ComputeJobStatus } from '../../src/ocean/OceanCompute'
|
2020-01-21 21:21:19 +01:00
|
|
|
|
|
|
|
use(spies)
|
|
|
|
|
|
|
|
const responsify = async data => ({
|
|
|
|
ok: true,
|
|
|
|
json: () => Promise.resolve(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('OceanCompute', () => {
|
|
|
|
let ocean: Ocean
|
|
|
|
let account: Account
|
|
|
|
let compute: OceanCompute
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
;[account] = await ocean.accounts.list()
|
|
|
|
compute = ocean.compute // eslint-disable-line prefer-destructuring
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
spy.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#start()', () => {
|
|
|
|
it('should start a new job', async () => {
|
|
|
|
spy.on(ocean.utils.fetch, 'post', () => responsify({ jobId: 'my-job-id' }))
|
|
|
|
|
|
|
|
const response = await compute.start(account, 'xxx', 'xxx')
|
|
|
|
assert(response.jobId === 'my-job-id')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#stop()', () => {
|
|
|
|
it('should stop a job', async () => {
|
2020-01-28 18:00:06 +01:00
|
|
|
spy.on(ocean.utils.fetch, 'put', () =>
|
|
|
|
responsify({ status: ComputeJobStatus.Completed })
|
|
|
|
)
|
2020-01-21 21:21:19 +01:00
|
|
|
|
|
|
|
const response = await compute.stop(account, 'xxx', 'xxx')
|
2020-01-28 18:00:06 +01:00
|
|
|
assert(response.status === ComputeJobStatus.Completed)
|
2020-01-21 21:21:19 +01:00
|
|
|
})
|
|
|
|
})
|
2020-01-22 11:30:27 +01:00
|
|
|
|
|
|
|
describe('#restart()', () => {
|
|
|
|
it('should restart a job', async () => {
|
|
|
|
spy.on(ocean.utils.fetch, 'put', () => responsify({ status: 6 }))
|
|
|
|
spy.on(ocean.utils.fetch, 'post', () => responsify({ jobId: 'my-job-id' }))
|
|
|
|
|
|
|
|
const response = await compute.restart(account, 'xxx', 'xxx')
|
|
|
|
assert(response.jobId === 'my-job-id')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#delete()', () => {
|
|
|
|
it('should delete a job', async () => {
|
2020-01-28 18:00:06 +01:00
|
|
|
spy.on(ocean.utils.fetch, 'delete', () =>
|
|
|
|
responsify({ status: ComputeJobStatus.Deleted })
|
|
|
|
)
|
2020-01-22 11:30:27 +01:00
|
|
|
|
|
|
|
const response = await compute.delete(account, 'xxx', 'xxx')
|
2020-01-28 18:00:06 +01:00
|
|
|
assert(response.status === ComputeJobStatus.Deleted)
|
2020-01-22 11:30:27 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#status()', () => {
|
|
|
|
it('should get the status of one job', async () => {
|
2020-01-28 18:00:06 +01:00
|
|
|
spy.on(ocean.utils.fetch, 'get', () =>
|
|
|
|
responsify([{ status: ComputeJobStatus.Started }])
|
|
|
|
)
|
2020-01-22 11:30:27 +01:00
|
|
|
|
|
|
|
const response = await compute.status(account, 'xxx', 'xxx')
|
|
|
|
assert(response.length === 1)
|
2020-01-28 18:00:06 +01:00
|
|
|
assert(response[0].status === ComputeJobStatus.Started)
|
2020-01-22 11:30:27 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the status of multiple jobs', async () => {
|
|
|
|
spy.on(ocean.utils.fetch, 'get', () =>
|
2020-01-28 18:00:06 +01:00
|
|
|
responsify([
|
|
|
|
{ status: ComputeJobStatus.Started },
|
|
|
|
{ status: ComputeJobStatus.Started }
|
|
|
|
])
|
2020-01-22 11:30:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const response = await compute.status(account, 'xxx')
|
|
|
|
assert(response.length === 2)
|
2020-01-28 18:00:06 +01:00
|
|
|
assert(response[0].status === ComputeJobStatus.Started)
|
2020-01-22 11:30:27 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get all jobs for one owner', async () => {
|
|
|
|
spy.on(ocean.utils.fetch, 'get', () =>
|
2020-01-28 18:00:06 +01:00
|
|
|
responsify([
|
|
|
|
{ status: ComputeJobStatus.Started },
|
|
|
|
{ status: ComputeJobStatus.Started }
|
|
|
|
])
|
2020-01-22 11:30:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const response = await compute.status(account)
|
|
|
|
assert(response.length === 2)
|
2020-01-28 18:00:06 +01:00
|
|
|
assert(response[0].status === ComputeJobStatus.Started)
|
2020-01-22 11:30:27 +01:00
|
|
|
})
|
|
|
|
})
|
2020-01-21 21:21:19 +01:00
|
|
|
})
|