-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
quotas: refactor/expand tests #5688
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
|
@@ -39,6 +39,33 @@ def __init__(self, **kwargs): | |
super(RateLimited, self).__init__(True, **kwargs) | ||
|
||
|
||
class BasicQuota(object): | ||
__slots__ = ['key', 'limit', 'window', 'reason_code', 'enforce'] | ||
|
||
def __init__(self, key, limit=0, window=60, reason_code=None, | ||
enforce=True): | ||
# the key is effectively the unique identifier for enforcing this quota | ||
self.key = key | ||
# maximum number of events in the given window, 0 indicates "no limit" | ||
self.limit = limit | ||
# time in seconds that this quota reflects | ||
self.window = window | ||
# a machine readable string | ||
self.reason_code = reason_code | ||
# should this quota be hard-enforced (or just tracked) | ||
self.enforce = enforce | ||
|
||
def __eq__(self, other): | ||
return isinstance(other, BasicQuota) and hash(self) == hash(other) | ||
|
||
def __hash__(self): | ||
return hash((self.key, self.limit, self.window, self.reason_code, self.enforce)) | ||
|
||
def __repr__(self): | ||
return u'<{} key={} limit={} window={}>'.format( | ||
type(self).__name__, self.key, self.limit, self.window) | ||
|
||
|
||
class Quota(Service): | ||
""" | ||
Quotas handle tracking a project's event usage (at a per minute tick) and | ||
|
@@ -47,12 +74,59 @@ class Quota(Service): | |
""" | ||
__all__ = ( | ||
'get_maximum_quota', 'get_organization_quota', 'get_project_quota', | ||
'is_rate_limited', 'translate_quota', 'validate', | ||
'get_quotas', 'is_rate_limited', 'translate_quota', 'validate', | ||
) | ||
|
||
def __init__(self, **options): | ||
pass | ||
|
||
def get_actionable_quotas(self, project, key=None): | ||
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. 👍 |
||
""" | ||
Return all implemented quotas which are enabled and actionable. | ||
|
||
This simply suppresses any configured quotas which aren't enabled. | ||
""" | ||
return [ | ||
quota | ||
for quota in self.get_quotas(project, key=key) | ||
# a zero limit means "no limit", not "reject all" | ||
if quota.limit > 0 | ||
and quota.window > 0 | ||
] | ||
|
||
def get_quotas(self, project, key=None): | ||
""" | ||
Return a list of all configured quotas, even ones which aren't | ||
enabled. | ||
""" | ||
if key: | ||
key.project = project | ||
pquota = self.get_project_quota(project) | ||
oquota = self.get_organization_quota(project.organization) | ||
results = [ | ||
BasicQuota( | ||
key='p:{}'.format(project.id), | ||
limit=pquota[0], | ||
window=pquota[1], | ||
reason_code='project_quota', | ||
), | ||
BasicQuota( | ||
key='o:{}'.format(project.organization.id), | ||
limit=oquota[0], | ||
window=oquota[1], | ||
reason_code='org_quota', | ||
), | ||
] | ||
if key: | ||
kquota = self.get_key_quota(key) | ||
results.append(BasicQuota( | ||
key='k:{}'.format(key.id), | ||
limit=kquota[0], | ||
window=kquota[1], | ||
reason_code='key_quota', | ||
)) | ||
return results | ||
|
||
def is_rate_limited(self, project, key=None): | ||
return NotRateLimited() | ||
|
||
|
@@ -79,7 +153,8 @@ def get_project_quota(self, project): | |
|
||
org = getattr(project, '_organization_cache', None) | ||
if not org: | ||
org = Organization.objects.get_from_cache(id=project.organization_id) | ||
org = Organization.objects.get_from_cache( | ||
id=project.organization_id) | ||
project._organization_cache = org | ||
|
||
max_quota_share = int(OrganizationOption.objects.get_value( | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
from six import StringIO | ||
|
||
from sentry.models import ( | ||
Group, GroupTagKey, GroupTagValue, Event, TagKey, TagValue | ||
Group, GroupTagKey, GroupTagValue, Event, OrganizationOption, TagKey, TagValue | ||
) | ||
from sentry.testutils import TestCase, TransactionTestCase | ||
from sentry.testutils.helpers import get_auth_header | ||
|
@@ -398,6 +398,25 @@ def test_protocol_v6(self): | |
|
||
assert instance.message == 'hello' | ||
|
||
def test_basic_quotas(self): | ||
results = [] | ||
for n in range(10): | ||
results.append(self._postWithHeader( | ||
data={'message': 'hello'}, | ||
key=self.projectkey.public_key, | ||
secret=self.projectkey.secret_key, | ||
protocol='6', | ||
)) | ||
|
||
# 1 per hour | ||
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. Talked about this Friday, but this needs be the first thing to happen in this test |
||
OrganizationOption.objects.set_value( | ||
self.project.organization, 'sentry:account-rate-limit', 1, | ||
) | ||
|
||
assert results[0].status_code == 200 | ||
for r in results[1:]: | ||
assert r.status_code == 429 | ||
|
||
|
||
class DepdendencyTest(TestCase): | ||
def raise_import_error(self, package): | ||
|
This file contains hidden or 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
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.
Technically should match the attributes here and not the hashes