1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-10-08 12:40:55 -04:00
parent 2c7f9afa09
commit 5fbbb99046
3 changed files with 127 additions and 92 deletions

View File

@ -20,92 +20,108 @@
*/ */
import { CustomResourceDefinition } from "../endpoints"; import { CustomResourceDefinition } from "../endpoints";
import type { KubeObjectMetadata } from "../kube-object";
describe("Crds", () => { describe("Crds", () => {
describe("getVersion", () => { describe("getVersion", () => {
it("should get the first version name from the list of versions", () => { it("should throw if none of the versions are served", () => {
const crd = new CustomResourceDefinition({ const crd = new CustomResourceDefinition({
apiVersion: "foo", apiVersion: "apiextensions.k8s.io/v1",
kind: "CustomResourceDefinition", kind: "CustomResourceDefinition",
metadata: {} as KubeObjectMetadata, metadata: {
name: "foo",
resourceVersion: "12345",
uid: "12345",
},
spec: {
versions: [
{
name: "123",
served: false,
storage: false,
},
{
name: "1234",
served: false,
storage: false,
},
],
},
}); });
crd.spec = { expect(() => crd.getVersion()).toThrowError("Failed to find a version for CustomResourceDefinition foo");
versions: [ });
{
name: "123", it("should should get the version that is both served and stored", () => {
served: false, const crd = new CustomResourceDefinition({
storage: false, apiVersion: "apiextensions.k8s.io/v1",
} kind: "CustomResourceDefinition",
] metadata: {
} as any; name: "foo",
resourceVersion: "12345",
uid: "12345",
},
spec: {
versions: [
{
name: "123",
served: true,
storage: true,
},
{
name: "1234",
served: false,
storage: false,
},
],
},
});
expect(crd.getVersion()).toBe("123"); expect(crd.getVersion()).toBe("123");
}); });
it("should get the first version name from the list of versions (length 2)", () => { it("should should get the version that is both served and stored even with version field", () => {
const crd = new CustomResourceDefinition({ const crd = new CustomResourceDefinition({
apiVersion: "foo", apiVersion: "apiextensions.k8s.io/v1",
kind: "CustomResourceDefinition", kind: "CustomResourceDefinition",
metadata: {} as KubeObjectMetadata, metadata: {
name: "foo",
resourceVersion: "12345",
uid: "12345",
},
spec: {
version: "abc",
versions: [
{
name: "123",
served: true,
storage: true,
},
{
name: "1234",
served: false,
storage: false,
},
],
},
}); });
crd.spec = {
versions: [
{
name: "123",
served: false,
storage: false,
},
{
name: "1234",
served: false,
storage: false,
}
]
} as any;
expect(crd.getVersion()).toBe("123"); expect(crd.getVersion()).toBe("123");
}); });
it("should get the first version name from the list of versions (length 2) even with version field", () => { it("should get the version name from the version field", () => {
const crd = new CustomResourceDefinition({ const crd = new CustomResourceDefinition({
apiVersion: "foo", apiVersion: "apiextensions.k8s.io/v1beta1",
kind: "CustomResourceDefinition", kind: "CustomResourceDefinition",
metadata: {} as KubeObjectMetadata, metadata: {
name: "foo",
resourceVersion: "12345",
uid: "12345",
},
spec: {
version: "abc",
}
}); });
crd.spec = {
version: "abc",
versions: [
{
name: "123",
served: false,
storage: false,
},
{
name: "1234",
served: false,
storage: false,
}
]
} as any;
expect(crd.getVersion()).toBe("123");
});
it("should get the first version name from the version field", () => {
const crd = new CustomResourceDefinition({
apiVersion: "foo",
kind: "CustomResourceDefinition",
metadata: {} as KubeObjectMetadata,
});
crd.spec = {
version: "abc"
} as any;
expect(crd.getVersion()).toBe("abc"); expect(crd.getVersion()).toBe("abc");
}); });
}); });

View File

@ -40,11 +40,19 @@ type AdditionalPrinterColumnsV1Beta = AdditionalPrinterColumnsCommon & {
JSONPath: string; JSONPath: string;
}; };
export interface CRDVersion {
name: string;
served: boolean;
storage: boolean;
schema?: object; // required in v1 but not present in v1beta
additionalPrinterColumns?: AdditionalPrinterColumnsV1[];
}
export interface CustomResourceDefinition { export interface CustomResourceDefinition {
spec: { spec: {
group: string; group: string;
/** /**
* This is deprecated for apiextensions.k8s.io/v1 but used previously * @deprecated for apiextensions.k8s.io/v1 but used previously
*/ */
version?: string; version?: string;
names: { names: {
@ -54,19 +62,19 @@ export interface CustomResourceDefinition {
listKind: string; listKind: string;
}; };
scope: "Namespaced" | "Cluster" | string; scope: "Namespaced" | "Cluster" | string;
validation?: any; /**
versions?: { * @deprecated for apiextensions.k8s.io/v1 but used previously
name: string; */
served: boolean; validation?: object;
storage: boolean; versions?: CRDVersion[];
schema?: unknown; // required in v1 but not present in v1beta
additionalPrinterColumns?: AdditionalPrinterColumnsV1[]
}[];
conversion: { conversion: {
strategy?: string; strategy?: string;
webhook?: any; webhook?: any;
}; };
additionalPrinterColumns?: AdditionalPrinterColumnsV1Beta[]; // removed in v1 /**
* @deprecated for apiextensions.k8s.io/v1 but used previously
*/
additionalPrinterColumns?: AdditionalPrinterColumnsV1Beta[];
}; };
status: { status: {
conditions: { conditions: {
@ -141,25 +149,38 @@ export class CustomResourceDefinition extends KubeObject {
return this.spec.scope; return this.spec.scope;
} }
getVersion() { getPreferedVersion(): CRDVersion {
// Prefer the modern `versions` over the legacy `version` // Prefer the modern `versions` over the legacy `version`
for (const version of this.spec.versions || []) { if (this.spec.versions) {
/** for (const version of this.spec.versions) {
* If the version is not served then 404 errors will occur /**
* We should also prefer the storage version * If the version is not served then 404 errors will occur
*/ * We should also prefer the storage version
if (version.served && version.storage) { */
return version.name; if (version.served && version.storage) {
return version;
}
} }
} } else if (this.spec.version) {
const { additionalPrinterColumns: apc } = this.spec;
const additionalPrinterColumns = apc?.map(({ JSONPath, ...apc}) => ({ ...apc, jsonPath: JSONPath }));
if (this.spec.version) { return {
return this.spec.version; name: this.spec.version,
served: true,
storage: true,
schema: this.spec.validation,
additionalPrinterColumns,
};
} }
throw new Error(`Failed to find a version for CustomResourceDefinition ${this.metadata.name}`); throw new Error(`Failed to find a version for CustomResourceDefinition ${this.metadata.name}`);
} }
getVersion() {
return this.getPreferedVersion().name;
}
isNamespaced() { isNamespaced() {
return this.getScope() === "Namespaced"; return this.getScope() === "Namespaced";
} }
@ -177,17 +198,14 @@ export class CustomResourceDefinition extends KubeObject {
} }
getPrinterColumns(ignorePriority = true): AdditionalPrinterColumnsV1[] { getPrinterColumns(ignorePriority = true): AdditionalPrinterColumnsV1[] {
const columns = this.spec.versions?.find(a => this.getVersion() == a.name)?.additionalPrinterColumns const columns = this.getPreferedVersion().additionalPrinterColumns ?? [];
?? this.spec.additionalPrinterColumns?.map(({ JSONPath, ...rest }) => ({ ...rest, jsonPath: JSONPath })) // map to V1 shape
?? [];
return columns return columns
.filter(column => column.name != "Age") .filter(column => column.name != "Age" && (ignorePriority || !column.priority));
.filter(column => ignorePriority ? true : !column.priority);
} }
getValidation() { getValidation() {
return JSON.stringify(this.spec.validation ?? this.spec.versions?.[0]?.schema, null, 2); return JSON.stringify(this.getPreferedVersion().schema, null, 2);
} }
getConditions() { getConditions() {

View File

@ -28,6 +28,7 @@ import { StorageHelper } from "./storageHelper";
import { ClusterStore } from "../../common/cluster-store"; import { ClusterStore } from "../../common/cluster-store";
import logger from "../../main/logger"; import logger from "../../main/logger";
import { getHostedClusterId, getPath } from "../../common/utils"; import { getHostedClusterId, getPath } from "../../common/utils";
import { isTestEnv } from "../../common/vars";
const storage = observable({ const storage = observable({
initialized: false, initialized: false,
@ -50,7 +51,7 @@ export function createAppStorage<T>(key: string, defaultValue: T, clusterId?: st
const fileName = `${clusterId ?? "app"}.json`; const fileName = `${clusterId ?? "app"}.json`;
const filePath = path.resolve(folder, fileName); const filePath = path.resolve(folder, fileName);
if (!storage.initialized) { if (!storage.initialized && !isTestEnv) {
init(); // called once per cluster-view init(); // called once per cluster-view
} }