Skip to content

Commit dcaec01

Browse files
authored
Check for error from Azure when using the private-cluster feature (#270)
* Check for error from Azure Move the error check for Azure earlier, so that a well defined error is thrown on error instead of a JSONSyntax error. The issue is that when Azure returns an error, like when there's an issue with the access to the principal used. When this happens, the stdout field will be an empty string, and the error message will be set. * Restore check for deserialized exitCode
1 parent 7dae909 commit dcaec01

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/types/privatekubectl.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import {PrivateKubectl} from './privatekubectl'
2+
import * as exec from '@actions/exec'
23

34
describe('Private kubectl', () => {
45
const testString = `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`
5-
const mockKube = new PrivateKubectl('')
6+
const mockKube = new PrivateKubectl(
7+
'kubectlPath',
8+
'namespace',
9+
true,
10+
'resourceGroup',
11+
'resourceName'
12+
)
613

714
it('should extract filenames correctly', () => {
815
expect(mockKube.extractFilesnames(testString)).toEqual(
916
'test.yml test2.yml test3.yml test4.yml test5.yml'
1017
)
1118
})
19+
20+
test('Should throw well defined Error on error from Azure', async () => {
21+
const errorMsg = 'An error message'
22+
jest.spyOn(exec, 'getExecOutput').mockImplementation(async () => {
23+
return {exitCode: 1, stdout: '', stderr: errorMsg}
24+
})
25+
26+
await expect(mockKube.executeCommand('az', 'test')).rejects.toThrow(
27+
Error(
28+
`Call to private cluster failed. Command: 'kubectl az test --insecure-skip-tls-verify --namespace namespace', errormessage: ${errorMsg}`
29+
)
30+
)
31+
})
1232
})

src/types/privatekubectl.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,18 @@ export class PrivateKubectl extends Kubectl {
7575
runOutput
7676
)}`
7777
)
78+
79+
if (runOutput.exitCode !== 0) {
80+
throw Error(
81+
`Call to private cluster failed. Command: '${kubectlCmd}', errormessage: ${runOutput.stderr}`
82+
)
83+
}
84+
7885
const runObj: {logs: string; exitCode: number} = JSON.parse(
7986
runOutput.stdout
8087
)
8188
if (!silent) core.info(runObj.logs)
82-
if (runOutput.exitCode !== 0 && runObj.exitCode !== 0) {
89+
if (runObj.exitCode !== 0) {
8390
throw Error(`failed private cluster Kubectl command: ${kubectlCmd}`)
8491
}
8592

0 commit comments

Comments
 (0)