|
2 | 2 | Unit test functions to test JSON Schema validation
|
3 | 3 | """
|
4 | 4 |
|
5 |
| -import pytest |
| 5 | +import jsonschema |
| 6 | +from tljh.config_schema import config_schema |
6 | 7 |
|
7 |
| -from tljh import config |
8 |
| - |
9 |
| - |
10 |
| -@pytest.mark.parametrize( |
11 |
| - "valid_config", |
12 |
| - [ |
13 |
| - # Valid configuration with JupyterLab as default app and HTTPS enabled with Let's Encrypt |
14 |
| - { |
15 |
| - "user_environment": {"default_app": "jupyterlab"}, |
16 |
| - "https": { |
17 |
| - "enabled": True, |
18 |
| - "letsencrypt": { |
19 |
| - |
20 |
| - "domains": ["example.com"], |
21 |
| - }, |
22 |
| - }, |
23 |
| - }, |
24 |
| - # Valid configuration with classic notebook UI |
25 |
| - {"user_environment": {"default_app": "classic"}, "https": {"enabled": False}}, |
26 |
| - # Valid configuration with culling service enabled |
27 |
| - { |
28 |
| - "user_environment": {"default_app": "jupyterlab"}, |
29 |
| - "https": {"enabled": False}, |
30 |
| - "services": { |
31 |
| - "cull": { |
32 |
| - "enabled": True, |
33 |
| - "timeout": 3600, |
34 |
| - "every": 600, |
35 |
| - "concurrency": 5, |
36 |
| - "users": True, |
37 |
| - "max_age": 86400, |
38 |
| - "remove_named_servers": False, |
39 |
| - } |
40 |
| - }, |
41 |
| - }, |
42 |
| - # Valid configuration of resource limits |
43 |
| - { |
44 |
| - "user_environment": {"default_app": "jupyterlab"}, |
45 |
| - "https": {"enabled": False}, |
46 |
| - "limits": {"memory": "2G", "cpu": 1.5}, |
47 |
| - }, |
48 |
| - # Valid configuration with TLS certificates instead of Let's Encrypt |
49 |
| - { |
50 |
| - "user_environment": {"default_app": "jupyterlab"}, |
51 |
| - "https": { |
52 |
| - "enabled": True, |
53 |
| - "tls": { |
54 |
| - "key": "/etc/tljh/tls/private.key", |
55 |
| - "cert": "/etc/tljh/tls/certificate.crt", |
56 |
| - }, |
57 |
| - }, |
58 |
| - }, |
59 |
| - # Valid configuration with TLS and custom HTTPS address/port |
60 |
| - { |
61 |
| - "user_environment": {"default_app": "jupyterlab"}, |
62 |
| - "https": { |
63 |
| - "enabled": True, |
64 |
| - "address": "192.168.1.1", |
65 |
| - "port": 443, |
66 |
| - "tls": { |
67 |
| - "key": "/etc/tljh/tls/private.key", |
68 |
| - "cert": "/etc/tljh/tls/certificate.crt", |
69 |
| - }, |
70 |
| - }, |
71 |
| - }, |
72 |
| - ], |
73 |
| -) |
74 |
| -def test_valid_configs(valid_config): |
75 |
| - """Test that known good configs pass validation without errors.""" |
76 |
| - try: |
77 |
| - config.validate_config(valid_config, validate=True) |
78 |
| - except Exception as e: |
79 |
| - pytest.fail(f"Valid config failed validation: {e}") |
80 |
| - |
81 |
| - |
82 |
| -@pytest.mark.parametrize( |
83 |
| - "invalid_config", |
84 |
| - [ |
85 |
| - # Invalid default_app value |
86 |
| - { |
87 |
| - "user_environment": {"default_app": "saturnlab"}, |
88 |
| - "https": { |
89 |
| - "enabled": True, |
90 |
| - "letsencrypt": { |
91 |
| - |
92 |
| - "domains": ["sub.example.com"], |
93 |
| - }, |
94 |
| - }, |
95 |
| - }, |
96 |
| - # Invalid domains type (should be array) |
97 |
| - { |
98 |
| - "user_environment": {"default_app": "jupyterlab"}, |
99 |
| - "https": { |
100 |
| - "enabled": True, |
101 |
| - "letsencrypt": {"email": "not-an-email", "domains": "example.com"}, |
102 |
| - }, |
103 |
| - }, |
104 |
| - # Extra unexpected property |
105 |
| - { |
106 |
| - "user_environment": {"default_app": "jupyterlab"}, |
107 |
| - "https": { |
108 |
| - "enabled": True, |
109 |
| - "letsencrypt": { |
110 |
| - |
111 |
| - "domains": ["example.com"], |
112 |
| - "extra_property": "invalid", |
113 |
| - }, |
114 |
| - }, |
115 |
| - }, |
116 |
| - # Invalid culling service config (timeout should be an integer) |
117 |
| - { |
118 |
| - "user_environment": {"default_app": "jupyterlab"}, |
119 |
| - "https": {"enabled": False}, |
120 |
| - "services": { |
121 |
| - "cull": { |
122 |
| - "enabled": True, |
123 |
| - "timeout": "one hour", |
124 |
| - "every": 600, |
125 |
| - "concurrency": 5, |
126 |
| - "users": True, |
127 |
| - "max_age": 86400, |
128 |
| - "remove_named_servers": False, |
129 |
| - } |
130 |
| - }, |
131 |
| - }, |
132 |
| - # Invalid resource limits (negative CPU value) |
133 |
| - { |
134 |
| - "user_environment": {"default_app": "jupyterlab"}, |
135 |
| - "https": {"enabled": False}, |
136 |
| - "limits": {"memory": "2G", "cpu": -1}, |
137 |
| - }, |
138 |
| - ], |
139 |
| -) |
140 |
| -def test_invalid_configs(invalid_config): |
141 |
| - """Test that known bad configs raise validation errors.""" |
142 |
| - with pytest.raises(SystemExit) as exc_info: |
143 |
| - config.validate_config(invalid_config, validate=True) |
144 |
| - |
145 |
| - assert exc_info.value.code == 1 |
| 8 | +def test_valid_config_json_schema(): |
| 9 | + """Validate that the JSON schema fits its $schema specification""" |
| 10 | + validator_class = jsonschema.validators.validator_for(config_schema) |
| 11 | + validator_class.check_schema(config_schema) |
0 commit comments