Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix root-ownership race conditions in meta-test #262

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions test/common-tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,31 @@ const find = require('which').sync('find')
require('tap').teardown(() => {
// work around windows folder locking
process.chdir(returnCwd)
try {
if (isSudo) {
// running tests as sudo. ensure we didn't leave any root-owned
// files in the cache by mistake.
const args = [ commonCache, '-uid', '0' ]
const found = spawnSync(find, args)
const output = found && found.stdout && found.stdout.toString()
if (output.length) {
const er = new Error('Root-owned files left in cache!')
er.testName = main
er.files = output.trim().split('\n')
throw er
process.on('exit', () => {
try {
if (isSudo) {
// running tests as sudo. ensure we didn't leave any root-owned
// files in the cache by mistake.
const args = [ commonCache, '-uid', '0' ]
const found = spawnSync(find, args)
const output = found && found.stdout && found.stdout.toString()
if (output.length) {
const er = new Error('Root-owned files left in cache!')
er.testName = main
er.files = output.trim().split('\n')
throw er
}
}
if (!process.env.NO_TEST_CLEANUP) {
rimraf.sync(exports.pkg)
rimraf.sync(commonCache)
}
} catch (e) {
if (process.platform !== 'win32') {
throw e
}
}
if (!process.env.NO_TEST_CLEANUP) {
rimraf.sync(exports.pkg)
rimraf.sync(commonCache)
}
} catch (e) {
if (process.platform !== 'win32') {
throw e
}
}
})
})

var port = exports.port = 15443 + testId
Expand Down
20 changes: 20 additions & 0 deletions test/tap/meta-test-flaky-root-ownership-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const t = require('tap')
if (!process.getuid || process.getuid() !== 0 || !process.env.SUDO_UID || !process.env.SUDO_GID) {
t.pass('this test only runs in sudo mode')
t.end()
process.exit(0)
}

const common = require('../common-tap.js')
const fs = require('fs')
const mkdirp = require('mkdirp')
mkdirp.sync(common.cache + '/root/owned')
fs.writeFileSync(common.cache + '/root/owned/file.txt', 'should be chowned')
const chown = require('chownr')

// this will fire after t.teardown() but before process.on('exit')
setTimeout(() => {
chown.sync(common.cache, +process.env.SUDO_UID, +process.env.SUDO_GID)
}, 100)

t.pass('this is fine')