From 411ba202da77853690c36bb5420067b52bf0cf47 Mon Sep 17 00:00:00 2001 From: rkilpadi Date: Wed, 3 Aug 2022 10:27:53 -0700 Subject: [PATCH 1/2] Move code for getting list of custom bootstrap actions to utils --- frontend/src/old-pages/Clusters/Details.tsx | 21 ++------------------- frontend/src/old-pages/Clusters/util.tsx | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/frontend/src/old-pages/Clusters/Details.tsx b/frontend/src/old-pages/Clusters/Details.tsx index 3ecf47b8..36a9589e 100644 --- a/frontend/src/old-pages/Clusters/Details.tsx +++ b/frontend/src/old-pages/Clusters/Details.tsx @@ -12,7 +12,7 @@ import React from 'react'; import { useNavigate, useParams } from "react-router-dom" import { useState } from '../../store' -import { getIn } from '../../util' +import { getScripts } from './util' // UI Elements import Tabs from "@awsui/components-react/tabs" @@ -35,24 +35,7 @@ export default function ClusterTabs() { const customActions = useState([...clusterPath, 'config', 'HeadNode', 'CustomActions']); const selectedClusterName = useState(['app', 'clusters', 'selected']); - let allScripts = []; - const scriptName = (script: any) => { - let suffix = script.slice(script.lastIndexOf('/') + 1); - return suffix.slice(0, suffix.lastIndexOf('.')); - } - - for(let actionName of ['OnNodeStart', 'OnNodeConfigured']) - { - if(getIn(customActions, [actionName, 'Script'])) - allScripts.push(scriptName(getIn(customActions, [actionName, 'Script']))); - for(let arg of (getIn(customActions, [actionName, 'Args']) || [])) - { - if(arg.length > 0 && arg[0] !== '-') - allScripts.push(scriptName(arg)); - } - } - - let accountingEnabled = allScripts.includes('slurm-accounting'); + let accountingEnabled = getScripts(customActions).includes('slurm-accounting'); let navigate = useNavigate(); let params = useParams(); diff --git a/frontend/src/old-pages/Clusters/util.tsx b/frontend/src/old-pages/Clusters/util.tsx index 243e9ccb..04435a2d 100644 --- a/frontend/src/old-pages/Clusters/util.tsx +++ b/frontend/src/old-pages/Clusters/util.tsx @@ -12,6 +12,7 @@ import jsyaml from 'js-yaml'; import { DescribeCluster, GetConfiguration } from '../../model' import { clearState, getState, setState } from '../../store' +import { getIn } from '../../util' export function selectCluster(clusterName: any, navigate: any) { @@ -26,3 +27,23 @@ export function selectCluster(clusterName: any, navigate: any) if(oldClusterName !== clusterName) setState(['app', 'clusters', 'selected'], name); } + +export function getScripts(customActions: Record | null) { + const scriptName = (script: string) => { + let suffix = script.slice(script.lastIndexOf('/') + 1); + return suffix.slice(0, suffix.lastIndexOf('.')); + } + + let allScripts = []; + for(let actionName of ['OnNodeStart', 'OnNodeConfigured']) + { + if(getIn(customActions, [actionName, 'Script'])) + allScripts.push(scriptName(getIn(customActions, [actionName, 'Script']))); + for(let arg of (getIn(customActions, [actionName, 'Args']) || [])) + { + if(arg.length > 0 && arg[0] !== '-') + allScripts.push(scriptName(arg)); + } + } + return allScripts +} From 6baee057b0065b4b08a32a0ecaa8ced0f40c1706 Mon Sep 17 00:00:00 2001 From: rkilpadi Date: Wed, 3 Aug 2022 11:23:43 -0700 Subject: [PATCH 2/2] Add tests for getScripts --- .../Clusters/__tests__/util.test.tsx | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 frontend/src/old-pages/Clusters/__tests__/util.test.tsx diff --git a/frontend/src/old-pages/Clusters/__tests__/util.test.tsx b/frontend/src/old-pages/Clusters/__tests__/util.test.tsx new file mode 100644 index 00000000..41987202 --- /dev/null +++ b/frontend/src/old-pages/Clusters/__tests__/util.test.tsx @@ -0,0 +1,72 @@ +import { getScripts } from "../util"; + +describe("Given a function to get script names of all custom actions", () => { + + describe("when a custom action to be run on node startup is provided", () => { + it("should return the name of the script", () => { + const scriptNames = getScripts({ + OnNodeStart: { + Script: 'https://www.website.com/test/directory/test-script.sh', + Args: [] + } + }) + expect(scriptNames).toEqual(['test-script']) + }) + }) + + describe("when a custom action to be run on node configuration is provided", () => { + it("should return the name of the script", () => { + const scriptNames = getScripts({ + OnNodeConfigured: { + Script: 'https://www.website.com/test/directory/test-script.sh', + Args: [] + } + }) + expect(scriptNames).toEqual(['test-script']) + }) + }) + + describe("when a custom action with arguments is provided", () => { + it("should return the names of all scripts", () => { + const scriptNames = getScripts({ + OnNodeConfigured: { + Script: 'https://www.website.com/test/test-multi-runner.py', + Args: [ + 'https://www.website.com/directory/test.sh', + '-123456', + '-abcdef' + ] + } + }) + expect(scriptNames.sort()).toEqual(['test', 'test-multi-runner']) + }) + }) + + describe("when multiple custom actions and arguments are provided", () => { + it("should return the names of all scripts", () => { + const scriptNames = getScripts({ + OnNodeStart: { + Script: 'https://www.website.com/test/directory/testing.sh', + Args: ['-abcdef'] + }, + OnNodeConfigured: { + Script: 'https://www.website.com/test/test.py', + Args: [ + 'https://www.website.com/directory/tests.sh', + '-123456', + '-abcdef', + 'https://www.website.com/test/directory/test-multi-runner.py' + ] + } + }) + expect(scriptNames.sort()).toEqual(['test', 'test-multi-runner', 'testing', 'tests']) + }) + }) + + describe("when no custom actions are provided", () => { + it("should return an empty array", () => { + const scriptNames = getScripts(null) + expect(scriptNames).toEqual([]) + }) + }) +})