mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fixup tests
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
008b990e57
commit
7407c24767
@ -22,8 +22,10 @@ import type { DetectClusterMetadata } from "../../main/cluster-detectors/detect-
|
|||||||
import detectClusterMetadataInjectable from "../../main/cluster-detectors/detect-cluster-metadata.injectable";
|
import detectClusterMetadataInjectable from "../../main/cluster-detectors/detect-cluster-metadata.injectable";
|
||||||
import type { ClusterConnection } from "../../main/cluster/cluster-connection.injectable";
|
import type { ClusterConnection } from "../../main/cluster/cluster-connection.injectable";
|
||||||
import clusterConnectionInjectable from "../../main/cluster/cluster-connection.injectable";
|
import clusterConnectionInjectable from "../../main/cluster/cluster-connection.injectable";
|
||||||
|
import type { KubeAuthProxy } from "../../main/kube-auth-proxy/create-kube-auth-proxy.injectable";
|
||||||
|
import createKubeAuthProxyInjectable from "../../main/kube-auth-proxy/create-kube-auth-proxy.injectable";
|
||||||
|
import type { Mocked } from "../../test-utils/mock-interface";
|
||||||
import { flushPromises } from "@k8slens/test-utils";
|
import { flushPromises } from "@k8slens/test-utils";
|
||||||
import { setTimeout } from "timers/promises";
|
|
||||||
|
|
||||||
describe("Refresh Cluster Accessibility Technical Tests", () => {
|
describe("Refresh Cluster Accessibility Technical Tests", () => {
|
||||||
let builder: ApplicationBuilder;
|
let builder: ApplicationBuilder;
|
||||||
@ -32,6 +34,7 @@ describe("Refresh Cluster Accessibility Technical Tests", () => {
|
|||||||
let listNamespaceMock: AsyncFnMock<CoreV1Api["listNamespace"]>;
|
let listNamespaceMock: AsyncFnMock<CoreV1Api["listNamespace"]>;
|
||||||
let k8sRequestMock: AsyncFnMock<K8sRequest>;
|
let k8sRequestMock: AsyncFnMock<K8sRequest>;
|
||||||
let detectClusterMetadataMock: AsyncFnMock<DetectClusterMetadata>;
|
let detectClusterMetadataMock: AsyncFnMock<DetectClusterMetadata>;
|
||||||
|
let kubeAuthProxyMock: Mocked<KubeAuthProxy>;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
builder = getApplicationBuilder();
|
builder = getApplicationBuilder();
|
||||||
@ -40,6 +43,14 @@ describe("Refresh Cluster Accessibility Technical Tests", () => {
|
|||||||
|
|
||||||
mainDi.override(broadcastMessageInjectable, () => async () => {});
|
mainDi.override(broadcastMessageInjectable, () => async () => {});
|
||||||
|
|
||||||
|
kubeAuthProxyMock = {
|
||||||
|
apiPrefix: "/some-api-prefix",
|
||||||
|
port: 0,
|
||||||
|
exit: jest.fn(),
|
||||||
|
run: asyncFn(),
|
||||||
|
};
|
||||||
|
mainDi.override(createKubeAuthProxyInjectable, () => () => kubeAuthProxyMock);
|
||||||
|
|
||||||
detectClusterMetadataMock = asyncFn();
|
detectClusterMetadataMock = asyncFn();
|
||||||
mainDi.override(detectClusterMetadataInjectable, () => detectClusterMetadataMock);
|
mainDi.override(detectClusterMetadataInjectable, () => detectClusterMetadataMock);
|
||||||
|
|
||||||
@ -101,10 +112,16 @@ describe("Refresh Cluster Accessibility Technical Tests", () => {
|
|||||||
cluster = clusterStore.getById("some-cluster-id") ?? (() => { throw new Error("missing cluster"); })();
|
cluster = clusterStore.getById("some-cluster-id") ?? (() => { throw new Error("missing cluster"); })();
|
||||||
clusterConnection = mainDi.inject(clusterConnectionInjectable, cluster);
|
clusterConnection = mainDi.inject(clusterConnectionInjectable, cluster);
|
||||||
refreshPromise = clusterConnection.refreshAccessibilityAndMetadata();
|
refreshPromise = clusterConnection.refreshAccessibilityAndMetadata();
|
||||||
|
});
|
||||||
|
|
||||||
// NOTE: I don't know why these are all are required to get the tests to pass
|
it("starts kubeAuthProxy", () => {
|
||||||
await flushPromises();
|
expect(kubeAuthProxyMock.run).toBeCalled();
|
||||||
await setTimeout(50);
|
});
|
||||||
|
|
||||||
|
describe("when kubeAuthProxy has started running and its port is found", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
kubeAuthProxyMock.port = 1235;
|
||||||
|
await kubeAuthProxyMock.run.resolve();
|
||||||
await flushPromises();
|
await flushPromises();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -428,6 +445,7 @@ describe("Refresh Cluster Accessibility Technical Tests", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const nonCoreApiResponse = {
|
const nonCoreApiResponse = {
|
||||||
groups: [
|
groups: [
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
|||||||
import type { Cluster } from "../../common/cluster/cluster";
|
import type { Cluster } from "../../common/cluster/cluster";
|
||||||
import createKubeAuthProxyInjectable from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
|
import createKubeAuthProxyInjectable from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
|
||||||
import kubeAuthProxyCertificateInjectable from "../kube-auth-proxy/kube-auth-proxy-certificate.injectable";
|
import kubeAuthProxyCertificateInjectable from "../kube-auth-proxy/kube-auth-proxy-certificate.injectable";
|
||||||
import type { KubeAuthProxy } from "../kube-auth-proxy/kube-auth-proxy";
|
import type { KubeAuthProxy } from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
|
||||||
|
|
||||||
export interface KubeAuthProxyServer {
|
export interface KubeAuthProxyServer {
|
||||||
getApiTarget(isLongRunningRequest?: boolean): Promise<ServerOptions>;
|
getApiTarget(isLongRunningRequest?: boolean): Promise<ServerOptions>;
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import type { KubeAuthProxyDependencies } from "./kube-auth-proxy";
|
import type { KubeAuthProxyDependencies } from "./kube-auth-proxy";
|
||||||
import { KubeAuthProxy } from "./kube-auth-proxy";
|
import { KubeAuthProxyImpl } from "./kube-auth-proxy";
|
||||||
import type { Cluster } from "../../common/cluster/cluster";
|
import type { Cluster } from "../../common/cluster/cluster";
|
||||||
import spawnInjectable from "../child-process/spawn.injectable";
|
import spawnInjectable from "../child-process/spawn.injectable";
|
||||||
import kubeAuthProxyCertificateInjectable from "./kube-auth-proxy-certificate.injectable";
|
import kubeAuthProxyCertificateInjectable from "./kube-auth-proxy-certificate.injectable";
|
||||||
@ -15,6 +15,13 @@ import getPortFromStreamInjectable from "../utils/get-port-from-stream.injectabl
|
|||||||
import getDirnameOfPathInjectable from "../../common/path/get-dirname.injectable";
|
import getDirnameOfPathInjectable from "../../common/path/get-dirname.injectable";
|
||||||
import broadcastConnectionUpdateInjectable from "../cluster/broadcast-connection-update.injectable";
|
import broadcastConnectionUpdateInjectable from "../cluster/broadcast-connection-update.injectable";
|
||||||
|
|
||||||
|
export interface KubeAuthProxy {
|
||||||
|
readonly apiPrefix: string;
|
||||||
|
readonly port: number;
|
||||||
|
run: () => Promise<void>;
|
||||||
|
exit: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
export type CreateKubeAuthProxy = (cluster: Cluster, env: NodeJS.ProcessEnv) => KubeAuthProxy;
|
export type CreateKubeAuthProxy = (cluster: Cluster, env: NodeJS.ProcessEnv) => KubeAuthProxy;
|
||||||
|
|
||||||
const createKubeAuthProxyInjectable = getInjectable({
|
const createKubeAuthProxyInjectable = getInjectable({
|
||||||
@ -33,7 +40,7 @@ const createKubeAuthProxyInjectable = getInjectable({
|
|||||||
return (cluster, env) => {
|
return (cluster, env) => {
|
||||||
const clusterUrl = new URL(cluster.apiUrl.get());
|
const clusterUrl = new URL(cluster.apiUrl.get());
|
||||||
|
|
||||||
return new KubeAuthProxy({
|
return new KubeAuthProxyImpl({
|
||||||
...dependencies,
|
...dependencies,
|
||||||
proxyCert: di.inject(kubeAuthProxyCertificateInjectable, clusterUrl.hostname),
|
proxyCert: di.inject(kubeAuthProxyCertificateInjectable, clusterUrl.hostname),
|
||||||
broadcastConnectionUpdate: di.inject(broadcastConnectionUpdateInjectable, cluster),
|
broadcastConnectionUpdate: di.inject(broadcastConnectionUpdateInjectable, cluster),
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import type { Logger } from "../../common/logger";
|
|||||||
import type { WaitUntilPortIsUsed } from "./wait-until-port-is-used/wait-until-port-is-used.injectable";
|
import type { WaitUntilPortIsUsed } from "./wait-until-port-is-used/wait-until-port-is-used.injectable";
|
||||||
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
||||||
import type { BroadcastConnectionUpdate } from "../cluster/broadcast-connection-update.injectable";
|
import type { BroadcastConnectionUpdate } from "../cluster/broadcast-connection-update.injectable";
|
||||||
|
import type { KubeAuthProxy } from "./create-kube-auth-proxy.injectable";
|
||||||
|
|
||||||
const startingServeMatcher = "starting to serve on (?<address>.+)";
|
const startingServeMatcher = "starting to serve on (?<address>.+)";
|
||||||
const startingServeRegex = Object.assign(TypedRegEx(startingServeMatcher, "i"), {
|
const startingServeRegex = Object.assign(TypedRegEx(startingServeMatcher, "i"), {
|
||||||
@ -33,7 +34,7 @@ export interface KubeAuthProxyDependencies {
|
|||||||
broadcastConnectionUpdate: BroadcastConnectionUpdate;
|
broadcastConnectionUpdate: BroadcastConnectionUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class KubeAuthProxy {
|
export class KubeAuthProxyImpl implements KubeAuthProxy {
|
||||||
public readonly apiPrefix = `/${randomBytes(8).toString("hex")}`;
|
public readonly apiPrefix = `/${randomBytes(8).toString("hex")}`;
|
||||||
|
|
||||||
public get port(): number {
|
public get port(): number {
|
||||||
|
|||||||
17
packages/core/src/test-utils/mock-interface.ts
Normal file
17
packages/core/src/test-utils/mock-interface.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { AsyncFnMock } from "@async-fn/jest";
|
||||||
|
|
||||||
|
type GetMockedType<T> =
|
||||||
|
T extends (...args: any[]) => Promise<any>
|
||||||
|
? AsyncFnMock<T>
|
||||||
|
: T extends (...args: any[]) => any
|
||||||
|
? jest.MockedFunction<T>
|
||||||
|
: T;
|
||||||
|
|
||||||
|
export type Mocked<T extends object> = {
|
||||||
|
-readonly [P in keyof T]: GetMockedType<T[P]>;
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user