mirror of
https://github.com/Azure/k8s-set-context.git
synced 2026-06-27 01:29:26 +08:00
* encoding input variable added * encoding input variable added * encoding input variable added * corrected unit tests * corrected unit tests * prettier edits * working on tests * working on tests * working on tests * minor edits * minor edits * better logic structure * added tests for edge cases * edited to enum
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as fs from 'fs'
|
||||
import * as core from '@actions/core'
|
||||
import {getRequiredInputError} from '../../tests/util'
|
||||
import {createKubeconfig, getDefaultKubeconfig} from './default'
|
||||
|
||||
@@ -62,6 +63,47 @@ describe('Default kubeconfig', () => {
|
||||
|
||||
expect(getDefaultKubeconfig()).toBe(kc)
|
||||
})
|
||||
|
||||
test('returns kubeconfig as plaintext when encoding is plaintext', () => {
|
||||
const kc = 'example kc'
|
||||
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||
if (name === 'method') return 'default'
|
||||
if (name === 'kubeconfig-encoding') return 'plaintext'
|
||||
if (name === 'kubeconfig') return kc
|
||||
return ''
|
||||
})
|
||||
expect(getDefaultKubeconfig()).toBe(kc)
|
||||
})
|
||||
|
||||
test('it gets default config through base64 kubeconfig input', () => {
|
||||
const kc = 'example kc'
|
||||
const base64Kc = Buffer.from(kc, 'utf-8').toString('base64')
|
||||
|
||||
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||
if (name === 'method') return 'default'
|
||||
if (name === 'kubeconfig-encoding') return 'base64'
|
||||
if (name === 'kubeconfig') return base64Kc
|
||||
return ''
|
||||
})
|
||||
|
||||
expect(getDefaultKubeconfig()).toBe(kc)
|
||||
})
|
||||
|
||||
test('it throws error for unknown kubeconfig-encoding', () => {
|
||||
const kc = 'example kc'
|
||||
const unknownEncoding = 'foobar'
|
||||
|
||||
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||
if (name === 'method') return 'default'
|
||||
if (name === 'kubeconfig-encoding') return unknownEncoding
|
||||
if (name === 'kubeconfig') return kc
|
||||
return ''
|
||||
})
|
||||
|
||||
expect(() => getDefaultKubeconfig()).toThrow(
|
||||
"Invalid kubeconfig-encoding: 'foobar'. Must be 'plaintext' or 'base64'."
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
test('it defaults to default method', () => {
|
||||
|
||||
@@ -44,7 +44,27 @@ export function getDefaultKubeconfig(): string {
|
||||
}
|
||||
default: {
|
||||
core.debug('Setting context using kubeconfig')
|
||||
return core.getInput('kubeconfig', {required: true})
|
||||
enum Encoding {
|
||||
Base64 = 'base64',
|
||||
Plaintext = 'plaintext'
|
||||
}
|
||||
|
||||
const rawKubeconfig = core.getInput('kubeconfig', {required: true})
|
||||
const encoding =
|
||||
core.getInput('kubeconfig-encoding')?.toLowerCase() ||
|
||||
Encoding.Plaintext
|
||||
|
||||
if (encoding !== Encoding.Base64 && encoding !== Encoding.Plaintext) {
|
||||
throw new Error(
|
||||
`Invalid kubeconfig-encoding: '${encoding}'. Must be 'plaintext' or 'base64'.`
|
||||
)
|
||||
}
|
||||
const kubeconfig =
|
||||
encoding === Encoding.Base64
|
||||
? Buffer.from(rawKubeconfig, 'base64').toString('utf-8')
|
||||
: rawKubeconfig
|
||||
|
||||
return kubeconfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user