Technical requirements for behavioural unit tests (#5084)

* Implement a lot of technical requirements for behavioural unit tests

Note: the crux of this was to make routing env-agnostic, and not based on URLs as magic strings, but instead something type-enforced.

Note: extension-based routes comply to same exact interface by "late-registering" their routes when installed. Routes are just injectables.

Note: another chunk of global shared state is no more.

Note: a lot of explicit side effects have been cornered to injectables.

Note: a lot of stuff has become reactive as part if this.

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make a directory commonly available

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Require id for <Select /> to prevent non-deterministic renders

This was caused by global state in a 3rd party lib: "react-select".

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Specify id for all <Select /> to satisfy previous commit

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Prevent explicit side effect in component by using existing dependency instead

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Extract instantiation of "conf" as injectables for causing side effects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Introduce a legacy-helper to make gradual refactoring of inheritors of Singleton easier

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Make legacy unit tests for hotbar green and more simple by using the new legacy helper

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Temporarily kludge all unit tests green with a disclaimer about allowing side-effects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove kludge in previous commit by explicitly permitting specific side effects where old unit tests require it

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Prevent old unit test with side effects from accessing file system

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Migrate to actual typing for di.permitSideEffects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Prevent unit tests from failing because of non-standard method of HTML-element not present in js-dom

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Adapt integration tests to recent changes

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Fix code style

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Fix artifact from bad rebase

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Add a deprecation from a review comment

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove change that is not required

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove redundant comment

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix code style

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove redundant file

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix bad merge

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Improve variable name

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Tweak logger interface to be more descriptive

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make injecting legacy singleton always provide new instance

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove conditional typing when not needed

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Improve naming of variable

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove unnecessary code style changes

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove flag for causing side effects from too broad scope

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Override side-effects in unit test using injectable instead of monkey patching

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Flag some side-effects and add general overrides

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Fix unit tests in CI by removing explicit side-effect

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove explicit side-effect from getting default shell

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce abstraction for getting absolute paths

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Switch to using abstraction for getting absolute path to control explicit side effect

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce abstraction for joining paths

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Switch to using abstraction for joining paths to control explicit side effect

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix fake implementation for join paths

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix test after removing explicit side effect

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove explicit side effects from kubeconfig-syncs

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix arguments after removing explicit side effect

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make registrators not async for not being needed anymore

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make generalCatalogEntities non-observable, as there is no requirement

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove redundant code

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Simplify logic for registering general catalog entity sources

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Add TODO

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Replace function for getting application menu items with reactive solution

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix typo in interface name

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove global shared state usages of hot bar store

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove redundant enum

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Iku-tursoandJanne Savolainen authored and GitHub committed 2022-03-31 16:57:05 +03:00
1 parent ab2cd27a27
commit a277cfcf02
583 files changed
+13073 -6556

No files matched your search

@@ -3,23 +3,15 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { makeObservable, computed } from "mobx";
import type { IComputedValue } from "mobx";
import { disposeOnUnmount, observer } from "mobx-react";
import { Redirect, Route, Router, Switch } from "react-router";
import { UserManagementRoute } from "../../components/+user-management/route";
import { Redirect } from "react-router";
import { ConfirmDialog } from "../../components/confirm-dialog";
import { ClusterOverview } from "../../components/+cluster/cluster-overview";
import { Events } from "../../components/+events/events";
import { DeploymentScaleDialog } from "../../components/+workloads-deployments/deployment-scale-dialog";
import { CronJobTriggerDialog } from "../../components/+workloads-cronjobs/cronjob-trigger-dialog";
import { CustomResourcesRoute } from "../../components/+custom-resources/route";
import { ClusterPageRegistry, getExtensionPageUrl } from "../../../extensions/registries/page-registry";
import { ClusterPageMenuRegistration, ClusterPageMenuRegistry } from "../../../extensions/registries";
import { StatefulSetScaleDialog } from "../../components/+workloads-statefulsets/statefulset-scale-dialog";
import { ReplicaSetScaleDialog } from "../../components/+workloads-replicasets/replicaset-scale-dialog";
import { CommandContainer } from "../../components/command-palette/command-container";
import * as routes from "../../../common/routes";
import { TabLayout, TabLayoutRoute } from "../../components/layout/tab-layout";
import { ErrorBoundary } from "../../components/error-boundary";
import { MainLayout } from "../../components/layout/main-layout";
import { Notifications } from "../../components/notifications";
@@ -27,37 +19,24 @@ import { KubeObjectDetails } from "../../components/kube-object-details";
import { KubeConfigDialog } from "../../components/kubeconfig-dialog";
import { Sidebar } from "../../components/layout/sidebar";
import { Dock } from "../../components/dock";
import { NamespacesRoute } from "../../components/+namespaces/route";
import { NetworkRoute } from "../../components/+network/route";
import { NodesRoute } from "../../components/+nodes/route";
import { WorkloadsRoute } from "../../components/+workloads/route";
import { ConfigRoute } from "../../components/+config/route";
import { StorageRoute } from "../../components/+storage/route";
import { watchHistoryState } from "../../remote-helpers/history-updater";
import { PortForwardDialog } from "../../port-forward";
import { DeleteClusterDialog } from "../../components/delete-cluster-dialog";
import type { NamespaceStore } from "../../components/+namespaces/namespace-store/namespace.store";
import { withInjectables } from "@ogre-tools/injectable-react";
import namespaceStoreInjectable from "../../components/+namespaces/namespace-store/namespace-store.injectable";
import type { ClusterId } from "../../../common/cluster-types";
import hostedClusterInjectable from "../../../common/cluster-store/hosted-cluster.injectable";
import type { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
import type { KubeObject } from "../../../common/k8s-api/kube-object";
import type { Disposer } from "../../../common/utils";
import kubeWatchApiInjectable from "../../kube-watch-api/kube-watch-api.injectable";
import historyInjectable from "../../navigation/history.injectable";
import type { History } from "history";
import type { IsAllowedResource } from "../../../common/utils/is-allowed-resource.injectable";
import isAllowedResourceInjectable from "../../../common/utils/is-allowed-resource.injectable";
import { HelmRoute } from "../../components/+helm/route";
import type { KubeResource } from "../../../common/rbac";
import currentRouteComponentInjectable from "../../routes/current-route-component.injectable";
import startUrlInjectable from "./start-url.injectable";
interface Dependencies {
history: History;
namespaceStore: NamespaceStore;
hostedClusterId: ClusterId;
subscribeStores: (stores: KubeObjectStore<KubeObject>[]) => Disposer;
isAllowedResource: IsAllowedResource;
currentRouteComponent: IComputedValue<React.ElementType>;
startUrl: IComputedValue<string>;
}
@observer
@@ -66,7 +45,6 @@ class NonInjectedClusterFrame extends React.Component<Dependencies> {
constructor(props: Dependencies) {
super(props);
makeObservable(this);
}
componentDidMount() {
@@ -78,121 +56,40 @@ class NonInjectedClusterFrame extends React.Component<Dependencies> {
]);
}
@computed get startUrl() {
const resources : KubeResource[] = ["events", "nodes", "pods"];
render() {
const Component = this.props.currentRouteComponent.get();
return resources.every(x => this.props.isAllowedResource(x))
? routes.clusterURL()
: routes.workloadsURL();
}
getTabLayoutRoutes(menuItem: ClusterPageMenuRegistration) {
const routes: TabLayoutRoute[] = [];
if (!menuItem.id) {
return routes;
if (!Component) {
return <Redirect to={this.props.startUrl.get()} />;
}
ClusterPageMenuRegistry.getInstance().getSubItems(menuItem).forEach((subMenu) => {
const page = ClusterPageRegistry.getInstance().getByPageTarget(subMenu.target);
if (page) {
routes.push({
routePath: page.url,
url: getExtensionPageUrl(subMenu.target),
title: subMenu.title,
component: page.components.Page,
});
}
});
return routes;
}
renderExtensionTabLayoutRoutes() {
return ClusterPageMenuRegistry.getInstance().getRootItems().map((menu, index) => {
const tabRoutes = this.getTabLayoutRoutes(menu);
if (tabRoutes.length > 0) {
const pageComponent = () => <TabLayout tabs={tabRoutes}/>;
return <Route key={`extension-tab-layout-route-${index}`} component={pageComponent} path={tabRoutes.map((tab) => tab.routePath)}/>;
} else {
const page = ClusterPageRegistry.getInstance().getByPageTarget(menu.target);
if (page) {
return <Route key={`extension-tab-layout-route-${index}`} path={page.url} component={page.components.Page}/>;
}
}
return null;
});
}
renderExtensionRoutes() {
return ClusterPageRegistry.getInstance().getItems().map((page, index) => {
const menu = ClusterPageMenuRegistry.getInstance().getByPage(page);
if (!menu) {
return <Route key={`extension-route-${index}`} path={page.url} component={page.components.Page}/>;
}
return null;
});
}
render() {
return (
<Router history={this.props.history}>
<ErrorBoundary>
<MainLayout sidebar={<Sidebar />} footer={<Dock />}>
<Switch>
<Route component={ClusterOverview} {...routes.clusterRoute}/>
<Route component={NodesRoute} {...routes.nodesRoute}/>
<Route component={WorkloadsRoute} {...routes.workloadsRoute}/>
<Route component={ConfigRoute} {...routes.configRoute}/>
<Route component={NetworkRoute} {...routes.networkRoute}/>
<Route component={StorageRoute} {...routes.storageRoute}/>
<Route component={NamespacesRoute} {...routes.namespacesRoute}/>
<Route component={Events} {...routes.eventRoute}/>
<Route component={CustomResourcesRoute} {...routes.crdRoute}/>
<Route component={UserManagementRoute} {...routes.usersManagementRoute}/>
<Route component={HelmRoute} {...routes.helmRoute}/>
{this.renderExtensionTabLayoutRoutes()}
{this.renderExtensionRoutes()}
<Redirect exact from="/" to={this.startUrl}/>
<ErrorBoundary>
<MainLayout sidebar={<Sidebar />} footer={<Dock />}>
<Component />
</MainLayout>
<Route render={({ location }) => {
Notifications.error(`Unknown location ${location.pathname}, redirecting to main page.`);
return <Redirect to={this.startUrl} />;
}} />
</Switch>
</MainLayout>
<Notifications/>
<ConfirmDialog/>
<KubeObjectDetails/>
<KubeConfigDialog/>
<DeploymentScaleDialog/>
<StatefulSetScaleDialog/>
<ReplicaSetScaleDialog/>
<CronJobTriggerDialog/>
<PortForwardDialog/>
<DeleteClusterDialog/>
<CommandContainer clusterId={this.props.hostedClusterId}/>
</ErrorBoundary>
</Router>
<Notifications />
<ConfirmDialog />
<KubeObjectDetails />
<KubeConfigDialog />
<DeploymentScaleDialog />
<StatefulSetScaleDialog />
<ReplicaSetScaleDialog />
<CronJobTriggerDialog />
<PortForwardDialog />
<DeleteClusterDialog />
<CommandContainer />
</ErrorBoundary>
);
}
}
export const ClusterFrame = withInjectables<Dependencies>(NonInjectedClusterFrame, {
getProps: di => ({
history: di.inject(historyInjectable),
namespaceStore: di.inject(namespaceStoreInjectable),
hostedClusterId: di.inject(hostedClusterInjectable).id,
subscribeStores: di.inject(kubeWatchApiInjectable).subscribeStores,
isAllowedResource: di.inject(isAllowedResourceInjectable),
startUrl: di.inject(startUrlInjectable),
currentRouteComponent: di.inject(currentRouteComponentInjectable),
}),
});
@@ -0,0 +1,34 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import type { KubeResource } from "../../../common/rbac";
import isAllowedResourceInjectable from "../../../common/utils/is-allowed-resource.injectable";
import clusterOverviewRouteInjectable from "../../../common/front-end-routing/routes/cluster/overview/cluster-overview-route.injectable";
import workloadsOverviewRouteInjectable from "../../../common/front-end-routing/routes/cluster/workloads/overview/workloads-overview-route.injectable";
import { buildURL } from "../../../common/utils/buildUrl";
const startUrlInjectable = getInjectable({
id: "start-url",
instantiate: (di) => {
const isAllowedResource = (resourceName: any) => di.inject(isAllowedResourceInjectable, resourceName);
const clusterOverviewRoute = di.inject(clusterOverviewRouteInjectable);
const workloadOverviewRoute = di.inject(workloadsOverviewRouteInjectable);
const clusterOverviewUrl = buildURL(clusterOverviewRoute.path);
const workloadOverviewUrl = buildURL(workloadOverviewRoute.path);
return computed(() => {
const resources: KubeResource[] = ["events", "nodes", "pods"];
return resources.every((resourceName) => isAllowedResource(resourceName))
? clusterOverviewUrl
: workloadOverviewUrl;
});
},
});
export default startUrlInjectable;
@@ -9,6 +9,7 @@ import ipcRendererInjectable from "../../../app-paths/get-value-from-registered-
import bindProtocolAddRouteHandlersInjectable from "../../../protocol-handler/bind-protocol-add-route-handlers/bind-protocol-add-route-handlers.injectable";
import lensProtocolRouterRendererInjectable from "../../../protocol-handler/lens-protocol-router-renderer/lens-protocol-router-renderer.injectable";
import catalogEntityRegistryInjectable from "../../../api/catalog-entity-registry/catalog-entity-registry.injectable";
import registerIpcListenersInjectable from "../../../ipc/register-ipc-listeners.injectable";
const initRootFrameInjectable = getInjectable({
id: "init-root-frame",
@@ -19,6 +20,8 @@ const initRootFrameInjectable = getInjectable({
return initRootFrame({
loadExtensions: extensionLoader.loadOnClusterManagerRenderer,
registerIpcListeners: di.inject(registerIpcListenersInjectable),
ipcRenderer: di.inject(ipcRendererInjectable),
bindProtocolAddRouteHandlers: di.inject(
@@ -4,16 +4,18 @@
*/
import { delay } from "../../../../common/utils";
import { broadcastMessage } from "../../../../common/ipc";
import { registerIpcListeners } from "../../../ipc/register-listeners";
import logger from "../../../../common/logger";
import { unmountComponentAtNode } from "react-dom";
import type { ExtensionLoading } from "../../../../extensions/extension-loader";
import type { CatalogEntityRegistry } from "../../../api/catalog-entity-registry";
import { bundledExtensionsLoaded } from "../../../../common/ipc/extension-handling";
interface Dependencies {
loadExtensions: () => Promise<ExtensionLoading[]>;
registerIpcListeners: () => void;
// TODO: Move usages of third party library behind abstraction
ipcRenderer: { send: (name: string) => void };
@@ -31,6 +33,7 @@ export const initRootFrame =
bindProtocolAddRouteHandlers,
lensProtocolRouterRenderer,
ipcRenderer,
registerIpcListeners,
catalogEntityRegistry,
}: Dependencies) =>
+5 -19
View File
@@ -5,7 +5,6 @@
import { injectSystemCAs } from "../../../common/system-ca";
import React from "react";
import { Route, Router, Switch } from "react-router";
import { observer } from "mobx-react";
import { ClusterManager } from "../../components/cluster-manager";
import { ErrorBoundary } from "../../components/error-boundary";
@@ -15,21 +14,14 @@ import { CommandContainer } from "../../components/command-palette/command-conta
import { ipcRenderer } from "electron";
import { IpcRendererNavigationEvents } from "../../navigation/events";
import { ClusterFrameHandler } from "../../components/cluster-manager/lens-views";
import historyInjectable from "../../navigation/history.injectable";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { History } from "history";
injectSystemCAs();
interface Dependencies {
history: History;
}
@observer
class NonInjectedRootFrame extends React.Component<Dependencies> {
export class RootFrame extends React.Component {
static displayName = "RootFrame";
constructor(props: Dependencies) {
constructor(props: any) {
super(props);
ClusterFrameHandler.createInstance();
@@ -41,20 +33,14 @@ class NonInjectedRootFrame extends React.Component<Dependencies> {
render() {
return (
<Router history={this.props.history}>
<>
<ErrorBoundary>
<Switch>
<Route component={ClusterManager} />
</Switch>
<ClusterManager />
</ErrorBoundary>
<Notifications />
<ConfirmDialog />
<CommandContainer />
</Router>
</>
);
}
}
export const RootFrame = withInjectables(NonInjectedRootFrame, {
getProps: (di) => ({ history: di.inject(historyInjectable) }),
});