-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(packages/testcontainers): add
PlaygroundApiContainer
(#2024)
#### 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
Showing
4 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/testcontainers/__tests__/containers/PlaygroundApiContainer.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,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) | ||
}) | ||
) | ||
}) |
8 changes: 5 additions & 3 deletions
8
packages/testcontainers/__tests__/containers/WhaleApiContainer.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
71 changes: 71 additions & 0 deletions
71
packages/testcontainers/src/containers/AppContainer/PlaygroundApiContainer.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,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') | ||
} | ||
} |
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