-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testhelper: introduce class SMBClient using pysmb
We use the python module pysmb to access the SMB server. The helper class hides the complexities and helps us bypass the smbclient binary to connect to the remote samba server giving us more flexibility in the python tests. Signed-off-by: Sachin Prabhu <[email protected]>
- Loading branch information
Showing
5 changed files
with
121 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .testhelper import * # noqa: F401, F403 | ||
from .cmdhelper import * # noqa: F401, F403 | ||
from .fshelper import * # noqa: F401, F403 | ||
from .smbclient import * # noqa: F401, F403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from smb.SMBConnection import SMBConnection # type: ignore | ||
import smb # type: ignore | ||
import typing | ||
import io | ||
|
||
|
||
class SMBClient: | ||
"""Use pysmb to access the SMB server""" | ||
|
||
def __init__(self, hostname: str, share: str, username: str, passwd: str): | ||
self.server = hostname | ||
self.share = share | ||
self.username = username | ||
self.password = passwd | ||
self.error = False | ||
self.connected = False | ||
self.connect() | ||
|
||
def connect(self) -> None: | ||
try: | ||
self.ctx = SMBConnection( | ||
self.username, | ||
self.password, | ||
"smbclient", | ||
self.server, | ||
use_ntlm_v2=True, | ||
) | ||
self.ctx.connect(self.server) | ||
self.connected = True | ||
except smb.base.SMBTimeout: | ||
self.error = False | ||
|
||
def disconnect(self) -> None: | ||
self.connected = False | ||
self.ctx.close() | ||
|
||
def readdir(self, path: str = "/") -> typing.List: | ||
ret = [] | ||
try: | ||
dentries = self.ctx.listPath(self.share, path) | ||
except smb.smb_structs.OperationFailure: | ||
self.error = True | ||
raise | ||
else: | ||
ret = [dent.filename for dent in dentries] | ||
return ret | ||
|
||
def mkdir(self, dpath: str) -> None: | ||
try: | ||
self.ctx.createDirectory(self.share, dpath) | ||
except smb.smb_structs.OperationFailure: | ||
self.error = True | ||
raise | ||
|
||
def rmdir(self, dpath: str) -> None: | ||
self.ctx.deleteDirectory(self.share, dpath) | ||
|
||
def unlink(self, fpath: str) -> None: | ||
self.ctx.deleteFiles(self.share, fpath) | ||
|
||
def simple_write(self, fpath: str, teststr: str) -> None: | ||
file_obj = io.BytesIO(teststr.encode()) | ||
try: | ||
self.ctx.storeFile(self.share, fpath, file_obj) | ||
except smb.smb_structs.OperationFailure: | ||
self.error = True | ||
raise | ||
finally: | ||
file_obj.close() | ||
|
||
def simple_read(self, fpath: str) -> str: | ||
readobj = io.BytesIO() | ||
try: | ||
self.ctx.retrieveFile(self.share, fpath, readobj) | ||
except smb.smb_structs.OperationFailure: | ||
self.error = True | ||
raise | ||
else: | ||
ret = readobj.getvalue().decode("ascii") | ||
finally: | ||
readobj.close() | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters