]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_crud.py
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_crud.py
1 # pylint: disable=C0102
2
3 import json
4 from typing import NamedTuple
5
6 import pytest
7 from jsonschema import validate
8
9 from ..controllers._crud import ArrayHorizontalContainer, \
10 ArrayVerticalContainer, Form, FormField, HorizontalContainer, SecretStr, \
11 VerticalContainer, serialize
12
13
14 def assertObjectEquals(a, b):
15 assert json.dumps(a) == json.dumps(b)
16
17
18 class NamedTupleMock(NamedTuple):
19 foo: int
20 var: str
21
22
23 class NamedTupleSecretMock(NamedTuple):
24 foo: int
25 var: str
26 key: SecretStr
27
28
29 @pytest.mark.parametrize("inp,out", [
30 (["foo", "var"], ["foo", "var"]),
31 (NamedTupleMock(1, "test"), {"foo": 1, "var": "test"}),
32 (NamedTupleSecretMock(1, "test", "imaginethisisakey"), {"foo": 1, "var": "test",
33 "key": "***********"}),
34 ((1, 2, 3), [1, 2, 3]),
35 (set((1, 2, 3)), [1, 2, 3]),
36 ])
37 def test_serialize(inp, out):
38 assertObjectEquals(serialize(inp), out)
39
40
41 def test_schema():
42 form = Form(path='/cluster/user/create',
43 root_container=VerticalContainer('Create user', key='create_user', fields=[
44 FormField('User entity', key='user_entity', field_type=str),
45 ArrayHorizontalContainer('Capabilities', key='caps', fields=[
46 FormField('left', field_type=str, key='left'),
47 FormField('right', key='right', field_type=str)
48 ]),
49 ArrayVerticalContainer('ah', key='ah', fields=[
50 FormField('top', key='top', field_type=str),
51 FormField('bottom', key='bottom', field_type=str)
52 ]),
53 HorizontalContainer('oh', key='oh', fields=[
54 FormField('left', key='left', field_type=str),
55 FormField('right', key='right', field_type=str)
56 ]),
57 VerticalContainer('ov', key='ov', fields=[
58 FormField('top', key='top', field_type=str),
59 FormField('bottom', key='bottom', field_type=bool)
60 ]),
61 ]))
62 form_dict = form.to_dict()
63 schema = {'schema': form_dict['control_schema'], 'layout': form_dict['ui_schema']}
64 validate(instance={'user_entity': 'foo',
65 'caps': [{'left': 'foo', 'right': 'foo2'}],
66 'ah': [{'top': 'foo', 'bottom': 'foo2'}],
67 'oh': {'left': 'foo', 'right': 'foo2'},
68 'ov': {'top': 'foo', 'bottom': True}}, schema=schema['schema'])