Skip to content

Commit

Permalink
fix: all tests are working now, queue added to arguments and need to …
Browse files Browse the repository at this point in the history
…be removed from the cli
  • Loading branch information
SvenKeimpema committed May 22, 2023
1 parent 9533a4a commit 4655d7a
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 31 deletions.
10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = ['invoke', 'fabric', 'psutil']
dependencies = ['invoke', 'fabric', 'psutil', "plumbum>=1.8.1"]

[project.optional-dependencies]
dev = [
Expand Down Expand Up @@ -60,6 +60,12 @@ parser_angular_patch_types = "fix,perf,refactor,build,chore,patch"
dependencies = [
"coverage[toml]>=6.5",
"pytest",
"pytest_asyncio",
"invoke",
"fabric",
"decorator",
"psutil",
"plumbum>=1.8.1"
]
[tool.hatch.envs.default.scripts]
test = "pytest {args:tests}"
Expand All @@ -82,7 +88,7 @@ dependencies = [
"black>=23.1.0",
"mypy>=1.0.0",
"ruff>=0.0.243",
"plumbum>=1.8.1"
"plumbum>=1.8.1",
]
[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:src/edwh_sshfs_plugin tests}"
Expand Down
27 changes: 21 additions & 6 deletions src/edwh_sshfs_plugin/fabfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import asyncio
import socket
import getpass
import os
import subprocess

import invoke
from plumbum import BG
from plumbum.cmd import ssh, sshfs
from plumbum.commands.processes import run_proc
from fabric import task


STOP_RUNNING = False

def get_available_port(c):
# get remote available ports
remote_ports = get_remote_available_ports(c)
Expand Down Expand Up @@ -55,7 +62,7 @@ def unmount_dir(c, dir):


@task()
def remote_mount(c, workstation_dir, server_dir):
async def remote_mount(c, workstation_dir, server_dir, queue=None):
if not hasattr(c, "host"):
print("please give up a host using -H")
exit(255)
Expand All @@ -67,22 +74,30 @@ def remote_mount(c, workstation_dir, server_dir):
"-o StrictHostKeyChecking=no,reconnect,ServerAliveInterval=3,ServerAliveCountMax=3",
f"{getpass.getuser()}@127.0.0.1:{workstation_dir}", f"{server_dir}"]

print(f"starting sshfs with(started when nothing happens): {str(sshfs_cmd)}")
if not queue:
print(f"starting sshfs with(started when nothing happens): {str(sshfs_cmd)}")
try:
sshfs_cmd()
sshfs_cmd & BG
await queue.get()
c.run(f"pkill -kill -f \"sshfs\" && umount {server_dir}")
except KeyboardInterrupt:
unmount_dir(c, server_dir)


@task()
def local_mount(c, workstation_dir, server_dir):
async def local_mount(c, workstation_dir, server_dir, queue=None):
os.popen(f"lsof -n {workstation_dir} 2>/dev/null")
if not hasattr(c, "host"):
print("please give up a host using -H")
exit(255)
# TODO: remove mount on exit
sshfs_cmd = sshfs["-f", "-o", "default_permissions,StrictHostKeyChecking=no,reconnect", f"{c.user}@{c.host}:{server_dir}", workstation_dir]

print("starting sshfs with(started when nothing happens):", sshfs_cmd)
if not queue:
print("running sshfs...")

sshfs_cmd & BG
await queue.get()

sshfs_cmd()
local_connection = invoke.context.Context()
local_connection.run(f"umount {workstation_dir}", hide=True)
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest


def pytest_addoption(parser):
parser.addoption("--host", action="store", default="default name")


@pytest.fixture(scope='session')
def host(request):
host_value = request.config.option.host
if host_value is None:
pytest.skip()
return host_value
23 changes: 0 additions & 23 deletions tests/sshfs_tests.py

This file was deleted.

