Compare commits

...

2 Commits

Author SHA1 Message Date
Florent Beauchamp
d6d7e87fe5 fix: remove root need for openVhd.integ.spec.js and merge.integ.spec.js 2021-11-12 11:24:00 +01:00
Florent Beauchamp
00f02c795f feat(vhd-lib): tests shouldn't need root access to run 2021-11-10 14:06:24 +01:00
4 changed files with 92 additions and 85 deletions

View File

@@ -55,9 +55,9 @@ test('It creates an alias', async () => {
test('alias must have *.alias.vhd extension', async () => { test('alias must have *.alias.vhd extension', async () => {
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const aliasPath = `${tempDir}/invalidalias.vhd` const aliasPath = 'invalidalias.vhd'
const targetPath = `${tempDir}/targets.vhd` const targetPath = 'targets.vhd'
expect(async () => await VhdAbstract.createAlias(handler, aliasPath, targetPath)).rejects.toThrow() expect(async () => await VhdAbstract.createAlias(handler, aliasPath, targetPath)).rejects.toThrow()
expect(await fs.exists(aliasPath)).toEqual(false) expect(await fs.exists(aliasPath)).toEqual(false)
@@ -66,9 +66,9 @@ test('alias must have *.alias.vhd extension', async () => {
test('alias must not be chained', async () => { test('alias must not be chained', async () => {
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const aliasPath = `${tempDir}/valid.alias.vhd` const aliasPath = 'valid.alias.vhd'
const targetPath = `${tempDir}/an.other.valid.alias.vhd` const targetPath = 'an.other.valid.alias.vhd'
expect(async () => await VhdAbstract.createAlias(handler, aliasPath, targetPath)).rejects.toThrow() expect(async () => await VhdAbstract.createAlias(handler, aliasPath, targetPath)).rejects.toThrow()
expect(await fs.exists(aliasPath)).toEqual(false) expect(await fs.exists(aliasPath)).toEqual(false)
}) })
@@ -78,19 +78,17 @@ test('It rename and unlink a VHDFile', async () => {
const initalSize = 4 const initalSize = 4
const rawFileName = `${tempDir}/randomfile` const rawFileName = `${tempDir}/randomfile`
await createRandomFile(rawFileName, initalSize) await createRandomFile(rawFileName, initalSize)
const vhdFileName = `${tempDir}/randomfile.vhd` await convertFromRawToVhd(rawFileName, `${tempDir}/randomfile.vhd`)
await convertFromRawToVhd(rawFileName, vhdFileName)
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const { size } = await fs.stat(vhdFileName) const { size } = await fs.stat(`${tempDir}/randomfile.vhd`)
const targetFileName = `${tempDir}/renamed.vhd`
await VhdAbstract.rename(handler, vhdFileName, targetFileName) await VhdAbstract.rename(handler, 'randomfile.vhd', 'renamed.vhd')
expect(await fs.exists(vhdFileName)).toEqual(false) expect(await fs.exists(`${tempDir}/randomfile.vhd`)).toEqual(false)
const { size: renamedSize } = await fs.stat(targetFileName) const { size: renamedSize } = await fs.stat(`${tempDir}/renamed.vhd`)
expect(size).toEqual(renamedSize) expect(size).toEqual(renamedSize)
await VhdAbstract.unlink(handler, targetFileName) await VhdAbstract.unlink(handler, 'renamed.vhd')
expect(await fs.exists(targetFileName)).toEqual(false) expect(await fs.exists(`${tempDir}/renamed.vhd`)).toEqual(false)
}) })
}) })
@@ -100,16 +98,15 @@ test('It rename and unlink a VhdDirectory', async () => {
await createRandomVhdDirectory(vhdDirectory, initalSize) await createRandomVhdDirectory(vhdDirectory, initalSize)
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const vhd = yield openVhd(handler, vhdDirectory) const vhd = yield openVhd(handler, 'randomfile.dir')
expect(vhd.header.cookie).toEqual('cxsparse') expect(vhd.header.cookie).toEqual('cxsparse')
expect(vhd.footer.cookie).toEqual('conectix') expect(vhd.footer.cookie).toEqual('conectix')
const targetFileName = `${tempDir}/renamed.vhd` await VhdAbstract.rename(handler, 'randomfile.dir', 'renamed.vhd')
await VhdAbstract.rename(handler, vhdDirectory, targetFileName) expect(await fs.exists(`${tempDir}/randomfile.dir`)).toEqual(false)
expect(await fs.exists(vhdDirectory)).toEqual(false) await VhdAbstract.unlink(handler, `renamed.vhd`)
await VhdAbstract.unlink(handler, targetFileName) expect(await fs.exists(`${tempDir}/renamed.vhd`)).toEqual(false)
expect(await fs.exists(targetFileName)).toEqual(false)
}) })
}) })
@@ -123,17 +120,17 @@ test('It create , rename and unlink alias', async () => {
const aliasFileNameRenamed = `${tempDir}/aliasFileNameRenamed.alias.vhd` const aliasFileNameRenamed = `${tempDir}/aliasFileNameRenamed.alias.vhd`
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
await VhdAbstract.createAlias(handler, aliasFileName, vhdFileName) await VhdAbstract.createAlias(handler, 'aliasFileName.alias.vhd', 'randomfile.vhd')
expect(await fs.exists(aliasFileName)).toEqual(true) expect(await fs.exists(aliasFileName)).toEqual(true)
expect(await fs.exists(vhdFileName)).toEqual(true) expect(await fs.exists(vhdFileName)).toEqual(true)
await VhdAbstract.rename(handler, aliasFileName, aliasFileNameRenamed) await VhdAbstract.rename(handler, 'aliasFileName.alias.vhd', 'aliasFileNameRenamed.alias.vhd')
expect(await fs.exists(aliasFileName)).toEqual(false) expect(await fs.exists(aliasFileName)).toEqual(false)
expect(await fs.exists(vhdFileName)).toEqual(true) expect(await fs.exists(vhdFileName)).toEqual(true)
expect(await fs.exists(aliasFileNameRenamed)).toEqual(true) expect(await fs.exists(aliasFileNameRenamed)).toEqual(true)
await VhdAbstract.unlink(handler, aliasFileNameRenamed) await VhdAbstract.unlink(handler, 'aliasFileNameRenamed.alias.vhd')
expect(await fs.exists(aliasFileName)).toEqual(false) expect(await fs.exists(aliasFileName)).toEqual(false)
expect(await fs.exists(vhdFileName)).toEqual(false) expect(await fs.exists(vhdFileName)).toEqual(false)
expect(await fs.exists(aliasFileNameRenamed)).toEqual(false) expect(await fs.exists(aliasFileNameRenamed)).toEqual(false)

View File

@@ -32,24 +32,34 @@ test('resolve return the path in argument for a non alias file ', async () => {
test('resolve get the path of the target file for an alias', async () => { test('resolve get the path of the target file for an alias', async () => {
await Disposable.use(async function* () { await Disposable.use(async function* () {
// same directory // same directory
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const tempDirFomRemoteUrl = tempDir.slice(1) // remove the / which is included in the remote url await handler.mkdir(`alias`)
const alias = `${tempDirFomRemoteUrl}/alias.alias.vhd` const aliasPath = 'alias/alias.alias.vhd'
await handler.writeFile(alias, 'target.vhd') const testOneCombination = async ({ targetPath, targetContent }) => {
expect(await resolveAlias(handler, alias)).toEqual(`${tempDirFomRemoteUrl}/target.vhd`) await handler.writeFile(aliasPath, targetPath, { flags: 'w' })
const resolved = await resolveAlias(handler, aliasPath)
expect(resolved).toEqual(targetContent)
await handler.unlink(aliasPath)
}
// the alias contain the relative path to the file. The resolved values is the full path from the root of the remote
const combinations = [
{ targetPath: `../targets.vhd`, targetContent: `targets.vhd` },
{ targetPath: `targets.vhd`, targetContent: `alias/targets.vhd` },
{ targetPath: `sub/targets.vhd`, targetContent: `alias/sub/targets.vhd` },
{ targetPath: `../sibling/targets.vhd`, targetContent: `sibling/targets.vhd` },
]
// different directory for (const { targetPath, targetContent } of combinations) {
await handler.mkdir(`${tempDirFomRemoteUrl}/sub/`) await testOneCombination({ targetPath, targetContent })
await handler.writeFile(alias, 'sub/target.vhd', { flags: 'w' }) }
expect(await resolveAlias(handler, alias)).toEqual(`${tempDirFomRemoteUrl}/sub/target.vhd`)
}) })
}) })
test('resolve throws an error an alias to an alias', async () => { test('resolve throws an error an alias to an alias', async () => {
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const alias = `${tempDir}/alias.alias.vhd` const alias = 'alias.alias.vhd'
const target = `${tempDir}/target.alias.vhd` const target = 'target.alias.vhd'
await handler.writeFile(alias, target) await handler.writeFile(alias, target)
expect(async () => await resolveAlias(handler, alias)).rejects.toThrow(Error) expect(async () => await resolveAlias(handler, alias)).rejects.toThrow(Error)
}) })

View File

@@ -26,53 +26,53 @@ afterEach(async () => {
test('coalesce works in normal cases', async () => { test('coalesce works in normal cases', async () => {
const mbOfRandom = 5 const mbOfRandom = 5
const randomFileName = `${tempDir}/randomfile` const randomFilePath = `${tempDir}/randomfile`
const random2FileName = `${tempDir}/randomfile2` const random2FilePath = `${tempDir}/randomfile2`
const smallRandomFileName = `${tempDir}/small_randomfile` const smallRandomFilePath = `${tempDir}/small_randomfile`
const parentFileName = `${tempDir}/parent.vhd` const parentFilePath = `${tempDir}/parent.vhd`
const child1FileName = `${tempDir}/child1.vhd` const child1FilePath = `${tempDir}/child1.vhd`
const child2FileName = `${tempDir}/child2.vhd` const child2FilePath = `${tempDir}/child2.vhd`
const recoveredFileName = `${tempDir}/recovered` const recoveredFilePath = `${tempDir}/recovered`
await createRandomFile(randomFileName, mbOfRandom) await createRandomFile(randomFilePath, mbOfRandom)
await createRandomFile(smallRandomFileName, Math.ceil(mbOfRandom / 2)) await createRandomFile(smallRandomFilePath, Math.ceil(mbOfRandom / 2))
await execa('qemu-img', ['create', '-fvpc', parentFileName, mbOfRandom + 1 + 'M']) await execa('qemu-img', ['create', '-fvpc', parentFilePath, mbOfRandom + 1 + 'M'])
await checkFile(parentFileName) await checkFile(parentFilePath)
await convertFromRawToVhd(randomFileName, child1FileName) await convertFromRawToVhd(randomFilePath, child1FilePath)
const handler = getHandler({ url: 'file://' }) const handler = getHandler({ url: `file://${tempDir}/` })
await execa('vhd-util', ['snapshot', '-n', child2FileName, '-p', child1FileName]) await execa('vhd-util', ['snapshot', '-n', child2FilePath, '-p', child1FilePath])
const vhd = new VhdFile(handler, child2FileName) const vhd = new VhdFile(handler, 'child2.vhd')
await vhd.readHeaderAndFooter() await vhd.readHeaderAndFooter()
await vhd.readBlockAllocationTable() await vhd.readBlockAllocationTable()
vhd.footer.creatorApplication = 'xoa' vhd.footer.creatorApplication = 'xoa'
await vhd.writeFooter() await vhd.writeFooter()
const originalSize = await handler._getSize(randomFileName) const originalSize = await handler._getSize('randomfile')
await checkFile(child1FileName) await checkFile(child1FilePath)
await chainVhd(handler, parentFileName, handler, child1FileName, true) await chainVhd(handler, 'parent.vhd', handler, 'child1.vhd', true)
await checkFile(child1FileName) await checkFile(child1FilePath)
await chainVhd(handler, child1FileName, handler, child2FileName, true) await chainVhd(handler, 'child1.vhd', handler, 'child2.vhd', true)
await checkFile(child2FileName) await checkFile(child2FilePath)
const smallRandom = await fs.readFile(smallRandomFileName) const smallRandom = await fs.readFile(smallRandomFilePath)
const newVhd = new VhdFile(handler, child2FileName) const newVhd = new VhdFile(handler, 'child2.vhd')
await newVhd.readHeaderAndFooter() await newVhd.readHeaderAndFooter()
await newVhd.readBlockAllocationTable() await newVhd.readBlockAllocationTable()
await newVhd.writeData(5, smallRandom) await newVhd.writeData(5, smallRandom)
await checkFile(child2FileName) await checkFile(child2FilePath)
await checkFile(child1FileName) await checkFile(child1FilePath)
await checkFile(parentFileName) await checkFile(parentFilePath)
await vhdMerge(handler, parentFileName, handler, child1FileName) await vhdMerge(handler, 'parent.vhd', handler, 'child1.vhd')
await checkFile(parentFileName) await checkFile(parentFilePath)
await chainVhd(handler, parentFileName, handler, child2FileName, true) await chainVhd(handler, 'parent.vhd', handler, 'child2.vhd', true)
await checkFile(child2FileName) await checkFile(child2FilePath)
await vhdMerge(handler, parentFileName, handler, child2FileName) await vhdMerge(handler, 'parent.vhd', handler, 'child2.vhd')
await checkFile(parentFileName) await checkFile(parentFilePath)
await recoverRawContent(parentFileName, recoveredFileName, originalSize) await recoverRawContent(parentFilePath, recoveredFilePath, originalSize)
await execa('cp', [randomFileName, random2FileName]) await execa('cp', [randomFilePath, random2FilePath])
const fd = await fs.open(random2FileName, 'r+') const fd = await fs.open(random2FilePath, 'r+')
try { try {
await fs.write(fd, smallRandom, 0, smallRandom.length, 5 * SECTOR_SIZE) await fs.write(fd, smallRandom, 0, smallRandom.length, 5 * SECTOR_SIZE)
} finally { } finally {
await fs.close(fd) await fs.close(fd)
} }
expect(await fs.readFile(recoveredFileName)).toEqual(await fs.readFile(random2FileName)) expect(await fs.readFile(recoveredFilePath)).toEqual(await fs.readFile(random2FilePath))
}) })

View File

@@ -29,16 +29,16 @@ test('It opens a vhd file ( alias or not)', async () => {
const vhdFileName = `${tempDir}/randomfile.vhd` const vhdFileName = `${tempDir}/randomfile.vhd`
await convertFromRawToVhd(rawFileName, vhdFileName) await convertFromRawToVhd(rawFileName, vhdFileName)
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file://' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}/` })
const vhd = yield openVhd(handler, vhdFileName) const vhd = yield openVhd(handler, 'randomfile.vhd')
expect(vhd.header.cookie).toEqual('cxsparse') expect(vhd.header.cookie).toEqual('cxsparse')
expect(vhd.footer.cookie).toEqual('conectix') expect(vhd.footer.cookie).toEqual('conectix')
const aliasFileName = `${tempDir}/out.alias.vhd` await VhdAbstract.createAlias(handler, 'out.alias.vhd', 'randomfile.vhd')
await VhdAbstract.createAlias(handler, aliasFileName, vhdFileName) const alias = yield openVhd(handler, 'out.alias.vhd')
const alias = yield openVhd(handler, aliasFileName)
expect(alias.header.cookie).toEqual('cxsparse') expect(alias.header.cookie).toEqual('cxsparse')
expect(alias.footer.cookie).toEqual('conectix') expect(alias.footer.cookie).toEqual('conectix')
expect(alias._path?.path).toEqual('/randomfile.vhd')
}) })
}) })
@@ -48,15 +48,15 @@ test('It opens a vhd directory', async () => {
await createRandomVhdDirectory(vhdDirectory, initalSize) await createRandomVhdDirectory(vhdDirectory, initalSize)
await Disposable.use(async function* () { await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file://' }) const handler = yield getSyncedHandler({ url: `file://${tempDir}/` })
const vhd = yield openVhd(handler, vhdDirectory) const vhd = yield openVhd(handler, 'randomfile.dir')
expect(vhd.header.cookie).toEqual('cxsparse') expect(vhd.header.cookie).toEqual('cxsparse')
expect(vhd.footer.cookie).toEqual('conectix') expect(vhd.footer.cookie).toEqual('conectix')
const aliasFileName = `${tempDir}/out.alias.vhd` await VhdAbstract.createAlias(handler, 'out.alias.vhd', 'randomfile.dir')
await VhdAbstract.createAlias(handler, aliasFileName, vhdDirectory) const alias = yield openVhd(handler, 'out.alias.vhd')
const alias = yield openVhd(handler, aliasFileName)
expect(alias.header.cookie).toEqual('cxsparse') expect(alias.header.cookie).toEqual('cxsparse')
expect(alias.footer.cookie).toEqual('conectix') expect(alias.footer.cookie).toEqual('conectix')
expect(alias._path).toEqual('randomfile.dir')
}) })
}) })