From bda065e2a438d74e162904be6ea160ecba718cab Mon Sep 17 00:00:00 2001 From: Jaiveer Katariya Date: Thu, 1 Aug 2024 13:08:39 -0400 Subject: [PATCH] this might work --- src/types/privatekubectl.test.ts | 27 ++++++++++++-------- src/types/privatekubectl.ts | 30 ++++++++--------------- src/utilities/manifestUpdateUtils.test.ts | 5 ++-- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/types/privatekubectl.test.ts b/src/types/privatekubectl.test.ts index 3b735f25b..56fdcb2f6 100644 --- a/src/types/privatekubectl.test.ts +++ b/src/types/privatekubectl.test.ts @@ -1,12 +1,13 @@ +import * as fileUtils from '../utilities/fileUtils' import { PrivateKubectl, extractFileNames, - replaceFileNamesWithBaseNames + replaceFileNamesWithNamesRelativeToTemp } from './privatekubectl' import * as exec from '@actions/exec' describe('Private kubectl', () => { - const testString = `kubectl annotate -f testdir/test.yml,test2.yml,testdir/subdir/test3.yml -f test4.yml --filename test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832` + const testString = `kubectl annotate -f /tmp/testdir/test.yml,/tmp/test2.yml,/tmp/testdir/subdir/test3.yml -f /tmp/test4.yml --filename /tmp/test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832` const mockKube = new PrivateKubectl( 'kubectlPath', 'namespace', @@ -15,19 +16,25 @@ describe('Private kubectl', () => { 'resourceName' ) + const spy = jest + .spyOn(fileUtils, 'getTempDirectory') + .mockImplementation(() => { + return '/tmp' + }) + it('should extract filenames correctly', () => { expect(extractFileNames(testString)).toEqual([ - 'testdir/test.yml', - 'test2.yml', - 'testdir/subdir/test3.yml', - 'test4.yml', - 'test5.yml' + '/tmp/testdir/test.yml', + '/tmp/test2.yml', + '/tmp/testdir/subdir/test3.yml', + '/tmp/test4.yml', + '/tmp/test5.yml' ]) }) - it('should replace filenames with basenames correctly', () => { - expect(replaceFileNamesWithBaseNames(testString)).toEqual( - `kubectl annotate -f test.yml,test2.yml,test3.yml -f test4.yml --filename test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832` + it('should replace filenames with relative locations in tmp correctly', () => { + expect(replaceFileNamesWithNamesRelativeToTemp(testString)).toEqual( + `kubectl annotate -f testdir/test.yml,test2.yml,testdir/subdir/test3.yml -f test4.yml --filename test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832` ) }) diff --git a/src/types/privatekubectl.ts b/src/types/privatekubectl.ts index 1d3d96a23..293d9359d 100644 --- a/src/types/privatekubectl.ts +++ b/src/types/privatekubectl.ts @@ -19,8 +19,7 @@ export class PrivateKubectl extends Kubectl { } if (this.containsFilenames(kubectlCmd)) { - // For private clusters, files will referenced solely by their basename - kubectlCmd = replaceFileNamesWithBaseNames(kubectlCmd) + kubectlCmd = replaceFileNamesWithNamesRelativeToTemp(kubectlCmd) addFileFlag = true } @@ -44,21 +43,9 @@ export class PrivateKubectl extends Kubectl { ] if (addFileFlag) { - const filenames = extractFileNames(kubectlCmd) - const tempDirectory = getTempDirectory() eo.cwd = tempDirectory privateClusterArgs.push(...['--file', '.']) - - for (const filename of filenames) { - try { - this.moveFileToTempManifestDir(filename) - } catch (e) { - core.debug( - `Error moving file ${filename} to temp directory: ${e}` - ) - } - } } core.debug( @@ -138,22 +125,25 @@ export class PrivateKubectl extends Kubectl { } } -export function replaceFileNamesWithBaseNames(kubectlCmd: string) { +export function replaceFileNamesWithNamesRelativeToTemp(kubectlCmd: string) { let filenames = extractFileNames(kubectlCmd) - let basenames = filenames.map((filename) => path.basename(filename)) + core.debug(`filenames originally provided in kubectl command: ${filenames}`) + let relativeNames = filenames.map((filename) => + path.relative(getTempDirectory(), filename) + ) let result = kubectlCmd - if (filenames.length != basenames.length) { + if (filenames.length != relativeNames.length) { throw Error( - 'replacing filenames with basenames, ' + + 'replacing filenames with relative path from temp dir, ' + filenames.length + ' filenames != ' + - basenames.length + + relativeNames.length + 'basenames' ) } for (let index = 0; index < filenames.length; index++) { - result = result.replace(filenames[index], basenames[index]) + result = result.replace(filenames[index], relativeNames[index]) } return result } diff --git a/src/utilities/manifestUpdateUtils.test.ts b/src/utilities/manifestUpdateUtils.test.ts index 36001977a..26fb0eaf3 100644 --- a/src/utilities/manifestUpdateUtils.test.ts +++ b/src/utilities/manifestUpdateUtils.test.ts @@ -1,10 +1,11 @@ import * as fileUtils from './fileUtils' import * as manifestUpdateUtils from './manifestUpdateUtils' +import * as path from 'path' import * as fs from 'fs' describe('manifestUpdateUtils', () => { - jest.spyOn(fileUtils, 'getTempDirectory').mockImplementation(() => { - return '/tmp' + jest.spyOn(fileUtils, 'moveFileToTmpDir').mockImplementation((filename) => { + return path.join('/tmp', filename) }) jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {}) jest.spyOn(fs, 'readFileSync').mockImplementation((filename) => {