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

Cf export tool3 #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions taskLogger/firebase/TaskLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
59 changes: 59 additions & 0 deletions taskLogger/firebase/test/TaskLogger.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down