-
Notifications
You must be signed in to change notification settings - Fork 23
feat(dns): Add smarter wait for changing dns zone asserts #822
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
Draft
azgabur
wants to merge
1
commit into
Kuadrant:main
Choose a base branch
from
azgabur:dns_sleep
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| from typing import Dict, Union | ||
| from urllib.parse import urlparse, ParseResult | ||
|
|
||
| import backoff | ||
| import dns.resolver | ||
| from weakget import weakget | ||
|
|
||
|
|
@@ -227,3 +228,44 @@ def domain_match(first: str, second: str): | |
| if second[0] == "*": | ||
| return second[2:] == ".".join(first.split(".")[1:]) | ||
| return False | ||
|
|
||
|
|
||
| def wait_for_dns( | ||
|
Contributor
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. We should consider creating a class for our own dns resolver at this point |
||
| hostname: str, | ||
| value: str, | ||
| not_in: bool = False, | ||
| resolver: dns.resolver.Resolver = dns.resolver.get_default_resolver(), | ||
| ) -> dns.resolver.HostAnswers: | ||
| """This method returns dns answer from dns.resolver.resolve_name(hostname) but has backoff functionality | ||
| to retry on common errors such as NXDOMAIN or NoAnswer. Additionally retries until specified value is in the DNS | ||
| answer. This can be used when a change to a zone is expected and to assert the change. | ||
|
|
||
| :param hostname: Hostname for DNS query | ||
| :param value: Expected value in answer | ||
| :param not_in: Default false; If true, the method will wait until the value is not in the DNS answer | ||
| :param resolver: Default dns.resolver; Specify your own resolver class | ||
|
|
||
| :return: HostAnswers class containing answers for A and AAAA queries | ||
| """ | ||
| # pylint: disable=unnecessary-lambda-assignment | ||
| # it is indeed necessary so it can be overridden | ||
| backoff_function = lambda answer: value not in answer.addresses() | ||
| if not_in: | ||
| backoff_function = lambda answer: value in answer.addresses() | ||
|
|
||
| @backoff.on_predicate( | ||
| backoff.runtime, | ||
| backoff_function, | ||
| value=lambda answer: min(map(lambda rrset: rrset.ttl, answer.values())), | ||
fabikova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| max_time=300, | ||
| ) | ||
| @backoff.on_exception( | ||
| backoff.constant, | ||
| (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers), | ||
| interval=5, | ||
| max_time=300, | ||
| ) | ||
| def _assert_dns(): | ||
| return resolver.resolve_name(hostname) | ||
|
|
||
| return _assert_dns() | ||
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.
What would this new function return after the unsuccessful backoff after 300 seconds? I suspect it will fail out on resolvement after the exception backoff is done.
Should we add something like
assert answer is not Noneor similar after, so it is clear where test failed in case of dns timeout or something?