graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""AST-based checks that app-author code (devshell apps, scaffold templates)
|
|
2
|
+
imports only graphcore's canonical public modules - never a bare
|
|
3
|
+
`graphcore.public` import (a namespace only; it re-exports nothing) or a
|
|
4
|
+
kernel-internal path.
|
|
5
|
+
|
|
6
|
+
Static checks can only see literal `import`/`from ... import ...`
|
|
7
|
+
statements; they cannot validate imports built dynamically at runtime
|
|
8
|
+
(``importlib.import_module`` with a computed name, etc.).
|
|
9
|
+
"""
|
|
10
|
+
import ast
|
|
11
|
+
import re
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from django.test import SimpleTestCase
|
|
15
|
+
|
|
16
|
+
from graphcore.tests.public_api_manifest import MODULES
|
|
17
|
+
|
|
18
|
+
REPO_ROOT = Path(__file__).resolve().parents[4]
|
|
19
|
+
|
|
20
|
+
CANONICAL_MODULES = tuple(MODULES.keys())
|
|
21
|
+
|
|
22
|
+
# relative_path -> (allowed import prefix, reason). Framework bootstrap files
|
|
23
|
+
# run before app discovery / after Django app-registry readiness and
|
|
24
|
+
# legitimately reach past the app-author surface for that one import.
|
|
25
|
+
EXCEPTIONS: dict[str, tuple[str, str]] = {
|
|
26
|
+
'graphcore_projects/devshell/backend/devshell/settings.py': (
|
|
27
|
+
'graphcore.registry.app_discovery',
|
|
28
|
+
'project settings must discover installed apps before django.setup() runs',
|
|
29
|
+
),
|
|
30
|
+
'graphcore_projects/devshell/backend/devshell/asgi.py': (
|
|
31
|
+
'graphcore.transport',
|
|
32
|
+
'ASGI wiring imports websocket routes and the agent frontend only after the app registry is ready',
|
|
33
|
+
),
|
|
34
|
+
'distro/backend/graphcore_project_kit/scaffold/project_template/backend/project_name/settings.py-tpl': (
|
|
35
|
+
'graphcore.registry.app_discovery',
|
|
36
|
+
'generated project settings bootstrap, same requirement as devshell',
|
|
37
|
+
),
|
|
38
|
+
'distro/backend/graphcore_project_kit/scaffold/project_template/backend/project_name/asgi.py-tpl': (
|
|
39
|
+
'graphcore.transport',
|
|
40
|
+
'generated project ASGI wiring, same requirement as devshell',
|
|
41
|
+
),
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
_TEMPLATE_EXPR = re.compile(r'\{\{.*?\}\}', re.DOTALL)
|
|
46
|
+
_TEMPLATE_TAG = re.compile(r'\{%.*?%\}', re.DOTALL)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _strip_template_syntax(source: str) -> str:
|
|
50
|
+
"""Scaffold .py-tpl files use Jinja-style `{{ name }}` substitutions in
|
|
51
|
+
code position (e.g. class names) - replace them with a placeholder
|
|
52
|
+
identifier so the result is parseable Python. Import statements never
|
|
53
|
+
contain template syntax, so this never hides a real import."""
|
|
54
|
+
source = _TEMPLATE_EXPR.sub('_template_value', source)
|
|
55
|
+
return _TEMPLATE_TAG.sub('', source)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def imported_modules(source: str):
|
|
59
|
+
for node in ast.walk(ast.parse(_strip_template_syntax(source))):
|
|
60
|
+
if isinstance(node, ast.Import):
|
|
61
|
+
yield from (alias.name for alias in node.names)
|
|
62
|
+
elif isinstance(node, ast.ImportFrom) and not node.level:
|
|
63
|
+
base = node.module or ''
|
|
64
|
+
yield base
|
|
65
|
+
yield from (base + '.' + alias.name for alias in node.names)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def violations(source: str, relative_path: str = '') -> list[str]:
|
|
69
|
+
"""graphcore.* imports in `source` that aren't a canonical public module,
|
|
70
|
+
honoring the one-import exception recorded for `relative_path`."""
|
|
71
|
+
exception = EXCEPTIONS.get(relative_path)
|
|
72
|
+
failures = []
|
|
73
|
+
for name in imported_modules(source):
|
|
74
|
+
if not (name == 'graphcore' or name.startswith('graphcore.')):
|
|
75
|
+
continue
|
|
76
|
+
if any(name == m or name.startswith(m + '.') for m in CANONICAL_MODULES):
|
|
77
|
+
continue
|
|
78
|
+
if exception and (name == exception[0] or name.startswith(exception[0] + '.')):
|
|
79
|
+
continue
|
|
80
|
+
failures.append(name)
|
|
81
|
+
return failures
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
AGENT_FRONTEND_API = 'graphcore.agent_frontend'
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def agent_frontend_violations(source: str, backend_modules: frozenset[str] = frozenset()) -> list[str]:
|
|
88
|
+
"""Imports an agent frontend may not make: any graphcore module outside the
|
|
89
|
+
agent frontend API, Django, or an app's backend package."""
|
|
90
|
+
failures = []
|
|
91
|
+
for name in imported_modules(source):
|
|
92
|
+
top = name.split('.')[0]
|
|
93
|
+
if top == 'graphcore':
|
|
94
|
+
if not (name == AGENT_FRONTEND_API or name.startswith(AGENT_FRONTEND_API + '.')):
|
|
95
|
+
failures.append(name)
|
|
96
|
+
elif top == 'django' or top in backend_modules:
|
|
97
|
+
failures.append(name)
|
|
98
|
+
return failures
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _app_backend_modules(*apps_dirs: Path) -> frozenset[str]:
|
|
102
|
+
"""The Django app package name of every `apps/<name>/backend/<name>/`."""
|
|
103
|
+
return frozenset(
|
|
104
|
+
app.name for apps_dir in apps_dirs if apps_dir.is_dir()
|
|
105
|
+
for app in apps_dir.iterdir() if (app / 'backend' / app.name / '__init__.py').is_file()
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class AppAuthorBoundaryTests(SimpleTestCase):
|
|
110
|
+
def setUp(self):
|
|
111
|
+
if not (REPO_ROOT / 'graphcore_projects').is_dir() or not (REPO_ROOT / 'distro').is_dir():
|
|
112
|
+
self.skipTest('not running inside a full monorepo checkout')
|
|
113
|
+
|
|
114
|
+
def _assert_tree_clean(self, root: Path, pattern: str):
|
|
115
|
+
paths = [p for p in root.rglob(pattern) if '__pycache__' not in p.parts]
|
|
116
|
+
self.assertTrue(paths, f'expected to find files under {root}')
|
|
117
|
+
for path in paths:
|
|
118
|
+
relative = str(path.relative_to(REPO_ROOT))
|
|
119
|
+
with self.subTest(path=relative):
|
|
120
|
+
self.assertEqual(violations(path.read_text(), relative), [])
|
|
121
|
+
|
|
122
|
+
def test_devshell_apps_use_canonical_imports(self):
|
|
123
|
+
self._assert_tree_clean(REPO_ROOT / 'graphcore_projects' / 'devshell' / 'apps', '*.py')
|
|
124
|
+
|
|
125
|
+
def test_devshell_project_bootstrap_uses_canonical_imports_or_recorded_exception(self):
|
|
126
|
+
self._assert_tree_clean(REPO_ROOT / 'graphcore_projects' / 'devshell' / 'backend' / 'devshell', '*.py')
|
|
127
|
+
|
|
128
|
+
def test_scaffold_app_template_uses_canonical_imports(self):
|
|
129
|
+
scaffold = REPO_ROOT / 'distro' / 'backend' / 'graphcore_project_kit' / 'scaffold' / 'app_template'
|
|
130
|
+
self._assert_tree_clean(scaffold, '*.py-tpl')
|
|
131
|
+
|
|
132
|
+
def test_scaffold_project_template_uses_canonical_imports_or_recorded_exception(self):
|
|
133
|
+
scaffold = (
|
|
134
|
+
REPO_ROOT / 'distro' / 'backend' / 'graphcore_project_kit' / 'scaffold' / 'project_template'
|
|
135
|
+
/ 'backend' / 'project_name'
|
|
136
|
+
)
|
|
137
|
+
self._assert_tree_clean(scaffold, '*.py-tpl')
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class AgentFrontendBoundaryTests(SimpleTestCase):
|
|
141
|
+
"""An app's agent frontend imports only `graphcore.agent_frontend`, so it can't
|
|
142
|
+
reach the backend it ships with (and can move to a server of its own)."""
|
|
143
|
+
|
|
144
|
+
def setUp(self):
|
|
145
|
+
if not (REPO_ROOT / 'apps').is_dir() or not (REPO_ROOT / 'graphcore_projects').is_dir():
|
|
146
|
+
self.skipTest('not running inside a full monorepo checkout')
|
|
147
|
+
|
|
148
|
+
def _assert_agent_frontends_clean(self, apps_dir: Path):
|
|
149
|
+
backend_modules = _app_backend_modules(REPO_ROOT / 'apps', apps_dir)
|
|
150
|
+
paths = [
|
|
151
|
+
p for p in apps_dir.glob('*/backend/*_agent_frontend/**/*.py') if '__pycache__' not in p.parts
|
|
152
|
+
]
|
|
153
|
+
self.assertTrue(paths, f'expected agent frontends under {apps_dir}')
|
|
154
|
+
for path in paths:
|
|
155
|
+
with self.subTest(path=str(path.relative_to(REPO_ROOT))):
|
|
156
|
+
self.assertEqual(agent_frontend_violations(path.read_text(), backend_modules), [])
|
|
157
|
+
|
|
158
|
+
def test_builtin_apps(self):
|
|
159
|
+
self._assert_agent_frontends_clean(REPO_ROOT / 'apps')
|
|
160
|
+
|
|
161
|
+
def test_devshell_apps(self):
|
|
162
|
+
self._assert_agent_frontends_clean(REPO_ROOT / 'graphcore_projects' / 'devshell' / 'apps')
|
|
163
|
+
|
|
164
|
+
def test_scaffold_app_template(self):
|
|
165
|
+
package = (
|
|
166
|
+
REPO_ROOT / 'distro' / 'backend' / 'graphcore_project_kit' / 'scaffold' / 'app_template'
|
|
167
|
+
/ 'backend' / 'app_name_agent_frontend'
|
|
168
|
+
)
|
|
169
|
+
paths = sorted(package.glob('*.py-tpl'))
|
|
170
|
+
self.assertTrue(paths)
|
|
171
|
+
for path in paths:
|
|
172
|
+
with self.subTest(path=path.name):
|
|
173
|
+
self.assertEqual(agent_frontend_violations(path.read_text(), frozenset({'app_name'})), [])
|
|
174
|
+
|
|
175
|
+
def test_the_checker_rejects_what_an_agent_frontend_may_not_import(self):
|
|
176
|
+
backend = frozenset({'notes'})
|
|
177
|
+
for source in (
|
|
178
|
+
'from notes.models import Note',
|
|
179
|
+
'import notes',
|
|
180
|
+
'import django',
|
|
181
|
+
'from django.conf import settings',
|
|
182
|
+
'from graphcore.public.strategies import SetAttributes',
|
|
183
|
+
'from graphcore import agent_frontend',
|
|
184
|
+
):
|
|
185
|
+
with self.subTest(source=source):
|
|
186
|
+
self.assertTrue(agent_frontend_violations(source, backend))
|
|
187
|
+
|
|
188
|
+
def test_the_checker_accepts_the_agent_frontend_api_and_other_libraries(self):
|
|
189
|
+
source = (
|
|
190
|
+
'import json\n'
|
|
191
|
+
'from graphcore.agent_frontend import agent_view, operation\n'
|
|
192
|
+
'from graphcore.agent_frontend.base import type_tools\n'
|
|
193
|
+
'from .helpers import render\n'
|
|
194
|
+
)
|
|
195
|
+
self.assertEqual(agent_frontend_violations(source, frozenset({'notes'})), [])
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
class BoundaryCheckerNegativeFixtureTests(SimpleTestCase):
|
|
199
|
+
"""The checker itself must actually reject non-canonical and internal imports."""
|
|
200
|
+
|
|
201
|
+
def test_rejects_bare_public_namespace_import(self):
|
|
202
|
+
self.assertTrue(violations('from graphcore.public import Entity, register_entity'))
|
|
203
|
+
|
|
204
|
+
def test_rejects_singular_legacy_shaped_path(self):
|
|
205
|
+
self.assertTrue(violations('from graphcore.public.strategy import Strategy'))
|
|
206
|
+
|
|
207
|
+
def test_rejects_bare_internal_package_import(self):
|
|
208
|
+
self.assertTrue(violations('from graphcore import domain'))
|
|
209
|
+
|
|
210
|
+
def test_rejects_internal_implementation_path(self):
|
|
211
|
+
self.assertTrue(violations('from graphcore.domain.entity.Entity import Entity'))
|
|
212
|
+
self.assertTrue(violations('from graphcore.registry.decorators import register_entity'))
|
|
213
|
+
|
|
214
|
+
def test_accepts_canonical_imports(self):
|
|
215
|
+
self.assertEqual(violations('from graphcore.public.entities import Entity, register_entity'), [])
|
|
216
|
+
self.assertEqual(violations('from graphcore.public.auth.perms import Read'), [])
|
|
217
|
+
self.assertEqual(violations('from graphcore.public.apps import GraphCoreAppConfig'), [])
|
|
218
|
+
|
|
219
|
+
def test_rejects_implementation_paths_behind_public_facades(self):
|
|
220
|
+
self.assertTrue(violations('from graphcore.apps import GraphCoreAppConfig'))
|
|
221
|
+
self.assertTrue(violations('from graphcore.errors import ConcurrentModificationError'))
|
|
222
|
+
self.assertTrue(violations('from graphcore.api import GraphAPI'))
|
|
223
|
+
|
|
224
|
+
def test_exception_is_scoped_to_its_exact_file(self):
|
|
225
|
+
source = 'from graphcore.registry.app_discovery import discover_graphcore_apps'
|
|
226
|
+
self.assertEqual(
|
|
227
|
+
violations(source, 'graphcore_projects/devshell/backend/devshell/settings.py'), [],
|
|
228
|
+
)
|
|
229
|
+
self.assertTrue(violations(source, 'graphcore_projects/devshell/apps/authorization_testbed/backend/x.py'))
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Structural checks on the public API manifest, plus export checks on the
|
|
2
|
+
canonical facades it describes."""
|
|
3
|
+
import importlib
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from django.test import SimpleTestCase
|
|
8
|
+
|
|
9
|
+
from graphcore.tests.public_api_manifest import MODULES
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ManifestStructureTests(SimpleTestCase):
|
|
13
|
+
def test_no_duplicate_symbols_within_a_module(self):
|
|
14
|
+
for module, contract in MODULES.items():
|
|
15
|
+
with self.subTest(module=module):
|
|
16
|
+
self.assertEqual(len(contract.symbols), len(set(contract.symbols)))
|
|
17
|
+
|
|
18
|
+
def test_import_phase_is_valid(self):
|
|
19
|
+
for module, contract in MODULES.items():
|
|
20
|
+
with self.subTest(module=module):
|
|
21
|
+
self.assertIn(contract.import_phase, ('pre_setup', 'post_setup'))
|
|
22
|
+
|
|
23
|
+
def test_each_canonical_symbol_has_exactly_one_owning_module(self):
|
|
24
|
+
owners: dict[str, str] = {}
|
|
25
|
+
for module, contract in MODULES.items():
|
|
26
|
+
for symbol in contract.symbols:
|
|
27
|
+
self.assertNotIn(
|
|
28
|
+
symbol, owners,
|
|
29
|
+
f'{symbol} is claimed by both {owners.get(symbol)} and {module}',
|
|
30
|
+
)
|
|
31
|
+
owners[symbol] = module
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class CanonicalExportTests(SimpleTestCase):
|
|
35
|
+
"""Every canonical module exports exactly its manifest symbols, under
|
|
36
|
+
Django setup (the test runner already called django.setup())."""
|
|
37
|
+
|
|
38
|
+
def test_module_all_matches_manifest(self):
|
|
39
|
+
for module, contract in MODULES.items():
|
|
40
|
+
with self.subTest(module=module):
|
|
41
|
+
mod = importlib.import_module(module)
|
|
42
|
+
self.assertEqual(set(mod.__all__), set(contract.symbols))
|
|
43
|
+
for symbol in contract.symbols:
|
|
44
|
+
self.assertTrue(hasattr(mod, symbol))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ImportPhaseTests(SimpleTestCase):
|
|
48
|
+
"""pre_setup modules must import in a fresh subprocess with no
|
|
49
|
+
DJANGO_SETTINGS_MODULE and without loading Django models."""
|
|
50
|
+
|
|
51
|
+
def test_pre_setup_modules_import_without_django_setup(self):
|
|
52
|
+
pre_setup = [m for m, c in MODULES.items() if c.import_phase == 'pre_setup']
|
|
53
|
+
script = (
|
|
54
|
+
"import sys\n"
|
|
55
|
+
"sys.modules.pop('DJANGO_SETTINGS_MODULE', None)\n"
|
|
56
|
+
f"for name in {pre_setup!r}:\n"
|
|
57
|
+
" __import__(name)\n"
|
|
58
|
+
"assert 'django.db.models' not in sys.modules, 'pre_setup import pulled in Django models'\n"
|
|
59
|
+
)
|
|
60
|
+
env = {k: v for k, v in __import__('os').environ.items() if k != 'DJANGO_SETTINGS_MODULE'}
|
|
61
|
+
result = subprocess.run([sys.executable, '-c', script], env=env, capture_output=True, text=True)
|
|
62
|
+
self.assertEqual(result.returncode, 0, result.stderr)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Live public-api renderer walks graphcore.public, not implementation modules."""
|
|
2
|
+
import importlib
|
|
3
|
+
import pkgutil
|
|
4
|
+
|
|
5
|
+
from django.test import SimpleTestCase
|
|
6
|
+
|
|
7
|
+
import graphcore.public as public
|
|
8
|
+
from graphcore.docs.public_api import public_api_docs_renderer
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PublicApiRenderTests(SimpleTestCase):
|
|
12
|
+
def test_lists_every_public_module_that_exports_symbols(self):
|
|
13
|
+
text = public_api_docs_renderer()
|
|
14
|
+
for info in pkgutil.walk_packages(public.__path__, public.__name__ + "."):
|
|
15
|
+
names = getattr(importlib.import_module(info.name), "__all__", ())
|
|
16
|
+
if names:
|
|
17
|
+
self.assertIn(f"## {info.name}", text)
|
|
18
|
+
for symbol in names:
|
|
19
|
+
self.assertIn(symbol, text)
|
|
20
|
+
|
|
21
|
+
def test_does_not_list_implementation_modules(self):
|
|
22
|
+
headings = [line for line in public_api_docs_renderer().splitlines() if line.startswith("## ")]
|
|
23
|
+
self.assertTrue(headings)
|
|
24
|
+
for heading in headings:
|
|
25
|
+
self.assertTrue(heading.startswith("## graphcore.public."))
|
|
26
|
+
|
|
27
|
+
def test_skips_dataclass_signature_stubs(self):
|
|
28
|
+
self.assertNotIn("Read()", public_api_docs_renderer())
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""The composition root: the store identity (one value, in three places),
|
|
2
|
+
and building a runtime from the configured factory."""
|
|
3
|
+
from django.test import SimpleTestCase, TestCase, override_settings
|
|
4
|
+
|
|
5
|
+
from graphcore.domain.entity.Entity import Entity
|
|
6
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
7
|
+
from graphcore.runtime import configured_runtime, django_runtime
|
|
8
|
+
from graphcore.types.entity_id import backend_of
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class RuntimeBackendIdentityTests(TestCase):
|
|
12
|
+
def setUp(self):
|
|
13
|
+
self.runtime = django_runtime(DEFAULT_REGISTRY)
|
|
14
|
+
|
|
15
|
+
def test_the_runtime_carries_the_stores_identity(self):
|
|
16
|
+
self.assertEqual(self.runtime.backend_id, self.runtime.repository.backend_id())
|
|
17
|
+
|
|
18
|
+
def test_the_entity_service_routes_against_the_same_value(self):
|
|
19
|
+
self.assertEqual(self.runtime.entity_service.backend_id, self.runtime.backend_id)
|
|
20
|
+
|
|
21
|
+
def test_a_minted_id_names_the_store(self):
|
|
22
|
+
entity = self.runtime.entity_service.create(Entity)
|
|
23
|
+
|
|
24
|
+
self.assertEqual(backend_of(entity.entity_id), self.runtime.backend_id)
|
|
25
|
+
|
|
26
|
+
def test_handle_defaults_to_unset(self):
|
|
27
|
+
self.assertIsNone(self.runtime.handle)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _recording_factory(registry, *, worker):
|
|
31
|
+
return ('built from', registry, worker)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ConfiguredRuntimeTests(SimpleTestCase):
|
|
35
|
+
@override_settings(GRAPHCORE_RUNTIME_FACTORY='graphcore.tests.test_runtime._recording_factory')
|
|
36
|
+
def test_uses_the_configured_factory_with_the_process_registry(self):
|
|
37
|
+
self.assertEqual(configured_runtime(), ('built from', DEFAULT_REGISTRY, False))
|
|
38
|
+
self.assertEqual(configured_runtime(worker=True), ('built from', DEFAULT_REGISTRY, True))
|
|
39
|
+
|
|
40
|
+
@override_settings(GRAPHCORE_RUNTIME_FACTORY='graphcore.tests.test_runtime._recording_factory')
|
|
41
|
+
def test_each_call_builds_anew(self):
|
|
42
|
+
self.assertIsNot(configured_runtime(), configured_runtime())
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Dependency direction checks for shared types and their inner consumers."""
|
|
2
|
+
import ast
|
|
3
|
+
from importlib.util import resolve_name
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from django.test import SimpleTestCase
|
|
8
|
+
|
|
9
|
+
import graphcore
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
ROOT = Path(graphcore.__file__).resolve().parent
|
|
13
|
+
LEGACY = (
|
|
14
|
+
'graphcore.api', 'graphcore.results', 'graphcore.graph.EntityFilter',
|
|
15
|
+
'graphcore.auth.authz.primitives', 'graphcore.types.api',
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def imported_modules(source, package):
|
|
20
|
+
for node in ast.walk(ast.parse(source)):
|
|
21
|
+
if isinstance(node, ast.Import):
|
|
22
|
+
yield from (alias.name for alias in node.names)
|
|
23
|
+
elif isinstance(node, ast.ImportFrom):
|
|
24
|
+
base = node.module or ''
|
|
25
|
+
if node.level:
|
|
26
|
+
base = resolve_name('.' * node.level + base, package)
|
|
27
|
+
yield base
|
|
28
|
+
yield from (base + '.' + alias.name for alias in node.names)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def violations(source, package, shared=False):
|
|
32
|
+
failures = []
|
|
33
|
+
for name in imported_modules(source, package):
|
|
34
|
+
if shared:
|
|
35
|
+
allowed = (name.split('.')[0] in sys.stdlib_module_names
|
|
36
|
+
or name == 'graphcore.types' or name.startswith('graphcore.types.')
|
|
37
|
+
or name == 'graphcore.errors' or name.startswith('graphcore.errors.'))
|
|
38
|
+
if not allowed:
|
|
39
|
+
failures.append(name)
|
|
40
|
+
elif any(name == prefix or name.startswith(prefix + '.') for prefix in LEGACY):
|
|
41
|
+
failures.append(name)
|
|
42
|
+
return failures
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class SharedTypeBoundaryTests(SimpleTestCase):
|
|
46
|
+
def test_shared_package_has_only_inward_imports(self):
|
|
47
|
+
for path in (ROOT / 'types').rglob('*.py'):
|
|
48
|
+
if 'tests' in path.parts:
|
|
49
|
+
continue
|
|
50
|
+
with self.subTest(path=path.name):
|
|
51
|
+
self.assertEqual(violations(path.read_text(), 'graphcore.types', shared=True), [])
|
|
52
|
+
|
|
53
|
+
def test_inner_components_do_not_import_outer_type_definitions(self):
|
|
54
|
+
for folder in ('domain', 'graph', 'execution', 'auth'):
|
|
55
|
+
for path in (ROOT / folder).rglob('*.py'):
|
|
56
|
+
if 'tests' in path.parts:
|
|
57
|
+
continue
|
|
58
|
+
package = 'graphcore.' + '.'.join(path.relative_to(ROOT).parts[:-1])
|
|
59
|
+
with self.subTest(path=str(path.relative_to(ROOT))):
|
|
60
|
+
self.assertEqual(violations(path.read_text(), package), [])
|
|
61
|
+
|
|
62
|
+
def test_checker_rejects_guarded_and_relative_outward_imports(self):
|
|
63
|
+
cases = [
|
|
64
|
+
('from graphcore.api.principal import Principal', 'graphcore.graph', False),
|
|
65
|
+
('from ..api import Principal', 'graphcore.graph', False),
|
|
66
|
+
('from graphcore import api', 'graphcore.graph', False),
|
|
67
|
+
('from .. import api', 'graphcore.graph', False),
|
|
68
|
+
('from graphcore.types import api', 'graphcore.graph', False),
|
|
69
|
+
('if TYPE_CHECKING:\n from django.db.models import QuerySet', 'graphcore.types', True),
|
|
70
|
+
('from ..domain.entity.Entity import Entity', 'graphcore.types', True),
|
|
71
|
+
]
|
|
72
|
+
for source, package, shared in cases:
|
|
73
|
+
with self.subTest(source=source):
|
|
74
|
+
self.assertTrue(violations(source, package, shared))
|
|
75
|
+
|
|
76
|
+
def test_checker_accepts_canonical_imports(self):
|
|
77
|
+
self.assertEqual(violations('from ..types.identity import Principal', 'graphcore.graph'), [])
|
|
78
|
+
self.assertEqual(violations('from .access import Role', 'graphcore.types', shared=True), [])
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
ORM_FREE = (
|
|
82
|
+
ROOT / 'domain',
|
|
83
|
+
ROOT / 'graph' / 'EntityService.py',
|
|
84
|
+
ROOT / 'auth' / 'authz',
|
|
85
|
+
ROOT / 'persistence',
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
DJANGO_ALLOWED = {
|
|
89
|
+
# Storage is Django's only below the repository seam.
|
|
90
|
+
ROOT / 'persistence' / 'django',
|
|
91
|
+
# A documented deployment posture (GRAPHCORE_PUBLIC_USER_READ) read from
|
|
92
|
+
# settings - configuration, not persistence.
|
|
93
|
+
ROOT / 'auth' / 'authz' / 'kernel_policy.py',
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _orm_free_sources():
|
|
98
|
+
for target in ORM_FREE:
|
|
99
|
+
paths = [target] if target.is_file() else target.rglob('*.py')
|
|
100
|
+
for path in paths:
|
|
101
|
+
if 'tests' in path.parts or '__pycache__' in path.parts:
|
|
102
|
+
continue
|
|
103
|
+
if any(path == allowed or allowed in path.parents for allowed in DJANGO_ALLOWED):
|
|
104
|
+
continue
|
|
105
|
+
yield path
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class OrmFreeLayerTests(SimpleTestCase):
|
|
109
|
+
"""Entities, the graph engine and authorization decide over the
|
|
110
|
+
repository contract, never over Django. Only `persistence/django`
|
|
111
|
+
knows what a model is."""
|
|
112
|
+
|
|
113
|
+
def test_no_django_imports_above_the_repository_seam(self):
|
|
114
|
+
for path in _orm_free_sources():
|
|
115
|
+
with self.subTest(path=str(path.relative_to(ROOT))):
|
|
116
|
+
django_imports = [
|
|
117
|
+
name for name in imported_modules(
|
|
118
|
+
path.read_text(), 'graphcore.' + '.'.join(path.relative_to(ROOT).parts[:-1]),
|
|
119
|
+
)
|
|
120
|
+
if name == 'django' or name.startswith('django.')
|
|
121
|
+
]
|
|
122
|
+
self.assertEqual(django_imports, [])
|
|
123
|
+
|
|
124
|
+
def test_the_gate_actually_covers_the_files_it_names(self):
|
|
125
|
+
covered = {path.name for path in _orm_free_sources()}
|
|
126
|
+
for expected in ('Entity.py', 'EntityService.py', 'access_port.py',
|
|
127
|
+
'default_access_adapter.py', 'repository.py'):
|
|
128
|
+
self.assertIn(expected, covered)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Shared types stay independent of application startup."""
|
|
2
|
+
import os
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
import textwrap
|
|
6
|
+
from dataclasses import FrozenInstanceError
|
|
7
|
+
from typing import get_type_hints
|
|
8
|
+
|
|
9
|
+
from django.test import SimpleTestCase
|
|
10
|
+
|
|
11
|
+
from graphcore.errors import InvalidRequest
|
|
12
|
+
from graphcore.types import access, api, entities, identity, query, schedules
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SharedTypeTests(SimpleTestCase):
|
|
16
|
+
def run_python(self, source):
|
|
17
|
+
env = os.environ.copy()
|
|
18
|
+
env.pop('DJANGO_SETTINGS_MODULE', None)
|
|
19
|
+
result = subprocess.run(
|
|
20
|
+
[sys.executable, '-c', textwrap.dedent(source)],
|
|
21
|
+
env=env, capture_output=True, text=True, timeout=30,
|
|
22
|
+
)
|
|
23
|
+
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
24
|
+
|
|
25
|
+
def test_imports_without_django_settings(self):
|
|
26
|
+
self.run_python('''
|
|
27
|
+
import sys
|
|
28
|
+
from typing import get_type_hints
|
|
29
|
+
from graphcore.types import identity, access, entities, api, query
|
|
30
|
+
for module in (identity, access, entities, api, query):
|
|
31
|
+
for value in vars(module).values():
|
|
32
|
+
if isinstance(value, type) and value.__module__ == module.__name__:
|
|
33
|
+
# EntityFilter.where is a TYPE_CHECKING-only forward
|
|
34
|
+
# ref to query.Predicate (a real import would be
|
|
35
|
+
# circular: query.py imports EntityFilter itself).
|
|
36
|
+
get_type_hints(value, localns=vars(query))
|
|
37
|
+
forbidden = ('django', 'graphcore.api', 'graphcore.domain',
|
|
38
|
+
'graphcore.registry', 'graphcore.graph', 'graphcore.execution')
|
|
39
|
+
assert not [name for name in sys.modules
|
|
40
|
+
if any(name == prefix or name.startswith(prefix + '.')
|
|
41
|
+
for prefix in forbidden)]
|
|
42
|
+
''')
|
|
43
|
+
|
|
44
|
+
def test_identity_and_role_semantics(self):
|
|
45
|
+
principal = identity.Principal('user-1')
|
|
46
|
+
self.assertEqual((principal.iss, principal.sub, principal.token), (None, None, None))
|
|
47
|
+
with self.assertRaises(FrozenInstanceError):
|
|
48
|
+
principal.token = 'token'
|
|
49
|
+
viewer = access.Role('viewer', frozenset({access.Read()}))
|
|
50
|
+
same_name = access.Role('viewer', frozenset({access.Write()}))
|
|
51
|
+
self.assertEqual(same_name, viewer)
|
|
52
|
+
self.assertEqual(hash(same_name), hash(viewer))
|
|
53
|
+
self.assertEqual(identity.PUBLIC, '*')
|
|
54
|
+
|
|
55
|
+
def test_tombstone_wire_shapes(self):
|
|
56
|
+
for cause in ('forbidden', 'missing', 'unreachable', 'undelegated'):
|
|
57
|
+
expected = {'id': 'e1', 'entity_id': 'e1', 'forbidden': cause == 'forbidden'}
|
|
58
|
+
if cause != 'forbidden':
|
|
59
|
+
expected['cause'] = cause
|
|
60
|
+
self.assertEqual(entities.Tombstone('e1', cause).to_client(), expected)
|
|
61
|
+
|
|
62
|
+
def test_tombstone_deleted_wire_shape(self):
|
|
63
|
+
# Matches the frontend's `deleted === true` key (`store.ts`) - no
|
|
64
|
+
# `forbidden`/`cause` keys alongside it.
|
|
65
|
+
self.assertEqual(
|
|
66
|
+
entities.Tombstone('e1', 'deleted').to_client(),
|
|
67
|
+
{'id': 'e1', 'entity_id': 'e1', 'deleted': True},
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def test_facade_payload_serialization(self):
|
|
71
|
+
self.assertEqual(api.StrategyOutcome('pending').to_dict(), {'status': 'pending'})
|
|
72
|
+
self.assertEqual(api.StrategyOutcome('succeeded', ret_val=None).to_dict(),
|
|
73
|
+
{'status': 'succeeded', 'ret_val': {}})
|
|
74
|
+
self.assertEqual(api.StrategyOutcome('failed', error='denied', client_request_id='c1').to_dict(),
|
|
75
|
+
{'status': 'failed', 'error': 'denied', 'error_code': None, 'error_details': {},
|
|
76
|
+
'client_request_id': 'c1'})
|
|
77
|
+
self.assertEqual(api.StrategyOutcome('failed', error='stale', error_code='conflict',
|
|
78
|
+
error_details={'a': 1}).to_dict(),
|
|
79
|
+
{'status': 'failed', 'error': 'stale', 'error_code': 'conflict', 'error_details': {'a': 1}})
|
|
80
|
+
grant = access.GrantInfo('u1', access.Role('admin'), 'owner', resource_id='e1')
|
|
81
|
+
self.assertEqual(api.AccessSummary('e1', True, [grant]).to_dict(), {
|
|
82
|
+
'resource_id': 'e1', 'can_manage': True,
|
|
83
|
+
'grants': [{'subject_id': 'u1', 'role': 'admin', 'provenance': 'owner',
|
|
84
|
+
'grant_entity_id': None, 'resource_id': 'e1'}],
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
def test_orm_annotations_resolve_after_setup(self):
|
|
88
|
+
from graphcore.auth.authz.access_port import AccessPort
|
|
89
|
+
from graphcore.auth.authz.adapter.default_access_adapter import DefaultAccessAdapter
|
|
90
|
+
from graphcore.graph.EntityService import EntityService
|
|
91
|
+
from graphcore.persistence.repository import EntityRepository
|
|
92
|
+
from graphcore.persistence.django.compiler import QueryCompiler
|
|
93
|
+
from graphcore.api.graph_api import GraphAPI
|
|
94
|
+
|
|
95
|
+
for cls in (AccessPort, DefaultAccessAdapter, EntityRepository):
|
|
96
|
+
for name, method in vars(cls).items():
|
|
97
|
+
if callable(method) and getattr(method, '__annotations__', None):
|
|
98
|
+
with self.subTest(cls=cls.__name__, method=name):
|
|
99
|
+
get_type_hints(method)
|
|
100
|
+
for method in (EntityService.list_entities, GraphAPI.list_entities, QueryCompiler.compile):
|
|
101
|
+
self.assertTrue(get_type_hints(method))
|
|
102
|
+
|
|
103
|
+
def test_service_import_orders_before_discovery(self):
|
|
104
|
+
modules = [
|
|
105
|
+
'graphcore.auth.authz.access_port',
|
|
106
|
+
'graphcore.auth.authz.adapter.default_access_adapter',
|
|
107
|
+
'graphcore.graph.EntityService',
|
|
108
|
+
'graphcore.api.graph_api',
|
|
109
|
+
]
|
|
110
|
+
for order in (modules, list(reversed(modules))):
|
|
111
|
+
with self.subTest(first=order[0]):
|
|
112
|
+
self.run_python(textwrap.dedent('''
|
|
113
|
+
import importlib
|
|
114
|
+
import django
|
|
115
|
+
from graphcore.apps import GraphCoreConfig
|
|
116
|
+
from django.conf import settings
|
|
117
|
+
from graphcore import test_settings
|
|
118
|
+
class WithoutDiscovery(GraphCoreConfig):
|
|
119
|
+
def ready(self):
|
|
120
|
+
pass
|
|
121
|
+
config = {key: getattr(test_settings, key) for key in dir(test_settings)
|
|
122
|
+
if key.isupper()}
|
|
123
|
+
config['INSTALLED_APPS'] = [
|
|
124
|
+
'__main__.WithoutDiscovery' if app == 'graphcore' else app
|
|
125
|
+
for app in config['INSTALLED_APPS']
|
|
126
|
+
]
|
|
127
|
+
settings.configure(**config)
|
|
128
|
+
django.setup()
|
|
129
|
+
''') + '''
|
|
130
|
+
for name in ''' + repr(order) + ''':
|
|
131
|
+
importlib.import_module(name)
|
|
132
|
+
from graphcore.registry.discovery import discover_all
|
|
133
|
+
discover_all()
|
|
134
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
135
|
+
assert DEFAULT_REGISTRY.entities.get_all_entities()
|
|
136
|
+
''')
|
|
137
|
+
|
|
138
|
+
def test_cron_is_validated_field_by_field(self):
|
|
139
|
+
for cron in ('* * * * *', '*/5 * * * *', '0,30 9-17 * * mon-fri', '0 0 1 jan *', '15 3 */2 1-6/2 0'):
|
|
140
|
+
self.assertEqual(schedules.validate_cron(f' {cron} '), cron)
|
|
141
|
+
for cron in ('61 * * * *', '* 24 * * *', '* * 0 * *', '* * * 13 *', '* * * * 7', '*/0 * * * *',
|
|
142
|
+
'every minute', '* * * *', 'a * * * *', None):
|
|
143
|
+
with self.assertRaises(InvalidRequest, msg=cron):
|
|
144
|
+
schedules.validate_cron(cron)
|