From ec5c30bf749b5aa499acaf94ca97f28938c11732 Mon Sep 17 00:00:00 2001 From: Abhimanyu Babbar Date: Mon, 29 Apr 2024 16:32:24 +0530 Subject: [PATCH] chore: added test for generateFunctionName --- src/util/customTransformer-faas.js | 15 ++++++++++----- test/__tests__/user_transformation.test.js | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/util/customTransformer-faas.js b/src/util/customTransformer-faas.js index faa6a6ffa2..9ac9804097 100644 --- a/src/util/customTransformer-faas.js +++ b/src/util/customTransformer-faas.js @@ -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 = '') { @@ -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 ?! @@ -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( @@ -142,8 +146,9 @@ async function runOpenFaasUserTransform( userTransformation, libraryVersionIds, testMode, - HASH_SECRET, + process.env.OPENFAAS_FN_HASH_SECRET, ); + if (testMode) { await setOpenFaasUserTransform( userTransformation, diff --git a/test/__tests__/user_transformation.test.js b/test/__tests__/user_transformation.test.js index 77c7eaa257..c4f4a8792a 100644 --- a/test/__tests__/user_transformation.test.js +++ b/test/__tests__/user_transformation.test.js @@ -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"); @@ -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}` @@ -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();