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,159 @@
|
|
|
1
|
+
from unittest.mock import Mock, patch
|
|
2
|
+
from django.test import TestCase
|
|
3
|
+
from graphcore.persistence.django.cache import DjangoCache
|
|
4
|
+
|
|
5
|
+
_TEST_TTL = 900
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DjangoCacheTestCase(TestCase):
|
|
9
|
+
"""Test cases for DjangoCache's generic primitives"""
|
|
10
|
+
|
|
11
|
+
def setUp(self):
|
|
12
|
+
"""Set up test fixtures"""
|
|
13
|
+
self.cache = DjangoCache(entity_ttl=_TEST_TTL)
|
|
14
|
+
# Mock the underlying Django cache
|
|
15
|
+
self.cache.cache = Mock()
|
|
16
|
+
|
|
17
|
+
def test_get(self):
|
|
18
|
+
"""Test get retrieves value from cache"""
|
|
19
|
+
self.cache.cache.get.return_value = "test_value"
|
|
20
|
+
|
|
21
|
+
result = self.cache.get("test_key")
|
|
22
|
+
|
|
23
|
+
self.assertEqual(result, "test_value")
|
|
24
|
+
self.cache.cache.get.assert_called_once_with("test_key")
|
|
25
|
+
|
|
26
|
+
def test_get_returns_none_for_missing_key(self):
|
|
27
|
+
"""Test get returns None for missing key"""
|
|
28
|
+
self.cache.cache.get.return_value = None
|
|
29
|
+
|
|
30
|
+
result = self.cache.get("missing_key")
|
|
31
|
+
|
|
32
|
+
self.assertIsNone(result)
|
|
33
|
+
|
|
34
|
+
def test_set(self):
|
|
35
|
+
"""Test set stores value in cache"""
|
|
36
|
+
self.cache.set("test_key", "test_value")
|
|
37
|
+
|
|
38
|
+
self.cache.cache.set.assert_called_once_with("test_key", "test_value", None)
|
|
39
|
+
|
|
40
|
+
def test_set_with_timeout(self):
|
|
41
|
+
"""Test set stores value with timeout"""
|
|
42
|
+
self.cache.set("test_key", "test_value", timeout=300)
|
|
43
|
+
|
|
44
|
+
self.cache.cache.set.assert_called_once_with("test_key", "test_value", 300)
|
|
45
|
+
|
|
46
|
+
def test_delete(self):
|
|
47
|
+
"""Test delete removes key from cache"""
|
|
48
|
+
self.cache.delete("test_key")
|
|
49
|
+
|
|
50
|
+
self.cache.cache.delete.assert_called_once_with("test_key")
|
|
51
|
+
|
|
52
|
+
def test_get_many(self):
|
|
53
|
+
"""Test get_many retrieves multiple values from cache"""
|
|
54
|
+
self.cache.cache.get_many.return_value = {
|
|
55
|
+
"key1": "value1",
|
|
56
|
+
"key2": "value2"
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
result = self.cache.get_many(["key1", "key2"])
|
|
60
|
+
|
|
61
|
+
self.assertEqual(result, {"key1": "value1", "key2": "value2"})
|
|
62
|
+
self.cache.cache.get_many.assert_called_once_with(["key1", "key2"])
|
|
63
|
+
|
|
64
|
+
def test_set_many(self):
|
|
65
|
+
"""Test set_many stores multiple values in cache"""
|
|
66
|
+
test_dict = {"key1": "value1", "key2": "value2"}
|
|
67
|
+
|
|
68
|
+
self.cache.set_many(test_dict)
|
|
69
|
+
|
|
70
|
+
self.cache.cache.set_many.assert_called_once_with(test_dict, None)
|
|
71
|
+
|
|
72
|
+
def test_set_many_with_timeout(self):
|
|
73
|
+
"""Test set_many stores multiple values with timeout"""
|
|
74
|
+
test_dict = {"key1": "value1", "key2": "value2"}
|
|
75
|
+
|
|
76
|
+
self.cache.set_many(test_dict, timeout=600)
|
|
77
|
+
|
|
78
|
+
self.cache.cache.set_many.assert_called_once_with(test_dict, 600)
|
|
79
|
+
|
|
80
|
+
def test_delete_many(self):
|
|
81
|
+
"""Test delete_many removes multiple keys from cache"""
|
|
82
|
+
keys = ["key1", "key2", "key3"]
|
|
83
|
+
|
|
84
|
+
self.cache.delete_many(keys)
|
|
85
|
+
|
|
86
|
+
self.cache.cache.delete_many.assert_called_once_with(keys)
|
|
87
|
+
|
|
88
|
+
def test_set_complex_object(self):
|
|
89
|
+
"""Test set stores complex objects in cache"""
|
|
90
|
+
complex_obj = {"nested": {"key": "value"}, "list": [1, 2, 3]}
|
|
91
|
+
|
|
92
|
+
self.cache.set("complex_key", complex_obj)
|
|
93
|
+
|
|
94
|
+
self.cache.cache.set.assert_called_once_with("complex_key", complex_obj, None)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class EntityCacheTestCase(TestCase):
|
|
98
|
+
"""Test the namespaced, generation-prefixed entity cache against a real backend.
|
|
99
|
+
|
|
100
|
+
Uses the actual configured cache (LocMemCache in test_settings) rather than
|
|
101
|
+
a Mock, since generation bumping relies on real add()/incr() semantics.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def setUp(self):
|
|
105
|
+
self.cache = DjangoCache(entity_ttl=_TEST_TTL)
|
|
106
|
+
self.cache.cache.clear()
|
|
107
|
+
|
|
108
|
+
def test_get_generation_initializes_to_zero(self):
|
|
109
|
+
"""A fresh cache starts at generation 0."""
|
|
110
|
+
self.assertEqual(self.cache.get_generation(), 0)
|
|
111
|
+
|
|
112
|
+
def test_bump_generation_increments(self):
|
|
113
|
+
"""Each bump advances the generation by one."""
|
|
114
|
+
self.assertEqual(self.cache.bump_generation(), 1)
|
|
115
|
+
self.assertEqual(self.cache.bump_generation(), 2)
|
|
116
|
+
self.assertEqual(self.cache.get_generation(), 2)
|
|
117
|
+
|
|
118
|
+
def test_set_entity_uses_finite_timeout(self):
|
|
119
|
+
"""Entity writes get a finite TTL, not timeout=None."""
|
|
120
|
+
with patch.object(self.cache.cache, 'set') as mock_set:
|
|
121
|
+
self.cache.set_entity("e1", "payload")
|
|
122
|
+
args, kwargs = mock_set.call_args
|
|
123
|
+
self.assertIsNotNone(args[2])
|
|
124
|
+
|
|
125
|
+
def test_get_set_entity_round_trip(self):
|
|
126
|
+
"""A value set via set_entity is retrievable via get_entity."""
|
|
127
|
+
self.cache.set_entity("e1", {"name": "test"})
|
|
128
|
+
self.assertEqual(self.cache.get_entity("e1"), {"name": "test"})
|
|
129
|
+
|
|
130
|
+
def test_bump_generation_invalidates_prior_entity_keys(self):
|
|
131
|
+
"""Bumping the generation makes previously-cached entity keys unreachable,
|
|
132
|
+
while an unrelated key written directly to the backend survives."""
|
|
133
|
+
self.cache.set_entity("e1", "old-payload")
|
|
134
|
+
self.cache.cache.set("unrelated-key", "still-here", None)
|
|
135
|
+
|
|
136
|
+
self.cache.bump_generation()
|
|
137
|
+
|
|
138
|
+
self.assertIsNone(self.cache.get_entity("e1"))
|
|
139
|
+
self.assertEqual(self.cache.cache.get("unrelated-key"), "still-here")
|
|
140
|
+
|
|
141
|
+
def test_get_entities_batch_uses_one_generation_read(self):
|
|
142
|
+
"""entity_keys computes all keys from a single generation lookup."""
|
|
143
|
+
keys = self.cache.entity_keys(["e1", "e2", "e3"])
|
|
144
|
+
generation = self.cache.get_generation()
|
|
145
|
+
self.assertEqual(keys, {
|
|
146
|
+
"e1": f"gc:{generation}:entity:e1",
|
|
147
|
+
"e2": f"gc:{generation}:entity:e2",
|
|
148
|
+
"e3": f"gc:{generation}:entity:e3",
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
def test_clear_all_entities_bumps_generation(self):
|
|
152
|
+
"""clear_all_entities invalidates via a generation bump, not cache.clear()."""
|
|
153
|
+
self.cache.set_entity("e1", "payload")
|
|
154
|
+
self.cache.cache.set("unrelated-key", "still-here", None)
|
|
155
|
+
|
|
156
|
+
self.cache.clear_all_entities()
|
|
157
|
+
|
|
158
|
+
self.assertIsNone(self.cache.get_entity("e1"))
|
|
159
|
+
self.assertEqual(self.cache.cache.get("unrelated-key"), "still-here")
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from django.test import SimpleTestCase
|
|
2
|
+
|
|
3
|
+
from graphcore.graph.entity_cache import NullEntityCache
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class NullEntityCacheTests(SimpleTestCase):
|
|
7
|
+
def test_stores_nothing(self):
|
|
8
|
+
cache = NullEntityCache()
|
|
9
|
+
cache.set_entity('gc://aaaaaaaaaa/bbbbbbbbbbbb', {'x': 1})
|
|
10
|
+
self.assertIsNone(cache.get_entity('gc://aaaaaaaaaa/bbbbbbbbbbbb'))
|
|
11
|
+
self.assertEqual(cache.get_entities(['gc://aaaaaaaaaa/bbbbbbbbbbbb']), {})
|
|
12
|
+
cache.delete_entity('gc://aaaaaaaaaa/bbbbbbbbbbbb')
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from django.core.management.base import BaseCommand
|
|
2
|
+
|
|
3
|
+
from graphcore.runtime import get_runtime
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Command(BaseCommand):
|
|
7
|
+
"""`graphcore dev reset`: invalidate the entity cache for local dev.
|
|
8
|
+
|
|
9
|
+
Bumps the generation instead of `cache.clear()`, which would also flush
|
|
10
|
+
sessions and anything else sharing the same Redis-backed cache - see
|
|
11
|
+
`graph/entity_cache.py`'s module docstring.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
help = "Invalidate the entity cache (generation bump), leaving sessions and other cache entries alone"
|
|
15
|
+
|
|
16
|
+
def handle(self, *args, **options):
|
|
17
|
+
generation = get_runtime().cache.bump_generation()
|
|
18
|
+
self.stdout.write(f"Entity cache reset (generation {generation}).")
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from asgiref.sync import async_to_sync
|
|
2
|
+
from django.core.management.base import BaseCommand, CommandError
|
|
3
|
+
|
|
4
|
+
from graphcore.auth.authn.users import find_users_by_username
|
|
5
|
+
from graphcore.auth.authz.lookups import find_group_by_key
|
|
6
|
+
from graphcore.auth.authz.kernel_writes import add_member, grant
|
|
7
|
+
from graphcore.auth.authz.defaults import ADMINS_GROUP_KEY
|
|
8
|
+
from graphcore.domain.entity.user.User import User
|
|
9
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
10
|
+
from graphcore.runtime import get_runtime
|
|
11
|
+
from graphcore.auth.authz.defaults import ADMIN
|
|
12
|
+
from graphcore.types.entity_id import is_entity_id
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Command(BaseCommand):
|
|
16
|
+
"""`graphcore createadmin`: add a user to, or remove them from,
|
|
17
|
+
`graphcore.admins` - the operator's way in, and the recovery path if the
|
|
18
|
+
group is emptied or loses its `admin` grant on the deployment."""
|
|
19
|
+
|
|
20
|
+
help = "Make a user a deployment administrator (or --remove one)"
|
|
21
|
+
|
|
22
|
+
def add_arguments(self, parser):
|
|
23
|
+
parser.add_argument('user', help="Username or principal entity id (gc://...)")
|
|
24
|
+
parser.add_argument('--remove', action='store_true', help="Remove the user from the admins")
|
|
25
|
+
parser.add_argument('--force', action='store_true', help="Allow removing the last admin")
|
|
26
|
+
|
|
27
|
+
def handle(self, *args, **options):
|
|
28
|
+
message = async_to_sync(self._run)(options['user'], options['remove'], options['force'])
|
|
29
|
+
self.stdout.write(message)
|
|
30
|
+
|
|
31
|
+
async def _run(self, user_ref: str, remove: bool, force: bool) -> str:
|
|
32
|
+
entity_service = get_runtime().entity_service
|
|
33
|
+
user = await self._resolve(entity_service, user_ref)
|
|
34
|
+
admins = await find_group_by_key(entity_service, ADMINS_GROUP_KEY)
|
|
35
|
+
if admins is None:
|
|
36
|
+
raise CommandError("No graphcore.admins group - run migrate first")
|
|
37
|
+
|
|
38
|
+
if not remove:
|
|
39
|
+
with kernel_internal_context():
|
|
40
|
+
await add_member(entity_service, admins, user.entity_id, None)
|
|
41
|
+
await grant(entity_service, entity_service.deployment_entity_id, admins.entity_id, ADMIN.name, None)
|
|
42
|
+
return f"{user.username or user.entity_id} is a deployment administrator."
|
|
43
|
+
|
|
44
|
+
if user.entity_id not in admins.member_ids:
|
|
45
|
+
return f"{user.username or user.entity_id} is not a deployment administrator."
|
|
46
|
+
if admins.member_ids == [user.entity_id] and not force:
|
|
47
|
+
raise CommandError("Refusing to remove the last administrator (pass --force)")
|
|
48
|
+
admins.remove_member(user.entity_id)
|
|
49
|
+
with kernel_internal_context():
|
|
50
|
+
await entity_service.save_entity(admins, None)
|
|
51
|
+
return f"{user.username or user.entity_id} is no longer a deployment administrator."
|
|
52
|
+
|
|
53
|
+
async def _resolve(self, entity_service, user_ref: str) -> User:
|
|
54
|
+
if is_entity_id(user_ref):
|
|
55
|
+
user = await entity_service.repository.load(user_ref)
|
|
56
|
+
if not isinstance(user, User):
|
|
57
|
+
raise CommandError(f"{user_ref} is not a user")
|
|
58
|
+
return user
|
|
59
|
+
users = await find_users_by_username(entity_service, user_ref)
|
|
60
|
+
if not users:
|
|
61
|
+
raise CommandError(f"No user named {user_ref!r} - they must sign in once first")
|
|
62
|
+
if len(users) > 1:
|
|
63
|
+
ids = ', '.join(u.entity_id for u in users)
|
|
64
|
+
raise CommandError(f"Several users are named {user_ref!r}; pass one of: {ids}")
|
|
65
|
+
return users[0]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from django.core.management.base import BaseCommand, CommandError
|
|
2
|
+
|
|
3
|
+
from graphcore.runtime import get_runtime
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _perm(perm) -> str:
|
|
7
|
+
return type(perm).__name__
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _names(items, empty: str = '(none)') -> str:
|
|
11
|
+
return ', '.join(items) if items else empty
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Command(BaseCommand):
|
|
15
|
+
"""`graphcore policy`: each registered type's effective access policy,
|
|
16
|
+
as `AccessPolicy.describe` resolves it."""
|
|
17
|
+
|
|
18
|
+
help = "Print the effective access policy of every entity type (or one)"
|
|
19
|
+
|
|
20
|
+
def add_arguments(self, parser):
|
|
21
|
+
parser.add_argument('type_id', nargs='?', help="An entity_type_id, e.g. graphcore.Grant")
|
|
22
|
+
|
|
23
|
+
def handle(self, *args, **options):
|
|
24
|
+
self.registry = get_runtime().registry
|
|
25
|
+
entities = self.registry.entities.get_all_entities()
|
|
26
|
+
type_id = options.get('type_id')
|
|
27
|
+
if type_id:
|
|
28
|
+
if type_id not in entities:
|
|
29
|
+
raise CommandError(f"Unknown entity type {type_id!r}")
|
|
30
|
+
entities = {type_id: entities[type_id]}
|
|
31
|
+
self.stdout.write('\n\n'.join(
|
|
32
|
+
self._describe(tid, cls) for tid, cls in sorted(entities.items())
|
|
33
|
+
))
|
|
34
|
+
|
|
35
|
+
def _describe(self, type_id: str, entity_cls) -> str:
|
|
36
|
+
policy = self.registry.policy
|
|
37
|
+
described = policy.describe(entity_cls)
|
|
38
|
+
can_contain = described.can_contain
|
|
39
|
+
contained_by = described.contained_by
|
|
40
|
+
lines = [
|
|
41
|
+
type_id,
|
|
42
|
+
f" shareable: {'yes' if described.shareable else 'no'}",
|
|
43
|
+
f" creatable: {'yes' if described.creatable else 'no'}",
|
|
44
|
+
f" create: {_perm(described.create)}",
|
|
45
|
+
f" read: {_perm(described.read)}",
|
|
46
|
+
f" delete: {_perm(described.delete)}",
|
|
47
|
+
f" can_contain: {'any' if can_contain is None else _names([c.__name__ for c in can_contain])}",
|
|
48
|
+
f" contained_by: {'any parent or none' if contained_by is None else _names([c.__name__ for c in contained_by])}",
|
|
49
|
+
f" public: {_names(sorted(_perm(p) for p in described.public))}",
|
|
50
|
+
f" roles: {_names([role.name for role in described.roles])}",
|
|
51
|
+
" strategies:",
|
|
52
|
+
]
|
|
53
|
+
allowed = described.allowed_strategies
|
|
54
|
+
listed = allowed if allowed is not None else described.strategies
|
|
55
|
+
for strategy in sorted(listed, key=lambda s: s.__name__):
|
|
56
|
+
lines.append(f" {strategy.__name__}: {_perm(policy.required_permission(entity_cls, strategy))}")
|
|
57
|
+
if allowed is None:
|
|
58
|
+
lines.append(f" any other strategy: {_perm(described.read)} if read-only, otherwise Write")
|
|
59
|
+
return '\n'.join(lines)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
|
|
4
|
+
from asgiref.sync import async_to_sync
|
|
5
|
+
from django.core.management.base import BaseCommand, CommandError
|
|
6
|
+
|
|
7
|
+
import graphcore.tasks as kernel_tasks
|
|
8
|
+
from graphcore.celery import app
|
|
9
|
+
from graphcore.runtime import get_runtime
|
|
10
|
+
|
|
11
|
+
# Beat writes last_run_at back on its own sync, every few minutes.
|
|
12
|
+
BEAT_SYNC_SLACK = 240
|
|
13
|
+
# Celery's built-in tasks run on every worker but `inspect registered` omits them.
|
|
14
|
+
BUILTIN_PREFIX = 'celery.'
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _aware(when):
|
|
18
|
+
return when if when is None or when.tzinfo else when.replace(tzinfo=timezone.utc)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Command(BaseCommand):
|
|
22
|
+
"""`graphcore status`: the workers that answer, whether each runs every
|
|
23
|
+
kernel task, and every task the scheduler holds. Exits non-zero on a
|
|
24
|
+
problem, so scripts can gate on it."""
|
|
25
|
+
|
|
26
|
+
help = "Show Celery workers, their kernel tasks, and the scheduler's held tasks"
|
|
27
|
+
|
|
28
|
+
def add_arguments(self, parser):
|
|
29
|
+
parser.add_argument('--timeout', type=float, default=2.0, help="Seconds to wait for workers")
|
|
30
|
+
parser.add_argument('--json', action='store_true', help="Print the report as JSON")
|
|
31
|
+
|
|
32
|
+
def handle(self, *args, **options):
|
|
33
|
+
report = self.report(options['timeout'])
|
|
34
|
+
if options['json']:
|
|
35
|
+
self.stdout.write(json.dumps(report, indent=2))
|
|
36
|
+
else:
|
|
37
|
+
self.render(report)
|
|
38
|
+
if report['problems']:
|
|
39
|
+
raise CommandError(f"{len(report['problems'])} problem(s)")
|
|
40
|
+
|
|
41
|
+
def report(self, timeout: float) -> dict:
|
|
42
|
+
problems = []
|
|
43
|
+
registered = app.control.inspect(timeout=timeout).registered() or {}
|
|
44
|
+
on_workers = set().union(*registered.values()) if registered else set()
|
|
45
|
+
if not registered:
|
|
46
|
+
problems.append('No Celery worker answered; CPU strategies and scheduled ticks will not run.')
|
|
47
|
+
|
|
48
|
+
kernel = {}
|
|
49
|
+
for name in sorted(getattr(kernel_tasks, attr).name for attr in kernel_tasks.__all__):
|
|
50
|
+
kernel[name] = name in on_workers
|
|
51
|
+
if registered and not kernel[name]:
|
|
52
|
+
problems.append(f'No worker registers {name}.')
|
|
53
|
+
|
|
54
|
+
now = datetime.now(timezone.utc)
|
|
55
|
+
held = []
|
|
56
|
+
for task in async_to_sync(get_runtime().scheduler.held)():
|
|
57
|
+
last = _aware(task.last_run_at)
|
|
58
|
+
held.append({
|
|
59
|
+
'name': task.name, 'task': task.task, 'schedule': task.schedule, 'enabled': task.enabled,
|
|
60
|
+
'args': list(task.args), 'last_run_at': last.isoformat() if last else None,
|
|
61
|
+
})
|
|
62
|
+
if registered and task.task not in on_workers and not task.task.startswith(BUILTIN_PREFIX):
|
|
63
|
+
problems.append(f'{task.name} fires {task.task}, which no worker registers.')
|
|
64
|
+
if task.enabled and task.every_seconds and last is not None \
|
|
65
|
+
and (now - last).total_seconds() > 2 * task.every_seconds + BEAT_SYNC_SLACK:
|
|
66
|
+
problems.append(f'{task.name} has not run for over twice its interval; is Beat running?')
|
|
67
|
+
|
|
68
|
+
workers = {worker: sorted(names) for worker, names in sorted(registered.items())}
|
|
69
|
+
return {'workers': workers, 'kernel_tasks': kernel, 'held': held, 'problems': problems}
|
|
70
|
+
|
|
71
|
+
def render(self, report: dict) -> None:
|
|
72
|
+
now = datetime.now(timezone.utc)
|
|
73
|
+
self.stdout.write('Workers')
|
|
74
|
+
for worker, names in report['workers'].items():
|
|
75
|
+
self.stdout.write(f' {worker} {len(names)} tasks')
|
|
76
|
+
if not report['workers']:
|
|
77
|
+
self.stdout.write(' (none answered)')
|
|
78
|
+
|
|
79
|
+
self.stdout.write('\nKernel tasks')
|
|
80
|
+
for name, found in report['kernel_tasks'].items():
|
|
81
|
+
self.stdout.write(f" {name} {'registered' if found else 'missing'}")
|
|
82
|
+
|
|
83
|
+
self.stdout.write('\nScheduler')
|
|
84
|
+
for task in report['held']:
|
|
85
|
+
state = 'enabled' if task['enabled'] else 'paused'
|
|
86
|
+
last = task['last_run_at']
|
|
87
|
+
ran = (f"last run {int((now - datetime.fromisoformat(last)).total_seconds())}s ago"
|
|
88
|
+
if last else 'never run')
|
|
89
|
+
self.stdout.write(f" {task['name']} {task['schedule']} {task['task']} {state} {ran}")
|
|
90
|
+
if not report['held']:
|
|
91
|
+
self.stdout.write(' (nothing held)')
|
|
92
|
+
|
|
93
|
+
if report['problems']:
|
|
94
|
+
self.stdout.write('\nProblems')
|
|
95
|
+
for problem in report['problems']:
|
|
96
|
+
self.stdout.write(f' - {problem}')
|
|
97
|
+
else:
|
|
98
|
+
self.stdout.write('\nOK')
|
|
File without changes
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from io import StringIO
|
|
2
|
+
|
|
3
|
+
from asgiref.sync import async_to_sync
|
|
4
|
+
from django.core.management import CommandError, call_command
|
|
5
|
+
from django.test import TransactionTestCase
|
|
6
|
+
|
|
7
|
+
from graphcore.auth.authz.lookups import find_grant, find_group_by_key
|
|
8
|
+
from graphcore.auth.authz.defaults import ADMINS_GROUP_KEY
|
|
9
|
+
from graphcore.domain.entity.user.User import User
|
|
10
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
11
|
+
from graphcore.runtime import get_runtime
|
|
12
|
+
from graphcore.types.access import ManageAccess
|
|
13
|
+
from graphcore.types.identity import Principal
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CreateAdminCommandTests(TransactionTestCase):
|
|
17
|
+
def setUp(self):
|
|
18
|
+
import graphcore.runtime as runtime_module
|
|
19
|
+
|
|
20
|
+
# A flush re-mints the backend identity; don't reuse a stale process runtime.
|
|
21
|
+
runtime_module._runtime = None
|
|
22
|
+
self.service = get_runtime().entity_service
|
|
23
|
+
|
|
24
|
+
def _user(self, username):
|
|
25
|
+
async def create():
|
|
26
|
+
user = self.service.create(User, username=username)
|
|
27
|
+
with kernel_internal_context():
|
|
28
|
+
return await self.service.save_entity(user, Principal(principal_entity_id=user.entity_id))
|
|
29
|
+
return async_to_sync(create)()
|
|
30
|
+
|
|
31
|
+
def _admin_ids(self):
|
|
32
|
+
return async_to_sync(find_group_by_key)(self.service, ADMINS_GROUP_KEY).member_ids
|
|
33
|
+
|
|
34
|
+
def _call(self, *args):
|
|
35
|
+
out = StringIO()
|
|
36
|
+
call_command('graphcore_createadmin', *args, stdout=out)
|
|
37
|
+
return out.getvalue()
|
|
38
|
+
|
|
39
|
+
def test_adds_by_username_and_by_entity_id(self):
|
|
40
|
+
alice, bob = self._user('alice'), self._user('bob')
|
|
41
|
+
self._call('alice')
|
|
42
|
+
self._call(bob.entity_id)
|
|
43
|
+
self.assertEqual(set(self._admin_ids()), {alice.entity_id, bob.entity_id})
|
|
44
|
+
|
|
45
|
+
def test_restores_the_admins_grant_on_the_deployment(self):
|
|
46
|
+
async def demote():
|
|
47
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
48
|
+
existing = await find_grant(self.service, self.service.deployment_entity_id, admins.entity_id)
|
|
49
|
+
existing.role_name = 'member'
|
|
50
|
+
with kernel_internal_context():
|
|
51
|
+
await self.service.save_entity(existing, None)
|
|
52
|
+
return admins
|
|
53
|
+
admins = async_to_sync(demote)()
|
|
54
|
+
alice = self._user('alice')
|
|
55
|
+
|
|
56
|
+
self._call('alice')
|
|
57
|
+
|
|
58
|
+
restored = async_to_sync(find_grant)(self.service, self.service.deployment_entity_id, admins.entity_id)
|
|
59
|
+
self.assertEqual(restored.role_name, 'admin')
|
|
60
|
+
principal = Principal(principal_entity_id=alice.entity_id)
|
|
61
|
+
self.assertTrue(async_to_sync(get_runtime().access.allows)(
|
|
62
|
+
principal, ManageAccess(), self.service.deployment_entity_id,
|
|
63
|
+
))
|
|
64
|
+
|
|
65
|
+
def test_unknown_user_fails(self):
|
|
66
|
+
with self.assertRaises(CommandError):
|
|
67
|
+
self._call('nobody')
|
|
68
|
+
|
|
69
|
+
def test_remove_refuses_the_last_admin_without_force(self):
|
|
70
|
+
alice, bob = self._user('alice'), self._user('bob')
|
|
71
|
+
self._call('alice')
|
|
72
|
+
self._call('bob')
|
|
73
|
+
self._call('bob', '--remove')
|
|
74
|
+
with self.assertRaises(CommandError):
|
|
75
|
+
self._call('alice', '--remove')
|
|
76
|
+
self._call('alice', '--remove', '--force')
|
|
77
|
+
self.assertEqual(self._admin_ids(), [])
|
|
78
|
+
self.assertNotIn(bob.entity_id, self._admin_ids())
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from io import StringIO
|
|
2
|
+
|
|
3
|
+
from django.core.management import call_command
|
|
4
|
+
from django.core.management.base import CommandError
|
|
5
|
+
from django.test import TestCase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _policy(*args) -> str:
|
|
9
|
+
out = StringIO()
|
|
10
|
+
call_command('graphcore_policy', *args, stdout=out)
|
|
11
|
+
return out.getvalue()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PolicyCommandTests(TestCase):
|
|
15
|
+
def test_describes_one_type(self):
|
|
16
|
+
output = _policy('graphcore.Grant')
|
|
17
|
+
|
|
18
|
+
self.assertTrue(output.startswith('graphcore.Grant\n'))
|
|
19
|
+
self.assertIn(' read: ManageAccess', output)
|
|
20
|
+
self.assertIn(' delete: ManageAccess', output)
|
|
21
|
+
self.assertIn(' can_contain: (none)', output)
|
|
22
|
+
for line in (
|
|
23
|
+
'CreateGrant: ManageAccess',
|
|
24
|
+
'ChangeGrantRole: ManageAccess',
|
|
25
|
+
'RevokeGrant: ManageAccess',
|
|
26
|
+
):
|
|
27
|
+
self.assertIn(line, output)
|
|
28
|
+
self.assertNotIn('any other strategy', output)
|
|
29
|
+
|
|
30
|
+
def test_a_type_without_an_allowlist_says_what_other_strategies_need(self):
|
|
31
|
+
output = _policy('graphcore.Entity')
|
|
32
|
+
|
|
33
|
+
self.assertIn(' delete: Delete', output)
|
|
34
|
+
self.assertIn(' can_contain: any', output)
|
|
35
|
+
self.assertIn(' contained_by: any parent or none', output)
|
|
36
|
+
self.assertIn('any other strategy: Read if read-only, otherwise Write', output)
|
|
37
|
+
|
|
38
|
+
def test_describes_every_type_by_default(self):
|
|
39
|
+
output = _policy()
|
|
40
|
+
|
|
41
|
+
for type_id in ('graphcore.Deployment', 'graphcore.Grant', 'graphcore.Group'):
|
|
42
|
+
self.assertIn(f'{type_id}\n', output)
|
|
43
|
+
|
|
44
|
+
def test_an_unknown_type_is_an_error(self):
|
|
45
|
+
with self.assertRaisesMessage(CommandError, 'nope.Nothing'):
|
|
46
|
+
_policy('nope.Nothing')
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime, timedelta, timezone
|
|
3
|
+
from io import StringIO
|
|
4
|
+
from unittest.mock import patch
|
|
5
|
+
|
|
6
|
+
from django.core.management import call_command
|
|
7
|
+
from django.core.management.base import CommandError
|
|
8
|
+
from django.test import SimpleTestCase
|
|
9
|
+
|
|
10
|
+
from graphcore.management.commands import graphcore_status
|
|
11
|
+
from graphcore.types.schedules import HeldTask
|
|
12
|
+
|
|
13
|
+
KERNEL_TASKS = [
|
|
14
|
+
'graphcore.tasks.reconcile_runs', 'graphcore.tasks.reconcile_schedules',
|
|
15
|
+
'graphcore.tasks.run_attempt', 'graphcore.tasks.scheduled_tick',
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class StatusCommandTests(SimpleTestCase):
|
|
20
|
+
def status(self, registered, held=(), *flags):
|
|
21
|
+
inspect = patch.object(graphcore_status.app.control, 'inspect')
|
|
22
|
+
runtime = patch.object(graphcore_status, 'get_runtime')
|
|
23
|
+
out = StringIO()
|
|
24
|
+
with inspect as mock_inspect, runtime as mock_runtime:
|
|
25
|
+
mock_inspect.return_value.registered.return_value = registered
|
|
26
|
+
async def held_tasks():
|
|
27
|
+
return list(held)
|
|
28
|
+
mock_runtime.return_value.scheduler.held = held_tasks
|
|
29
|
+
try:
|
|
30
|
+
call_command('graphcore_status', *flags, stdout=out)
|
|
31
|
+
return out.getvalue(), None
|
|
32
|
+
except CommandError as error:
|
|
33
|
+
return out.getvalue(), error
|
|
34
|
+
|
|
35
|
+
def test_a_worker_with_every_kernel_task_is_ok(self):
|
|
36
|
+
now = datetime.now(timezone.utc)
|
|
37
|
+
held = [HeldTask('graphcore_reconcile_runs', 'graphcore.tasks.reconcile_runs', 'every 300s',
|
|
38
|
+
True, last_run_at=now - timedelta(seconds=30), every_seconds=300)]
|
|
39
|
+
output, error = self.status({'celery@w1': KERNEL_TASKS}, held)
|
|
40
|
+
self.assertIsNone(error)
|
|
41
|
+
self.assertIn('graphcore.tasks.run_attempt registered', output)
|
|
42
|
+
self.assertIn('graphcore_reconcile_runs every 300s', output)
|
|
43
|
+
self.assertTrue(output.rstrip().endswith('OK'))
|
|
44
|
+
|
|
45
|
+
def test_no_worker_is_a_problem(self):
|
|
46
|
+
output, error = self.status({})
|
|
47
|
+
self.assertIsNotNone(error)
|
|
48
|
+
self.assertIn('No Celery worker answered', output)
|
|
49
|
+
|
|
50
|
+
def test_a_held_task_no_worker_registers_is_a_problem(self):
|
|
51
|
+
held = [HeldTask('scheduled_strategy_x', 'graphcore.tasks.execute_strategy_task', '* * * * *', True)]
|
|
52
|
+
output, error = self.status({'celery@w1': KERNEL_TASKS}, held)
|
|
53
|
+
self.assertIsNotNone(error)
|
|
54
|
+
self.assertIn('fires graphcore.tasks.execute_strategy_task, which no worker registers', output)
|
|
55
|
+
|
|
56
|
+
def test_a_held_celery_builtin_is_not_a_problem(self):
|
|
57
|
+
held = [HeldTask('celery.backend_cleanup', 'celery.backend_cleanup', '0 4 * * *', True)]
|
|
58
|
+
output, error = self.status({'celery@w1': KERNEL_TASKS}, held)
|
|
59
|
+
self.assertIsNone(error, output)
|
|
60
|
+
|
|
61
|
+
def test_a_missing_kernel_task_is_a_problem(self):
|
|
62
|
+
output, error = self.status({'celery@w1': KERNEL_TASKS[1:]})
|
|
63
|
+
self.assertIsNotNone(error)
|
|
64
|
+
self.assertIn('No worker registers graphcore.tasks.reconcile_runs', output)
|
|
65
|
+
|
|
66
|
+
def test_an_interval_task_that_stopped_running_points_at_beat(self):
|
|
67
|
+
stale = datetime.now(timezone.utc) - timedelta(hours=1)
|
|
68
|
+
held = [HeldTask('graphcore_reconcile_runs', 'graphcore.tasks.reconcile_runs', 'every 300s',
|
|
69
|
+
True, last_run_at=stale, every_seconds=300)]
|
|
70
|
+
output, error = self.status({'celery@w1': KERNEL_TASKS}, held)
|
|
71
|
+
self.assertIsNotNone(error)
|
|
72
|
+
self.assertIn('is Beat running?', output)
|
|
73
|
+
|
|
74
|
+
def test_json_reports_each_held_task_with_its_arguments(self):
|
|
75
|
+
held = [HeldTask('scheduled_strategy_t', 'graphcore.tasks.scheduled_tick', '* * * * *', False,
|
|
76
|
+
args=('t', 'owner'))]
|
|
77
|
+
output, error = self.status({'celery@w1': KERNEL_TASKS}, held, '--json')
|
|
78
|
+
self.assertIsNone(error)
|
|
79
|
+
report = json.loads(output)
|
|
80
|
+
self.assertEqual(report['held'], [{
|
|
81
|
+
'name': 'scheduled_strategy_t', 'task': 'graphcore.tasks.scheduled_tick',
|
|
82
|
+
'schedule': '* * * * *', 'enabled': False, 'args': ['t', 'owner'], 'last_run_at': None,
|
|
83
|
+
}])
|
|
84
|
+
self.assertTrue(all(report['kernel_tasks'].values()))
|
|
85
|
+
self.assertEqual(report['problems'], [])
|