|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +import { execFileSync } from 'child_process'; |
| 16 | +import fs from 'fs'; |
| 17 | +import os from 'os'; |
| 18 | +import path from 'path'; |
15 | 19 | import { expect } from 'chai'; |
16 | | -import DBSQLLogger from '../../lib/DBSQLLogger'; |
| 20 | +import sinon from 'sinon'; |
| 21 | +import winston from 'winston'; |
| 22 | +import DBSQLLogger, { LOG_FILE_UNAVAILABLE_WARNING_CODE } from '../../lib/DBSQLLogger'; |
17 | 23 | import { LogLevel } from '../../lib/contracts/IDBSQLLogger'; |
18 | 24 |
|
| 25 | +async function waitForFileContent(file: string, text: string, timeoutMs = 2000): Promise<string> { |
| 26 | + const deadline = Date.now() + timeoutMs; |
| 27 | + const read = () => (fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : ''); |
| 28 | + while (!read().includes(text) && Date.now() < deadline) { |
| 29 | + // eslint-disable-next-line no-await-in-loop |
| 30 | + await new Promise((resolve) => { |
| 31 | + setTimeout(resolve, 20); |
| 32 | + }); |
| 33 | + } |
| 34 | + return read(); |
| 35 | +} |
| 36 | + |
19 | 37 | describe('DBSQLLogger level subscription', () => { |
20 | 38 | it('getLevel reflects the configured level', () => { |
21 | 39 | const logger = new DBSQLLogger({ level: LogLevel.warn }); |
@@ -60,3 +78,236 @@ describe('DBSQLLogger level subscription', () => { |
60 | 78 | expect(logger.getLevel()).to.equal(LogLevel.debug); |
61 | 79 | }); |
62 | 80 | }); |
| 81 | + |
| 82 | +describe('DBSQLLogger file transport path resolution', () => { |
| 83 | + let tmpDir: string; |
| 84 | + let logger: DBSQLLogger | undefined; |
| 85 | + let fileSettled: Promise<void> | undefined; |
| 86 | + |
| 87 | + // The File transport opens its stream asynchronously; teardown waits for that |
| 88 | + // before closing the logger and deleting the directory. |
| 89 | + function createLogger(filepath: string, level?: LogLevel): DBSQLLogger { |
| 90 | + const instance = new DBSQLLogger({ filepath, level }); |
| 91 | + const file = instance.transports.file; |
| 92 | + fileSettled = file |
| 93 | + ? new Promise<void>((resolve) => { |
| 94 | + file.once('open', () => resolve()); |
| 95 | + file.once('error', () => resolve()); |
| 96 | + }) |
| 97 | + : undefined; |
| 98 | + return instance; |
| 99 | + } |
| 100 | + |
| 101 | + beforeEach(() => { |
| 102 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dbsql-logger-')); |
| 103 | + }); |
| 104 | + |
| 105 | + afterEach(async () => { |
| 106 | + if (fileSettled) { |
| 107 | + await fileSettled; |
| 108 | + } |
| 109 | + logger?.logger.close(); |
| 110 | + logger = undefined; |
| 111 | + fileSettled = undefined; |
| 112 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('writes to a default file inside the directory when filepath is an existing directory', () => { |
| 116 | + logger = createLogger(tmpDir); |
| 117 | + |
| 118 | + expect(logger.transports.file).to.not.be.undefined; |
| 119 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 120 | + expect(logger.transports.file?.filename).to.equal('databricks-sql-nodejs.log'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('writes log lines to the default file inside the directory', async () => { |
| 124 | + logger = createLogger(tmpDir); |
| 125 | + logger.transports.console.silent = true; |
| 126 | + |
| 127 | + await logger.log(LogLevel.info, 'hello from DBSQLLogger test'); |
| 128 | + |
| 129 | + const content = await waitForFileContent(path.join(tmpDir, 'databricks-sql-nodejs.log'), 'hello from'); |
| 130 | + expect(content).to.contain('hello from DBSQLLogger test'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('resolves an existing directory with a trailing separator to the default file inside it', () => { |
| 134 | + logger = createLogger(`${tmpDir}${path.sep}`); |
| 135 | + |
| 136 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 137 | + expect(logger.transports.file?.filename).to.equal('databricks-sql-nodejs.log'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('keeps writing to an existing file when filepath has a trailing separator', () => { |
| 141 | + const filepath = path.join(tmpDir, 'logs'); |
| 142 | + fs.writeFileSync(filepath, ''); |
| 143 | + logger = createLogger(`${filepath}${path.sep}`); |
| 144 | + |
| 145 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 146 | + expect(logger.transports.file?.filename).to.equal('logs'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('treats a non-existent filepath with a trailing separator as a file, as in 3.0.0', () => { |
| 150 | + const filepath = path.join(tmpDir, 'logs'); |
| 151 | + logger = createLogger(`${filepath}${path.sep}`); |
| 152 | + |
| 153 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 154 | + expect(logger.transports.file?.filename).to.equal('logs'); |
| 155 | + expect(fs.statSync(filepath).isFile()).to.be.true; |
| 156 | + }); |
| 157 | + |
| 158 | + it('does not open a FIFO synchronously', async function fifoTest() { |
| 159 | + if (process.platform === 'win32') { |
| 160 | + this.skip(); |
| 161 | + } |
| 162 | + const fifo = path.join(tmpDir, 'pipe.log'); |
| 163 | + execFileSync('mkfifo', [fifo]); |
| 164 | + // A non-blocking reader lets any writer open the FIFO without waiting. Keep |
| 165 | + // it open until winston's own open completes, or that open (and the test |
| 166 | + // process) would hang. |
| 167 | + const readerFd = fs.openSync(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK); |
| 168 | + const openSync = sinon.spy(fs, 'openSync'); |
| 169 | + try { |
| 170 | + logger = createLogger(fifo); |
| 171 | + } finally { |
| 172 | + openSync.restore(); |
| 173 | + } |
| 174 | + await fileSettled; |
| 175 | + fs.closeSync(readerFd); |
| 176 | + |
| 177 | + expect(openSync.called).to.be.false; |
| 178 | + expect(logger.transports.file?.filename).to.equal('pipe.log'); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('when the log file cannot be opened', () => { |
| 182 | + let emitWarning: sinon.SinonStub; |
| 183 | + |
| 184 | + beforeEach(() => { |
| 185 | + emitWarning = sinon.stub(process, 'emitWarning'); |
| 186 | + }); |
| 187 | + |
| 188 | + afterEach(() => { |
| 189 | + emitWarning.restore(); |
| 190 | + }); |
| 191 | + |
| 192 | + const cases = [ |
| 193 | + { |
| 194 | + name: 'a parent path is a regular file', |
| 195 | + filepath: (dir: string) => { |
| 196 | + fs.writeFileSync(path.join(dir, 'not-a-dir'), ''); |
| 197 | + return path.join(dir, 'not-a-dir', 'driver.log'); |
| 198 | + }, |
| 199 | + }, |
| 200 | + { |
| 201 | + name: 'the resolved log file is a directory (EISDIR)', |
| 202 | + filepath: (dir: string) => { |
| 203 | + fs.mkdirSync(path.join(dir, 'databricks-sql-nodejs.log')); |
| 204 | + return dir; |
| 205 | + }, |
| 206 | + }, |
| 207 | + ]; |
| 208 | + |
| 209 | + for (const { name, filepath } of cases) { |
| 210 | + it(`falls back to console only with a process warning when ${name}`, () => { |
| 211 | + logger = createLogger(filepath(tmpDir), LogLevel.error); |
| 212 | + |
| 213 | + expect(logger.transports.file).to.be.undefined; |
| 214 | + expect(emitWarning.calledOnce).to.be.true; |
| 215 | + expect(emitWarning.firstCall.args[0]).to.contain('logging to console only'); |
| 216 | + expect(emitWarning.firstCall.args[1]).to.deep.equal({ code: LOG_FILE_UNAVAILABLE_WARNING_CODE }); |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + it('falls back to console only for a read-only log file (EACCES)', function readOnlyLogFileTest() { |
| 221 | + if (process.platform === 'win32' || process.getuid?.() === 0) { |
| 222 | + this.skip(); |
| 223 | + } |
| 224 | + const filepath = path.join(tmpDir, 'readonly.log'); |
| 225 | + fs.writeFileSync(filepath, ''); |
| 226 | + fs.chmodSync(filepath, 0o444); |
| 227 | + try { |
| 228 | + logger = createLogger(filepath, LogLevel.error); |
| 229 | + |
| 230 | + expect(logger.transports.file).to.be.undefined; |
| 231 | + expect(emitWarning.calledOnce).to.be.true; |
| 232 | + expect(emitWarning.firstCall.args[0]).to.contain('EACCES'); |
| 233 | + expect(emitWarning.firstCall.args[0]).to.contain('logging to console only'); |
| 234 | + expect(emitWarning.firstCall.args[1]).to.deep.equal({ code: LOG_FILE_UNAVAILABLE_WARNING_CODE }); |
| 235 | + } finally { |
| 236 | + fs.chmodSync(filepath, 0o600); |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + it('keeps delivering console logs after the fallback', async () => { |
| 241 | + fs.mkdirSync(path.join(tmpDir, 'databricks-sql-nodejs.log')); |
| 242 | + const seen: string[] = []; |
| 243 | + const consoleLog = sinon.stub(winston.transports.Console.prototype, 'log').callsFake((...args: unknown[]) => { |
| 244 | + seen.push((args[0] as { message: string }).message); |
| 245 | + (args[1] as () => void)(); |
| 246 | + }); |
| 247 | + try { |
| 248 | + logger = createLogger(tmpDir); |
| 249 | + // Far more than the logger's stream buffers hold; an unopened file |
| 250 | + // transport used to stall delivery to every transport at that point. |
| 251 | + const lines = 1000; |
| 252 | + for (let i = 0; i < lines; i += 1) { |
| 253 | + logger.logger.info(`line ${i} ${'x'.repeat(100)}`); |
| 254 | + } |
| 255 | + const deadline = Date.now() + 1500; |
| 256 | + while (seen.length < lines && Date.now() < deadline) { |
| 257 | + // eslint-disable-next-line no-await-in-loop |
| 258 | + await new Promise((resolve) => { |
| 259 | + setTimeout(resolve, 20); |
| 260 | + }); |
| 261 | + } |
| 262 | + |
| 263 | + expect(seen).to.have.length(lines); |
| 264 | + expect(seen[lines - 1]).to.match(new RegExp(`^line ${lines - 1} `)); |
| 265 | + } finally { |
| 266 | + consoleLog.restore(); |
| 267 | + } |
| 268 | + }); |
| 269 | + }); |
| 270 | + |
| 271 | + it('uses a non-existent filepath unchanged', () => { |
| 272 | + const filepath = path.join(tmpDir, 'custom.log'); |
| 273 | + logger = createLogger(filepath); |
| 274 | + |
| 275 | + expect(logger.transports.file).to.not.be.undefined; |
| 276 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 277 | + expect(logger.transports.file?.filename).to.equal('custom.log'); |
| 278 | + }); |
| 279 | + |
| 280 | + it('creates missing parent directories and still attaches the file transport', () => { |
| 281 | + const nestedDir = path.join(tmpDir, 'a', 'b'); |
| 282 | + const filepath = path.join(nestedDir, 'driver.log'); |
| 283 | + logger = createLogger(filepath); |
| 284 | + |
| 285 | + expect(logger.transports.file).to.not.be.undefined; |
| 286 | + expect(logger.transports.file?.dirname).to.equal(nestedDir); |
| 287 | + expect(logger.transports.file?.filename).to.equal('driver.log'); |
| 288 | + expect(fs.statSync(nestedDir).isDirectory()).to.be.true; |
| 289 | + expect(fs.existsSync(filepath)).to.be.true; |
| 290 | + }); |
| 291 | + |
| 292 | + it('does not mkdir an existing directory (mkdir of a Windows drive root throws EPERM)', () => { |
| 293 | + const mkdirSync = sinon.stub(fs, 'mkdirSync').throws(Object.assign(new Error('EPERM'), { code: 'EPERM' })); |
| 294 | + try { |
| 295 | + logger = createLogger(path.join(tmpDir, 'driver.log')); |
| 296 | + } finally { |
| 297 | + mkdirSync.restore(); |
| 298 | + } |
| 299 | + |
| 300 | + expect(mkdirSync.called).to.be.false; |
| 301 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 302 | + expect(logger.transports.file?.filename).to.equal('driver.log'); |
| 303 | + }); |
| 304 | + |
| 305 | + it('uses an existing file path unchanged', () => { |
| 306 | + const filepath = path.join(tmpDir, 'existing.log'); |
| 307 | + fs.writeFileSync(filepath, ''); |
| 308 | + logger = createLogger(filepath); |
| 309 | + |
| 310 | + expect(logger.transports.file?.dirname).to.equal(tmpDir); |
| 311 | + expect(logger.transports.file?.filename).to.equal('existing.log'); |
| 312 | + }); |
| 313 | +}); |
0 commit comments