chore: 补充单元测试

This commit is contained in:
xiaojunnuo
2026-05-01 09:16:46 +08:00
parent 80092823db
commit 0a0f1e90e1
24 changed files with 656 additions and 187 deletions
@@ -0,0 +1,178 @@
showRunStrategy: false
default:
strategy:
runStrategy: 1
name: VolcengineDeployToVKE
title: 火山引擎-替换VKE证书
icon: svg:icon-volcengine
group: volcengine
desc: 替换火山引擎VKE集群中的TLS Secret证书
input:
cert:
title: 域名证书
helper: 请选择前置任务输出的域名证书
component:
name: output-selector
from:
- ':cert:'
required: true
order: 0
certDomains:
title: 当前证书域名
component:
name: cert-domains-getter
mergeScript: |2-
return {
component:{
inputKey: ctx.compute(({form})=>{
return form.cert
}),
}
}
template: false
required: false
order: 0
accessId:
title: Access授权
helper: 火山引擎AccessKeyId、AccessKeySecret
component:
name: access-selector
type: volcengine
required: true
order: 0
regionId:
title: Region
helper: VKE集群所在地域
component:
name: a-select
options:
- label: 北京
value: cn-beijing
- label: 上海
value: cn-shanghai
- label: 广州
value: cn-guangzhou
- label: 香港
value: cn-hongkong
- label: 柔佛
value: ap-southeast-1
- label: 雅加达
value: ap-southeast-3
value: cn-beijing
required: true
order: 0
clusterId:
title: VKE集群
component:
name: remote-select
vModel: value
mode: tags
type: plugin
action: onGetClusterList
search: false
pager: false
multi: true
watches:
- certDomains
- accessId
- accessId
- regionId
required: true
mergeScript: |2-
return {
component:{
form: ctx.compute(({form})=>{
return form
})
},
}
helper: 选择要替换证书的VKE集群,也可以手动输入集群ID
order: 0
kubeconfigType:
title: Kubeconfig类型
helper: Public需要集群API Server已开启公网访问;Private需要Certd能访问集群私网地址
component:
name: a-select
options:
- label: 公网
value: Public
- label: 私网
value: Private
- label: 集群内
value: TargetCluster
value: Public
required: true
order: 0
namespace:
title: 命名空间
value: default
component:
placeholder: 命名空间
required: true
order: 0
targetType:
title: 替换方式
helper: 按Ingress会自动读取spec.tls[].secretName;按Secret需要手动填写Secret名称
component:
name: a-select
options:
- label: 按Ingress替换
value: ingress
- label: 按Secret替换
value: secret
value: ingress
required: true
order: 0
ingressName:
title: IngressName
required: true
helper: 根据Ingress名称查找TLS Secret并替换
mergeScript: |2-
return {
show: ctx.compute(({form}) => form.targetType === 'ingress'),
required: ctx.compute(({form}) => form.targetType === 'ingress')
}
order: 0
secretName:
title: Secret名称
required: true
helper: 存储TLS证书的Secret名称,可填写多个
component:
name: a-select
vModel: value
mode: tags
open: false
mergeScript: |2-
return {
show: ctx.compute(({form}) => form.targetType === 'secret'),
required: ctx.compute(({form}) => form.targetType === 'secret')
}
order: 0
createOnNotFound:
title: Secret自动创建
helper: 如果Secret不存在,则创建kubernetes.io/tls类型Secret
value: false
component:
name: a-switch
vModel: checked
order: 0
skipTLSVerify:
title: 忽略证书校验
helper: 连接Kubernetes API Server时跳过TLS校验
value: false
component:
name: a-switch
vModel: checked
order: 0
output: {}
pluginType: deploy
type: builtIn
scriptFilePath: /plugins/plugin-volcengine/plugins/plugin-deploy-to-vke.js
+2
View File
@@ -19,6 +19,7 @@
"dev-new": "cross-env NODE_ENV=dev-new mwtsc --watch --run @midwayjs/mock/app",
"rm-newdb": "rimraf ./data/db-new.sqlite",
"test": "cross-env NODE_ENV=unittest mocha",
"test:unit": "cross-env NODE_ENV=unittest mocha --no-config --node-option no-warnings --node-option loader=ts-node/esm \"src/**/*.test.ts\"",
"cov": "cross-env c8 --all --reporter=text --reporter=lcovonly pnpm run test",
"lint": "mwts check",
"lint:fix": "mwts fix",
@@ -161,6 +162,7 @@
"mocha": "^10.2.0",
"prettier": "^2.8.8",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"tslib": "^2.8.1",
"typescript": "^5.4.2",
"why-is-node-running": "^3.2.2"
@@ -0,0 +1,54 @@
/// <reference types="mocha" />
/// <reference types="node" />
import assert from "node:assert/strict";
import { RandomUtil } from "./random.js";
describe("RandomUtil.randomStr", () => {
it("generates an 8-character alphanumeric string by default", () => {
const result = RandomUtil.randomStr();
assert.equal(result.length, 8);
assert.match(result, /^[A-Za-z0-9]+$/);
});
it("uses the requested length", () => {
assert.equal(RandomUtil.randomStr(0), "");
assert.equal(RandomUtil.randomStr(1).length, 1);
assert.equal(RandomUtil.randomStr(16).length, 16);
});
it("supports a custom character set", () => {
assert.equal(RandomUtil.randomStr(6, "A"), "AAAAAA");
});
it("supports the legacy true option as alphanumeric mode", () => {
const result = RandomUtil.randomStr(32, true);
assert.match(result, /^[A-Za-z0-9]+$/);
});
it("can generate from numbers only", () => {
const result = RandomUtil.randomStr(12, { letters: false });
assert.match(result, /^[0-9]+$/);
});
it("can generate from caller-provided option character sets", () => {
assert.equal(RandomUtil.randomStr(4, { numbers: "7", letters: false }), "7777");
assert.equal(RandomUtil.randomStr(4, { numbers: false, letters: "x" }), "xxxx");
assert.equal(RandomUtil.randomStr(4, { numbers: false, letters: false, specials: "!" }), "!!!!");
});
it("can generate from built-in specials only", () => {
const result = RandomUtil.randomStr(20, { numbers: false, letters: false, specials: true });
assert.match(result, /^[~!@#$%^*()_+\-=[\]{}|;:,./<>?]+$/);
});
it("rejects an empty character set", () => {
assert.throws(() => RandomUtil.randomStr(4, ""), /at least one available character/);
assert.throws(() => RandomUtil.randomStr(4, { numbers: false, letters: false }), /at least one available character/);
});
});
+9 -4
View File
@@ -2,14 +2,16 @@ const numbers = "0123456789";
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const specials = "~!@#$%^*()_+-=[]{}|;:,./<>?";
type RandomStrOptions = true | string | { numbers?: false | string; letters?: false | string; specials?: boolean | string };
/**
* Generate random string
* @param {Number} length
* @param {Object} options
*/
function randomStr(length, options?) {
length || (length = 8);
options || (options = {});
function randomStr(length?: number, options?: RandomStrOptions) {
length ?? (length = 8);
options ?? (options = {});
let chars = "";
let result = "";
@@ -32,6 +34,10 @@ function randomStr(length, options?) {
}
}
if (chars.length === 0) {
throw new Error("randomStr requires at least one available character");
}
while (length > 0) {
length--;
result += chars[Math.floor(Math.random() * chars.length)];
@@ -40,4 +46,3 @@ function randomStr(length, options?) {
}
export const RandomUtil = { randomStr };
+2 -8
View File
@@ -16,16 +16,10 @@
"pretty": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"typeRoots": [ "./typings", "./node_modules/@types"],
"typeRoots": ["./typings", "./node_modules/@types"],
"outDir": "dist",
"rootDir": "src",
"preserveWatchOutput": true
},
"exclude": [
"*.js",
"*.ts",
"dist",
"node_modules",
"test"
]
"exclude": ["*.js", "*.ts", "dist", "node_modules", "src/**/*.test.ts", "test"]
}