Skip to content

Commit

Permalink
feat(packages/testcontainers): add PlaygroundApiContainer (#2024)
Browse files Browse the repository at this point in the history
#### What this PR does / why we need it:

All the benefits for `playground-api` without the hassle of setting them
up. Use it directly in Jest tests.

```ts
let defid: StartedNativeChainContainer
let whale: StartedWhaleApiContainer
let playground: StartedPlaygroundApiContainer

beforeAll(async () => {
  const network = await new Network().start()

  defid = await new NativeChainContainer()
    .withNetwork(network)
    .withPreconfiguredRegtestMasternode()
    .start()

  whale = await new WhaleApiContainer()
    .withNetwork(network)
    .withNativeChain(defid, network)
    .start()

  playground = await new PlaygroundApiContainer()
    .withNetwork(network)
    .withNativeChain(defid, network)
    .start()
})

afterAll(async () => {
  await whale.stop()
  await defid.stop()
  await playground.stop()
})

it('should playground.waitForReady() and do something with the setups', async () => {
  await playground.waitForReady()

  const whaleApi = new WhaleApiClient(whale.getWhaleApiClientOptions())
  console.log(await whaleApi.poolpairs.list(1))
})

```
  • Loading branch information
fuxingloh authored Feb 3, 2023
1 parent d353e96 commit 5964709
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Network } from 'testcontainers'
import { WhaleApiClient } from '@defichain/whale-api-client'
import { PlaygroundApiClient } from '@defichain/playground-api-client'

import {
NativeChainContainer,
StartedNativeChainContainer,
StartedWhaleApiContainer,
WhaleApiContainer,
PlaygroundApiContainer,
StartedPlaygroundApiContainer
} from '../../src'

let defid: StartedNativeChainContainer
let whale: StartedWhaleApiContainer
let playground: StartedPlaygroundApiContainer

beforeAll(async () => {
const network = await new Network().start()

defid = await new NativeChainContainer()
.withNetwork(network)
.withPreconfiguredRegtestMasternode()
.start()

whale = await new WhaleApiContainer()
.withNetwork(network)
.withNativeChain(defid, network)
.start()

playground = await new PlaygroundApiContainer()
.withNetwork(network)
.withNativeChain(defid, network)
.start()
})

afterAll(async () => {
await whale.stop()
await defid.stop()
await playground.stop()
})

it('should playground.waitForReady()', async () => {
await playground.waitForReady()

const playgroundApi = new PlaygroundApiClient(playground.getPlaygroundApiClientOptions())
const { block } = await playgroundApi.playground.info()
expect(block.count).toBeGreaterThanOrEqual(150)

// Check if any PoolPair is created.
const whaleApi = new WhaleApiClient(whale.getWhaleApiClientOptions())
const data = await whaleApi.poolpairs.list(1)
expect(data[0]).toStrictEqual(
expect.objectContaining({
name: expect.any(String)
})
)
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NativeChainContainer, StartedNativeChainContainer } from '@defichain/testcontainers'
import { Network } from 'testcontainers'
import { WhaleApiClient } from '@defichain/whale-api-client'

import {
NativeChainContainer,
StartedNativeChainContainer,
StartedWhaleApiContainer,
WhaleApiContainer
} from '@defichain/testcontainers/dist/containers/AppContainer/WhaleApiContainer'
import { WhaleApiClient } from '@defichain/whale-api-client'
} from '../../src'

let defid: StartedNativeChainContainer
let whale: StartedWhaleApiContainer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { GenericContainer, StartedNetwork } from 'testcontainers'
import { AbstractStartedContainer } from 'testcontainers/dist/modules/abstract-started-container'
import { waitForCondition } from '../../utils'
import { StartedNativeChainContainer } from '../NativeChainContainer'
import fetch from 'cross-fetch'

// eslint-disable-next-line
// @ts-ignore because `package.json` will always be available in the root of pnpm package
import packageJson from '../../../package.json'

/**
* For local environment, `:latest` tag will be used as there isn't pipeline to automatically rebuild image locally.
*/
const PLAYGROUND_VERSION = packageJson.version === '0.0.0' ? 'latest' : packageJson.version

const PLAYGROUND_API_PORT = 3000

export class PlaygroundApiContainer extends GenericContainer {
constructor (image: string = `ghcr.io/jellyfishsdk/playground-api:${PLAYGROUND_VERSION}`) {
super(image)
this.withExposedPorts(PLAYGROUND_API_PORT).withStartupTimeout(120_000)
}

public withNativeChain (
container: StartedNativeChainContainer,
network: StartedNetwork
): this {
const ipAddress = container.getIpAddress(network.getName())
this.withEnvironment({
PLAYGROUND_DEFID_URL: `http://${container.rpcUser}:${container.rpcPassword}@${ipAddress}:19554/`
})
return this
}

public async start (): Promise<StartedPlaygroundApiContainer> {
return new StartedPlaygroundApiContainer(await super.start())
}
}

export class StartedPlaygroundApiContainer extends AbstractStartedContainer {
public getContainerPort (): number {
return PLAYGROUND_API_PORT
}

public getPort (): number {
return this.getMappedPort(this.getContainerPort())
}

getEndpoint (): string {
return `http://localhost:${this.getPort()}`
}

getPlaygroundApiClientOptions (): { url: string, version: 'v0' } {
return {
url: this.getEndpoint(),
version: 'v0'
}
}

async waitForReady (timeout: number = 590000): Promise<void> {
const url = `${this.getEndpoint()}/_actuator/probes/readiness`

return await waitForCondition(async () => {
const response = await fetch(url, {
method: 'GET'
})
const { data } = await response.json()
return data.details.playground.status === 'up'
}, timeout, 200, 'waitForIndexedBlockHeight')
}
}
1 change: 1 addition & 0 deletions packages/testcontainers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './containers/RegTestContainer/LoanContainer'

export * from './containers/AppContainer/WhaleSanityContainer'
export * from './containers/AppContainer/WhaleApiContainer'
export * from './containers/AppContainer/PlaygroundApiContainer'

export * from './containers/NativeChainContainer'
export * from './containers/NativeChainRpc'
Expand Down

0 comments on commit 5964709

Please sign in to comment.