Replace jest.mock's with overriding dependencies (#6014)

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

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen authored and GitHub committed 2022-08-23 13:04:00 +03:00
1 parent 03d2389f01
commit 81f7bd3c2c
50 files changed
+788 -605

No files matched your search

@@ -1,153 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { sortCharts } from "../../../common/utils";
import type { HelmRepo } from "../../../common/helm/helm-repo";
const charts = new Map([
["stable", {
"invalid-semver": sortCharts([
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.3.0",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver but more",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.4.0",
repo: "stable",
digest: "test",
created: "now",
},
]),
"apm-server": sortCharts([
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.7",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.6",
repo: "stable",
digest: "test",
created: "now",
},
]),
"redis": sortCharts([
{
apiVersion: "3.0.0",
name: "apm-server",
version: "1.0.0",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "0.0.9",
repo: "stable",
digest: "test",
created: "now",
},
]),
}],
["experiment", {
"fairwind": sortCharts([
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.1",
repo: "experiment",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.2",
repo: "experiment",
digest: "test",
deprecated: true,
created: "now",
},
]),
}],
["bitnami", {
"hotdog": sortCharts([
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.1",
repo: "bitnami",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.2",
repo: "bitnami",
digest: "test",
created: "now",
},
]),
"pretzel": sortCharts([
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0",
repo: "bitnami",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0.1",
repo: "bitnami",
digest: "test",
created: "now",
},
]),
}],
]);
export class HelmChartManager {
constructor(private repo: HelmRepo){ }
static forRepo(repo: HelmRepo) {
return new this(repo);
}
public async charts(): Promise<any> {
return charts.get(this.repo.name) ?? {};
}
}
+149 -2
View File
@@ -8,8 +8,8 @@ import listHelmChartsInjectable from "../helm-service/list-helm-charts.injectabl
import getActiveHelmRepositoriesInjectable from "../repositories/get-active-helm-repositories/get-active-helm-repositories.injectable";
import type { AsyncResult } from "../../../common/utils/async-result";
import type { HelmRepo } from "../../../common/helm/helm-repo";
jest.mock("../helm-chart-manager");
import { sortCharts } from "../../../common/utils";
import helmChartManagerInjectable from "../helm-chart-manager.injectable";
describe("Helm Service tests", () => {
let listHelmCharts: () => Promise<any>;
@@ -20,6 +20,11 @@ describe("Helm Service tests", () => {
getActiveHelmRepositoriesMock = jest.fn();
di.override(
helmChartManagerInjectable,
(di, repo) => new HelmChartManagerFake(repo) as unknown,
);
di.override(getActiveHelmRepositoriesInjectable, () => getActiveHelmRepositoriesMock);
di.unoverride(listHelmChartsInjectable);
@@ -195,3 +200,145 @@ describe("Helm Service tests", () => {
});
});
});
const charts = new Map([
["stable", {
"invalid-semver": sortCharts([
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.3.0",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver but more",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.4.0",
repo: "stable",
digest: "test",
created: "now",
},
]),
"apm-server": sortCharts([
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.7",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.6",
repo: "stable",
digest: "test",
created: "now",
},
]),
"redis": sortCharts([
{
apiVersion: "3.0.0",
name: "apm-server",
version: "1.0.0",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "0.0.9",
repo: "stable",
digest: "test",
created: "now",
},
]),
}],
["experiment", {
"fairwind": sortCharts([
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.1",
repo: "experiment",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.2",
repo: "experiment",
digest: "test",
deprecated: true,
created: "now",
},
]),
}],
["bitnami", {
"hotdog": sortCharts([
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.1",
repo: "bitnami",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.2",
repo: "bitnami",
digest: "test",
created: "now",
},
]),
"pretzel": sortCharts([
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0",
repo: "bitnami",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0.1",
repo: "bitnami",
digest: "test",
created: "now",
},
]),
}],
]);
class HelmChartManagerFake {
constructor(private repo: HelmRepo){ }
public async charts(): Promise<any> {
return charts.get(this.repo.name) ?? {};
}
}
@@ -0,0 +1,19 @@
/**
* 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";
export interface ChartCacheEntry {
data: string; // serialized JSON
mtimeMs: number;
}
export type HelmChartManagerCache = Map<string, ChartCacheEntry>;
const helmChartManagerCacheInjectable = getInjectable({
id: "helm-chart-manager-cache",
instantiate: (): HelmChartManagerCache => new Map(),
});
export default helmChartManagerCacheInjectable;
@@ -0,0 +1,26 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import type { HelmRepo } from "../../common/helm/helm-repo";
import { HelmChartManager } from "./helm-chart-manager";
import helmChartManagerCacheInjectable from "./helm-chart-manager-cache.injectable";
import loggerInjectable from "../../common/logger.injectable";
const helmChartManagerInjectable = getInjectable({
id: "helm-chart-manager",
instantiate: (di, repo: HelmRepo) => {
const cache = di.inject(helmChartManagerCacheInjectable);
const logger = di.inject(loggerInjectable);
return new HelmChartManager(repo, { cache, logger });
},
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, repo: HelmRepo) => repo.name,
}),
});
export default helmChartManagerInjectable;
+11 -16
View File
@@ -5,39 +5,34 @@
import fs from "fs";
import * as yaml from "js-yaml";
import logger from "../logger";
import type { RepoHelmChartList } from "../../common/k8s-api/endpoints/helm-charts.api";
import { iter, put, sortCharts } from "../../common/utils";
import { execHelm } from "./exec";
import type { SetRequired } from "type-fest";
import { assert } from "console";
import type { HelmRepo } from "../../common/helm/helm-repo";
interface ChartCacheEntry {
data: string; // serialized JSON
mtimeMs: number;
}
import type { HelmChartManagerCache } from "./helm-chart-manager-cache.injectable";
import type { Logger } from "../../common/logger";
export interface HelmCacheFile {
apiVersion: string;
entries: RepoHelmChartList;
}
export class HelmChartManager {
static readonly #cache = new Map<string, ChartCacheEntry>();
interface Dependencies {
cache: HelmChartManagerCache;
logger: Logger;
}
export class HelmChartManager {
protected readonly repo: SetRequired<HelmRepo, "cacheFilePath">;
private constructor(repo: HelmRepo) {
constructor(repo: HelmRepo, private dependencies: Dependencies) {
assert(repo.cacheFilePath, "CacheFilePath must be provided on the helm repo");
this.repo = repo as SetRequired<HelmRepo, "cacheFilePath">;
}
static forRepo(repo: HelmRepo) {
return new this(repo);
}
public async chartVersions(name: string) {
const charts = await this.charts();
@@ -48,7 +43,7 @@ export class HelmChartManager {
try {
return await this.cachedYaml();
} catch(error) {
logger.error("HELM-CHART-MANAGER]: failed to list charts", { error });
this.dependencies.logger.error("HELM-CHART-MANAGER]: failed to list charts", { error });
return {};
}
@@ -84,7 +79,7 @@ export class HelmChartManager {
const normalized = normalizeHelmCharts(this.repo.name, data.entries);
return put(
HelmChartManager.#cache,
this.dependencies.cache,
this.repo.name,
{
data: JSON.stringify(normalized),
@@ -94,7 +89,7 @@ export class HelmChartManager {
}
protected async cachedYaml(): Promise<RepoHelmChartList> {
let cacheEntry = HelmChartManager.#cache.get(this.repo.name);
let cacheEntry = this.dependencies.cache.get(this.repo.name);
if (!cacheEntry) {
cacheEntry = await this.updateYamlCache();
@@ -3,14 +3,16 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { HelmChartManager } from "../helm-chart-manager";
import getActiveHelmRepositoryInjectable from "../repositories/get-active-helm-repository.injectable";
import type { HelmRepo } from "../../../common/helm/helm-repo";
import helmChartManagerInjectable from "../helm-chart-manager.injectable";
const getHelmChartValuesInjectable = getInjectable({
id: "get-helm-chart-values",
instantiate: (di) => {
const getActiveHelmRepository = di.inject(getActiveHelmRepositoryInjectable);
const getChartManager = (repo: HelmRepo) => di.inject(helmChartManagerInjectable, repo);
return async (repoName: string, chartName: string, version = "") => {
const repo = await getActiveHelmRepository(repoName);
@@ -19,7 +21,7 @@ const getHelmChartValuesInjectable = getInjectable({
return undefined;
}
return HelmChartManager.forRepo(repo).getValues(chartName, version);
return getChartManager(repo).getValues(chartName, version);
};
},
@@ -3,14 +3,16 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { HelmChartManager } from "../helm-chart-manager";
import getActiveHelmRepositoryInjectable from "../repositories/get-active-helm-repository.injectable";
import type { HelmRepo } from "../../../common/helm/helm-repo";
import helmChartManagerInjectable from "../helm-chart-manager.injectable";
const getHelmChartInjectable = getInjectable({
id: "get-helm-chart",
instantiate: (di) => {
const getActiveHelmRepository = di.inject(getActiveHelmRepositoryInjectable);
const getChartManager = (repo: HelmRepo) => di.inject(helmChartManagerInjectable, repo);
return async (repoName: string, chartName: string, version = "") => {
const repo = await getActiveHelmRepository(repoName);
@@ -19,7 +21,7 @@ const getHelmChartInjectable = getInjectable({
return undefined;
}
const chartManager = HelmChartManager.forRepo(repo);
const chartManager = getChartManager(repo);
return {
readme: await chartManager.getReadme(chartName, version),
@@ -5,14 +5,16 @@
import { getInjectable } from "@ogre-tools/injectable";
import assert from "assert";
import { object } from "../../../common/utils";
import { HelmChartManager } from "../helm-chart-manager";
import getActiveHelmRepositoriesInjectable from "../repositories/get-active-helm-repositories/get-active-helm-repositories.injectable";
import type { HelmRepo } from "../../../common/helm/helm-repo";
import helmChartManagerInjectable from "../helm-chart-manager.injectable";
const listHelmChartsInjectable = getInjectable({
id: "list-helm-charts",
instantiate: (di) => {
const getActiveHelmRepositories = di.inject(getActiveHelmRepositoriesInjectable);
const getChartManager = (repo: HelmRepo) => di.inject(helmChartManagerInjectable, repo);
return async () => {
const result = await getActiveHelmRepositories();
@@ -27,7 +29,7 @@ const listHelmChartsInjectable = getInjectable({
async (repo) =>
[
repo.name,
await HelmChartManager.forRepo(repo).charts(),
await getChartManager(repo).charts(),
] as const,
),
),