-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1413: Support automatic cleanups for NodeSchemas without a sub resource #1419
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
cartography/data/jobs/cleanup/github_org_and_users_cleanup.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
import pytest | ||
|
||
from cartography.graph.cleanupbuilder import _build_cleanup_node_and_rel_queries | ||
from cartography.graph.cleanupbuilder import _build_cleanup_rel_query_no_sub_resource | ||
from cartography.graph.cleanupbuilder import build_cleanup_queries | ||
from cartography.graph.job import get_parameters | ||
from cartography.models.aws.emr import EMRClusterToAWSAccount | ||
from cartography.models.github.users import GitHubOrganizationUserSchema | ||
from tests.data.graph.querybuilder.sample_models.asset_with_non_kwargs_tgm import FakeEC2InstanceSchema | ||
from tests.data.graph.querybuilder.sample_models.asset_with_non_kwargs_tgm import FakeEC2InstanceToAWSAccount | ||
from tests.data.graph.querybuilder.sample_models.interesting_asset import InterestingAssetSchema | ||
|
@@ -120,14 +122,49 @@ def test_get_params_from_queries(): | |
assert set(get_parameters(queries)) == {'UPDATE_TAG', 'sub_resource_id', 'LIMIT_SIZE'} | ||
|
||
|
||
def test_build_cleanup_queries_selected_rels_no_sub_res_raises_exc(): | ||
""" | ||
Test that not specifying the sub resource rel as a selected_relationship in build_cleanup_queries raises exception | ||
""" | ||
with pytest.raises(ValueError, match='node_schema without a sub resource relationship is not supported'): | ||
build_cleanup_queries(SimpleNodeSchema()) | ||
|
||
|
||
def test_build_cleanup_node_and_rel_queries_sub_res_tgm_not_validated_raises_exc(): | ||
with pytest.raises(ValueError, match='must have set_in_kwargs=True'): | ||
_build_cleanup_node_and_rel_queries(FakeEC2InstanceSchema(), FakeEC2InstanceToAWSAccount()) | ||
|
||
|
||
def test_build_cleanup_queries_no_sub_resource(): | ||
# Ensure that we only clean up stale relationships for node schemas that don't have a sub resource defined. | ||
# That is, we do not delete stale nodes. | ||
actual_queries: list[str] = build_cleanup_queries(GitHubOrganizationUserSchema()) | ||
expected_queries = [ | ||
''' | ||
MATCH (n:GitHubUser) | ||
MATCH (n)-[r:MEMBER_OF]->(:GitHubOrganization) | ||
WHERE r.lastupdated <> $UPDATE_TAG | ||
WITH r LIMIT $LIMIT_SIZE | ||
DELETE r; | ||
''', | ||
''' | ||
MATCH (n:GitHubUser) | ||
MATCH (n)-[r:ADMIN_OF]->(:GitHubOrganization) | ||
WHERE r.lastupdated <> $UPDATE_TAG | ||
WITH r LIMIT $LIMIT_SIZE | ||
DELETE r; | ||
''', | ||
] | ||
assert clean_query_list(actual_queries) == clean_query_list(expected_queries) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: fodder for a different PR - might be better to compare sets here instead of lists |
||
|
||
|
||
def test_build_cleanup_queries_no_rels(): | ||
# Ensure that no queries are generated if we try to clean up a node with no defined relationships | ||
actual_queries: list[str] = build_cleanup_queries(SimpleNodeSchema()) | ||
expected_queries = [] | ||
assert clean_query_list(actual_queries) == clean_query_list(expected_queries) | ||
|
||
|
||
def test_build_cleanup_rel_query_no_sub_resource_raises_on_sub_resource(): | ||
""" | ||
Test that _build_cleanup_rel_query_no_sub_resource raises ValueError when given a node schema | ||
that has a sub_resource_relationship defined. | ||
""" | ||
# InterestingAssetSchema has a sub_resource_relationship defined | ||
node_schema = InterestingAssetSchema() | ||
rel_schema = InterestingAssetToHelloAssetRel() | ||
|
||
with pytest.raises(ValueError, match="Expected InterestingAsset to not exist"): | ||
_build_cleanup_rel_query_no_sub_resource(node_schema, rel_schema) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed anymore because GitHubOrganizations and GitHubUsers have been migrated to use the data model. The data model automatically ensures the required indexes are created.