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 () => {
await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
const aliasPath = `${tempDir}/invalidalias.vhd`
const targetPath = `${tempDir}/targets.vhd`
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const aliasPath = 'invalidalias.vhd'
const targetPath = 'targets.vhd'
expect(async () => await VhdAbstract.createAlias(handler, aliasPath, targetPath)).rejects.toThrow()
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 () => {
await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
const aliasPath = `${tempDir}/valid.alias.vhd`
const targetPath = `${tempDir}/an.other.valid.alias.vhd`
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const aliasPath = 'valid.alias.vhd'
const targetPath = 'an.other.valid.alias.vhd'
expect(async () => await VhdAbstract.createAlias(handler, aliasPath, targetPath)).rejects.toThrow()
expect(await fs.exists(aliasPath)).toEqual(false)
})
@@ -78,19 +78,17 @@ test('It rename and unlink a VHDFile', async () => {
const initalSize = 4
const rawFileName = `${tempDir}/randomfile`
await createRandomFile(rawFileName, initalSize)
const vhdFileName = `${tempDir}/randomfile.vhd`
await convertFromRawToVhd(rawFileName, vhdFileName)
await convertFromRawToVhd(rawFileName, `${tempDir}/randomfile.vhd`)
await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
const { size } = await fs.stat(vhdFileName)
const targetFileName = `${tempDir}/renamed.vhd`
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const { size } = await fs.stat(`${tempDir}/randomfile.vhd`)
await VhdAbstract.rename(handler, vhdFileName, targetFileName)
expect(await fs.exists(vhdFileName)).toEqual(false)
const { size: renamedSize } = await fs.stat(targetFileName)
await VhdAbstract.rename(handler, 'randomfile.vhd', 'renamed.vhd')
expect(await fs.exists(`${tempDir}/randomfile.vhd`)).toEqual(false)
const { size: renamedSize } = await fs.stat(`${tempDir}/renamed.vhd`)
expect(size).toEqual(renamedSize)
await VhdAbstract.unlink(handler, targetFileName)
expect(await fs.exists(targetFileName)).toEqual(false)
await VhdAbstract.unlink(handler, 'renamed.vhd')
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 Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
const vhd = yield openVhd(handler, vhdDirectory)
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const vhd = yield openVhd(handler, 'randomfile.dir')
expect(vhd.header.cookie).toEqual('cxsparse')
expect(vhd.footer.cookie).toEqual('conectix')
const targetFileName = `${tempDir}/renamed.vhd`
await VhdAbstract.rename(handler, vhdDirectory, targetFileName)
expect(await fs.exists(vhdDirectory)).toEqual(false)
await VhdAbstract.unlink(handler, targetFileName)
expect(await fs.exists(targetFileName)).toEqual(false)
await VhdAbstract.rename(handler, 'randomfile.dir', 'renamed.vhd')
expect(await fs.exists(`${tempDir}/randomfile.dir`)).toEqual(false)
await VhdAbstract.unlink(handler, `renamed.vhd`)
expect(await fs.exists(`${tempDir}/renamed.vhd`)).toEqual(false)
})
})
@@ -123,17 +120,17 @@ test('It create , rename and unlink alias', async () => {
const aliasFileNameRenamed = `${tempDir}/aliasFileNameRenamed.alias.vhd`
await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
await VhdAbstract.createAlias(handler, aliasFileName, vhdFileName)
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
await VhdAbstract.createAlias(handler, 'aliasFileName.alias.vhd', 'randomfile.vhd')
expect(await fs.exists(aliasFileName)).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(vhdFileName)).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(vhdFileName)).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 () => {
await Disposable.use(async function* () {
// same directory
const handler = yield getSyncedHandler({ url: 'file:///' })
const tempDirFomRemoteUrl = tempDir.slice(1) // remove the / which is included in the remote url
const alias = `${tempDirFomRemoteUrl}/alias.alias.vhd`
await handler.writeFile(alias, 'target.vhd')
expect(await resolveAlias(handler, alias)).toEqual(`${tempDirFomRemoteUrl}/target.vhd`)
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
await handler.mkdir(`alias`)
const aliasPath = 'alias/alias.alias.vhd'
const testOneCombination = async ({ targetPath, targetContent }) => {
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
await handler.mkdir(`${tempDirFomRemoteUrl}/sub/`)
await handler.writeFile(alias, 'sub/target.vhd', { flags: 'w' })
expect(await resolveAlias(handler, alias)).toEqual(`${tempDirFomRemoteUrl}/sub/target.vhd`)
for (const { targetPath, targetContent } of combinations) {
await testOneCombination({ targetPath, targetContent })
}
})
})
test('resolve throws an error an alias to an alias', async () => {
await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
const alias = `${tempDir}/alias.alias.vhd`
const target = `${tempDir}/target.alias.vhd`
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const alias = 'alias.alias.vhd'
const target = 'target.alias.vhd'
await handler.writeFile(alias, target)
expect(async () => await resolveAlias(handler, alias)).rejects.toThrow(Error)
})

View File

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