Skip to content

[Testing] Test SSH file transfer with images. #208

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

Open
wants to merge 48 commits into
base: main
Choose a base branch
from

Conversation

JoeZiminski
Copy link
Member

@JoeZiminski JoeZiminski commented Oct 9, 2023

This PR implements SSH tests through a docker image. They key changes in this PR are:

  • update code_test_and_deploy.yml to selectively run SSH tests in linux runners. This is because the Windows and macOS runners already are in a container, so it's not possible to run a container from within these containers ('nested containerisation').
  • Allow SSH transfer from other ports than the default port 22, by setting an environment variable. This was done for tests as port 22 is already used in GitHub actions.This is added in canonical_configs.py. At the moment this is not documented / exposed to users. In general I don't think there will be much need to SSH from other ports except for 22. But it's possible it will be needed.
  • A Dockerfile is added that spins up an ubuntu image with SSH setup so that it can be SSHed into from tests.
  • ssh_test_utils.py now contains all machinery to SSH into the docker image.

The rest of this PR is pretty much refactoring. base_transfer.py, test_ssh_file_transfer.py, test_ssh_setup.py just contains the already-existing tests. The new additions is testing the SSH through images not through a configuration where paths etc. were entered into the conftest.py. However, any feedback on these tests is of course welcome.

@JoeZiminski JoeZiminski marked this pull request as draft October 10, 2023 10:16
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch 2 times, most recently from 7f9781b to 7d6add8 Compare October 24, 2023 12:15
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch 2 times, most recently from 8d0f0a3 to 9442694 Compare November 10, 2023 09:18
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from 88591cb to bafb5d0 Compare April 10, 2024 21:44
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from d503958 to b4bb0a4 Compare April 18, 2024 19:22
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from 3bae225 to 689db1d Compare April 19, 2024 16:49
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from acb1d2b to 085ef63 Compare April 19, 2024 16:58
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from b49073e to 8f4d341 Compare April 22, 2024 11:01
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from 085397d to ddb81d5 Compare April 22, 2024 14:10
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from 208d049 to f47884c Compare April 22, 2024 17:16
@JoeZiminski JoeZiminski force-pushed the test_ssh_with_image branch from f47884c to b26163b Compare April 22, 2024 17:19
@JoeZiminski JoeZiminski marked this pull request as ready for review April 29, 2024 20:05
@JoeZiminski JoeZiminski requested a review from niksirbi April 29, 2024 20:55
Copy link
Member

@niksirbi niksirbi left a comment

Choose a reason for hiding this comment

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

My intermediate report on this:

  • The ssh tests failed when locally running on MacOS (M2), with docker running
  • The contributing guide should be updated to let contributors know how to handle local ssh tests (either skip them or configure their local machines accordingly).

sudo service mysql stop # free up port 3306 for ssh tests: https://github.com/orgs/community/discussions/25550
pytest
else
pytest -k "not test_combinations_ssh_transfer and not test_ssh_setup"
Copy link
Member

Choose a reason for hiding this comment

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

does this mean that on other OSes pytest will run all tests except the ones specified here? That's what we'd want/expect right?

Copy link
Contributor

@cs7-shrey cs7-shrey left a comment

Choose a reason for hiding this comment

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

Hey @JoeZiminski I tried the tests in this PR on linux (kali) and windows. With a few changes, the tests work as expected.

I have added some comments.

Comment on lines +101 to +102
else:
build_command = "docker build ."
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
else:
build_command = "docker build ."
else:
build_command = "docker build -t ssh_server ."

We should tag the docker image so that docker run can work.

RUN apt-get install nano

# Create an SSH user
RUN useradd -rm -d /home/sshuser -s /bin/bash -g root -G sudo -u 1000 sshuser
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
RUN useradd -rm -d /home/sshuser -s /bin/bash -g root -G sudo -u 1000 sshuser
RUN useradd -rm -d /home/sshuser -s /bin/bash -g root -G sudo sshuser

The -u 1000 flag throws up an error saying "the UID is not unique" so we might as well just skip it as I don't think we would need it.

Comment on lines +96 to +100
build_command = "sudo docker build -t ssh_server ."
run_command = (
f"sudo docker run -d -p {PORT}:22 "
f"--name {container_name} ssh_server"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if this is specific to my OS / docker installation but when I run docker, the docker.sock is present at /home/shrey/.docker/desktop/docker.sock and running docker commands with sudo expects docker.sock to be at /var/run/docker.sock. And with that, the code gets a return code of 1 (as we assert later in the code) because sudo docker <command> fails to execute but commands without sudo execute perfectly.

TLDR; Running docker commands with sudo incorrectly outputs that docker daemon is not running.

This was tested on kali linux. Probably mac will have a similar behaviour.

I think we can add a preliminary check by running something like docker images and sudo docker images and then use sudo according to whichever outputs a return code 0


sudo = "sudo " if platform.system() == "Linux" else ""

subprocess.run(f"{sudo}docker rm -f {container_name}", shell=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

same issue with sudo here

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.

3 participants