From 8174709bbf3d89def8b102d34b0d5ee4c3584f6b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 13 Jul 2020 00:10:47 -0700 Subject: [PATCH] add more tests --- src/datatokens/Datatokens.ts | 6 +- src/ocean/Compute.ts | 149 +++++++++++++++------------ src/ocean/Ocean.ts | 2 +- src/provider/Provider.ts | 2 +- test/integration/ComputeFlow.test.ts | 142 +++++++++++++++---------- 5 files changed, 172 insertions(+), 129 deletions(-) diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index b8f90f99..d8d1201d 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -40,9 +40,9 @@ export class DataTokens { */ public async create(metaDataStoreURI: string, account: Account): Promise { // Create factory contract object - const factory = new this.web3.eth.Contract( - this.factoryABI, this.factoryAddress, {from: account} - ) + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { + from: account + }) const estGas = await factory.methods .createToken(metaDataStoreURI) .estimateGas(function (err, estGas) { diff --git a/src/ocean/Compute.ts b/src/ocean/Compute.ts index 98659140..7a132d1a 100644 --- a/src/ocean/Compute.ts +++ b/src/ocean/Compute.ts @@ -2,18 +2,14 @@ import { SearchQuery } from '../metadatastore/MetadataStore' import { DDO } from '../ddo/DDO' import { Metadata } from '../ddo/interfaces/Metadata' import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm' -import { - Service, - ServiceComputePrivacy, - ServiceCompute -} from '../ddo/interfaces/Service' +import { Service, ServiceComputePrivacy, ServiceCompute } from '../ddo/interfaces/Service' import { EditableMetadata } from '../ddo/interfaces/EditableMetadata' import Account from './Account' import DID from './DID' import { SubscribablePromise } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' -import {Output} from "./interfaces/ComputeOutput"; -import {ComputeJob} from "./interfaces/ComputeJob"; +import { Output } from './interfaces/ComputeOutput' +import { ComputeJob } from './interfaces/ComputeJob' // import { WebServiceConnector } from './utils/WebServiceConnector' // import { Output } from './interfaces/ComputeOutput' // import { ComputeJob } from './interfaces/ComputeJob' @@ -72,7 +68,7 @@ export class Compute extends Instantiable { algorithmMeta?: MetadataAlgorithm, output?: Output, serviceIndex?: string, - serviceType?: string + serviceType?: string ): Promise { output = this.checkOutput(consumerAccount, output) if (did) { @@ -88,7 +84,6 @@ export class Compute extends Instantiable { serviceIndex, serviceType, tokenAddress - ) return computeJobsList[0] as ComputeJob } else return null @@ -167,25 +162,45 @@ export class Compute extends Instantiable { } public createServerAttributes( - serverId: string, serverType: string, cost: string, - cpu: string, gpu: string, memory: string, - disk: string, maxExecutionTime: number + serverId: string, + serverType: string, + cost: string, + cpu: string, + gpu: string, + memory: string, + disk: string, + maxExecutionTime: number ): object { return { - serverId, serverType, cost, cpu, gpu, memory, disk, maxExecutionTime + serverId, + serverType, + cost, + cpu, + gpu, + memory, + disk, + maxExecutionTime } } - public createContainerAttributes(image: string, tag: string, checksum: string): object { - return {image, tag, checksum} + public createContainerAttributes( + image: string, + tag: string, + checksum: string + ): object { + return { image, tag, checksum } } public createClusterAttributes(type: string, url: string): object { - return {type, url} + return { type, url } } public createProviderAttributes( - type: string, description: string, cluster: object, containers: object[], servers: object[] + type: string, + description: string, + cluster: object, + containers: object[], + servers: object[] ): object { return { type, @@ -204,7 +219,7 @@ export class Compute extends Instantiable { datePublished: string, providerAttributes: object, computePrivacy?: ServiceComputePrivacy, - timeout?: number, + timeout?: number ): ServiceCompute { const name = 'dataAssetComputingService' if (!timeout) timeout = 3600 @@ -226,13 +241,12 @@ export class Compute extends Instantiable { } } - if (computePrivacy) - service.attributes.main.privacy = computePrivacy + if (computePrivacy) service.attributes.main.privacy = computePrivacy return service as ServiceCompute } - /** + /** * Check the output object and add default properties if needed * @param {Account} consumerAccount The account of the consumer ordering the service. * @param {Output} output Output section used for publishing the result. @@ -270,52 +284,51 @@ export class Compute extends Instantiable { owner: output.owner || consumerAccount.getId() } } - } // "creator": "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", - // "datePublished": "2019-04-09T19:02:11Z", - // "cost": "10", - // "timeout": 86400, - // "provider": { - // "type": "Azure", - // "description": "", - // "environment": { - // "cluster": { - // "type": "Kubernetes", - // "url": "http://10.0.0.17/xxx" - // }, - // "supportedContainers": [ - // { - // "image": "tensorflow/tensorflow", - // "tag": "latest", - // "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc" - // }, - // { - // "image": "tensorflow/tensorflow", - // "tag": "latest", - // "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc" - // } - // ], - // "supportedServers": [ - // { - // "serverId": "1", - // "serverType": "xlsize", - // "cost": "50", - // "cpu": "16", - // "gpu": "0", - // "memory": "128gb", - // "disk": "160gb", - // "maxExecutionTime": 86400 - // }, - // { - // "serverId": "2", - // "serverType": "medium", - // "cost": "10", - // "cpu": "2", - // "gpu": "0", - // "memory": "8gb", - // "disk": "80gb", - // "maxExecutionTime": 86400 - // } - // ] - // } +// "datePublished": "2019-04-09T19:02:11Z", +// "cost": "10", +// "timeout": 86400, +// "provider": { +// "type": "Azure", +// "description": "", +// "environment": { +// "cluster": { +// "type": "Kubernetes", +// "url": "http://10.0.0.17/xxx" +// }, +// "supportedContainers": [ +// { +// "image": "tensorflow/tensorflow", +// "tag": "latest", +// "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc" +// }, +// { +// "image": "tensorflow/tensorflow", +// "tag": "latest", +// "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc" +// } +// ], +// "supportedServers": [ +// { +// "serverId": "1", +// "serverType": "xlsize", +// "cost": "50", +// "cpu": "16", +// "gpu": "0", +// "memory": "128gb", +// "disk": "160gb", +// "maxExecutionTime": 86400 +// }, +// { +// "serverId": "2", +// "serverType": "medium", +// "cost": "10", +// "cpu": "2", +// "gpu": "0", +// "memory": "8gb", +// "disk": "80gb", +// "maxExecutionTime": 86400 +// } +// ] +// } diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index ac18104b..6a2cafc9 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -17,7 +17,7 @@ import { Instantiable, generateIntantiableConfigFromConfig } from '../Instantiable.abstract' -import {Compute} from "./Compute"; +import { Compute } from './Compute' /** * Main interface for Ocean Protocol. diff --git a/src/provider/Provider.ts b/src/provider/Provider.ts index d02c7f90..9c94eafa 100644 --- a/src/provider/Provider.ts +++ b/src/provider/Provider.ts @@ -186,7 +186,7 @@ export class Provider extends Instantiable { // 'algorithmDataToken': alg_data_token // switch fetch method - + let fetch switch (method) { diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 711ee119..86d314b4 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -3,7 +3,7 @@ import { DataTokens } from '../../src/datatokens/Datatokens' import { Ocean } from '../../src/ocean/Ocean' import config from './config' import { assert } from 'console' -import {ComputeJob} from "../../src/ocean/interfaces/ComputeJob"; +import { ComputeJob } from '../../src/ocean/interfaces/ComputeJob' import { Service, ServiceComputePrivacy, @@ -38,7 +38,7 @@ describe('Marketplace flow', () => { let cluster let servers let containers - let provider + let provider const dateCreated = new Date(Date.now()).toISOString().split('.')[0] + 'Z' // remove milliseconds @@ -88,7 +88,8 @@ describe('Marketplace flow', () => { license: 'CC-BY', files: [ { - url:'https://raw.githubusercontent.com/tbertinmahieux/MSongsDB/master/Tasks_Demos/CoverSongs/shs_dataset_test.txt', + url: + 'https://raw.githubusercontent.com/tbertinmahieux/MSongsDB/master/Tasks_Demos/CoverSongs/shs_dataset_test.txt', checksum: 'efb2c764274b745f5fc37f97c6b0e764', contentLength: '4535431', contentType: 'text/csv', @@ -102,9 +103,21 @@ describe('Marketplace flow', () => { it('Alice publishes dataset with a compute service', async () => { price = 10 // in datatoken - cluster = ocean.compute.createClusterAttributes('Kubernetes', 'http://10.0.0.17/xxx') + cluster = ocean.compute.createClusterAttributes( + 'Kubernetes', + 'http://10.0.0.17/xxx' + ) servers = [ - ocean.compute.createServerAttributes('1', 'xlsize', '50', '16', '0', '128gb', '160gb', timeout) + ocean.compute.createServerAttributes( + '1', + 'xlsize', + '50', + '16', + '0', + '128gb', + '160gb', + timeout + ) ] containers = [ ocean.compute.createContainerAttributes( @@ -120,23 +133,25 @@ describe('Marketplace flow', () => { containers, servers ) - + const computeService = ocean.compute.createComputeService( - alice, price, dateCreated, provider + alice, + price, + dateCreated, + provider ) ddo = await ocean.assets.create(asset, alice, [computeService], tokenAddress) assert(ddo.dataToken === tokenAddress) - }) - //alex + // alex it('should publish a dataset with a compute service object that does not allow rawAlgo', async () => { const origComputePrivacy = { allowRawAlgorithm: false, allowNetworkAccess: false, trustedAlgorithms: [] } - + const computeService = ocean.compute.createComputeService( alice, '1000', @@ -144,17 +159,22 @@ describe('Marketplace flow', () => { provider, origComputePrivacy as ServiceComputePrivacy ) - datasetNoRawAlgo = await ocean.assets.create(asset, alice, [computeService], tokenAddress) + datasetNoRawAlgo = await ocean.assets.create( + asset, + alice, + [computeService], + tokenAddress + ) assert(datasetNoRawAlgo.dataToken === tokenAddress) }) - + it('should publish a dataset with a compute service object that allows only algo with did:op:1234', async () => { const origComputePrivacy = { allowRawAlgorithm: false, allowNetworkAccess: false, trustedAlgorithms: ['did:op:1234'] } - + const computeService = ocean.compute.createComputeService( alice, '1000', @@ -162,10 +182,15 @@ describe('Marketplace flow', () => { provider, origComputePrivacy as ServiceComputePrivacy ) - datasetWithTrustedAlgo = await ocean.assets.create(asset, alice, [computeService], tokenAddress) + datasetWithTrustedAlgo = await ocean.assets.create( + asset, + alice, + [computeService], + tokenAddress + ) assert(datasetWithTrustedAlgo.dataToken === tokenAddress) }) - + it('should publish an algorithm', async () => { const algoAsset = { main: { @@ -176,22 +201,22 @@ describe('Marketplace flow', () => { license: 'CC-BY', files: [ { - url:'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', + url: + 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', contentType: 'text/js', encoding: 'UTF-8' - } ], - "algorithm": { - "language": "js", - "format": "docker-image", - "version": "0.1", - "container": { - "entrypoint": "node $ALGO", - "image": "node", - "tag": "10" + algorithm: { + language: 'js', + format: 'docker-image', + version: '0.1', + container: { + entrypoint: 'node $ALGO', + image: 'node', + tag: '10' } - }, + } } } const service1 = await ocean.assets.createAccessServiceAttributes( @@ -200,12 +225,15 @@ describe('Marketplace flow', () => { dateCreated, 0 ) - algorithmAsset = await ocean.assets.create(algoAsset, alice,[service1],tokenAddress) + algorithmAsset = await ocean.assets.create( + algoAsset, + alice, + [service1], + tokenAddress + ) assert(algorithmAsset.dataToken === tokenAddress) - }) - it('Alice mints 100 DTs and tranfers them to the compute marketplace', async () => { await datatoken.mint(tokenAddress, alice.getId(), tokenAmount) }) @@ -225,29 +253,32 @@ describe('Marketplace flow', () => { }) }) - it('Bob starts compute job', async () => { + it('Bob starts compute job with a raw Algo', async () => { const algorithmMeta = { - 'language': 'scala', - 'format': 'docker-image', - 'version': '0.1', - 'url': 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', - 'container': { - 'entrypoint': 'node $ALGO', - 'image': 'node', - 'tag': '10' + language: 'js', + format: 'docker-image', + version: '0.1', + url: + 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', + container: { + entrypoint: 'node $ALGO', + image: 'node', + tag: '10' } } - const output = { - - } - let order = await ocean.assets.order(ddo.id, computeService.type, bob.getId()) - let computeOrder=JSON.parse(order) - let tx=await datatoken.transfer( - computeOrder["dataToken"], - computeOrder["to"], - computeOrder["numTokens"], - computeOrder["from"] + const output = {} + const order = await ocean.assets.order( + ddo.id, + computeService.type, + bob.getId() + ) + const computeOrder = JSON.parse(order) + const tx = await datatoken.transfer( + computeOrder['dataToken'], + computeOrder['to'], + computeOrder['numTokens'], + computeOrder['from'] ) const response = await ocean.compute.start( ddo.id, @@ -262,24 +293,23 @@ describe('Marketplace flow', () => { computeService.type ) jobId = response.jobId - assert(response.status>=10) - + assert(response.status >= 10) }) it('Bob should get status of a compute job', async () => { const response = await ocean.compute.status(bob, ddo.id, jobId) - assert(response[0].jobId==jobId) + assert(response[0].jobId === jobId) }) - + it('should get status of all compute jobs for an address', async () => { const response = await ocean.compute.status(bob, undefined, undefined) - assert(response.length>0) - + assert(response.length > 0) }) - + // it('should not allow order the compute service with raw algo for dataset that does not allow raw algo', async () => {}) + // it('should not allow order the compute service with did != did:op:1234 for dataset that allows only did:op:1234 as algo', async () => {}) + // it('should start a compute job with a published algo', async () => { // it('Bob restarts compute job', async () => {}) - // it('Bob gets outputs', async () => {}) }) })