Skip to content

Commit

Permalink
test: make tests work with new kilt node cli (#796)
Browse files Browse the repository at this point in the history
* test: make tests work with new kilt node cli
* test: re-use integration test utils in bundle tests
* test: bump runtime version in integration test switch condition
  • Loading branch information
rflechtner committed Aug 22, 2023
1 parent bbda52b commit e0f7b86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
11 changes: 3 additions & 8 deletions tests/bundle/bundle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

/// <reference lib="dom" />

import { GenericContainer, Wait, StartedTestContainer } from 'testcontainers'
import type { StartedTestContainer } from 'testcontainers'
import { test, expect } from '@playwright/test'
import url from 'url'
import path from 'path'
import { getStartedTestContainer } from '../integration/utils.js'

declare global {
interface Window {
Expand All @@ -24,13 +25,7 @@ const WS_PORT = 9944

test.beforeAll(async () => {
// start dev node with testcontainers
testcontainer = await new GenericContainer(
process.env.TESTCONTAINERS_NODE_IMG || 'kiltprotocol/mashnet-node:latest'
)
.withCommand(['--dev', `--ws-port=${WS_PORT}`, '--ws-external'])
.withExposedPorts({ container: WS_PORT, host: WS_PORT })
.withWaitStrategy(Wait.forLogMessage(`:${WS_PORT}`))
.start()
testcontainer = await getStartedTestContainer(WS_PORT)
})

test('html bundle integration test', async ({ page }) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/ErrorHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ beforeAll(async () => {
it('records an extrinsic error when transferring less than the existential amount to new identity', async () => {
const transferTx = api.tx.balances.transfer(addressFromRandom(), 1)
const promise = submitTx(transferTx, paymentAccount)
if (api.runtimeVersion.specVersion.toBigInt() > 11_000n) {
if (api.runtimeVersion.specVersion.toBigInt() >= 11_200n) {
await expect(promise).rejects.toMatchInlineSnapshot(`
{
"token": "BelowMinimum",
Expand Down
56 changes: 38 additions & 18 deletions tests/integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,50 @@ import type {
} from '@kiltprotocol/types'
import { Crypto } from '@kiltprotocol/utils'

import { makeSigningKeyTool } from '../testUtils/index.js'
import { makeSigningKeyTool } from '../testUtils/TestUtils.js'

export const EXISTENTIAL_DEPOSIT = new BN(10 ** 13)
const ENDOWMENT = EXISTENTIAL_DEPOSIT.muln(10000)

const WS_PORT = 9944

async function getStartedTestContainer(): Promise<StartedTestContainer> {
try {
const image =
process.env.TESTCONTAINERS_NODE_IMG || 'kiltprotocol/mashnet-node'
console.log(`using testcontainer with image ${image}`)
const testcontainer = new GenericContainer(image)
.withCommand(['--dev', `--ws-port=${WS_PORT}`, '--ws-external'])
.withExposedPorts(WS_PORT)
.withWaitStrategy(Wait.forLogMessage(`:${WS_PORT}`))
const started = await testcontainer.start()
return started
} catch (error) {
console.error(
'Could not start the docker container via testcontainers, run with DEBUG=testcontainers* to debug'
)
throw error
export async function getStartedTestContainer(
hostPort?: number
): Promise<StartedTestContainer> {
const image =
process.env.TESTCONTAINERS_NODE_IMG || 'kiltprotocol/mashnet-node'
console.log(`using testcontainer with image ${image}`)
const strategies = [
['--dev', '--ws-external', `--ws-port=${WS_PORT}`],
['--dev', '--rpc-external', `--rpc-port=${WS_PORT}`],
]
// eslint-disable-next-line no-restricted-syntax
for (const args of strategies) {
console.log(`attempting to launch container with arguments ${args}`)
try {
const testcontainer = new GenericContainer(image)
.withCommand(args)
.withExposedPorts(
typeof hostPort === 'number'
? { host: hostPort, container: WS_PORT }
: WS_PORT
)
.withWaitStrategy(Wait.forLogMessage(`:${WS_PORT}`))
// eslint-disable-next-line no-await-in-loop
const started = await testcontainer.start()
console.log('container started and ready')
return started
} catch (error) {
console.warn(
'Failed to start container due to the following error:\n',
error
)
}
}
console.error(
'Could not start the docker container via testcontainers, run with DEBUG=testcontainers* to debug'
)
throw new Error('CONTAINER LAUNCH ERROR')
}

async function buildConnection(wsEndpoint: string): Promise<ApiPromise> {
Expand All @@ -69,7 +89,7 @@ export async function initializeApi(): Promise<ApiPromise> {
return buildConnection(TEST_WS_ADDRESS)
}
const started = await getStartedTestContainer()
const port = started.getMappedPort(9944)
const port = started.getMappedPort(WS_PORT)
const host = started.getHost()
const WS_ADDRESS = `ws://${host}:${port}`
console.log(`connecting to test container at ${WS_ADDRESS}`)
Expand Down

0 comments on commit e0f7b86

Please sign in to comment.