Skip to content

Commit

Permalink
fix+test(oci): found where it was dropping the stream?
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jan 15, 2024
1 parent c7c8156 commit 356764e
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 135 deletions.
4 changes: 2 additions & 2 deletions compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,14 @@ export class ContainerizedLocalRustCompiler extends LocalRustCompiler {
// Run the build container
const buildContainer = await this.buildImage.run({
name: `fadroma-build-${randomBytes(3).toString('hex')}`,
command: [ 'node', $(`/`, $(buildScript).basename).path, 'phase1', ],
command: [ 'node', $(`/`, $(buildScript).basename()).path, 'phase1', ],

Check failure on line 428 in compile/compile.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
entrypoint: '/usr/bin/env',
options: {
remove: true,
// Readonly mounts:
readonly: {
// - Script that will run in the container
[$(buildScript).path]: $(`/`, $(buildScript).basename).path,
[$(buildScript).path]: $(`/`, $(buildScript).basename()).path,

Check failure on line 435 in compile/compile.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
},
// Writable mounts:
writable: {
Expand Down
2 changes: 1 addition & 1 deletion compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@hackbg/file": "workspace:*",
"@hackbg/logs": "workspace:*",
"@hackbg/cmds": "workspace:^3.2.1",
"@hackbg/cmds": "workspace:*",

"@fadroma/agent": "workspace:*",
"@fadroma/oci": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"@hackbg/file": "workspace:*",
"@hackbg/logs": "workspace:*",
"@hackbg/cmds": "workspace:^3.2.1",
"@hackbg/cmds": "workspace:*",

"@fadroma/agent": "workspace:*"
},
Expand Down
14 changes: 7 additions & 7 deletions devnet/devnet-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ export async function testDevnetPlatform <
deepEqual(spawnOptions.extra.HostConfig.PortBindings, {
[`${String(devnet.nodePort)}/tcp`]: [ { HostPort: String(devnet.nodePort) } ]
}, "devnet port binding is present")
equal(await devnet.container, undefined)

ok(await devnet.created)
equal(await devnet.created, devnet)
equal(devnet.url.toString(), `http://${devnet.nodeHost}:${devnet.nodePort}/`)
ok((await devnet.container) instanceof OCIContainer)
equal((await devnet.container)!.name, `/${devnet.chainId}`)
ok(devnet.container instanceof OCIContainer)
equal(devnet.container.name, devnet.chainId)

ok(await devnet.started)
equal(await devnet.started, devnet)
const agent = await devnet.connect({ name: 'User1' })
ok(agent instanceof Connection)
equal(agent.chainId, devnet.chainId)
equal(agent.url, devnet.url)

ok(await devnet.paused)
equal(await devnet.paused, devnet)

ok(await devnet.export())

ok(await devnet.deleted)
equal(await devnet.deleted, devnet)

}
8 changes: 4 additions & 4 deletions devnet/devnet-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ export default abstract class DevnetContainer extends Chain.Backend {
}

/** Wait for the devnet to be created. */
declare readonly created: Promise<void>
declare readonly created: Promise<this>

/** Wait for the devnet to be deleted. */
declare readonly deleted: Promise<void>
declare readonly deleted: Promise<this>

/** Wait for the devnet to be started. */
declare readonly started: Promise<void>
declare readonly started: Promise<this>

/** Wait for the devnet to be stopped. */
declare readonly paused: Promise<void>
declare readonly paused: Promise<this>

/** Get info for named genesis account, including the mnemonic */
async getIdentity (
Expand Down
18 changes: 16 additions & 2 deletions devnet/devnet-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export async function createDevnetContainer (
& Parameters<typeof saveDevnetState>[0]
& Parameters<typeof containerOptions>[0]
& $D<'container'|'verbose'|'initScript'|'url'>
): Promise<void> {
) {
if (await devnet.container.exists()) {
devnet.log(`Found`, bold(devnet.chainId), `in container`, bold(devnet.container.shortId))
} else {
Expand Down Expand Up @@ -153,11 +153,12 @@ export async function createDevnetContainer (
devnet.log.debug(`Saved devnet receipt.`)
}
}
return devnet
}

export async function deleteDevnetContainer (
devnet: $D<'log'|'container'|'stateDir'|'paused'> & Parameters<typeof forceDelete>[0]
): Promise<void> {
) {
devnet.log('Deleting...')
let container
try {
Expand Down Expand Up @@ -191,6 +192,7 @@ export async function deleteDevnetContainer (
throw e
}
}
return devnet
}

/** Write the state of the devnet to a file.
Expand Down Expand Up @@ -238,6 +240,7 @@ export async function startDevnetContainer (
} else {
devnet.log.log('Container already started:', bold(devnet.chainId))
}
return devnet
}

export async function pauseDevnetContainer (
Expand All @@ -258,6 +261,7 @@ export async function pauseDevnetContainer (
}
}
devnet.running = false
return devnet
}

export async function connect <
Expand Down Expand Up @@ -482,6 +486,16 @@ export function initContainerState (devnet: DevnetContainer) {
defineGetter('started', () => starting)
return starting
})
defineGetter('paused', () => {
const pausing = pauseDevnetContainer(devnet)
defineGetter('paused', () => pausing)
return pausing
})
defineGetter('deleted', () => {
const deleting = deleteDevnetContainer(devnet)
defineGetter('deleted', () => deleting)
return deleting
})
}

//type State = 'missing'|'creating'|'paused'|'starting'|'running'|'pausing'|'deleting'
Expand Down
20 changes: 10 additions & 10 deletions devnet/devnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export default class DevnetCLI extends CLI {
return this.printUsage(arg0)
}

printUsageOnly = this.command2({
printUsageOnly = this.command({
name: 'usage',
info: 'print available commands without listing devnets',
args: ''
}, () => this.printUsage(this))

listPlatforms = this.command2({
listPlatforms = this.command({
name: 'platforms',
info: 'show supported platforms',
args: ''
Expand All @@ -60,7 +60,7 @@ export default class DevnetCLI extends CLI {
this.log.info()
})

listDevnets = this.command2({
listDevnets = this.command({
name: 'list',
info: 'list existing devnets',
args: ''
Expand Down Expand Up @@ -186,15 +186,15 @@ export default class DevnetCLI extends CLI {

})

launchDevnet = this.command2({
launchDevnet = this.command({
name: 'launch',
info: 'create and start a devnet',
args: 'PLATFORM VERSION [CHAIN-ID]'
}, async (platform: 'scrt'|'okp4', version: string, chainId?: string) => {
await (await this.createDevnet(platform, version, chainId)).started
})

createDevnet = this.command2({
createDevnet = this.command({
name: 'create',
info: 'create a devnet',
args: 'PLATFORM VERSION [CHAIN-ID]'
Expand Down Expand Up @@ -237,7 +237,7 @@ export default class DevnetCLI extends CLI {
return devnet
})

startDevnet = this.command2({
startDevnet = this.command({
name: 'start',
info: 'start a devnet',
args: 'CHAIN-ID'
Expand Down Expand Up @@ -309,31 +309,31 @@ export default class DevnetCLI extends CLI {
}
})

pauseDevnet = this.command2({
pauseDevnet = this.command({
name: 'pause',
info: 'pause a devnet',
args: 'CHAIN-ID'
}, (chainId: string) => {
throw new Error('not implemented')
})

exportDevnet = this.command2({
exportDevnet = this.command({
name: 'export',
info: 'export snapshot of devnet as container image',
args: 'CHAIN-ID [IMAGE-TAG]',
}, (chainId: string, imageTag?: string) => {
throw new Error('not implemented')
})

removeDevnet = this.command2({
removeDevnet = this.command({
name: 'remove',
info: 'erase a devnet',
args: 'CHAIN-ID'
}, (chainId: string) => {
throw new Error('not implemented')
})

pruneDevnets = this.command2({
pruneDevnets = this.command({
name: 'prune',
info: 'delete broken devnets',
args: ''
Expand Down
2 changes: 1 addition & 1 deletion devnet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@hackbg/file": "workspace:^",
"@hackbg/logs": "workspace:^",
"@hackbg/cmds": "workspace:^3.2.1",
"@hackbg/cmds": "workspace:^",

"@fadroma/agent": "workspace:*",
"@fadroma/scrt": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions oci/ensuite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
exclude:
- "coverage/**/*"
34 changes: 16 additions & 18 deletions oci/oci-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ import Docker from 'dockerode'
import type { OCIContainer } from './oci'
import { OCIConsole as Console, OCIError, bold } from './oci-base'

export function toDockerodeOptions (container: OCIContainer): Docker.ContainerCreateOptions {

const {
name,
image,
entrypoint,
command,
options: {
remove = false,
env = {},
exposed = [],
mapped = {},
readonly = {},
writable = {},
extra = {},
cwd
}
} = container
export function toDockerodeOptions ({
name,
image,
entrypoint,
command,
options: {
remove = false,
env = {},
exposed = [],
mapped = {},
readonly = {},
writable = {},
extra = {},
cwd
} = {}
}: OCIContainer): Docker.ContainerCreateOptions {

if (!image) {
throw new OCIError("Missing container image.")
Expand Down
2 changes: 2 additions & 0 deletions oci/oci.test.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM busybox
CMD ["sh", "-c", "while true; do sleep 1; done" ]
66 changes: 36 additions & 30 deletions oci/oci.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
import { Suite } from '@hackbg/ensuite'
import { OCIConnection, OCIImage, OCIContainer, defaultSocketPath } from './oci'
import { Core } from '@fadroma/agent'
import * as assert from 'node:assert'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

assert.throws(()=>new OCIConnection(123 as any))
export default new Suite([
['real', testContainerEngine],
])

{
export async function testContainerEngine () {
const engine = new OCIConnection()
const image = engine.image('tag')
const container = image.container('name')
const image = engine.image('hello-world')
assert.ok(image instanceof OCIImage)
assert.equal(image.engine, engine)
console.log('Pull or build...')
await image.pullOrBuild()
console.log('Check...')
await image.check()
console.log('Pull...')
await image.pull()
image.dockerfile = resolve(dirname(fileURLToPath(import.meta.url)), 'oci.test.Dockerfile')
console.log('Build...')
await image.build()
const container = image.container(`test-hello-${Core.randomBase16()}`)
assert.ok(container instanceof OCIContainer)
assert.equal(container.image, image)
}

{

const engine = OCIConnection.mock()

{
const image = engine.image('test', 'Dockerfile')
await image.pullOrBuild()
await image.check()
await image.pull()
await image.build()
const container = await image.run()
assert.equal(container.image, image)
await container.id
await container.shortId
await container.isRunning
await container.ip
}

{
const container = await engine.container('test')
assert.equal(container.api, engine.api)
assert.equal(container.image.api, engine.api)
}

assert.equal(await container.exists(), false)
console.log('Create container...')
await container.create()
assert.equal(await container.exists(), true)
assert.ok(container.id)
assert.equal(container.shortId, container.id.slice(0, 8))
assert.equal(await container.isRunning(), false)
console.log('Start container...')
await container.start()
assert.ok(await container.ip())
assert.equal(await container.isRunning(), true)
console.log('Kill container...')
await container.kill()
assert.equal(await container.isRunning(), false)
console.log('Remove container...')
//await container.remove()
//assert.equal(await container.exists(), false)
}
Loading

0 comments on commit 356764e

Please sign in to comment.