Skip to content

Commit

Permalink
Add Windows support for File and Service modules (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdas3001 committed May 20, 2023
1 parent 7e36cff commit 8520850
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
172 changes: 172 additions & 0 deletions testinfra/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ def get_module_class(cls, host):
return BSDFile
if host.system_info.type == "darwin":
return DarwinFile
if host.system_info.type == "windows":
return WindowsFile
raise NotImplementedError


Expand Down Expand Up @@ -330,3 +332,173 @@ class NetBSDFile(BSDFile):
@property
def sha256sum(self):
return self.check_output("cksum -a sha256 < %s", self.path)


class WindowsFile(File):
@property
def exists(self):
"""Test if file exists
>>> host.file(r"C:/Users").exists
True
>>> host.file(r"C:/nonexistent").exists
False
"""

return (
self.check_output(r"powershell -command \"Test-Path '%s'\"", self.path)
== "True"
)

@property
def is_file(self):
return (
self.check_output(
r"powershell -command \"(Get-Item '%s') -is [System.IO.FileInfo]\"",
self.path,
)
== "True"
)

@property
def is_directory(self):
return (
self.check_output(
r"powershell -command \"(Get-Item '%s') -is [System.IO.DirectoryInfo]\"",
self.path,
)
== "True"
)

@property
def is_pipe(self):
raise NotImplementedError

@property
def is_socket(self):
raise NotImplementedError

@property
def is_symlink(self):
return (
self.check_output(
r"powershell -command \"(Get-Item -Path '%s').Attributes -band [System.IO.FileAttributes]::ReparsePoint\"",
self.path,
)
== "True"
)

@property
def linked_to(self):
"""Resolve symlink
>>> host.file("C:/Users/lock").linked_to
'C:/Program Files/lock'
"""
return self.check_output(
r"powershell -command \"(Get-Item -Path '%s' -ReadOnly).FullName\"",
self.path,
)

@property
def user(self):
raise NotImplementedError

@property
def uid(self):
raise NotImplementedError

@property
def group(self):
raise NotImplementedError

@property
def gid(self):
raise NotImplementedError

@property
def mode(self):
raise NotImplementedError

def contains(self, pattern):
"""Checks content of file for pattern
This follows the regex syntax.
"""
return (
self.run_test(
r"powershell -command \"Select-String -Path '%s' -Pattern '%s'\"",
self.path,
pattern,
).stdout
!= ""
)

@property
def md5sum(self):
raise NotImplementedError

@property
def sha256sum(self):
raise NotImplementedError

def _get_content(self, decode):
out = self.run_expect([0], r"powershell -command \"cat -- '%s'\"", self.path)
if decode:
return out.stdout
return out.stdout_bytes

@property
def content(self):
"""Return file content as bytes
>>> host.file("C:/Windows/Temp/foo").content
b'caf\\xc3\\xa9'
"""
return self._get_content(False)

@property
def content_string(self):
"""Return file content as string
>>> host.file("C:/Windows/Temp/foo").content_string
'café'
"""
return self._get_content(True)

@property
def mtime(self):
"""Return time of last modification as datetime.datetime object
>>> host.file("C:/Windows/passwd").mtime
datetime.datetime(2015, 3, 15, 20, 25, 40)
"""
date_time_str = self.check_output(
r"powershell -command \"Get-ChildItem -Path '%s' | Select-Object -ExpandProperty LastWriteTime\"",
self.path,
)
return datetime.datetime.strptime(
date_time_str.strip(), "%A, %B %d, %Y %I:%M:%S %p"
)

@property
def size(self):
"""Return size of file in bytes"""
return int(
self.check_output(
r"powershell -command \"Get-Item -Path '%s' | Select-Object -ExpandProperty Length\"",
self.path,
)
)

def listdir(self):
"""Return list of items under the directory
>>> host.file("C:/Windows/Temp").listdir()
['foo_file', 'bar_dir']
"""
out = self.check_output(
r"powershell -command \"Get-ChildItem -Path '%s' | Select-Object -ExpandProperty Name\"",
self.path,
)
return [item.strip() for item in out.strip().split("\n")]
24 changes: 24 additions & 0 deletions testinfra/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def get_module_class(cls, host):
return OpenBSDService
if host.system_info.type == "netbsd":
return NetBSDService
if host.system_info.type == "windows":
return WindowsService
raise NotImplementedError

def __repr__(self):
Expand Down Expand Up @@ -275,3 +277,25 @@ def is_running(self):
@property
def is_enabled(self):
raise NotImplementedError


class WindowsService(Service):
@property
def is_running(self):
return (
self.check_output(
"Get-Service '%s' | Select -ExpandProperty Status",
self.name,
)
== "Running"
)

@property
def is_enabled(self):
return (
self.check_output(
"Get-Service '%s' | Select -ExpandProperty StartType",
self.name,
)
== "Automatic"
)

0 comments on commit 8520850

Please sign in to comment.