mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix migration not working, add unit tests
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
parent
35b42de27e
commit
4eea30a1e6
@ -185,7 +185,7 @@
|
|||||||
"jsonpath": "^1.0.2",
|
"jsonpath": "^1.0.2",
|
||||||
"lodash": "^4.17.15",
|
"lodash": "^4.17.15",
|
||||||
"mac-ca": "^1.0.4",
|
"mac-ca": "^1.0.4",
|
||||||
"make-synchronous": "^0.1.0",
|
"make-synchronous": "https://github.com/Nokel81/make-synchronous#e366285001da13f4d5c028888a56b9baf83f9cff",
|
||||||
"marked": "^1.1.0",
|
"marked": "^1.1.0",
|
||||||
"md5-file": "^5.0.0",
|
"md5-file": "^5.0.0",
|
||||||
"mobx": "^5.15.5",
|
"mobx": "^5.15.5",
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import type { WorkspaceId } from "./workspace-store";
|
import type { WorkspaceId } from "./workspace-store";
|
||||||
import path from "path";
|
import { ipcRenderer } from "electron";
|
||||||
import { app, ipcRenderer, remote } from "electron";
|
|
||||||
import { unlink } from "fs-extra";
|
import { unlink } from "fs-extra";
|
||||||
import { action, computed, observable, toJS } from "mobx";
|
import { action, computed, observable, toJS } from "mobx";
|
||||||
import { BaseStore } from "./base-store";
|
import { BaseStore } from "./base-store";
|
||||||
|
|||||||
@ -6,6 +6,10 @@ import { ClusterStore } from "./cluster-store";
|
|||||||
import { workspaceStore } from "./workspace-store";
|
import { workspaceStore } from "./workspace-store";
|
||||||
import { saveConfigToAppFiles } from "./kube-helpers";
|
import { saveConfigToAppFiles } from "./kube-helpers";
|
||||||
|
|
||||||
|
const testDataIcon = fs.readFileSync("test-data/cluster-store-migration-icon.png")
|
||||||
|
|
||||||
|
console.log("") // fix bug
|
||||||
|
|
||||||
let clusterStore: ClusterStore;
|
let clusterStore: ClusterStore;
|
||||||
|
|
||||||
describe("empty config", () => {
|
describe("empty config", () => {
|
||||||
@ -236,12 +240,13 @@ describe("pre 2.6.0 config with a cluster icon", () => {
|
|||||||
},
|
},
|
||||||
cluster1: {
|
cluster1: {
|
||||||
kubeConfig: "foo",
|
kubeConfig: "foo",
|
||||||
icon: "icon path",
|
icon: "icon_path",
|
||||||
preferences: {
|
preferences: {
|
||||||
terminalCWD: "/tmp"
|
terminalCWD: "/tmp"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
"icon_path": testDataIcon,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mockFs(mockOpts);
|
mockFs(mockOpts);
|
||||||
@ -257,7 +262,7 @@ describe("pre 2.6.0 config with a cluster icon", () => {
|
|||||||
const storedClusterData = clusterStore.clustersList[0];
|
const storedClusterData = clusterStore.clustersList[0];
|
||||||
expect(storedClusterData.hasOwnProperty('icon')).toBe(false);
|
expect(storedClusterData.hasOwnProperty('icon')).toBe(false);
|
||||||
expect(storedClusterData.preferences.hasOwnProperty('icon')).toBe(true);
|
expect(storedClusterData.preferences.hasOwnProperty('icon')).toBe(true);
|
||||||
expect(storedClusterData.preferences.icon).toBe("icon path");
|
expect(storedClusterData.preferences.icon.startsWith("data:image/jpeg;base64,")).toBe(true);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -274,7 +279,6 @@ describe("for a pre 2.7.0-beta.0 config without a workspace", () => {
|
|||||||
},
|
},
|
||||||
cluster1: {
|
cluster1: {
|
||||||
kubeConfig: "foo",
|
kubeConfig: "foo",
|
||||||
icon: "icon path",
|
|
||||||
preferences: {
|
preferences: {
|
||||||
terminalCWD: "/tmp"
|
terminalCWD: "/tmp"
|
||||||
}
|
}
|
||||||
@ -305,16 +309,20 @@ describe("pre 3.6.0-beta.1 config with an existing cluster", () => {
|
|||||||
'lens-cluster-store.json': JSON.stringify({
|
'lens-cluster-store.json': JSON.stringify({
|
||||||
__internal__: {
|
__internal__: {
|
||||||
migrations: {
|
migrations: {
|
||||||
version: "2.7.0"
|
version: "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
clusters: [
|
clusters: [
|
||||||
{
|
{
|
||||||
id: 'cluster1',
|
id: 'cluster1',
|
||||||
kubeConfig: 'kubeconfig content'
|
kubeConfig: 'kubeconfig content',
|
||||||
|
preferences: {
|
||||||
|
icon: "store://icon_path",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
}),
|
||||||
|
"icon_path": testDataIcon,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
mockFs(mockOpts);
|
mockFs(mockOpts);
|
||||||
@ -330,4 +338,9 @@ describe("pre 3.6.0-beta.1 config with an existing cluster", () => {
|
|||||||
const config = clusterStore.clustersList[0].kubeConfigPath;
|
const config = clusterStore.clustersList[0].kubeConfigPath;
|
||||||
expect(fs.readFileSync(config, "utf8")).toBe("kubeconfig content");
|
expect(fs.readFileSync(config, "utf8")).toBe("kubeconfig content");
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("migrates to modern format with icon not in file", async () => {
|
||||||
|
const { icon } = clusterStore.clustersList[0].preferences;
|
||||||
|
expect(icon.startsWith("data:image/jpeg;base64, ")).toBe(true);
|
||||||
|
})
|
||||||
})
|
})
|
||||||
@ -5,11 +5,14 @@ import path from "path"
|
|||||||
import { app, remote } from "electron"
|
import { app, remote } from "electron"
|
||||||
import { migration } from "../migration-wrapper";
|
import { migration } from "../migration-wrapper";
|
||||||
import fse from "fs-extra"
|
import fse from "fs-extra"
|
||||||
import fileType from "file-type";
|
|
||||||
import { ClusterModel } from "../../common/cluster-store";
|
import { ClusterModel } from "../../common/cluster-store";
|
||||||
import { loadConfig, saveConfigToAppFiles } from "../../common/kube-helpers";
|
import { loadConfig, saveConfigToAppFiles } from "../../common/kube-helpers";
|
||||||
import makeSynchronous from "make-synchronous"
|
import makeSynchronous from "make-synchronous"
|
||||||
|
|
||||||
|
const AsyncFunction = Object.getPrototypeOf(async function () { return }).constructor;
|
||||||
|
const getFileTypeFnString = `return require("file-type").fromBuffer(fileData)`;
|
||||||
|
const getFileType = new AsyncFunction("fileData", getFileTypeFnString);
|
||||||
|
|
||||||
export default migration({
|
export default migration({
|
||||||
version: "3.6.0-beta.1",
|
version: "3.6.0-beta.1",
|
||||||
run(store, printLog) {
|
run(store, printLog) {
|
||||||
@ -41,11 +44,11 @@ export default migration({
|
|||||||
* migrate cluster icon
|
* migrate cluster icon
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
if (cluster.preferences.icon) {
|
if (cluster.preferences?.icon) {
|
||||||
printLog(`migrating ${cluster.preferences.icon} for ${cluster.preferences.clusterName}`)
|
printLog(`migrating ${cluster.preferences.icon} for ${cluster.preferences.clusterName}`)
|
||||||
const iconPath = cluster.preferences.icon.replace("store://", "")
|
const iconPath = cluster.preferences.icon.replace("store://", "")
|
||||||
const fileData = fse.readFileSync(path.join(userDataPath, iconPath));
|
const fileData = fse.readFileSync(path.join(userDataPath, iconPath));
|
||||||
const { mime = "" } = makeSynchronous(() => fileType.fromBuffer(fileData))();
|
const { mime = "" } = makeSynchronous(getFileType, "node")(fileData);
|
||||||
|
|
||||||
if (!mime) {
|
if (!mime) {
|
||||||
printLog(`mime type not detected for ${cluster.preferences.clusterName}'s icon: ${iconPath}`)
|
printLog(`mime type not detected for ${cluster.preferences.clusterName}'s icon: ${iconPath}`)
|
||||||
@ -53,7 +56,7 @@ export default migration({
|
|||||||
|
|
||||||
cluster.preferences.icon = `data:${mime};base64, ${fileData.toString('base64')}`;
|
cluster.preferences.icon = `data:${mime};base64, ${fileData.toString('base64')}`;
|
||||||
} else {
|
} else {
|
||||||
delete cluster.preferences.icon;
|
delete cluster.preferences?.icon;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
printLog(`Failed to migrate cluster icon for cluster "${cluster.id}"`, error)
|
printLog(`Failed to migrate cluster icon for cluster "${cluster.id}"`, error)
|
||||||
|
|||||||
BIN
test-data/cluster-store-migration-icon.png
Normal file
BIN
test-data/cluster-store-migration-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
17
yarn.lock
17
yarn.lock
@ -7640,10 +7640,9 @@ make-plural@^6.2.1:
|
|||||||
resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-6.2.1.tgz#2790af1d05fb2fc35a111ce759ffdb0aca1339a3"
|
resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-6.2.1.tgz#2790af1d05fb2fc35a111ce759ffdb0aca1339a3"
|
||||||
integrity sha512-AmkruwJ9EjvyTv6AM8MBMK3TAeOJvhgTv5YQXzF0EP2qawhpvMjDpHvsdOIIT0Vn+BB0+IogmYZ1z+Ulm/m0Fg==
|
integrity sha512-AmkruwJ9EjvyTv6AM8MBMK3TAeOJvhgTv5YQXzF0EP2qawhpvMjDpHvsdOIIT0Vn+BB0+IogmYZ1z+Ulm/m0Fg==
|
||||||
|
|
||||||
make-synchronous@^0.1.0:
|
"make-synchronous@https://github.com/Nokel81/make-synchronous#e366285001da13f4d5c028888a56b9baf83f9cff":
|
||||||
version "0.1.0"
|
version "0.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/make-synchronous/-/make-synchronous-0.1.0.tgz#d117ef54364130775e900bd52726293cda420c0d"
|
resolved "https://github.com/Nokel81/make-synchronous#e366285001da13f4d5c028888a56b9baf83f9cff"
|
||||||
integrity sha512-KgHpheDCBClcMtPN9EKB+BXyL7cAM8zrRVq8FbJBZzPbXgWlJqRAPMNU6NyJ05kEp4pmMOEgvftK7Jnaxpz2bA==
|
|
||||||
dependencies:
|
dependencies:
|
||||||
subsume "^3.0.0"
|
subsume "^3.0.0"
|
||||||
type-fest "^0.16.0"
|
type-fest "^0.16.0"
|
||||||
@ -9557,6 +9556,11 @@ readable-stream@^3.1.1, readable-stream@^3.6.0:
|
|||||||
string_decoder "^1.1.1"
|
string_decoder "^1.1.1"
|
||||||
util-deprecate "^1.0.1"
|
util-deprecate "^1.0.1"
|
||||||
|
|
||||||
|
readable-web-to-node-stream@^2.0.0:
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/readable-web-to-node-stream/-/readable-web-to-node-stream-2.0.0.tgz#751e632f466552ac0d5c440cc01470352f93c4b7"
|
||||||
|
integrity sha512-+oZJurc4hXpaaqsN68GoZGQAQIA3qr09Or4fqEsargABnbe5Aau8hFn6ISVleT3cpY/0n/8drn7huyyEvTbghA==
|
||||||
|
|
||||||
readdirp@^2.2.1:
|
readdirp@^2.2.1:
|
||||||
version "2.2.1"
|
version "2.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.2.1.tgz#0e87622a3325aa33e892285caf8b4e846529a525"
|
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.2.1.tgz#0e87622a3325aa33e892285caf8b4e846529a525"
|
||||||
@ -10701,13 +10705,6 @@ subsume@^3.0.0:
|
|||||||
escape-string-regexp "^2.0.0"
|
escape-string-regexp "^2.0.0"
|
||||||
unique-string "^2.0.0"
|
unique-string "^2.0.0"
|
||||||
|
|
||||||
sumchecker@^2.0.2:
|
|
||||||
version "2.0.2"
|
|
||||||
resolved "https://registry.yarnpkg.com/sumchecker/-/sumchecker-2.0.2.tgz#0f42c10e5d05da5d42eea3e56c3399a37d6c5b3e"
|
|
||||||
integrity sha1-D0LBDl0F2l1C7qPlbDOZo31sWz4=
|
|
||||||
dependencies:
|
|
||||||
debug "^2.2.0"
|
|
||||||
|
|
||||||
sumchecker@^3.0.1:
|
sumchecker@^3.0.1:
|
||||||
version "3.0.1"
|
version "3.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/sumchecker/-/sumchecker-3.0.1.tgz#6377e996795abb0b6d348e9b3e1dfb24345a8e42"
|
resolved "https://registry.yarnpkg.com/sumchecker/-/sumchecker-3.0.1.tgz#6377e996795abb0b6d348e9b3e1dfb24345a8e42"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user