133 changes: 133 additions & 0 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import os
import socket
import invoke
from edwh_sshfs_plugin import fabfile
from fabric import Connection
import time
import subprocess
from multiprocessing import Process
import asyncio
import pytest
import logging

pytest_plugins = ("pytest_asyncio",)


def create_new_fabric_connection(host) -> Connection:
try:
connection = Connection(host=host)
except:
assert False, f"not able to connect to host({host})"
return connection


def is_given_host_valid(host):
assert "@" in host, "@ not given in --host parameter"


def test_is_port_open():
assert fabfile.get_local_available_port() == fabfile.get_local_available_port()
s = socket.socket()
open_port = fabfile.get_local_available_port()
s.bind(("127.0.0.1", int(open_port[0])))
s.listen(5)
assert open_port != fabfile.get_local_available_port()
s.close()


def test_ssh_connection(host):
conn = create_new_fabric_connection(host)
# no need for assert here because it will throw an exception if the ssh connection is wrong when executing ls
conn.run("ls", hide=True)


async def check_if_mount_exists(host):
await asyncio.sleep(3)
conn_for_mount = create_new_fabric_connection(host)
assert (
"is a mount"
in conn_for_mount.run("mountpoint test_sshfs_dir", warn=True, hide=True).stdout
)
conn_for_mount.close()

@pytest.mark.asyncio
async def test_remote_mount(host):
create_mount_conn = create_new_fabric_connection(host)
if create_mount_conn.run("if test -d test_sshfs_dir; then echo \"exist\"; fi", warn=True, hide=True).stdout == "":
# print(create_mount_conn.run("ls test_sshfs_dir", warn=True, hide=True).stdout == "")
# return
create_mount_conn.run("mkdir test_sshfs_dir", hide=True)

shared_queue = asyncio.Queue()

create_mount_task = asyncio.create_task(
fabfile.remote_mount(
create_mount_conn, f"{os.getcwd()}/tests/sshfs_test_dir", "test_sshfs_dir", shared_queue
)
)

asyncio.gather(create_mount_task)
await asyncio.sleep(5)

conn_for_mount = create_new_fabric_connection(host)
assert (
"is a mount"
in conn_for_mount.run("mountpoint test_sshfs_dir", warn=True, hide=True).stdout
)
# print(conn_for_mount.run("mountpoint test_sshfs_dir", warn=True, hide=True).stdout)
# tell remote_mount to stop running
await shared_queue.put(True)
shared_queue.join()
await asyncio.sleep(1)
conn_for_mount.close()
create_mount_conn.close()

c = create_new_fabric_connection(host)

assert (
"is a mount"
not in c.run("mountpoint test_sshfs_dir", warn=True, hide=True).stdout
)


async def check_local_folder_for_mount():
await asyncio.sleep(3)

conn = invoke.context.Context()
assert (
"is a mount"
in conn.run("mountpoint test_sshfs_dir", warn=True, hide=True).stdout
)
conn.close()


@pytest.mark.asyncio
async def test_local_mount(host):
create_mount_conn = create_new_fabric_connection(host)
create_mount_conn.run("umount test_sshfs_dir", warn=True, hide=True)
shared_queue = asyncio.Queue()

create_mount_task = asyncio.create_task(
fabfile.local_mount(
create_mount_conn, f"{os.getcwd()}/tests/sshfs_test_dir", "test_sshfs_dir", shared_queue
)
)

asyncio.gather(create_mount_task)

await asyncio.sleep(5)

conn = invoke.context.Context()
assert (
"is a mount"
in conn.run(f"mountpoint {os.getcwd()}/tests/sshfs_test_dir", warn=True, hide=True).stdout
)

await shared_queue.put(True)
shared_queue.join()
await asyncio.sleep(1)

assert (
"is a mount"
not in conn.run(f"mountpoint {os.getcwd()}/tests/sshfs_test_dir", warn=True, hide=True).stdout
)

0 comments on commit 4655d7a

Please sign in to comment.