test(resource-utils): add unit test for cleanForDownload method

This commit is contained in:
LiuYan 2024-02-01 11:47:15 +08:00
parent f4c39fcb6c
commit 1458fffad4
3 changed files with 196 additions and 98 deletions

View File

@ -0,0 +1,37 @@
import Secret from '@shell/models/secret';
describe('class Secret', () => {
it('should contains the type attribute if cleanForDownload', async() => {
const secret = new Secret({});
const yaml = `apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
`;
const cleanYaml = await secret.cleanForDownload(yaml);
expect(cleanYaml).toBe(yaml);
});
it('should remove id, links and actions keys if cleanForDownload', async() => {
const secret = new Secret({});
const expectedYamlStr = `apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: default
type: Opaque
`;
const part = `id: test_id
links:
view: https://example.com
actions:
remove: https://example.com`;
const yaml = `${ expectedYamlStr }
${ part }`;
const cleanYaml = await secret.cleanForDownload(yaml);
expect(cleanYaml).toBe(expectedYamlStr);
});
});

View File

@ -1,98 +0,0 @@
import _actions from '@shell/plugins/steve/actions';
const { cleanForDownload } = _actions;
describe('steve: actions', () => {
describe('steve > actions > cleanForDownload', () => {
it('should do nothing if the yaml is not passed', () => {
const r = cleanForDownload({});
expect(r).toBeUndefined();
});
it('should remove all root keys it expects to remove', () => {
const expectedYamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
`;
const yamlStr = `
id: test_id
links:
view: https://example.com
type: test_type
actions:
remove: https://example.com
${ expectedYamlStr }
`;
const cleanedYamlStr = cleanForDownload({}, yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
it('should remove all root keys it expects to remove, except the type key for the secret resource', () => {
const expectedYamlStr = `apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
`;
const yamlStr = `
id: test_id
links:
view: https://example.com
actions:
remove: https://example.com
${ expectedYamlStr }
`;
const cleanedYamlStr = cleanForDownload({}, yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
it('should remove all metadata keys it expects to remove', () => {
const expectedYamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
`;
const yamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
fields:
- kube-root-ca.crt
- 1
- 7d23h
relationships:
- rel: 'owner'
state: 'active'
`;
const cleanedYamlStr = cleanForDownload({}, yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
it('should remove all condition keys it expects to remove', () => {
const expectedYamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
status:
conditions:
- {}
- {}
- message: message
`;
const yamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
status:
conditions:
- error: 'error'
- transitioning: false
- message: message
`;
const cleanedYamlStr = cleanForDownload({}, yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
});
});

View File

@ -0,0 +1,159 @@
import { steveCleanForDownload } from '@shell/plugins/steve/resource-utils';
describe('steve: ressource-utils', () => {
it('should do nothing if the yaml is not passed', () => {
const r = steveCleanForDownload();
expect(r).toBeUndefined();
});
it('should remove all default rootKeys', () => {
const expectedYamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
`;
const yamlStr = `
id: test_id
links:
view: https://example.com2
type: test_type
actions:
remove: https://example.com
${ expectedYamlStr }
`;
const cleanedYamlStr = steveCleanForDownload(yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
it('should remove all the specified root keys', () => {
const part = `apiVersion: v1
kind: Secret
metadata:
name: my-secret`;
const rootKeyToYamlStringMap = {
id: 'id: test_id',
links: `links:
view: https://example.com`,
actions: `actions:
remove: https://example.com`,
type: 'type: Opaque'
};
const entries = Object.entries(rootKeyToYamlStringMap);
const yamlStr = `${ part }
${ entries.map(([_, str]) => str).join('\n') }`;
entries.forEach(([key, str]) => {
const expectedYamlStr = `${ part }
${ entries.filter(([k]) => k !== key).map(([_, str]) => str).join('\n') }\n`;
const cleanedYamlStr = steveCleanForDownload(yamlStr, { rootKeys: [key] });
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
});
it('should remove all default metadata keys', () => {
const expectedYamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
`;
const yamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
fields:
- kube-root-ca.crt
- 1
- 7d23h
relationships:
- rel: 'owner'
state: 'active'
`;
const cleanedYamlStr = steveCleanForDownload(yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
it('should remove all the specified metadata keys', () => {
const part = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap`;
const metadataKeyToYamlStringMap = {
fields:
` fields:
- kube-root-ca.crt
- 1
- 7d23h`,
relationships:
` relationships:
- rel: owner`,
state: ` state: active`
};
const entries = Object.entries(metadataKeyToYamlStringMap);
const yamlStr = `${ part }
${ entries.map(([_, str]) => str).join('\n') }`;
entries.forEach(([key, str]) => {
const expectedYamlStr = `${ part }
${ entries.filter(([k]) => k !== key).map(([_, str]) => str).join('\n') }\n`;
const cleanedYamlStr = steveCleanForDownload(yamlStr, { metadataKeys: [key] });
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
});
it('should remove all defalut condition keys', () => {
const expectedYamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
status:
conditions:
- {}
- {}
- message: message
`;
const yamlStr = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
status:
conditions:
- error: 'error'
- transitioning: false
- message: message
`;
const cleanedYamlStr = steveCleanForDownload(yamlStr);
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
it('should remove all the specified condition keys', () => {
const part = `apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
status:
conditions:
- message: message`;
const conditionKeyToYamlStringMap = {
error: ' - error: error',
transitioning: ' - transitioning: false'
};
const entries = Object.entries(conditionKeyToYamlStringMap);
const yamlStr = `${ part }
${ entries.map(([_, str]) => str).join('\n') }`;
entries.forEach(([key, str]) => {
const expectedYamlStr = `${ part }
${ entries.map(([k, str]) => k === key ? ' - {}' : str).join('\n') }\n`;
const cleanedYamlStr = steveCleanForDownload(yamlStr, { conditionKeys: [key] });
expect(cleanedYamlStr).toBe(expectedYamlStr);
});
});
});