Skip to content

Commit

Permalink
test(devnet): impl test passes (with extensive mocks)
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jan 13, 2024
1 parent f6ab7a2 commit c7c8156
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 33 deletions.
77 changes: 58 additions & 19 deletions devnet/devnet-impl.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ok, equal, throws } from 'node:assert'
import { OCIConnection, OCIImage, OCIContainer } from '@fadroma/oci'
import { OCIConnection, OCIImage, OCIContainer, Mock } from '@fadroma/oci'
import { Core } from '@fadroma/agent'
import * as Impl from './devnet-impl'
import $ from '@hackbg/file'
Expand Down Expand Up @@ -61,20 +61,25 @@ export default async () => {
log: new Console('createDevnetContainer'),
chainId: 'mock',
stateDir: undefined,
stateFile: { save (_) {} },
verbose: undefined,
initScript: undefined,
platformName: undefined,
platformVersion: undefined,
genesisAccounts: undefined,
container: new OCIContainer({
container: Object.assign(new OCIContainer({
id: 'mock-create',
image: new OCIImage({
engine: OCIConnection.mock(),
name: 'mock'
}),
}), {
inspect: async () => {
throw Object.assign(new Error(), {
statusCode: 404
})
}
}),
stateFile: {
save (_) {}
},
})

await Impl.startDevnetContainer({
Expand All @@ -85,40 +90,74 @@ export default async () => {
verbose: undefined,
running: undefined,
nodeHost: undefined,
waitString: undefined,
waitMore: undefined,
waitPort: undefined,
waitString: "mock-ready",
waitMore: 0,
waitPort: () => new Promise(resolve=>setTimeout(resolve, 1)),
created: undefined,
initScript: undefined,
stateDir: undefined,
container: new OCIContainer({
stateFile: { save (_) {} },
container: Object.defineProperties(new OCIContainer({
id: 'mock-start',
image: new OCIImage({
engine: OCIConnection.mock(),
name: 'mock'
}),
}), {
api: {
get () {
return Promise.resolve({
start: async () => {
},
logs: async () => {
return {
off () {},
on (event, callback) {
if (event === 'data') {
setTimeout(()=>{
callback('mock-ready')
}, 1)
}
}
}
}
})
}
}
}),
stateFile: {
save (_) {}
},
})

await Impl.pauseDevnetContainer({
log: new Console('pauseDevnetContainer'),
running: undefined,
container: new OCIContainer({
image: new OCIImage({
id: 'mock-pause',
image: Object.assign(new OCIImage({
engine: OCIConnection.mock(),
name: 'mock'
}),
})),
}),
})

await Impl.deleteDevnetContainer({
log: new Console('deleteDevnetContainer'),
stateDir: undefined,
paused: undefined,
container: new OCIContainer({
image: new OCIImage({ engine: OCIConnection.mock(), name: 'mock' }),
log: new Console('deleteDevnetContainer'),
stateDir: undefined,
paused: undefined,
container: Object.defineProperties(new OCIContainer({
id: 'mock-delete',
image: new OCIImage({
engine: OCIConnection.mock(),
name: 'mock'
}),
}), {
api: {
get () {
return Promise.resolve({
remove: async () => {
}
})
}
}
}),
})

Expand Down
14 changes: 9 additions & 5 deletions devnet/devnet-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export async function createDevnetContainer (
& Parameters<typeof containerOptions>[0]
& $D<'container'|'verbose'|'initScript'|'url'>
): Promise<void> {
if (await devnet.container.exists) {
if (await devnet.container.exists()) {
devnet.log(`Found`, bold(devnet.chainId), `in container`, bold(devnet.container.shortId))
} else {
if (devnet.verbose) {
Expand Down Expand Up @@ -231,7 +231,10 @@ export async function startDevnetContainer (
devnet.log.debug('Waiting for', bold(String(devnet.waitMore)), 'seconds...')
await new Promise(resolve=>setTimeout(resolve, devnet.waitMore))
//await Dock.Docker.waitSeconds(devnet.waitMore)
await devnet.waitPort({ host: devnet.nodeHost, port: Number(devnet.nodePort) })
await devnet.waitPort({
host: devnet.nodeHost,
port: Number(devnet.nodePort)
})
} else {
devnet.log.log('Container already started:', bold(devnet.chainId))
}
Expand All @@ -240,11 +243,12 @@ export async function startDevnetContainer (
export async function pauseDevnetContainer (
devnet: $D<'log'|'container'|'running'>
) {
const container = await devnet.container
if (container) {
if (await devnet.container.exists()) {
devnet.log.debug(`Stopping container:`, bold(devnet.container.shortId))
try {
if (await container.isRunning) await container.kill()
if (await devnet.container.isRunning()) {
await devnet.container.kill()
}
} catch (e) {
if (e.statusCode == 404) {
devnet.log.warn(`Container ${bold(devnet.container.shortId)} not found`)
Expand Down
8 changes: 5 additions & 3 deletions oci/oci-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function toDockerodeOptions (container: OCIContainer): Docker.ContainerCr

/* Is this equivalent to follow() and, if so, which implementation to keep? */
export function waitStream (
stream: { on: Function, off: Function, destroy: Function },
stream: { on: Function, off: Function, destroy?: Function },
expected: string,
thenDetach: boolean = true,
trail: (data: string) => unknown = ()=>{},
Expand All @@ -78,7 +78,9 @@ export function waitStream (
reject(error)
stream.off('error', waitStream_onError)
stream.off('data', waitStream_onData)
//stream.destroy()
if (stream.destroy) {
stream.destroy()
}
}

function waitStream_onData (data: any) {
Expand All @@ -91,7 +93,7 @@ export function waitStream (
if (dataStr.indexOf(expected) > -1) {
console.log(`Found expected message:`, bold(expected))
stream.off('data', waitStream_onData)
if (thenDetach) {
if (thenDetach && stream.destroy) {
stream.destroy()
}
resolve()
Expand Down
14 changes: 8 additions & 6 deletions oci/oci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { DockerHandle } from './oci-base'
import * as Mock from './oci-mock'
import { toDockerodeOptions, waitStream } from './oci-impl'

export { Mock }

/** Defaults to the `DOCKER_HOST` environment variable. */
export const defaultSocketPath = process.env.DOCKER_HOST || '/var/run/docker.sock'

Expand Down Expand Up @@ -169,7 +171,7 @@ export class OCIImage extends Deploy.ContractTemplate {
return this.api.getImage(this.name).inspect()
}

get exists (): Promise<boolean> {
exists (): Promise<boolean> {
return this.inspect().then(()=>true).catch(e=>{
if (e.statusCode === 404) return false
throw e
Expand Down Expand Up @@ -277,7 +279,7 @@ export class OCIContainer extends Deploy.ContractInstance {

constructor (properties: Partial<OCIContainer> = {}) {
super(properties)
assign(this, properties, ['engine', 'image', 'entrypoint', 'command', 'options'])
assign(this, properties, ['id', 'engine', 'image', 'entrypoint', 'command', 'options'])
this.log = new Console('OCIContainer')
hide(this, 'log')
}
Expand Down Expand Up @@ -305,18 +307,18 @@ export class OCIContainer extends Deploy.ContractInstance {
return (await this.api).inspect()
}

get exists (): Promise<boolean> {
exists (): Promise<boolean> {
return this.inspect().then(()=>true, e=>{
if (e.statusCode === 404) return false
throw e
})
}

get isRunning (): Promise<boolean> {
isRunning (): Promise<boolean> {
return this.inspect().then(state=>state.State.Running)
}

get ip (): Promise<string> {
ip (): Promise<string> {
return this.inspect().then(state=>state.NetworkSettings.IPAddress)
}

Expand Down Expand Up @@ -401,7 +403,7 @@ export class OCIContainer extends Deploy.ContractInstance {
}
}
return await waitStream(
stream as any,
stream,
expected,
thenDetach,
logFiltered,
Expand Down

0 comments on commit c7c8156

Please sign in to comment.