Skip to content

Commit

Permalink
Adapt all python code to PEP8 style standards
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Lepek <[email protected]>
  • Loading branch information
KamilLepek committed Jun 13, 2019
1 parent 1e2b8f1 commit e52d34c
Show file tree
Hide file tree
Showing 15 changed files with 849 additions and 792 deletions.
1 change: 1 addition & 0 deletions .pep8speaks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pycodestyle:
max-line-length: 100
ignore:
- E402 # module level import not at top of file
- W503 # line break after binary operator

no_blank_comment: True
8 changes: 4 additions & 4 deletions tests/functional/pyocf/types/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def write_insert(self):
def read_insert(self):
return self.value not in [CacheMode.PT, CacheMode.WO]


class EvictionPolicy(IntEnum):
LRU = 0
DEFAULT = LRU
Expand Down Expand Up @@ -306,7 +307,7 @@ def load_from_device(cls, device, name=""):
c.start_cache()
try:
c.load_cache(device)
except:
except: # noqa E722
c.stop()
raise

Expand All @@ -319,7 +320,7 @@ def start_on_device(cls, device, **kwargs):
c.start_cache()
try:
c.attach_device(device, force=True)
except:
except: # noqa E722
c.stop()
raise

Expand Down Expand Up @@ -529,13 +530,12 @@ def flush(self):
if c.results["error"]:
raise OcfError("Couldn't flush cache", c.results["error"])


def get_name(self):
self.read_lock()

try:
return str(self.owner.lib.ocf_cache_get_name(self), encoding="ascii")
except:
except: # noqa E722
raise OcfError("Couldn't get cache name")
finally:
self.read_unlock()
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/pyocf/types/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DataOps(Structure):


class Data:
DATA_POISON=0xA5
DATA_POISON = 0xA5
PAGE_SIZE = 4096

_instances_ = {}
Expand Down Expand Up @@ -109,7 +109,7 @@ def from_bytes(cls, source: bytes, offset: int = 0, size: int = 0):
def from_string(cls, source: str, encoding: str = "ascii"):
b = bytes(source, encoding)
# duplicate string to fill space up to sector boundary
padding_len = S.from_B(len(b), sector_aligned = True).B - len(b)
padding_len = S.from_B(len(b), sector_aligned=True).B - len(b)
padding = b * (padding_len // len(b) + 1)
padding = padding[:padding_len]
b = b + padding
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/pyocf/types/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def c_handle(io, opaque):
def end(self, err):
try:
self.callback(err)
except:
except: # noqa E722
pass

self.put()
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/pyocf/types/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def wait_predicate():
if stop.is_set() and not OcfLib.getInstance().ocf_queue_pending_io(queue):
break


class Queue:
_instances_ = {}

Expand Down Expand Up @@ -102,4 +103,3 @@ def stop(self):
self.kick_condition.notify_all()

self.thread.join()

2 changes: 1 addition & 1 deletion tests/functional/pyocf/types/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(self):
def get_instance(cls, ref: int):
try:
return cls._instances_[ref]
except:
except: # noqa E722
logging.getLogger("pyocf").error(
"OcfSharedObject corruption. wanted: {} instances: {}".format(
ref, cls._instances_
Expand Down
14 changes: 7 additions & 7 deletions tests/functional/pyocf/types/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class VolumeIoPriv(Structure):


class Volume(Structure):
VOLUME_POISON=0x13
VOLUME_POISON = 0x13

_fields_ = [("_storage", c_void_p)]
_instances_ = {}
Expand Down Expand Up @@ -184,7 +184,7 @@ def _open(ref):
uuid = str(uuid_ptr.contents._data, encoding="ascii")
try:
volume = Volume.get_by_uuid(uuid)
except:
except: # noqa E722 TODO:Investigate whether this really should be so broad
print("Tried to access unallocated volume {}".format(uuid))
print("{}".format(Volume._uuid_))
return -1
Expand Down Expand Up @@ -255,7 +255,7 @@ def submit_discard(self, discard):
memset(dst, 0, discard.contents._bytes)

discard.contents._end(discard, 0)
except:
except: # noqa E722
discard.contents._end(discard, -5)

def get_stats(self):
Expand All @@ -269,8 +269,7 @@ def submit_io(self, io):
self.stats[IoDir(io.contents._dir)] += 1

io_priv = cast(
OcfLib.getInstance().ocf_io_get_priv(io), POINTER(VolumeIoPriv)
)
OcfLib.getInstance().ocf_io_get_priv(io), POINTER(VolumeIoPriv))
offset = io_priv.contents._offset

if io.contents._dir == IoDir.WRITE:
Expand All @@ -286,7 +285,7 @@ def submit_io(self, io):
io_priv.contents._offset += io.contents._bytes

io.contents._end(io, 0)
except:
except: # noqa E722
io.contents._end(io, -5)

def dump(self, offset=0, size=0, ignore=VOLUME_POISON, **kwargs):
Expand Down Expand Up @@ -325,10 +324,11 @@ def reset_stats(self):
super().reset_stats()
self.stats["errors"] = {IoDir.WRITE: 0, IoDir.READ: 0}


class TraceDevice(Volume):
def __init__(self, size, trace_fcn=None, uuid=None):
super().__init__(size, uuid)
self.trace_fcn=trace_fcn
self.trace_fcn = trace_fcn

def submit_io(self, io):
submit = True
Expand Down
22 changes: 10 additions & 12 deletions tests/functional/pyocf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from ctypes import string_at


def print_buffer(buf, length, offset=0, width=16, ignore=0, stop_after_count_ignored=0, print_fcn=print):
def print_buffer(buf, length, offset=0, width=16, ignore=0,
stop_after_count_ignored=0, print_fcn=print):
end = int(offset) + int(length)
offset = int(offset)
ignored_lines = 0
Expand All @@ -15,16 +16,13 @@ def print_buffer(buf, length, offset=0, width=16, ignore=0, stop_after_count_ign
stop_after_count_ignored = int(stop_after_count_ignored / width)

for addr in range(offset, end, width):
cur_line = buf[addr : min(end, addr + width)]
cur_line = buf[addr: min(end, addr + width)]
byteline = ""
asciiline = ""
if not any(x != ignore for x in cur_line):
if stop_after_count_ignored and ignored_lines > stop_after_count_ignored:
print_fcn(
"<{} bytes of '0x{:02X}' encountered, stopping>".format(
stop_after_count_ignored * width, ignore
)
)
print_fcn("<{} bytes of '0x{:02X}' encountered, stopping>".
format(stop_after_count_ignored * width, ignore))
return
ignored_lines += 1
continue
Expand Down Expand Up @@ -71,23 +69,23 @@ def __index__(self):
return self.bytes

@classmethod
def from_B(cls, value, sector_aligned = False):
def from_B(cls, value, sector_aligned=False):
return cls(value, sector_aligned)

@classmethod
def from_KiB(cls, value, sector_aligned = False):
def from_KiB(cls, value, sector_aligned=False):
return cls(value * cls._KiB, sector_aligned)

@classmethod
def from_MiB(cls, value, sector_aligned = False):
def from_MiB(cls, value, sector_aligned=False):
return cls(value * cls._MiB, sector_aligned)

@classmethod
def from_GiB(cls, value, sector_aligned = False):
def from_GiB(cls, value, sector_aligned=False):
return cls(value * cls._GiB, sector_aligned)

@classmethod
def from_TiB(cls, value, sector_aligned = False):
def from_TiB(cls, value, sector_aligned=False):
return cls(value * cls._TiB, sector_aligned)

@classmethod
Expand Down
60 changes: 32 additions & 28 deletions tests/functional/tests/engine/test_wo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
# SPDX-License-Identifier: BSD-3-Clause-Clear
#

import pytest
from ctypes import c_int, memmove, cast, c_void_p
from enum import IntEnum
from itertools import product
import random

from pyocf.types.cache import Cache, CacheMode
from pyocf.types.core import Core
from pyocf.types.volume import Volume, ErrorDevice
from pyocf.types.volume import Volume
from pyocf.types.data import Data
from pyocf.types.io import IoDir
from pyocf.utils import Size
from pyocf.types.shared import OcfError, OcfCompletion
from pyocf.types.shared import OcfCompletion


def __io(io, queue, address, size, data, direction):
io.set_data(data, 0)
Expand All @@ -38,24 +38,29 @@ def _io(io, queue, address, size, data, offset, direction):
memmove(cast(data, c_void_p).value + offset, _data.handle, size)
return ret


def io_to_core(core, address, size, data, offset, direction):
return _io(core.new_core_io(), core.cache.get_default_queue(), address, size,
data, offset, direction)
data, offset, direction)


def io_to_exp_obj(core, address, size, data, offset, direction):
return _io(core.new_io(), core.cache.get_default_queue(), address, size, data,
offset, direction)
offset, direction)


def sector_to_region(sector, region_start):
i = 0
while i < len(region_start) - 1 and sector >= region_start[i + 1]:
i += 1
return i


class SectorStatus(IntEnum):
DIRTY = 0,
CLEAN = 1,
INVALID = 2,
DIRTY = 0,
CLEAN = 1,
INVALID = 2,


I = SectorStatus.INVALID
D = SectorStatus.DIRTY
Expand Down Expand Up @@ -85,6 +90,8 @@ class SectorStatus(IntEnum):
# - if clean, exported object sector no @n is filled with 100 + @n
# - if dirty, exported object sector no @n is filled with 200 + @n
#


def test_wo_read_data_consistency(pyocf_ctx):
# start sector for each region
region_start = [0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
Expand Down Expand Up @@ -114,11 +121,11 @@ def test_wo_read_data_consistency(pyocf_ctx):

data = {}
# memset n-th sector of core data with n
data[SectorStatus.INVALID] = bytes([x // SECTOR_SIZE for x in range (WORKSET_SIZE)])
data[SectorStatus.INVALID] = bytes([x // SECTOR_SIZE for x in range(WORKSET_SIZE)])
# memset n-th sector of clean data with n + 100
data[SectorStatus.CLEAN] = bytes([100 + x // SECTOR_SIZE for x in range (WORKSET_SIZE)])
data[SectorStatus.CLEAN] = bytes([100 + x // SECTOR_SIZE for x in range(WORKSET_SIZE)])
# memset n-th sector of dirty data with n + 200
data[SectorStatus.DIRTY] = bytes([200 + x // SECTOR_SIZE for x in range (WORKSET_SIZE)])
data[SectorStatus.DIRTY] = bytes([200 + x // SECTOR_SIZE for x in range(WORKSET_SIZE)])

result_b = bytes(WORKSET_SIZE)

Expand All @@ -137,30 +144,30 @@ def test_wo_read_data_consistency(pyocf_ctx):
combinations.append(S)
random.shuffle(combinations)

# add fixed test cases at the beginnning
# add fixed test cases at the beginning
combinations = fixed_combinations + combinations

for S in combinations[:ITRATION_COUNT]:
# write data to core and invalidate all CL
cache.change_cache_mode(cache_mode = CacheMode.PT)
io_to_exp_obj(core, WORKSET_OFFSET, len(data[SectorStatus.INVALID]), \
data[SectorStatus.INVALID], 0, IoDir.WRITE)
cache.change_cache_mode(cache_mode=CacheMode.PT)
io_to_exp_obj(core, WORKSET_OFFSET, len(data[SectorStatus.INVALID]),
data[SectorStatus.INVALID], 0, IoDir.WRITE)

# insert clean sectors
cache.change_cache_mode(cache_mode = CacheMode.WT)
cache.change_cache_mode(cache_mode=CacheMode.WT)
for sec in range(SECTOR_COUNT):
region = sector_to_region(sec, region_start)
if S[region] == SectorStatus.CLEAN:
io_to_exp_obj(core, WORKSET_OFFSET + SECTOR_SIZE * sec, SECTOR_SIZE, \
data[SectorStatus.CLEAN], sec * SECTOR_SIZE, IoDir.WRITE)
io_to_exp_obj(core, WORKSET_OFFSET + SECTOR_SIZE * sec, SECTOR_SIZE,
data[SectorStatus.CLEAN], sec * SECTOR_SIZE, IoDir.WRITE)

# write dirty sectors
cache.change_cache_mode(cache_mode = CacheMode.WO)
cache.change_cache_mode(cache_mode=CacheMode.WO)
for sec in range(SECTOR_COUNT):
region = sector_to_region(sec, region_start)
if S[region] == SectorStatus.DIRTY:
io_to_exp_obj(core, WORKSET_OFFSET + SECTOR_SIZE * sec, SECTOR_SIZE, \
data[SectorStatus.DIRTY], sec * SECTOR_SIZE, IoDir.WRITE)
io_to_exp_obj(core, WORKSET_OFFSET + SECTOR_SIZE * sec, SECTOR_SIZE,
data[SectorStatus.DIRTY], sec * SECTOR_SIZE, IoDir.WRITE)

for s in start_sec:
for e in end_sec:
Expand All @@ -171,17 +178,14 @@ def test_wo_read_data_consistency(pyocf_ctx):
START = s * SECTOR_SIZE
END = e * SECTOR_SIZE
size = (e - s + 1) * SECTOR_SIZE
assert(0 == io_to_exp_obj(core, WORKSET_OFFSET + START, size, \
result_b, START, IoDir.READ)), \
"error reading in WO mode: S={}, start={}, end={}".format( \
S, s, e)
assert(0 == io_to_exp_obj(core, WORKSET_OFFSET + START, size,
result_b, START, IoDir.READ)),\
"error reading in WO mode: S={}, start={}, end={}".format(S, s, e)

# verify read data
for sec in range(s, e + 1):
# just check the first byte of sector
region = sector_to_region(sec, region_start)
check_byte = sec * SECTOR_SIZE
assert(result_b[check_byte] == data[S[region]][check_byte]), \
"unexpected data in sector {}, S={}, s={}, e={}\n".format( \
sec, S, s, e)

"unexpected data in sector {}, S={}, s={}, e={}\n".format(sec, S, s, e)
Loading

0 comments on commit e52d34c

Please sign in to comment.