Skip to content
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

test: rewrite testing script in python #241

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Conversation

theyoyojo
Copy link
Contributor

Leveraging the pytest framework, refactor the tests one-for-one from test.sh into individual python functions that run in the same order and with the same dependencies on earlier tests.

@theyoyojo theyoyojo force-pushed the enhance_tests branch 6 times, most recently from 9b30a9b to 5660484 Compare February 21, 2025 13:27
@theyoyojo
Copy link
Contributor Author

There are some versioning issues with the dependencies.

Fedora 40->41 jumps to a new maintained fork of requests-unixsockets2 which is supposed to be a drop in replacement for the original, however it breaks the tests as I've written them (which was on fedora 40).

The right thing to do is likely to use a virtualenv to enforce consistent versioning

A bunch of relevant link:

msabramo/requests-unixsocket#73 (comment)

https://src.fedoraproject.org/rpms/python-requests-unixsocket

docker/docker-py#3257

https://gitlab.com/thelabnyc/requests-unixsocket2/-/commit/6f3b5757adf480c360b91acf5776009136c9496a

https://pypi.org/project/requests-unixsocket2/

Leveraging the pytest framework, refactor the tests one-for-one from
test.sh into individual python functions that run in the same order
and with the same dependencies on earlier tests.

Signed-off-by: Joel Savitz <joel@underground.software>
@theyoyojo
Copy link
Contributor Author

theyoyojo commented Feb 21, 2025

Fixes #242

@theyoyojo theyoyojo marked this pull request as ready for review February 21, 2025 17:37
@theyoyojo theyoyojo force-pushed the enhance_tests branch 3 times, most recently from 429a67e to 1b1bef9 Compare February 21, 2025 20:05
Signed-off-by: Joel Savitz <joel@underground.software>
Signed-off-by: Joel Savitz <joel@underground.software>
…rary

If radius was so good, why didn't they make a...

Signed-off-by: Joel Savitz <joel@underground.software>
pipe patchsets into append_journal to accomplish the same functionality

Signed-off-by: Joel Savitz <joel@underground.software>
Signed-off-by: Joel Savitz <joel@underground.software>
This project uses podman primarily.

Signed-off-by: Joel Savitz <joel@underground.software>
Add an API to the RocketCrew to create a user, including a simple caching
mechanism exposed to the caller. Simplifies to focus on more important
information about what's being checked.

Signed-off-by: Joel Savitz <joel@underground.software>
Signed-off-by: Joel Savitz <joel@underground.software>
Everything there runs at startup together.

Signed-off-by: Joel Savitz <joel@underground.software>
general_test.py is reduced to under 100 lines.

Signed-off-by: Joel Savitz <joel@underground.software>
Comment on lines 29 to 87
class SSLUnixSocketConnection(HTTPConnection):
"""Custom HTTPConnection that wraps a Unix socket with SSL."""

def __init__(self, unix_socket_path, **kwargs):
super().__init__("localhost", **kwargs) # Dummy host (not used)
self.unix_socket_path = unix_socket_path
self.ssl_context = create_urllib3_context()
self.ssl_context.load_verify_locations(CERT_PATH)

def _new_conn(self):
"""Create a new wrapped SSL connection over a Unix domain socket."""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(self.unix_socket_path)
return self.ssl_context.wrap_socket(sock, server_hostname="localhost")


class SSLUnixSocketConnectionPool(HTTPConnectionPool):
"""Custom ConnectionPool for Unix socket wrapped with SSL."""

def __init__(self, socket_path, **kwargs):
super().__init__("localhost", **kwargs) # Dummy host (not used)
self.socket_path = socket_path

def _new_conn(self):
return SSLUnixSocketConnection(self.socket_path)


class SSLUnixSocketAdapter(HTTPAdapter):
"""Transport adapter to route requests over a Unix socket with SSL."""

def __init__(self, socket_path, **kwargs):
self.socket_path = socket_path
super().__init__(**kwargs)

def get_connection(self, url, proxies=None):
return SSLUnixSocketConnectionPool(self.socket_path)


class UnixPOP3(poplib.POP3):
def __init__(self, socket_path, certfile, timeout=30):
self.timeout = timeout
# Create SSL context and load certificate
ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ssl_context.load_verify_locations(certfile)

# Create and connect Unix domain socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(socket_path)
# Wrap with SSL
self.sock = ssl_context.wrap_socket(sock, server_hostname='localhost.localdomain')
self.file = self.sock.makefile('rb')
self._debugging = 0


def singularity_pop_login(user, pass_):
mailbox = UnixPOP3(POP3S_SOCKET_PATH, CERT_PATH)
mailbox.user(user)
mailbox.pass_(pass_)
return mailbox
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I am insane, but like can't we just use pycurl to actually one to one map the curl commands we were using instead of contorting python's request library to do things it does not want to?

@charliemirabile
Copy link
Contributor

Also not totally sure I understand the motivation behind these changes more broadly. What benefit exactly does hundreds of lines of code churn and a half dozen new dependencies offer? Some of the code in the resulting general_test.py is kinda nice, but the fact that it now requires a bunch of support code where the prior was literally just a shell script that I could copy and paste the failing command from to debug what is going on nullifies a lot of that.

@theyoyojo
Copy link
Contributor Author

This is just a preparation step for more broad testing enhancements I'm working on, such as rewriting the email testing script as a part of the test suite and enabling more test driven development of automation features that we can't afford to ship broken. Using pytest you can also run specific tests from the suite with pytest -k which will more useful for tests that don't have dependencies on previous tests in the suite. That being said it's now much easier to write many clear tests of our existing code and also eliminates the issue with curl-minimal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants