-
Notifications
You must be signed in to change notification settings - Fork 912
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
fix: Fix integration test failures #5585
Conversation
3dd1b76
to
eae440f
Compare
eae440f
to
99d42d5
Compare
- fix exception handling when retr fails - test: Close connection on failure - test: Ensure server is running before it is queried
User output and service names recently changed.
- fix exception handling when retr fails - test: Close connection on failure - test: Ensure server is running before it is queried
User output and service names recently changed.
99d42d5
to
132b476
Compare
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.
thanks for cleaning up these broken tests @holmanb !
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.
Thanks @holmanb. Couple of thoughts below for your review. Generally what needs fixing is: extracting the return response
out of finally: and differentiating a File not found error from ftplib vs scheme errors.
--- a/cloudinit/url_helper.py
+++ b/cloudinit/url_helper.py
@@ -146,14 +146,20 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
ftp_tls.retrbinary(
f"RETR {url_parts.path}", callback=buffer.write
)
-
response = FtpResponse(buffer.getvalue(), url)
- LOG.debug("Closing connection")
except ftplib.error_perm as e:
+ if "No such file" in str(e):
+ raise UrlError(
+ cause=f"File {url_parts.path} not found",
+ code=NOT_FOUND,
+ headers=None,
+ url=url_parts.path,
+ ) from e
LOG.warning(
"Attempted to connect to an insecure ftp server but used "
"a scheme of ftps://, which is not allowed. Use ftp:// "
- "to allow connecting to insecure ftp servers."
+ "to allow connecting to insecure ftp servers. %s",
+ e,
)
raise UrlError(
cause=(
@@ -166,8 +172,8 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
url=url,
) from e
finally:
+ LOG.debug("Closing connection")
ftp_tls.close()
- return response
else:
try:
ftp = ftplib.FTP()
@@ -188,6 +194,13 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
ftp.retrbinary(f"RETR {url_parts.path}", callback=buffer.write)
response = FtpResponse(buffer.getvalue(), url)
except ftplib.all_errors as e:
+ if "No such file" in str(e):
+ raise UrlError(
+ cause=f"File {url_parts.path} not found",
+ code=NOT_FOUND,
+ headers=None,
+ url=url_parts.path,
+ ) from e
code = ftp_get_return_code_from_exception(e)
raise UrlError(
cause=(
@@ -201,7 +214,7 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
finally:
LOG.debug("Closing connection")
ftp.close()
- return response
+ return response
def _read_file(path: str, **kwargs) -> "FileResponse":
cloudinit/url_helper.py
Outdated
) | ||
|
||
response = FtpResponse(buffer.getvalue(), url) | ||
LOG.debug("Closing connection") |
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.
Let's just move this log into finally where we are actually closing the connection.
cloudinit/url_helper.py
Outdated
return response | ||
finally: | ||
ftp_tls.close() | ||
return response |
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.
response isn't set during error conditions/filenotfound etc. So, let's pull this out of the finally clause.
While we are at it, let's consolidate the two if/else clauses and only return response
if we successfully make it to the end of our try/except handling of either case statement.
cloudinit/url_helper.py
Outdated
LOG.debug("Creating a secure connection") | ||
ftp_tls.prot_p() | ||
LOG.debug("Reading file: %s", url_parts.path) | ||
ftp_tls.retrbinary( |
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.
The issue we have here in error handling is that "File not found" errors result in ftplib.error_perm
permanent errors that we are casting as URLError(cause="Attempted to connect to an insecure ftp server but used a scheme of ftps://"
We actually need to look either at error code '550' perhaps or "No such file or directory" in str(e) and raise the appropriate code=NOT_FOUND instead of warnings about invalid ftp: scheme.
Consolidated diff suggestion for your thoughts below.
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.
Nice catch, thanks! This doesn't get raised to users from the testing code path that I'm fixing, but yes this is more correct for other cases.
I don't like using exception strings as a differentiator. What if we just use another try/except block to differentiate the exception message.
Thanks for the review @blackboxsw! I just pushed a fixup commit - let me know what you think. |
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.
LGTM! ship it.
f"RETR {url_parts.path}", callback=buffer.write | ||
) | ||
|
||
return FtpResponse(buffer.getvalue(), url) |
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.
WFM, looks good and well contained. No need to create a local response and carry it to the end of the function.
Thanks for the reviews @a-dubs and @blackboxsw! |
- fix exception handling when retr fails - test: Close connection on failure - test: Ensure server is running before it is queried
User output and service names recently changed.
0398038
to
bdbdc8e
Compare
- fix exception handling when retr fails - test: Close connection on failure - test: Ensure server is running before it is queried
Proposed Commit Message
Additional Context
tests fixed:
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests/test_kernel_command_line_match/test_lxd_datasource_kernel_override_nocloud_net_ds_nocloud_net_s_https___raw_githubusercontent_com_canonical_cloud_init_main_tests_data_kernel_cmdline_match__/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests.datasources.test_nocloud/TestFTP/test_nocloud_ftp_unencrypted_server_succeeds/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests.datasources.test_nocloud/TestFTP/test_nocloud_ftps_encrypted_server_succeeds/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests.datasources.test_nocloud/TestFTP/test_nocloud_ftp_encrypted_server_fails/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests/test_kernel_command_line_match/test_lxd_disable_cloud_init_cmdline/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests/test_kernel_command_line_match/test_lxd_disable_cloud_init_file/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests/test_kernel_command_line_match/test_lxd_disable_cloud_init_env/
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-noble-lxd_vm/lastCompletedBuild/testReport/tests.integration_tests.datasources.test_nocloud/TestFTP/test_nocloud_ftps_unencrypted_server_fails/
Test Steps
Merge type