summaryrefslogtreecommitdiff
path: root/test/parallel/test-trace-events-fs-sync.js
diff options
context:
space:
mode:
authorChin Huang <chhuang@us.ibm.com>2018-03-21 11:07:23 -0700
committerJames M Snell <jasnell@gmail.com>2018-04-14 12:45:25 -0700
commit09c63460ebda30c7d5e7f40532a311fb2139803a (patch)
tree42c971e8c15751ffef0c7567b86a6904194bce79 /test/parallel/test-trace-events-fs-sync.js
parentc1a05e5c26492a2107d49cbc3ef86baece578a79 (diff)
downloadandroid-node-v8-09c63460ebda30c7d5e7f40532a311fb2139803a.tar.gz
android-node-v8-09c63460ebda30c7d5e7f40532a311fb2139803a.tar.bz2
android-node-v8-09c63460ebda30c7d5e7f40532a311fb2139803a.zip
src: add sync trace to fs
Add sync trace to fs operations which is enabled by the node.fs.sync trace event category. Also add a general test js file to verify all operations. PR-URL: https://github.com/nodejs/node/pull/19649 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'test/parallel/test-trace-events-fs-sync.js')
-rw-r--r--test/parallel/test-trace-events-fs-sync.js162
1 files changed, 162 insertions, 0 deletions
diff --git a/test/parallel/test-trace-events-fs-sync.js b/test/parallel/test-trace-events-fs-sync.js
new file mode 100644
index 0000000000..5bd9b99ba3
--- /dev/null
+++ b/test/parallel/test-trace-events-fs-sync.js
@@ -0,0 +1,162 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const cp = require('child_process');
+const fs = require('fs');
+
+const tests = new Array();
+const traceFile = 'node_trace.1.log';
+
+let gid = 1;
+let uid = 1;
+let skipSymlinks = false;
+
+// On Windows, creating symlinks requires admin privileges.
+// We'll check if we have enough privileges.
+if (common.isWindows) {
+ try {
+ const o = cp.execSync('whoami /priv');
+ if (!o.includes('SeCreateSymbolicLinkPrivilege')) {
+ skipSymlinks = true;
+ }
+ } catch (er) {
+ // better safe than sorry
+ skipSymlinks = true;
+ }
+} else {
+ gid = process.getgid();
+ uid = process.getuid();
+}
+
+tests['fs.sync.access'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.accessSync("fs.txt");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.chmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.chmodSync("fs.txt",100);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.chown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.chownSync("fs.txt",' + uid + ',' + gid + ');' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.close'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.copyfile'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.copyFileSync("fs.txt","a.txt");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.fchmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'const fd = fs.openSync("fs.txt", "r+");' +
+ 'fs.fchmodSync(fd,100);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.fchown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'const fd = fs.openSync("fs.txt", "r+");' +
+ 'fs.fchownSync(fd,' + uid + ',' + gid + ');' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.fdatasync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'const fd = fs.openSync("fs.txt", "r+");' +
+ 'fs.fdatasyncSync(fd);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.fstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.readFileSync("fs.txt");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.fsync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'const fd = fs.openSync("fs.txt", "r+");' +
+ 'fs.fsyncSync(fd);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'const fd = fs.openSync("fs.txt", "r+");' +
+ 'fs.ftruncateSync(fd, 1);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.futimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'const fd = fs.openSync("fs.txt", "r+");' +
+ 'fs.futimesSync(fd,1,1);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.link'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.linkSync("fs.txt", "linkx");' +
+ 'fs.unlinkSync("linkx");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.lstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.lstatSync("fs.txt");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp");' +
+ 'fs.rmdirSync("fstemp")';
+tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstest");' +
+ 'fs.rmdirSync(fp)';
+tests['fs.sync.open'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.read'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.readFileSync("fs.txt");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.readdir'] = 'fs.readdirSync("./")';
+tests['fs.sync.realpath'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.linkSync("fs.txt", "linkx");' +
+ 'fs.realpathSync.native("linkx");' +
+ 'fs.unlinkSync("linkx");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.rename'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.renameSync("fs.txt","xyz.txt"); ' +
+ 'fs.unlinkSync("xyz.txt")';
+tests['fs.sync.rmdir'] = 'fs.mkdirSync("fstemp");' +
+ 'fs.rmdirSync("fstemp")';
+tests['fs.sync.stat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.statSync("fs.txt");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.unlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.linkSync("fs.txt", "linkx");' +
+ 'fs.unlinkSync("linkx");' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.utimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.utimesSync("fs.txt",1,1);' +
+ 'fs.unlinkSync("fs.txt")';
+tests['fs.sync.write'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.unlinkSync("fs.txt")';
+
+// On windows, we need permissions to test symlink and readlink.
+// We'll only try to run these tests if we have enough privileges.
+if (!skipSymlinks) {
+ tests['fs.sync.symlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.symlinkSync("fs.txt", "linkx");' +
+ 'fs.unlinkSync("linkx");' +
+ 'fs.unlinkSync("fs.txt")';
+ tests['fs.sync.readlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
+ 'fs.symlinkSync("fs.txt", "linkx");' +
+ 'fs.readlinkSync("linkx");' +
+ 'fs.unlinkSync("linkx");' +
+ 'fs.unlinkSync("fs.txt")';
+}
+
+const tmpdir = require('../common/tmpdir');
+tmpdir.refresh();
+process.chdir(tmpdir.path);
+
+for (const tr in tests) {
+ const proc = cp.spawnSync(process.execPath,
+ [ '--trace-events-enabled',
+ '--trace-event-categories', 'node.fs.sync',
+ '-e', tests[tr] ]);
+ // Some AIX versions don't support futimes or utimes, so skip.
+ if (common.isAIX && proc.status !== 0 && tr === 'fs.sync.futimes') {
+ continue;
+ }
+ if (common.isAIX && proc.status !== 0 && tr === 'fs.sync.utimes') {
+ continue;
+ }
+
+ // Make sure the operation is successful.
+ assert.strictEqual(proc.status, 0, tr + ': ' + proc.stderr);
+
+ // Confirm that trace log file is created.
+ assert(common.fileExists(traceFile));
+ const data = fs.readFileSync(traceFile);
+ const traces = JSON.parse(data.toString()).traceEvents;
+ assert(traces.length > 0);
+
+ // C++ fs sync trace events should be generated.
+ assert(traces.some((trace) => {
+ if (trace.pid !== proc.pid)
+ return false;
+ if (trace.cat !== 'node,node.fs,node.fs.sync')
+ return false;
+ if (trace.name !== tr)
+ return false;
+ return true;
+ }));
+}