Skip to content

Commit

Permalink
chore: added test for generateFunctionName
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyubabbar committed Apr 29, 2024
1 parent 63e53b2 commit ec5c30b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/util/customTransformer-faas.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const {
} = require('./openfaas');
const { getLibraryCodeV1 } = require('./customTransforrmationsStore-v1');

const HASH_SECRET = process.env.HASH_SECRET || '';

const HASH_SECRET = process.env.OPENFAAS_FN_HASH_SECRET || '';
const libVersionIdsCache = new NodeCache();

function generateFunctionName(userTransformation, libraryVersionIds, testMode, hashSecret = '') {
Expand All @@ -28,7 +27,7 @@ function generateFunctionName(userTransformation, libraryVersionIds, testMode, h
);

if (hashSecret !== '') {
ids = ids.concat([hashSecret]).sort();
ids = ids.concat([hashSecret]);
}

// FIXME: Why the id's are sorted ?!
Expand Down Expand Up @@ -98,7 +97,12 @@ async function setOpenFaasUserTransform(
};
const functionName =
pregeneratedFnName ||
generateFunctionName(userTransformation, libraryVersionIds, testMode, HASH_SECRET);
generateFunctionName(
userTransformation,
libraryVersionIds,
testMode,
process.env.OPENFAAS_FN_HASH_SECRET,
);
const setupTime = new Date();

await setupFaasFunction(
Expand Down Expand Up @@ -142,8 +146,9 @@ async function runOpenFaasUserTransform(
userTransformation,
libraryVersionIds,
testMode,
HASH_SECRET,
process.env.OPENFAAS_FN_HASH_SECRET,
);

if (testMode) {
await setOpenFaasUserTransform(
userTransformation,
Expand Down
22 changes: 20 additions & 2 deletions test/__tests__/user_transformation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.mock("axios", () => ({
put: jest.fn()
}));

const { generateFunctionName } = require('../../src/util/customTransformer-faas.js');
const { Response, Headers } = jest.requireActual("node-fetch");
const lodashCore = require("lodash/core");
const _ = require("lodash");
Expand Down Expand Up @@ -90,8 +91,12 @@ const pyLibCode = (name, versionId) => {
}
}

const pyfaasFuncName = (workspaceId, versionId, libraryVersionIds=[]) => {
const ids = [workspaceId, versionId].concat(libraryVersionIds.sort());
const pyfaasFuncName = (workspaceId, versionId, libraryVersionIds=[], hashSecret="") => {
let ids = [workspaceId, versionId].concat(libraryVersionIds.sort());
if (hashSecret !== "") {
ids = ids.concat([hashSecret]);
}

const hash = crypto.createHash('md5').update(`${ids}`).digest('hex');

return `fn-${workspaceId}-${hash}`
Expand All @@ -107,6 +112,19 @@ const getfetchResponse = (resp, url) =>

let importNameLibraryVersionIdsMap;

describe("User transformation utils", () => {

it("generates the openfaas-fn name correctly", () => {
const fnName = generateFunctionName(
{workspaceId: 'workspaceId', transformationId: 'transformationId'},
[],
false,
'hash-secret');
expect(fnName).toEqual('fn-workspaceid-34a32ade07ebbc7bc5ea795b8200de9f');
});

});

describe("User transformation", () => {
beforeEach(() => {
jest.resetAllMocks();
Expand Down

0 comments on commit ec5c30b

Please sign in to comment.