Skip to content

Commit

Permalink
fix: api testing setup (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jan 2, 2023
1 parent f92d726 commit 0bd363e
Show file tree
Hide file tree
Showing 6 changed files with 584 additions and 740 deletions.
3 changes: 3 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@
"esbuild": "^0.14.38",
"execa": "^5.1.1",
"git-rev-sync": "^3.0.1",
"ipfs-http-client": "^57.0.3",
"ipfs-utils": "^9.0.4",
"miniflare": "^2.5.0",
"nft.storage-api": "https://gitpkg.now.sh/nftstorage/nft.storage/packages/api?019a505e8f4bb93a24b8c480646779f5e4b66326",
"npm-run-all": "^4.1.5",
"pg": "^8.7.3",
"p-map": "^5.3.0",
"p-retry": "^5.1.1",
"p-settle": "^5.0.0",
"p-wait-for": "^5.0.0",
"sade": "^1.7.4",
"smoke": "^3.1.1",
"uint8arrays": "^3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/api/scripts/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sade from 'sade'
import { buildCmd } from './build.js'
import { dbCmd } from './db.js'
import { dbSqlCmd } from './db-sql.js'
import { ipfsCmd } from '../node_modules/edge-gateway/scripts/ipfs.js'
import { ipfsCmd } from './ipfs.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
dotenv.config({
Expand Down
130 changes: 130 additions & 0 deletions packages/api/scripts/ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env node
import net from 'net'
import path from 'path'
import { fileURLToPath } from 'url'
import execa from 'execa'
import pWaitFor from 'p-wait-for'
import { create } from 'ipfs-http-client'
import globSource from 'ipfs-utils/src/files/glob-source.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const IPFS_API_URL = 'http://127.0.0.1:9089'

export async function ipfsCmd({
start,
stop,
composePath = path.join(__dirname, '../docker/docker-compose.yml'),
containerName = 'ipfs0',
}) {
const project = 'ipfs-daemon'

if (start) {
if (await isPortReachable(8080)) {
throw new Error(
'IPFS daemon is already running. Please check if you have any docker project or cluster deamon already running.'
)
}

await execa('docker-compose', [
'--file',
composePath,
'--project-name',
project,
'up',
'--detach',
])

await pWaitFor(async () => {
const { stdout } = await execa('docker', ['logs', '-t', containerName])
return stdout.includes('Daemon is ready')
})
console.log('docker started')

// Add fixture files
const client = create({ url: IPFS_API_URL })

await Promise.all([
// bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq
client.add('Hello nft.storage! 😎', {
rawLeaves: true,
}),
// bafkreibxkbyybantsznyvlq2bhf24u4gew7pj6erjgduqp4mvqv54qjng4
client.add('Hello nft.storage! 😎😎', {
rawLeaves: true,
}),
// bafkreihbjbbccwxn7hzv5hun5pxuswide7q3lhjvfbvmd7r3kf2sodybgi
client.add('Hello nft.storage! 😎😎😎', {
rawLeaves: true,
}),
])

// bafybeih74zqc6kamjpruyra4e4pblnwdpickrvk4hvturisbtveghflovq
for await (const _ of client.addAll(
globSource(path.join(__dirname, '../test/fixtures/directory'), '**/*'),
{
rawLeaves: true,
wrapWithDirectory: true,
cidVersion: 1,
}
)) {
}
}

if (stop) {
await execa('docker-compose', [
'--file',
composePath,
'--project-name',
project,
'stop',
])
await execa('docker-compose', [
'--file',
composePath,
'--project-name',
project,
'down',
'--volumes',
'--rmi',
'local',
'--remove-orphans',
])
}
}

/**
* @param {number} port
*/
export default async function isPortReachable(
port,
{ host = 'localhost', timeout = 1000 } = {}
) {
if (typeof host !== 'string') {
throw new TypeError('Specify a `host`')
}

const promise = new Promise((resolve, reject) => {
const socket = new net.Socket()

const onError = (err) => {
socket.destroy()
reject(err)
}

socket.setTimeout(timeout)
socket.once('error', onError)
socket.once('timeout', onError)

socket.connect(port, host, () => {
socket.end()
resolve(undefined)
})
})

try {
await promise
return true
} catch {
return false
}
}
1 change: 1 addition & 0 deletions packages/api/test/fixtures/directory/path
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello gateway.nft.storage resource!
2 changes: 1 addition & 1 deletion packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"swagger-ui-react": "^4.10.3"
},
"devDependencies": {
"@playwright/test": "^1.20.1",
"@playwright/test": "^1.29.1",
"@svgr/webpack": "^6.2.1",
"autoprefixer": "^10.4.7",
"eslint": "^8.4.1",
Expand Down
Loading

0 comments on commit 0bd363e

Please sign in to comment.