Skip to content

Commit

Permalink
everest-testing: Fix FtpThread and ftp_server fixture to always stop …
Browse files Browse the repository at this point in the history
…spawned FTP server in the end

Signed-off-by: Kai-Uwe Hermann <[email protected]>
  • Loading branch information
hikinggrass committed Nov 8, 2024
1 parent ba00175 commit 9c7a492
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions everest-testing/src/everest/testing/ocpp_utils/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,45 @@ def test_config():


class FtpThread(Thread):
def __init__(self, directory, port, test_config: OcppTestConfiguration, ftp_socket,
group = None, target = None, name = None, args = ..., kwargs = None, *, daemon = None):
super().__init__(group, target, name, args, kwargs, daemon=daemon)
self.directory = directory
self.port = port
self.test_config = test_config
self.ftp_socket = ftp_socket

def set_directory(self, directory):
self.directory = directory

def set_port(self, port):
self.port = port

def set_test_config(self, test_config: OcppTestConfiguration):
self.test_config = test_config

def set_socket(self, ftp_socket):
self.ftp_socket = ftp_socket

def stop(self):
self.server.close_all()

def run(self):
shutil.copyfile(self.test_config.firmware_info.update_file, os.path.join(
self.directory, "firmware_update.pnx"))
shutil.copyfile(self.test_config.firmware_info.update_file_signature,
os.path.join(self.directory, "firmware_update.pnx.base64"))

authorizer = DummyAuthorizer()
authorizer.add_user(getpass.getuser(), "12345", self.directory, perm="elradfmwMT")

handler = FTPHandler
handler.authorizer = authorizer

self.server = servers.FTPServer(self.ftp_socket, handler)

self.server.serve_forever()


@pytest.fixture
def ftp_server(test_config: OcppTestConfiguration):
Expand All @@ -120,29 +156,14 @@ def ftp_server(test_config: OcppTestConfiguration):
ftp_socket.bind(address)
port = ftp_socket.getsockname()[1]

def _ftp_server(test_config: OcppTestConfiguration, ftp_socket):
shutil.copyfile(test_config.firmware_info.update_file, os.path.join(
d, "firmware_update.pnx"))
shutil.copyfile(test_config.firmware_info.update_file_signature,
os.path.join(d, "firmware_update.pnx.base64"))

authorizer = DummyAuthorizer()
authorizer.add_user(getpass.getuser(), "12345", d, perm="elradfmwMT")

handler = FTPHandler
handler.authorizer = authorizer

server = servers.FTPServer(ftp_socket, handler)

server.serve_forever()

ftp_thread = FtpThread(target=_ftp_server, args=[test_config, ftp_socket])
ftp_thread = FtpThread(directory=d, port=port, test_config=test_config, ftp_socket=ftp_socket)
ftp_thread.daemon = True
ftp_thread.set_port(port)
ftp_thread.start()

yield ftp_thread

ftp_thread.stop()

shutil.rmtree(d)


Expand Down

0 comments on commit 9c7a492

Please sign in to comment.