mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add some basic unit tests for the following files: (#1050)
* Add some basic unit tests for cluster, kube-auth-proxy, and kubeconfig-manager - src/main/cluster.ts - src/main/kube-auth-proxy.ts - src/main/kubeconfig-manager.ts Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
abe6a4e0b1
commit
0960259279
@ -8,8 +8,8 @@ import { Application } from "spectron"
|
|||||||
import * as util from "../helpers/utils"
|
import * as util from "../helpers/utils"
|
||||||
import { spawnSync } from "child_process"
|
import { spawnSync } from "child_process"
|
||||||
|
|
||||||
const describeif = (condition : boolean) => condition ? describe : describe.skip
|
const describeif = (condition: boolean) => condition ? describe : describe.skip
|
||||||
const itif = (condition : boolean) => condition ? it : it.skip
|
const itif = (condition: boolean) => condition ? it : it.skip
|
||||||
|
|
||||||
jest.setTimeout(60000)
|
jest.setTimeout(60000)
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ describe("Lens integration tests", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const clickWhatsNew = async (app: Application) => {
|
const clickWhatsNew = async (app: Application) => {
|
||||||
await app.client.waitUntilTextExists("h1", "What's new")
|
await app.client.waitUntilTextExists("h1", "What's new?")
|
||||||
await app.client.click("button.primary")
|
await app.client.click("button.primary")
|
||||||
await app.client.waitUntilTextExists("h1", "Welcome")
|
await app.client.waitUntilTextExists("h1", "Welcome")
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ describe("Lens integration tests", () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const tests : {
|
const tests: {
|
||||||
drawer?: string
|
drawer?: string
|
||||||
drawerId?: string
|
drawerId?: string
|
||||||
pages: {
|
pages: {
|
||||||
@ -160,11 +160,10 @@ describe("Lens integration tests", () => {
|
|||||||
expectedSelector: string,
|
expectedSelector: string,
|
||||||
expectedText: string
|
expectedText: string
|
||||||
}[]
|
}[]
|
||||||
}[] = [
|
}[] = [{
|
||||||
{
|
|
||||||
drawer: "",
|
drawer: "",
|
||||||
drawerId: "",
|
drawerId: "",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Cluster",
|
name: "Cluster",
|
||||||
href: "cluster",
|
href: "cluster",
|
||||||
expectedSelector: "div.ClusterNoMetrics p",
|
expectedSelector: "div.ClusterNoMetrics p",
|
||||||
@ -174,7 +173,7 @@ describe("Lens integration tests", () => {
|
|||||||
{
|
{
|
||||||
drawer: "",
|
drawer: "",
|
||||||
drawerId: "",
|
drawerId: "",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Nodes",
|
name: "Nodes",
|
||||||
href: "nodes",
|
href: "nodes",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -184,7 +183,7 @@ describe("Lens integration tests", () => {
|
|||||||
{
|
{
|
||||||
drawer: "Workloads",
|
drawer: "Workloads",
|
||||||
drawerId: "workloads",
|
drawerId: "workloads",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Overview",
|
name: "Overview",
|
||||||
href: "workloads",
|
href: "workloads",
|
||||||
expectedSelector: "h5.box",
|
expectedSelector: "h5.box",
|
||||||
@ -225,12 +224,12 @@ describe("Lens integration tests", () => {
|
|||||||
href: "cronjobs",
|
href: "cronjobs",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Cron Jobs"
|
expectedText: "Cron Jobs"
|
||||||
} ]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
drawer: "Configuration",
|
drawer: "Configuration",
|
||||||
drawerId: "config",
|
drawerId: "config",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "ConfigMaps",
|
name: "ConfigMaps",
|
||||||
href: "configmaps",
|
href: "configmaps",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -259,12 +258,12 @@ describe("Lens integration tests", () => {
|
|||||||
href: "poddisruptionbudgets",
|
href: "poddisruptionbudgets",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Pod Disruption Budgets"
|
expectedText: "Pod Disruption Budgets"
|
||||||
} ]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
drawer: "Network",
|
drawer: "Network",
|
||||||
drawerId: "networks",
|
drawerId: "networks",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Services",
|
name: "Services",
|
||||||
href: "services",
|
href: "services",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -287,12 +286,12 @@ describe("Lens integration tests", () => {
|
|||||||
href: "network-policies",
|
href: "network-policies",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Network Policies"
|
expectedText: "Network Policies"
|
||||||
} ]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
drawer: "Storage",
|
drawer: "Storage",
|
||||||
drawerId: "storage",
|
drawerId: "storage",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Persistent Volume Claims",
|
name: "Persistent Volume Claims",
|
||||||
href: "persistent-volume-claims",
|
href: "persistent-volume-claims",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -309,12 +308,12 @@ describe("Lens integration tests", () => {
|
|||||||
href: "storage-classes",
|
href: "storage-classes",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Storage Classes"
|
expectedText: "Storage Classes"
|
||||||
} ]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
drawer: "",
|
drawer: "",
|
||||||
drawerId: "",
|
drawerId: "",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Namespaces",
|
name: "Namespaces",
|
||||||
href: "namespaces",
|
href: "namespaces",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -324,7 +323,7 @@ describe("Lens integration tests", () => {
|
|||||||
{
|
{
|
||||||
drawer: "",
|
drawer: "",
|
||||||
drawerId: "",
|
drawerId: "",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Events",
|
name: "Events",
|
||||||
href: "events",
|
href: "events",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -334,7 +333,7 @@ describe("Lens integration tests", () => {
|
|||||||
{
|
{
|
||||||
drawer: "Apps",
|
drawer: "Apps",
|
||||||
drawerId: "apps",
|
drawerId: "apps",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Charts",
|
name: "Charts",
|
||||||
href: "apps/charts",
|
href: "apps/charts",
|
||||||
expectedSelector: "div.HelmCharts input",
|
expectedSelector: "div.HelmCharts input",
|
||||||
@ -345,12 +344,12 @@ describe("Lens integration tests", () => {
|
|||||||
href: "apps/releases",
|
href: "apps/releases",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Releases"
|
expectedText: "Releases"
|
||||||
} ]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
drawer: "Access Control",
|
drawer: "Access Control",
|
||||||
drawerId: "users",
|
drawerId: "users",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Service Accounts",
|
name: "Service Accounts",
|
||||||
href: "service-accounts",
|
href: "service-accounts",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
@ -373,19 +372,18 @@ describe("Lens integration tests", () => {
|
|||||||
href: "pod-security-policies",
|
href: "pod-security-policies",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Pod Security Policies"
|
expectedText: "Pod Security Policies"
|
||||||
} ]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
drawer: "Custom Resources",
|
drawer: "Custom Resources",
|
||||||
drawerId: "custom-resources",
|
drawerId: "custom-resources",
|
||||||
pages: [ {
|
pages: [{
|
||||||
name: "Definitions",
|
name: "Definitions",
|
||||||
href: "crd/definitions",
|
href: "crd/definitions",
|
||||||
expectedSelector: "h5.title",
|
expectedSelector: "h5.title",
|
||||||
expectedText: "Custom Resources"
|
expectedText: "Custom Resources"
|
||||||
} ]
|
}]
|
||||||
},
|
}];
|
||||||
];
|
|
||||||
tests.forEach(({ drawer = "", drawerId = "", pages }) => {
|
tests.forEach(({ drawer = "", drawerId = "", pages }) => {
|
||||||
if (drawer !== "") {
|
if (drawer !== "") {
|
||||||
it(`shows ${drawer} drawer`, async () => {
|
it(`shows ${drawer} drawer`, async () => {
|
||||||
@ -394,7 +392,7 @@ describe("Lens integration tests", () => {
|
|||||||
await app.client.waitUntilTextExists(`a[href="/${pages[0].href}"]`, pages[0].name)
|
await app.client.waitUntilTextExists(`a[href="/${pages[0].href}"]`, pages[0].name)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pages.forEach(({name, href, expectedSelector, expectedText}) => {
|
pages.forEach(({ name, href, expectedSelector, expectedText }) => {
|
||||||
it(`shows ${drawer}->${name} page`, async () => {
|
it(`shows ${drawer}->${name} page`, async () => {
|
||||||
expect(clusterAdded).toBe(true)
|
expect(clusterAdded).toBe(true)
|
||||||
await app.client.click(`a[href="/${href}"]`)
|
await app.client.click(`a[href="/${href}"]`)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ export function setup(): Application {
|
|||||||
args: [],
|
args: [],
|
||||||
path: AppPaths[process.platform],
|
path: AppPaths[process.platform],
|
||||||
startTimeout: 30000,
|
startTimeout: 30000,
|
||||||
waitTimeout: 30000,
|
waitTimeout: 60000,
|
||||||
chromeDriverArgs: ['remote-debugging-port=9222'],
|
chromeDriverArgs: ['remote-debugging-port=9222'],
|
||||||
env: {
|
env: {
|
||||||
CICD: "true"
|
CICD: "true"
|
||||||
|
|||||||
@ -289,6 +289,7 @@
|
|||||||
"identity-obj-proxy": "^3.0.0",
|
"identity-obj-proxy": "^3.0.0",
|
||||||
"include-media": "^1.4.9",
|
"include-media": "^1.4.9",
|
||||||
"jest": "^26.0.1",
|
"jest": "^26.0.1",
|
||||||
|
"jest-mock-extended": "^1.0.10",
|
||||||
"make-plural": "^6.2.1",
|
"make-plural": "^6.2.1",
|
||||||
"material-design-icons": "^3.0.1",
|
"material-design-icons": "^3.0.1",
|
||||||
"mini-css-extract-plugin": "^0.9.0",
|
"mini-css-extract-plugin": "^0.9.0",
|
||||||
|
|||||||
165
src/main/__test__/cluster.test.ts
Normal file
165
src/main/__test__/cluster.test.ts
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
const logger = {
|
||||||
|
silly: jest.fn(),
|
||||||
|
debug: jest.fn(),
|
||||||
|
log: jest.fn(),
|
||||||
|
info: jest.fn(),
|
||||||
|
error: jest.fn(),
|
||||||
|
crit: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.mock("winston", () => ({
|
||||||
|
format: {
|
||||||
|
colorize: jest.fn(),
|
||||||
|
combine: jest.fn(),
|
||||||
|
simple: jest.fn(),
|
||||||
|
label: jest.fn(),
|
||||||
|
timestamp: jest.fn(),
|
||||||
|
printf: jest.fn()
|
||||||
|
},
|
||||||
|
createLogger: jest.fn().mockReturnValue(logger),
|
||||||
|
transports: {
|
||||||
|
Console: jest.fn(),
|
||||||
|
File: jest.fn(),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
jest.mock("../../common/ipc")
|
||||||
|
jest.mock("../context-handler")
|
||||||
|
jest.mock("request")
|
||||||
|
jest.mock("request-promise-native")
|
||||||
|
|
||||||
|
import { Console } from "console";
|
||||||
|
import mockFs from "mock-fs";
|
||||||
|
import { workspaceStore } from "../../common/workspace-store";
|
||||||
|
import { Cluster } from "../cluster"
|
||||||
|
import { ContextHandler } from "../context-handler";
|
||||||
|
import { getFreePort } from "../port";
|
||||||
|
import { V1ResourceAttributes } from "@kubernetes/client-node";
|
||||||
|
import { apiResources } from "../../common/rbac";
|
||||||
|
import request from "request-promise-native"
|
||||||
|
|
||||||
|
const mockedRequest = request as jest.MockedFunction<typeof request>
|
||||||
|
|
||||||
|
console = new Console(process.stdout, process.stderr) // fix mockFS
|
||||||
|
|
||||||
|
describe("create clusters", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const mockOpts = {
|
||||||
|
"minikube-config.yml": JSON.stringify({
|
||||||
|
apiVersion: "v1",
|
||||||
|
clusters: [{
|
||||||
|
name: "minikube",
|
||||||
|
cluster: {
|
||||||
|
server: "https://192.168.64.3:8443",
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
contexts: [{
|
||||||
|
context: {
|
||||||
|
cluster: "minikube",
|
||||||
|
user: "minikube",
|
||||||
|
},
|
||||||
|
name: "minikube",
|
||||||
|
}],
|
||||||
|
users: [{
|
||||||
|
name: "minikube",
|
||||||
|
}],
|
||||||
|
kind: "Config",
|
||||||
|
preferences: {},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
mockFs(mockOpts)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mockFs.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should be able to create a cluster from a cluster model and apiURL should be decoded", () => {
|
||||||
|
const c = new Cluster({
|
||||||
|
id: "foo",
|
||||||
|
contextName: "minikube",
|
||||||
|
kubeConfigPath: "minikube-config.yml",
|
||||||
|
workspace: workspaceStore.currentWorkspaceId
|
||||||
|
})
|
||||||
|
expect(c.apiUrl).toBe("https://192.168.64.3:8443")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("init should not throw if everything is in order", async () => {
|
||||||
|
const c = new Cluster({
|
||||||
|
id: "foo",
|
||||||
|
contextName: "minikube",
|
||||||
|
kubeConfigPath: "minikube-config.yml",
|
||||||
|
workspace: workspaceStore.currentWorkspaceId
|
||||||
|
})
|
||||||
|
await c.init(await getFreePort())
|
||||||
|
expect(logger.info).toBeCalledWith(expect.stringContaining("init success"), {
|
||||||
|
id: "foo",
|
||||||
|
apiUrl: "https://192.168.64.3:8443",
|
||||||
|
context: "minikube",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("activating cluster should try to connect to cluster and do a refresh", async () => {
|
||||||
|
const port = await getFreePort()
|
||||||
|
jest.spyOn(ContextHandler.prototype, "ensureServer");
|
||||||
|
|
||||||
|
const mockListNSs = jest.fn()
|
||||||
|
const mockKC = {
|
||||||
|
makeApiClient() {
|
||||||
|
return {
|
||||||
|
listNamespace: mockListNSs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jest.spyOn(Cluster.prototype, "canI")
|
||||||
|
.mockImplementationOnce((attr: V1ResourceAttributes): Promise<boolean> => {
|
||||||
|
expect(attr.namespace).toBe("default")
|
||||||
|
expect(attr.resource).toBe("pods")
|
||||||
|
expect(attr.verb).toBe("list")
|
||||||
|
return Promise.resolve(true)
|
||||||
|
})
|
||||||
|
.mockImplementation((attr: V1ResourceAttributes): Promise<boolean> => {
|
||||||
|
expect(attr.namespace).toBe("default")
|
||||||
|
expect(attr.verb).toBe("list")
|
||||||
|
return Promise.resolve(true)
|
||||||
|
})
|
||||||
|
jest.spyOn(Cluster.prototype, "getProxyKubeconfig").mockReturnValue(mockKC as any)
|
||||||
|
mockListNSs.mockImplementationOnce(() => ({
|
||||||
|
body: {
|
||||||
|
items: [{
|
||||||
|
metadata: {
|
||||||
|
name: "default",
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
mockedRequest.mockImplementationOnce(((uri: any, _options: any) => {
|
||||||
|
expect(uri).toBe(`http://localhost:${port}/api-kube/version`)
|
||||||
|
return Promise.resolve({ gitVersion: "1.2.3" })
|
||||||
|
}) as any)
|
||||||
|
|
||||||
|
const c = new Cluster({
|
||||||
|
id: "foo",
|
||||||
|
contextName: "minikube",
|
||||||
|
kubeConfigPath: "minikube-config.yml",
|
||||||
|
workspace: workspaceStore.currentWorkspaceId
|
||||||
|
})
|
||||||
|
await c.init(port)
|
||||||
|
await c.activate()
|
||||||
|
|
||||||
|
expect(ContextHandler.prototype.ensureServer).toBeCalled()
|
||||||
|
expect(mockedRequest).toBeCalled()
|
||||||
|
expect(c.accessible).toBe(true)
|
||||||
|
expect(c.allowedNamespaces.length).toBe(1)
|
||||||
|
expect(c.allowedResources.length).toBe(apiResources.length)
|
||||||
|
|
||||||
|
jest.resetAllMocks()
|
||||||
|
})
|
||||||
|
})
|
||||||
130
src/main/__test__/kube-auth-proxy.test.ts
Normal file
130
src/main/__test__/kube-auth-proxy.test.ts
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
const logger = {
|
||||||
|
silly: jest.fn(),
|
||||||
|
debug: jest.fn(),
|
||||||
|
log: jest.fn(),
|
||||||
|
info: jest.fn(),
|
||||||
|
error: jest.fn(),
|
||||||
|
crit: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.mock("winston", () => ({
|
||||||
|
format: {
|
||||||
|
colorize: jest.fn(),
|
||||||
|
combine: jest.fn(),
|
||||||
|
simple: jest.fn(),
|
||||||
|
label: jest.fn(),
|
||||||
|
timestamp: jest.fn(),
|
||||||
|
printf: jest.fn()
|
||||||
|
},
|
||||||
|
createLogger: jest.fn().mockReturnValue(logger),
|
||||||
|
transports: {
|
||||||
|
Console: jest.fn(),
|
||||||
|
File: jest.fn(),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
jest.mock("../../common/ipc")
|
||||||
|
jest.mock("child_process")
|
||||||
|
jest.mock("tcp-port-used")
|
||||||
|
|
||||||
|
import { Cluster } from "../cluster"
|
||||||
|
import { KubeAuthProxy } from "../kube-auth-proxy"
|
||||||
|
import { getFreePort } from "../port"
|
||||||
|
import { broadcastIpc } from "../../common/ipc"
|
||||||
|
import { ChildProcess, spawn, SpawnOptions } from "child_process"
|
||||||
|
import { Kubectl } from "../kubectl"
|
||||||
|
import { mock, MockProxy } from 'jest-mock-extended';
|
||||||
|
import { waitUntilUsed } from 'tcp-port-used';
|
||||||
|
import { Readable } from "stream"
|
||||||
|
|
||||||
|
const mockBroadcastIpc = broadcastIpc as jest.MockedFunction<typeof broadcastIpc>
|
||||||
|
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>
|
||||||
|
const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction<typeof waitUntilUsed>
|
||||||
|
|
||||||
|
describe("kube auth proxy tests", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("calling exit multiple times shouldn't throw", async () => {
|
||||||
|
const port = await getFreePort()
|
||||||
|
const kap = new KubeAuthProxy(new Cluster({ id: "foobar", kubeConfigPath: "fake-path.yml" }), port, {})
|
||||||
|
kap.exit()
|
||||||
|
kap.exit()
|
||||||
|
kap.exit()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("spawn tests", () => {
|
||||||
|
let port: number
|
||||||
|
let mockedCP: MockProxy<ChildProcess>
|
||||||
|
let listeners: Record<string, (...args: any[]) => void>
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
port = await getFreePort()
|
||||||
|
mockedCP = mock<ChildProcess>()
|
||||||
|
listeners = {}
|
||||||
|
|
||||||
|
jest.spyOn(Kubectl.prototype, "checkBinary").mockReturnValueOnce(Promise.resolve(true))
|
||||||
|
jest.spyOn(Kubectl.prototype, "ensureKubectl").mockReturnValueOnce(Promise.resolve(false))
|
||||||
|
mockedCP.on.mockImplementation((event: string, listener: (message: any, sendHandle: any) => void): ChildProcess => {
|
||||||
|
listeners[event] = listener
|
||||||
|
return mockedCP
|
||||||
|
})
|
||||||
|
mockedCP.stderr = mock<Readable>()
|
||||||
|
mockedCP.stderr.on.mockImplementation((event: string, listener: (message: any, sendHandle: any) => void): Readable => {
|
||||||
|
listeners[`stderr/${event}`] = listener
|
||||||
|
return mockedCP.stderr
|
||||||
|
})
|
||||||
|
mockedCP.stdout = mock<Readable>()
|
||||||
|
mockedCP.stdout.on.mockImplementation((event: string, listener: (message: any, sendHandle: any) => void): Readable => {
|
||||||
|
listeners[`stdout/${event}`] = listener
|
||||||
|
return mockedCP.stdout
|
||||||
|
})
|
||||||
|
mockSpawn.mockImplementationOnce((command: string, args: readonly string[], options: SpawnOptions): ChildProcess => {
|
||||||
|
expect(command).toBe(Kubectl.bundledKubectlPath)
|
||||||
|
return mockedCP
|
||||||
|
})
|
||||||
|
mockWaitUntilUsed.mockReturnValueOnce(Promise.resolve())
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should call spawn and broadcast errors", async () => {
|
||||||
|
const kap = new KubeAuthProxy(new Cluster({ id: "foobar", kubeConfigPath: "fake-path.yml" }), port, {})
|
||||||
|
await kap.run()
|
||||||
|
listeners["error"]({ message: "foobarbat" })
|
||||||
|
|
||||||
|
expect(mockBroadcastIpc).toBeCalledWith({ channel: "kube-auth:foobar", args: [{ data: "foobarbat", error: true }] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should call spawn and broadcast exit", async () => {
|
||||||
|
const kap = new KubeAuthProxy(new Cluster({ id: "foobar", kubeConfigPath: "fake-path.yml" }), port, {})
|
||||||
|
await kap.run()
|
||||||
|
listeners["exit"](0)
|
||||||
|
|
||||||
|
expect(mockBroadcastIpc).toBeCalledWith({ channel: "kube-auth:foobar", args: [{ data: "proxy exited with code: 0", error: false }] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should call spawn and broadcast errors from stderr", async () => {
|
||||||
|
const kap = new KubeAuthProxy(new Cluster({ id: "foobar", kubeConfigPath: "fake-path.yml" }), port, {})
|
||||||
|
await kap.run()
|
||||||
|
listeners["stderr/data"]("an error")
|
||||||
|
|
||||||
|
expect(mockBroadcastIpc).toBeCalledWith({ channel: "kube-auth:foobar", args: [{ data: "an error", error: true }] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should call spawn and broadcast stdout serving info", async () => {
|
||||||
|
const kap = new KubeAuthProxy(new Cluster({ id: "foobar", kubeConfigPath: "fake-path.yml" }), port, {})
|
||||||
|
await kap.run()
|
||||||
|
listeners["stdout/data"]("Starting to serve on")
|
||||||
|
|
||||||
|
expect(mockBroadcastIpc).toBeCalledWith({ channel: "kube-auth:foobar", args: [{ data: "Authentication proxy started\n" }] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should call spawn and broadcast stdout other info", async () => {
|
||||||
|
const kap = new KubeAuthProxy(new Cluster({ id: "foobar", kubeConfigPath: "fake-path.yml" }), port, {})
|
||||||
|
await kap.run()
|
||||||
|
listeners["stdout/data"]("some info")
|
||||||
|
|
||||||
|
expect(mockBroadcastIpc).toBeCalledWith({ channel: "kube-auth:foobar", args: [{ data: "some info" }] })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
112
src/main/__test__/kubeconfig-manager.test.ts
Normal file
112
src/main/__test__/kubeconfig-manager.test.ts
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
const logger = {
|
||||||
|
silly: jest.fn(),
|
||||||
|
debug: jest.fn(),
|
||||||
|
log: jest.fn(),
|
||||||
|
info: jest.fn(),
|
||||||
|
error: jest.fn(),
|
||||||
|
crit: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.mock("winston", () => ({
|
||||||
|
format: {
|
||||||
|
colorize: jest.fn(),
|
||||||
|
combine: jest.fn(),
|
||||||
|
simple: jest.fn(),
|
||||||
|
label: jest.fn(),
|
||||||
|
timestamp: jest.fn(),
|
||||||
|
printf: jest.fn()
|
||||||
|
},
|
||||||
|
createLogger: jest.fn().mockReturnValue(logger),
|
||||||
|
transports: {
|
||||||
|
Console: jest.fn(),
|
||||||
|
File: jest.fn(),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
import { KubeconfigManager } from "../kubeconfig-manager"
|
||||||
|
import mockFs from "mock-fs"
|
||||||
|
import { Cluster } from "../cluster";
|
||||||
|
import { workspaceStore } from "../../common/workspace-store";
|
||||||
|
import { ContextHandler } from "../context-handler";
|
||||||
|
import { getFreePort } from "../port";
|
||||||
|
import fse from "fs-extra"
|
||||||
|
import { loadYaml } from "@kubernetes/client-node";
|
||||||
|
import { Console } from "console";
|
||||||
|
|
||||||
|
console = new Console(process.stdout, process.stderr) // fix mockFS
|
||||||
|
|
||||||
|
describe("kubeconfig manager tests", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const mockOpts = {
|
||||||
|
"minikube-config.yml": JSON.stringify({
|
||||||
|
apiVersion: "v1",
|
||||||
|
clusters: [{
|
||||||
|
name: "minikube",
|
||||||
|
cluster: {
|
||||||
|
server: "https://192.168.64.3:8443",
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
contexts: [{
|
||||||
|
context: {
|
||||||
|
cluster: "minikube",
|
||||||
|
user: "minikube",
|
||||||
|
},
|
||||||
|
name: "minikube",
|
||||||
|
}],
|
||||||
|
users: [{
|
||||||
|
name: "minikube",
|
||||||
|
}],
|
||||||
|
kind: "Config",
|
||||||
|
preferences: {},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
mockFs(mockOpts)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mockFs.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should create 'temp' kube config with proxy", async () => {
|
||||||
|
const cluster = new Cluster({
|
||||||
|
id: "foo",
|
||||||
|
contextName: "minikube",
|
||||||
|
kubeConfigPath: "minikube-config.yml",
|
||||||
|
workspace: workspaceStore.currentWorkspaceId
|
||||||
|
})
|
||||||
|
const contextHandler = new ContextHandler(cluster)
|
||||||
|
const port = await getFreePort()
|
||||||
|
const kubeConfManager = await KubeconfigManager.create(cluster, contextHandler, port)
|
||||||
|
|
||||||
|
expect(logger.error).not.toBeCalled()
|
||||||
|
expect(kubeConfManager.getPath()).toBe("tmp/kubeconfig-foo")
|
||||||
|
const file = await fse.readFile(kubeConfManager.getPath())
|
||||||
|
const yml = loadYaml<any>(file.toString())
|
||||||
|
expect(yml["current-context"]).toBe("minikube")
|
||||||
|
expect(yml["clusters"][0]["cluster"]["server"]).toBe(`http://127.0.0.1:${port}/foo`)
|
||||||
|
expect(yml["users"][0]["name"]).toBe("proxy")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should remove 'temp' kube config on unlink and remove reference from inside class", async () => {
|
||||||
|
const cluster = new Cluster({
|
||||||
|
id: "foo",
|
||||||
|
contextName: "minikube",
|
||||||
|
kubeConfigPath: "minikube-config.yml",
|
||||||
|
workspace: workspaceStore.currentWorkspaceId
|
||||||
|
})
|
||||||
|
const contextHandler = new ContextHandler(cluster)
|
||||||
|
const port = await getFreePort()
|
||||||
|
const kubeConfManager = await KubeconfigManager.create(cluster, contextHandler, port)
|
||||||
|
|
||||||
|
const configPath = kubeConfManager.getPath()
|
||||||
|
expect(await fse.pathExists(configPath)).toBe(true)
|
||||||
|
await kubeConfManager.unlink()
|
||||||
|
expect(await fse.pathExists(configPath)).toBe(false)
|
||||||
|
await kubeConfManager.unlink() // doesn't throw
|
||||||
|
expect(kubeConfManager.getPath()).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
@ -93,7 +93,7 @@ export class Cluster implements ClusterModel {
|
|||||||
async init(port: number) {
|
async init(port: number) {
|
||||||
try {
|
try {
|
||||||
this.contextHandler = new ContextHandler(this);
|
this.contextHandler = new ContextHandler(this);
|
||||||
this.kubeconfigManager = new KubeconfigManager(this, this.contextHandler, port);
|
this.kubeconfigManager = await KubeconfigManager.create(this, this.contextHandler, port);
|
||||||
this.kubeProxyUrl = `http://localhost:${port}${apiKubePrefix}`;
|
this.kubeProxyUrl = `http://localhost:${port}${apiKubePrefix}`;
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
logger.info(`[CLUSTER]: "${this.contextName}" init success`, {
|
logger.info(`[CLUSTER]: "${this.contextName}" init success`, {
|
||||||
|
|||||||
@ -11,8 +11,12 @@ export class KubeconfigManager {
|
|||||||
protected configDir = app.getPath("temp")
|
protected configDir = app.getPath("temp")
|
||||||
protected tempFile: string;
|
protected tempFile: string;
|
||||||
|
|
||||||
constructor(protected cluster: Cluster, protected contextHandler: ContextHandler, protected port: number) {
|
private constructor(protected cluster: Cluster, protected contextHandler: ContextHandler, protected port: number) { }
|
||||||
this.init();
|
|
||||||
|
static async create(cluster: Cluster, contextHandler: ContextHandler, port: number) {
|
||||||
|
const kcm = new KubeconfigManager(cluster, contextHandler, port)
|
||||||
|
await kcm.init()
|
||||||
|
return kcm
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async init() {
|
protected async init() {
|
||||||
@ -72,8 +76,13 @@ export class KubeconfigManager {
|
|||||||
return tempFile;
|
return tempFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
unlink() {
|
async unlink() {
|
||||||
|
if (!this.tempFile) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
logger.info('Deleting temporary kubeconfig: ' + this.tempFile)
|
logger.info('Deleting temporary kubeconfig: ' + this.tempFile)
|
||||||
fs.unlinkSync(this.tempFile)
|
await fs.unlink(this.tempFile)
|
||||||
|
this.tempFile = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
yarn.lock
12
yarn.lock
@ -7080,6 +7080,13 @@ jest-message-util@^26.0.1:
|
|||||||
slash "^3.0.0"
|
slash "^3.0.0"
|
||||||
stack-utils "^2.0.2"
|
stack-utils "^2.0.2"
|
||||||
|
|
||||||
|
jest-mock-extended@^1.0.10:
|
||||||
|
version "1.0.10"
|
||||||
|
resolved "https://registry.yarnpkg.com/jest-mock-extended/-/jest-mock-extended-1.0.10.tgz#a4b1f5b0bb1121acf7c58cd5423d04c473532702"
|
||||||
|
integrity sha512-R2wKiOgEUPoHZ2kLsAQeQP2IfVEgo3oQqWLSXKdMXK06t3UHkQirA2Xnsdqg/pX6KPWTsdnrzE2ig6nqNjdgVw==
|
||||||
|
dependencies:
|
||||||
|
ts-essentials "^4.0.0"
|
||||||
|
|
||||||
jest-mock@^26.0.1:
|
jest-mock@^26.0.1:
|
||||||
version "26.0.1"
|
version "26.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-26.0.1.tgz#7fd1517ed4955397cf1620a771dc2d61fad8fd40"
|
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-26.0.1.tgz#7fd1517ed4955397cf1620a771dc2d61fad8fd40"
|
||||||
@ -11382,6 +11389,11 @@ truncate-utf8-bytes@^1.0.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
utf8-byte-length "^1.0.1"
|
utf8-byte-length "^1.0.1"
|
||||||
|
|
||||||
|
ts-essentials@^4.0.0:
|
||||||
|
version "4.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-4.0.0.tgz#506c42b270bbd0465574b90416533175b09205ab"
|
||||||
|
integrity sha512-uQJX+SRY9mtbKU+g9kl5Fi7AEMofPCvHfJkQlaygpPmHPZrtgaBqbWFOYyiA47RhnSwwnXdepUJrgqUYxoUyhQ==
|
||||||
|
|
||||||
ts-jest@^26.1.0:
|
ts-jest@^26.1.0:
|
||||||
version "26.3.0"
|
version "26.3.0"
|
||||||
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.3.0.tgz#6b2845045347dce394f069bb59358253bc1338a9"
|
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.3.0.tgz#6b2845045347dce394f069bb59358253bc1338a9"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user