diff --git a/package.json b/package.json index e9b81bf..166fdc7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@codefresh-io/task-logger", - "version": "1.0.0", + "version": "1.0.1", "description": "Codefresh utilities for working with build logs", "main": "index.js", "scripts": { diff --git a/taskLogger/firebase/TaskLogger.js b/taskLogger/firebase/TaskLogger.js index 3cbae9b..8f2804b 100644 --- a/taskLogger/firebase/TaskLogger.js +++ b/taskLogger/firebase/TaskLogger.js @@ -123,6 +123,29 @@ class FirebaseTaskLogger extends BaseTaskLogger { }); } + saveExportedVariables(vars = []) { + function splitEnvVarPair(envVarPair) { + const arr = envVarPair.split('='); + const key = _.head(arr.splice(0, 1)); + return { [key]: arr.join('=') }; + } + + const varObj = _(vars).map(splitEnvVarPair).reduce((a, b) => ({ ...a, ...b }), {}); + return Q.resolve(this.baseRef.child('debug').update({ + 'cf-export': { ...varObj } + })); + } + + loadExportedVariables() { + const value = Q.defer(); + this.baseRef.child('debug/cf-export').once('value', (snapshot) => { + if (value.promise.isPending()) { + value.resolve(snapshot.val()); + } + }); + return Q.timeout(value.promise, 5000); // Wait for value 5 sec. Reject if no value got + } + async restore() { const extraPrintData = { jobId: this.jobId }; return wrapWithRetry(async () => { diff --git a/taskLogger/firebase/test/TaskLogger.unit.spec.js b/taskLogger/firebase/test/TaskLogger.unit.spec.js index 0d0a83e..f522e11 100644 --- a/taskLogger/firebase/test/TaskLogger.unit.spec.js +++ b/taskLogger/firebase/test/TaskLogger.unit.spec.js @@ -113,6 +113,65 @@ describe('Firebase TaskLogger tests', () => { expect(result).to.be.equal('\x03'); streams._destroyStreams(); }); + + it('should save variables', async () => { + const taskLogger = await getTaskLoggerInstanceWithDebugger(); + const updateSpy = sinon.spy(); + taskLogger.baseRef = { + child: () => ({ + update: updateSpy, + }) + }; + await taskLogger.saveExportedVariables([ + 'qwerty=qwerty', + 'qwerty2=qwer=t=y2', + ]); + expect(updateSpy).to.have.been.calledWith({ + 'cf-export': { + qwerty: 'qwerty', + qwerty2: 'qwer=t=y2', + } + }); + }); + + it('should load variables', async () => { + const taskLogger = await getTaskLoggerInstanceWithDebugger(); + taskLogger.baseRef = { + child: () => ({ + once: (event, handler) => { + setTimeout(handler.bind(null, { + val: () => ({ + var1: 'value', + }), + }), 1000); + }, + }) + }; + const vars = await taskLogger.loadExportedVariables(); + expect(vars).to.be.eql({ + var1: 'value', + }); + }); + + it('should fail by timeout when load variables', async () => { + const taskLogger = await getTaskLoggerInstanceWithDebugger(); + taskLogger.baseRef = { + child: () => ({ + once: (event, handler) => { + setTimeout(handler.bind(null, { + val: () => ({ + var1: 'value', + }), + }), 10000); + }, + }) + }; + try { + await taskLogger.loadExportedVariables(); + } catch (err) { + expect(err.message).to.be.equal('Timed out after 5000 ms'); + } + }).timeout(11000); }); describe('negative', () => {