-
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
We use the python module pysmbc 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
89 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,50 @@ | ||
import smbc # type: ignore | ||
import typing | ||
import os | ||
|
||
|
||
class SMBClient: | ||
"""Use pysmbc 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.rooturi = f"smb://{self.server}/{self.share}" | ||
|
||
def auth_cb(se, sh, w, u, p): | ||
return (w, self.username, self.password) | ||
|
||
self.ctx = smbc.Context(auth_fn=auth_cb) | ||
|
||
def readdir(self, path: str = "/") -> typing.List: | ||
uri = self.rooturi + path | ||
d = self.ctx.opendir(uri) | ||
return d.getdents() | ||
|
||
def open( | ||
self, path: str, flags: int = os.O_RDWR, mode: int = 0o644 | ||
) -> typing.Any: | ||
uri = self.rooturi + path | ||
return self.ctx.open(uri, flags) | ||
|
||
def mkdir(self, dpath: str) -> None: | ||
self.ctx.mkdir(self.rooturi + dpath) | ||
|
||
def rmdir(self, dpath: str) -> None: | ||
self.ctx.rmdir(self.rooturi + dpath) | ||
|
||
def unlink(self, fpath: str) -> None: | ||
self.ctx.unlink(self.rooturi + fpath) | ||
|
||
def simple_write(self, fpath: str, teststr: str) -> None: | ||
f = self.open(fpath, os.O_WRONLY | os.O_CREAT) | ||
f.write(teststr) | ||
f.close() | ||
|
||
def simple_read(self, fpath: str) -> str: | ||
f = self.open(fpath) | ||
str = f.read() | ||
f.close() | ||
return str.decode("ascii") |
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