Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: fix up interface tests
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Sep 23, 2020
1 parent 4bb4b32 commit 1f5fb9f
Show file tree
Hide file tree
Showing 14 changed files with 644 additions and 499 deletions.
30 changes: 14 additions & 16 deletions packages/interface-ipfs-core/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const echoUrl = (text) => `${process.env.ECHO_SERVER}/download?data=${encodeURIC
const redirectUrl = (url) => `${process.env.ECHO_SERVER}/redirect?to=${encodeURI(url)}`
const uint8ArrayFromString = require('uint8arrays/from-string')
const last = require('it-last')
const UnixFS = require('ipfs-unixfs')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -378,21 +379,14 @@ module.exports = (common, options) => {
content: uint8ArrayFromString('some content ' + i)
}))

describe('without sharding', () => {
let ipfs

before(async function () {
const ipfsd = await common.spawn({
ipfsOptions: { EXPERIMENTAL: { sharding: false } }
})
ipfs = ipfsd.api
})

it('should be able to add dir without sharding', async () => {
const { path, cid } = await last(ipfs.addAll(testFiles))
expect(path).to.eql('test-folder')
expect(cid.toString()).to.eql('QmWWM8ZV6GPhqJ46WtKcUaBPNHN5yQaFsKDSQ1RE73w94Q')
})
it('should be able to add dir without sharding', async () => {
const { path, cid } = await last(ipfs.addAll(testFiles))
expect(path).to.eql('test-folder')
console.info(await ipfs.files.stat(`/ipfs/${cid}`))
const { value } = await ipfs.dag.get(cid)
const entry = UnixFS.unmarshal(value.Data)
console.info(entry)
expect(cid.toString()).to.eql('QmWWM8ZV6GPhqJ46WtKcUaBPNHN5yQaFsKDSQ1RE73w94Q')
})

describe('with sharding', () => {
Expand All @@ -406,7 +400,7 @@ module.exports = (common, options) => {
sharding: true
},
config: {
// enabled sharding for go
// enable sharding for go
Experimental: {
ShardingEnabled: true
}
Expand All @@ -419,6 +413,10 @@ module.exports = (common, options) => {
it('should be able to add dir with sharding', async () => {
const { path, cid } = await last(ipfs.addAll(testFiles))
expect(path).to.eql('test-folder')
console.info(await ipfs.files.stat(`/ipfs/${cid}`))
const { value } = await ipfs.dag.get(cid)
const entry = UnixFS.unmarshal(value.Data)
console.info(entry)
expect(cid.toString()).to.eql('Qmb3JNLq2KcvDTSGT23qNQkMrr4Y4fYMktHh6DtC7YatLa')
})
})
Expand Down
152 changes: 87 additions & 65 deletions packages/interface-ipfs-core/src/files/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,71 +304,6 @@ module.exports = (common, options) => {
expect((await ipfs.files.stat(finalDirPath)).type).to.equal('directory')
})

it('copies a file from a normal directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)

const file = `file-${Math.random()}.txt`
const filePath = `/${file}`
const finalFilePath = `${shardedDirPath}/${file}`

await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

await ipfs.files.cp(filePath, finalFilePath)

// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})

it('copies a file from a sharded directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const othershardedDirPath = await createShardedDirectory(ipfs)

const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${othershardedDirPath}/${file}`

await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

await ipfs.files.cp(filePath, finalFilePath)

// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
await expect(isShardAtPath(othershardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(othershardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})

it('copies a file from a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dir = `dir-${Math.random()}`
const dirPath = `/${dir}`

const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${dirPath}/${file}`

await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

await ipfs.files.mkdir(dirPath)

await ipfs.files.cp(filePath, finalFilePath)

// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(dirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})

it('should respect metadata when copying files', async function () {
const testSrcPath = `/test-${nanoid()}`
const testDestPath = `/test-${nanoid()}`
Expand Down Expand Up @@ -445,5 +380,92 @@ module.exports = (common, options) => {
timeout: 1
}))
})

describe('with sharding', () => {
let ipfs

before(async function () {
const ipfsd = await common.spawn({
ipfsOptions: {
EXPERIMENTAL: {
// enable sharding for js
sharding: true
},
config: {
// enable sharding for go
Experimental: {
ShardingEnabled: true
}
}
}
})
ipfs = ipfsd.api
})

it('copies a file from a normal directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)

const file = `file-${Math.random()}.txt`
const filePath = `/${file}`
const finalFilePath = `${shardedDirPath}/${file}`

await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

await ipfs.files.cp(filePath, finalFilePath)

// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})

it('copies a file from a sharded directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const othershardedDirPath = await createShardedDirectory(ipfs)

const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${othershardedDirPath}/${file}`

await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

await ipfs.files.cp(filePath, finalFilePath)

// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
await expect(isShardAtPath(othershardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(othershardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})

it('copies a file from a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dir = `dir-${Math.random()}`
const dirPath = `/${dir}`

const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${dirPath}/${file}`

await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

await ipfs.files.mkdir(dirPath)

await ipfs.files.cp(filePath, finalFilePath)

// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(dirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})
})
})
}
88 changes: 55 additions & 33 deletions packages/interface-ipfs-core/src/files/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,50 +164,72 @@ module.exports = (common, options) => {
}])
})

it('lists a sharded directory contents', async () => {
const fileCount = 1001
const dirPath = await createShardedDirectory(ipfs, fileCount)
const files = await all(ipfs.files.ls(dirPath))

expect(files.length).to.equal(fileCount)
it('should respect timeout option when listing files', async () => {
await testTimeout(() => drain(ipfs.files.ls('/', {
timeout: 1
})))
})

files.forEach(file => {
// should be a file
expect(file.type).to.equal(0)
describe('with sharding', () => {
let ipfs

before(async function () {
const ipfsd = await common.spawn({
ipfsOptions: {
EXPERIMENTAL: {
// enable sharding for js
sharding: true
},
config: {
// enable sharding for go
Experimental: {
ShardingEnabled: true
}
}
}
})
ipfs = ipfsd.api
})
})

it('lists a file inside a sharded directory directly', async () => {
const dirPath = await createShardedDirectory(ipfs)
const files = await all(ipfs.files.ls(dirPath))
const filePath = `${dirPath}/${files[0].name}`
it('lists a sharded directory contents', async () => {
const fileCount = 1001
const dirPath = await createShardedDirectory(ipfs, fileCount)
const files = await all(ipfs.files.ls(dirPath))

// should be able to ls new file directly
const file = await all(ipfs.files.ls(filePath))
expect(files.length).to.equal(fileCount)

expect(file).to.have.lengthOf(1).and.to.containSubset([files[0]])
})
files.forEach(file => {
// should be a file
expect(file.type).to.equal(0)
})
})

it('lists the contents of a directory inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `${shardedDirPath}/subdir-${Math.random()}`
const fileName = `small-file-${Math.random()}.txt`
it('lists a file inside a sharded directory directly', async () => {
const dirPath = await createShardedDirectory(ipfs)
const files = await all(ipfs.files.ls(dirPath))
const filePath = `${dirPath}/${files[0].name}`

await ipfs.files.mkdir(`${dirPath}`)
await ipfs.files.write(`${dirPath}/${fileName}`, Uint8Array.from([0, 1, 2, 3]), {
create: true
// should be able to ls new file directly
const file = await all(ipfs.files.ls(filePath))

expect(file).to.have.lengthOf(1).and.to.containSubset([files[0]])
})

const files = await all(ipfs.files.ls(dirPath))
it('lists the contents of a directory inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `${shardedDirPath}/subdir-${Math.random()}`
const fileName = `small-file-${Math.random()}.txt`

expect(files.length).to.equal(1)
expect(files.filter(file => file.name === fileName)).to.be.ok()
})
await ipfs.files.mkdir(`${dirPath}`)
await ipfs.files.write(`${dirPath}/${fileName}`, Uint8Array.from([0, 1, 2, 3]), {
create: true
})

it('should respect timeout option when listing files', async () => {
await testTimeout(() => drain(ipfs.files.ls('/', {
timeout: 1
})))
const files = await all(ipfs.files.ls(dirPath))

expect(files.length).to.equal(1)
expect(files.filter(file => file.name === fileName)).to.be.ok()
})
})
})
}
48 changes: 35 additions & 13 deletions packages/interface-ipfs-core/src/files/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,6 @@ module.exports = (common, options) => {
.that.satisfies(hash => multihash.decode(hash).name === 'sha2-512')
})

it('makes a directory inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `${shardedDirPath}/subdir-${Math.random()}`

await ipfs.files.mkdir(`${dirPath}`)

await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
await expect(ipfs.files.stat(shardedDirPath)).to.eventually.have.property('type', 'directory')

await expect(isShardAtPath(dirPath, ipfs)).to.eventually.be.false()
await expect(ipfs.files.stat(dirPath)).to.eventually.have.property('type', 'directory')
})

it('should make directory and have default mode', async function () {
await testMode(undefined, parseInt('0755', 8))
})
Expand Down Expand Up @@ -231,5 +218,40 @@ module.exports = (common, options) => {
timeout: 1
}))
})

describe('with sharding', () => {
let ipfs

before(async function () {
const ipfsd = await common.spawn({
ipfsOptions: {
EXPERIMENTAL: {
// enable sharding for js
sharding: true
},
config: {
// enable sharding for go
Experimental: {
ShardingEnabled: true
}
}
}
})
ipfs = ipfsd.api
})

it('makes a directory inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `${shardedDirPath}/subdir-${Math.random()}`

await ipfs.files.mkdir(`${dirPath}`)

await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
await expect(ipfs.files.stat(shardedDirPath)).to.eventually.have.property('type', 'directory')

await expect(isShardAtPath(dirPath, ipfs)).to.eventually.be.false()
await expect(ipfs.files.stat(dirPath)).to.eventually.have.property('type', 'directory')
})
})
})
}
Loading

0 comments on commit 1f5fb9f

Please sign in to comment.